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.
The above system can be represented by the equation,
[ M ] [ x ยจ ] + [ K ] [ x ] = 0
\displaystyle{[M] [\ddot{x}] + [K][x] = 0}
[ M ] [ x ยจ ] + [ K ] [ x ] = 0
where,
M = [ m 1 0 0 0 0 m 2 0 0 0 0 m 3 0 0 0 0 m 4 ] K = [ 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 ] x ยจ = [ x ยจ 1 x ยจ 2 x ยจ 3 x ยจ 4 ] ; x = [ x 1 x 2 x 3 x 4 ]
\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}
}
M K x ยจ โ = โฃ โก โ m 1 โ 0 0 0 โ 0 m 2 โ 0 0 โ 0 0 m 3 โ 0 โ 0 0 0 m 4 โ โ โฆ โค โ = โฃ โก โ 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 โ โ โฆ โค โ = โฃ โก โ x ยจ 1 โ x ยจ 2 โ x ยจ 3 โ x ยจ 4 โ โ โฆ โค โ ; x = โฃ โก โ x 1 โ x 2 โ x 3 โ x 4 โ โ โฆ โค โ โ Letโs take the following values and initial conditions for this problem.
M = [ 5 0 0 0 0 5 0 0 0 0 5 0 0 0 0 5 ] K = [ 10 โ 5 0 0 โ 5 10 โ 5 0 0 โ 5 10 โ 5 0 0 โ 5 5 ] F = [ 0 0 0 0 ] ; x ห ( 0 ) = [ 0 0 0 0 ] ; x ( 0 ) = [ 0.025 0.02 0.02 0.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}
}
M K F โ = โฃ โก โ 5 0 0 0 โ 0 5 0 0 โ 0 0 5 0 โ 0 0 0 5 โ โฆ โค โ = โฃ โก โ 10 โ 5 0 0 โ โ 5 10 โ 5 0 โ 0 โ 5 10 โ 5 โ 0 0 โ 5 5 โ โฆ โค โ = โฃ โก โ 0 0 0 0 โ โฆ โค โ ; x ห ( 0 ) = โฃ โก โ 0 0 0 0 โ โฆ โค โ ; x ( 0 ) = โฃ โก โ 0.025 0.02 0.02 0.001 โ โฆ โค โ โ 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
Copy
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}
}
x ยจ โ = โ [ M ] โ 1 [ K ] [ x ] = [ B ] [ x ] โ where, [ B ] = โ [ M ] โ 1 [ K ] \displaystyle{[B] = -[M]^{-1}[K]} [ B ] = โ [ M ] โ 1 [ K ] .
Now, to solve the equation x ยจ = B x \displaystyle{\ddot{x} = Bx} x ยจ = B x , 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 )
Copy
Some key points to note are,
The solution is arrived as three steps.Define the differential equation f
. Define the problem by specifying function, initial conditions and time span. Solving the defined problem. tspan
is the variable which specifies the range over which we are solving for x x x .The function f
is equated to second derivative x ยจ \ddot x x ยจ here. Generally, f
is a function of,du
- The derivative of u
u
- The variable being solvedp
- parameter for parameterized functionst
- time The function SecondOrderODEProblem
takes in the function f
, initial conditions du0
(x ห 0 \dot x_0 x ห 0 โ here) and u0 (x 0 x_0 x 0 โ here), and the time span tspan
. solve
generates dataset containing 9 columns - timestamp, x ห 1 , x ห 2 , x ห 3 , x ห 4 , x 1 , x 2 , x 3 , x 4 \displaystyle{\dot x_1, \dot x_2, \dot x_3, \dot x_4, x_1, x_2, x_3, x_4} x ห 1 โ , x ห 2 โ , x ห 3 โ , 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" ])
Copy
This is a post Iโve made as part of my learning experience. Let me know if you have any feedback.