Solving 2nd Order ODEs in Julia

The second order Linear Differential Equations can be solved using DifferentialEquations.jl package available in Julia repository. Here is an example problem being solved using Julia.

Consider a 4-degree of freedom undamped free vibration system as follows.

4dof-system

The above system can be represented by the equation,

[M][xยจ]+[K][x]=0 \displaystyle{[M] [\ddot{x}] + [K][x] = 0}

where,

M=[m10000m20000m30000m4]K=[k1+k2โˆ’k200โˆ’k2k2+k3โˆ’k300โˆ’k3k3+k4โˆ’k400โˆ’k4k4+k5]xยจ=[xยจ1xยจ2xยจ3xยจ4];x=[x1x2x3x4] \displaystyle{ \begin{aligned} M &=\begin{bmatrix} m_1 & 0 & 0 & 0 \\ 0 & m_2 & 0 & 0 \\ 0 & 0 & m_3 & 0 \\ 0 & 0 & 0 & m_4 \end{bmatrix} \\ K &=\begin{bmatrix} k_1+k_2 & -k_2 & 0 & 0 \\ -k_2 & k_2+k_3 & -k_3 & 0 \\ 0 & -k_3 & k_3+k_4 & -k_4 \\ 0 & 0 & -k_4 & k_4+k_5 \end{bmatrix} \\ \ddot{x} &=\begin{bmatrix} \ddot x_1 \\ \ddot x_2 \\ \ddot x_3 \\ \ddot x_4 \end{bmatrix}; x =\begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \end{bmatrix}\\ \end{aligned} }

Letโ€™s take the following values and initial conditions for this problem.

M=[5000050000500005]K=[10โˆ’500โˆ’510โˆ’500โˆ’510โˆ’500โˆ’55]F=[0000];xห™(0)=[0000];x(0)=[0.0250.020.020.001] \displaystyle{ \begin{aligned} M &=\begin{bmatrix} 5 & 0 & 0 & 0 \\ 0 & 5 & 0 & 0 \\ 0 & 0 & 5 & 0 \\ 0 & 0 & 0 & 5 \end{bmatrix} \\ K&=\begin{bmatrix} 10 & -5 & 0 & 0 \\ -5 & 10 & -5 & 0 \\ 0 & -5 & 10 & -5 \\ 0 & 0 & -5 & 5 \end{bmatrix} \\ F &=\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \end{bmatrix}; \dot{x}\left( 0 \right) =\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \end{bmatrix}; x\left( 0 \right) =\begin{bmatrix} 0.025 \\ 0.02 \\ 0.02 \\ 0.001 \end{bmatrix} \end{aligned} }
1
2
3
4
5
6
7
using DifferentialEquations
M = Array(I(4)*5)
K = Array(Tridiagonal([-5, -5, -5], [10, 10, 10, 5], [-5, -5, -5]))
F = zeros(4)
xฬ‡โ‚€ = zeros(4)
xโ‚€ = [0.025, 0.02, 0.02, 0.001]
B = -inv(M)*K

The above differential equation can be re-arranged as,

xยจ=โˆ’[M]โˆ’1[K][x]=[B][x] \displaystyle{ \begin{aligned} \ddot{x} &= -[M]^{-1}[K][x] \\ &= [B][x] \end{aligned} }

where, [B]=โˆ’[M]โˆ’1[K]\displaystyle{[B] = -[M]^{-1}[K]}.

Now, to solve the equation xยจ=Bx\displaystyle{\ddot{x} = Bx}, the following code is run in julia.

1
2
3
4
tspan = (0.0, 200.0)
f(du, u, p, t) = F+B*u
prob = SecondOrderODEProblem(f,xฬ‡โ‚€,xโ‚€,tspan)
sol = DifferentialEquations.solve(prob)

Some key points to note are,

  1. The solution is arrived as three steps.
    1. Define the differential equation f.
    2. Define the problem by specifying function, initial conditions and time span.
    3. Solving the defined problem.
  2. tspan is the variable which specifies the range over which we are solving for xx.
  3. The function f is equated to second derivative xยจ\ddot x here. Generally, f is a function of,
    1. du - The derivative of u
    2. u - The variable being solved
    3. p - parameter for parameterized functions
    4. t - time
  4. The function SecondOrderODEProblem takes in the function f, initial conditions du0(xห™0\dot x_0 here) and u0 (x0x_0 here), and the time span tspan.
  5. solve generates dataset containing 9 columns - timestamp, xห™1,xห™2,xห™3,xห™4,x1,x2,x3,x4\displaystyle{\dot x_1, \dot x_2, \dot x_3, \dot x_4, x_1, x_2, x_3, x_4}

The response (displacement) of the above system can be plotted as follows.

1
2
3
4
5
6
7
8
plot(sol, 
	vars=[5, 6, 7, 8], 
	tspan=(0, 100), 
	layout=(4,1), 
	size=(600, 800),
	ylabel=["xโ‚" "xโ‚‚" "xโ‚ƒ" "xโ‚„"],
	label=["xโ‚" "xโ‚‚" "xโ‚ƒ" "xโ‚„"],
	color=["Red" "Green" "Blue" "Black"])

image-20210323172221162

This is a post Iโ€™ve made as part of my learning experience. Let me know if you have any feedback.

Load Comments?