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

  • Context: MATLAB 
  • Thread starter Thread starter intrinsik
  • Start date Start date
  • Tags Tags
    2nd order Matlab Ode
Click For Summary
SUMMARY

This discussion focuses on solving a second-order ordinary differential equation (ODE) in matrix form using MATLAB's ode45 function. The user encounters errors in their implementation, specifically in the equation for dy. The system of equations is defined by matrices A, B, and C, which incorporate parameters such as mass and length. To resolve the issue, it is essential to convert the second-order equations into a system of four first-order equations, as outlined in MATLAB's documentation on higher-order ODEs.

PREREQUISITES
  • Understanding of second-order ordinary differential equations
  • Familiarity with MATLAB programming, specifically the ode45 function
  • Knowledge of matrix operations and linear algebra
  • Basic concepts of initial value problems in differential equations
NEXT STEPS
  • Review MATLAB's documentation on solving higher-order ODEs
  • Learn how to convert second-order ODEs to first-order systems
  • Explore MATLAB's symbolic toolbox for defining and manipulating equations
  • Practice implementing coupled differential equations in MATLAB
USEFUL FOR

This discussion is beneficial for engineers, mathematicians, and students who are working with differential equations in MATLAB, particularly those dealing with coupled systems and initial value problems.

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

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K