ode45 given a systems of equations help

1 vue (au cours des 30 derniers jours)
Francesco Latorre
Francesco Latorre le 19 Nov 2019
for an assignment, i have to use ode45 to graph the position of a system of differential equations, however these equations are too complex to be combined into a single equation with one variable. for simplicity ill use easier but similar formulas:
formula A: 1 = cos(Y) + (Y' ^ 2) + (x' ^2) + Sin(Y) - X''
formula B: sin (Y) = X'' *Cos(Y-X) + X' * Sin(Y-X) + Y''
how would i use ode45 to graph two plots: X(t) and Y(t) given these two formulas?

Réponses (2)

James Tursa
James Tursa le 19 Nov 2019
Modifié(e) : James Tursa le 19 Nov 2019
First, look over the examples in the ode45 doc.
Then figure out the order of your system, and that will tell you the size of your state vector. You have two 2nd order equations, so that means your state vector should be 2x2 = 4 elements. Using the nomenclature of the MATLAB doc to make things easier when you read them, I am going to call this state vector y. The definition of the y elements is:
y(1) = X
y(2) = Xdot
Y(3) = Y
y(4) = Ydot
Using those definitions, you need to write a derivative function. The outline of this function will look like this:
function dy = myderivative(t,y)
dy = zeros(size(y));
dy(1) = _____; % the derivative of y(1), i.e. the derivative of X, which is Xdot
dy(2) = _____; % the derivative of y(2), i.e. the derivative of Xdot, which is Xdoubledot
dy(3) = _____; % the derivative of y(3), i.e. the derivative of Y, which is Ydot
dy(4) = _____; % the derivative of y(4), i.e. the derivative of Ydot, which is Ydoubledot
end
You need to fill in the blanks in the above code.
Since the derivative of X is just Xdot, the dy(1) line is easy ... it is just y(2). Similar for dy(3).
The dy(2) and dy(4) lines are more work for you. You need to solve your two equations above for Xdoubledot and Ydoubledot and then use those expressions to write code.
Once this is done, I suggest you look again at the examples in the ode45 doc, particularly the one for the system of equations. That will give you clues for writing your code that calls ode45.
Give this a shot and then come back when you have some code for us to look at that we can help you with.

Francesco Latorre
Francesco Latorre le 14 Juin 2021
I thought I responded to this sooner but I just looked back and realized I never did. for anyone reading this, this answer worked perfectly and I completely understand how ode45 works, so I strongly recommend doing this as well!

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by