MATLAB How can I solve a 2nd order ODE in matrix form using Matlab?

AI Thread Summary
To solve a second-order differential equation in matrix form using Matlab, the user needs to convert the equations into a system of four first-order equations. The provided function attempts to define the system but encounters an error in the calculation of dy. The user should ensure that the matrix operations are correctly implemented, particularly in the expression for dy. Resources such as Matlab's documentation on "Higher Order ODEs" can provide guidance on the necessary transformations. Properly structuring the equations will help resolve the issues faced with ode45.
intrinsik
Messages
1
Reaction score
0
I'm trying to solve a 2nd order differential equation in matrix form. I'm not familiar with Matlab, and have tried solving this using tutorials on youtube.
Initially, theta1 = pi/4, theta2 = 7*pi/12, theta1_d = 0, and theta2_d =0. Time interval is (0,1.2).
When I try to solve this using ode45, I keep getting an error in my equation for dy (last line of function), and I'm not sure what I'm doing wrong. Can anyone help me out?

The equation I'm trying to solve is of the form
A*[theta1_dd;theta2_dd] + B*[theta1_d;theta2_d] = C
where theta1_d is the first derivative of theta, theta1_dd is the second derivative of theta, etc.

function dy = ode(t,y)
m1=2.52;
m2=1.30;
l1=0.33;
l2=0.32;
r1=l1/2;
r2=l2/2;
ic1=(m1*l1^2)/12;
ic2=(m2*l2^2)/12;
i1=ic1+(m1*r1^2);
i2=ic2+(m2*r2^2);
syms theta1 theta2 theta1_d theta2_d theta1_dd theta2_dd t1 t2;
A=[i1+(m2*l1^2), m2*r2*l1*cos(theta2-theta1);m2*r2*l1*cos(theta2-theta1), i2]
B=[0,-m2*r2*l1*sin(theta2-theta1)*theta2_d;-m2*r2*l1*sin(theta2-theta1)*theta1_d,0]
C=[t1-t2;t2]
u1=y(1);
u2=y(2);
dy=[u2;(C/A)-((B/A)*u2)];
end
 
Last edited:
Physics news on Phys.org
If you have two coupled 2nd order equations, you need to convert to a system of 4 coupled 1st order equations.
 
http://www.mathworks.com/help/matlab/math/ordinary-differential-equations.html

As DrClaude mentioned, see the section "Higher Order ODEs" under "Initial Value Problems" to see how to rewrite the equations as an equivalent system of first-order ODEs. The first example shows how to do this with the van der Pol equation.
 

Similar threads

Back
Top