MATLAB Answers

How to use ode45 to solve a system of two differential equation?

조회 수: 56(최근 30일)
Gleb A
Gleb A 22 Dec 2020
편집: James Tursa 22 Dec 2020
I need to use ode45 to solve this system:
(when q_1=q_1(t), q_2=q_2(t);
m, M, l, g, u - const)
There are no such examples in the documentation
How can I do it?


Star Strider
Star Strider 22 Dec 2020
Code it correctly in the Symbollic Math Toolbox, then use odeFunction to turn it into a form that the ODE solvers can use.
Alternatively, you can do what I have gotten used to doing, and use odeToVectorField and matlabFunction to create the function to be used with the numerical ODE solvers.

James Tursa
James Tursa 22 Dec 2020
편집: James Tursa 22 Dec 2020
If you don't have the Symbolic Toolbox, here is the procedure:
1) Write your equations in matrix form:
F*qdotdot + G*qdot + H*q + stuff = otherstuff
where q = [q1;q2], qdot = [q1dot;d2dot], qdotdot = [q1dotdot;q2dotdot]
F is a 2x2 matrix, G is a 2x2 matrix, H is a 2x2 matrix, stuff is a 2x1 vector, otherstuff is a 2x1 vector
2) Solve for the highest order derivatives:
qdotdot = F \ (otherstuff - G*qdot - H*q - stuff)
You don't need the explicit solution for qdotdot here ... you can leave it in the form above if you want.
3) Define first order equations by redefining variables:
y = [q1;q2;q1dot;q2dot]
so y(1) = q1, y(2) = q2, y(3) = q1dot, y(4) = q2dot
4) Rewrite all of your derivatives according to these new definitions:
ydot = [y(3);y(4);qdotdot]
where the qdotdot is replaced with the code in step (2) but using y(1), y(2), y(3), y(4) in place of q1, q2, q1dot, q2dot.
5) Code up step (4)
Use this as your function (handle) to feed to ode45( ).

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by