# Create a MATLAB code to convert Equations of Motion for Multi DOF system (2nd Order ODEs with multiple Variables) into matrix form(Coefficient Matrices)

13 views (last 30 days)
DHEERAJ SINGH on 18 Dec 2021
Answered: Himanshu on 29 Apr 2024
I got 6 Equations of Motion (second order ODEs with 6 independent variables) for a 6 DOF system by using the following Code:-
clear all;
clc;
syms L T P D M1 M2 I1 I2 x1(t) x2(t) x3(t) x4(t) x5(t) x6(t) t K1 K2 K3 K4 K5 K6 C1 C2 C3 C4 C5 C6 a b c d e f1 f2 f3 f4 f5 f6;
f1 = (M1*(diff(x1,t,2))+2*(C1+C3)*diff(x1,t)-2*C3*diff(x4)+2*(K1+K3)*x1-2*K3*x4)
f2 = (M1*(diff(x2,t,2))+2*(C2+C4)*diff(x2,t)-2*C4*diff(x5)+2*(K2+K4)*x2-2*K4*x5)
f3 = (I1*(diff(x3,t,2))+2*(C1*b^2+C2*a^2+C3*c^2+C4*e^2)*diff(x3,t))-2*(C3*c*d+C4*e^2)*diff(x6)+2*(K1*b^2+K2*a^2+K3*c^2+K4*e^2)*x3-2*(K3*c*d+K4*e^2)*x6
f4 = (M2*(diff(x4,t,2))-2*C3*diff(x1,t)+2*C3*diff(x4)-2*K3*x1+2*K3*x4)
f5 = (M2*(diff(x5,t,2))-2*C4*diff(x2,t)+2*C4*diff(x5)-2*K4*x2+2*K4*x5)
f6 = (I1*(diff(x6,t,2))+2*(C3*d^2+C4*e^2)*diff(x6,t))-2*(C3*c*d+C4*e^2)*diff(x3)+2*(K3*d^2+K4*e^2)*x6-2*(K3*c*d+K4*e^2)*x3
U can see that all Mass Coefficients are with second order derivates only, Damping(C) terms with single order derivates and Stiffness(K) terms with zero order derivates. In state space Matrix form, it can be seen as [M][]+[C][ẋ]+[K][x] = F.
Now, I need to convert these ODEs to State space form i.e. extract the M, C & K coefficients and make 6X6 matrices.
Presently, I am manually collecting all the terms and create the matrices. However, I want a matlab code which can auto populate the matrices.
Please create a code. Thank you.

Himanshu on 29 Apr 2024
Hey Dheeraj,
As per my understanding, you are trying to write a matlab script which can extract coefficients from the ODEs given by you and form a state space matrix.
Instead of manually collecting terms from these equations, you can employ MATLAB's symbolic computation capabilities. you can use the 'coeffs' function, which automatically extracts coefficients from the equations of motion. This function takes a set of equations and a set of variables as inputs, and it returns the coefficients of those variables in the equations. Please refer to the following link to go through the detailed documentation of this function.
Once you have the coefficients extracted, you can construct the mass, damping, and stiffness matrices by arranging these coefficients into the appropriate positions in the matrices. you can accomplish this using nested loops to iterate over the coefficients and fill in the matrices accordingly.
Hope this helps!