Hi guys, I need to solve this 3rd order ODE:
?⃛+2??̈+3??̇+4??=2, t≥0, ?̈(0)=?̇(0)=?(0)=0
This is my script:
[t,y] = ode45(@diffeq3,[0 50],[0;-1;1]);
plot(t,y)
function dy = diffeq3(t,y)
if t >= 0
dy(3) = 0
dy(2) = 0
dy(1) = 0
end
dy = zeros(3,1);
dy(1) = y(2);
dy(2) = y(3);
dy(3) = 2 - 2*y(3) - 3*y(2) - 4*y(1);
end
Did I do it right?
I got two questions, I got three curves which on is which?
And did I define t≥0, ?̈(0)=?̇(0)=?(0)=0 correct? With:
if t >= 0
dy(3) = 0
dy(2) = 0
dy(1) = 0
Thanks in advance for the help!

 Accepted Answer

Star Strider
Star Strider on 10 May 2020

1 vote

There appears to be a factor of ‘y(1)’ missing in the last 3 terms of the ‘dy(3)’ equation. (I gave your differential equation as posted to the odeToVectorField function to be certain.)
Also, the if block is not necessary. It does nothing, and produces slower code.

6 Comments

Sorry when I pasted in the equation something happened, it should be ?⃛+2?̈+3?̇+4?=2
This is the new script I made from your suggestion:
[t,y] = ode45(@diffeq3,[0 50],[0;-1;1]);
plot(t,y)
function dydt = diffeq3(t,y)
dydt = zeros(3,1);
dydt(1) = y(2);
dydt(2) = y(3);
dydt(3) = 2 - 2*y(3) - 3*y(2) - 4*y(1);
end
From what you wrote I take it as you don't have to define t≥0, ?̈(0)=?̇(0)=?(0)=0 and it does so automatically?
Also I get 3 curves and have no idea which one is which and would really appreciate the help. I'm really new to this with matlab and I'm still confused with ODE and how the graph it produces should look.
Also thanks for the help in advance!
The function appears to be correct.
From what you wrote I take it as you don't have to define t≥0, ?̈(0)=?̇(0)=?(0)=0 and it does so automatically?
Yes. The ODE integration functions (ode45 here) take the initial conditions as the third argument, so it is not only not necessary to define them inside the ODE function, it is not appropriate to do so.
Also I get 3 curves and have no idea which one is which and would really appreciate the help.
The curves appear in to order in which they appear in the differential equation vector, so here, y, , and in order. The symbolic Math Toolbox takes care of these in the odeToVectorField function, so use the second output and the string function to create a legend argument that will display them correctly:
syms y(t) t Y
D1y = diff(y);
D2y = diff(y,2);
D3y = diff(y,3);
Eq = D3y + 2*D2y + 3*D1y + 4*y == 2;
[VF,Subs] = odeToVectorField(Eq)
diffeq3 = matlabFunction(VF, 'Vars',{t,Y})
[t,y] = ode45(diffeq3,[0 50],[0;-1;1]);
plot(t,y)
legend(string(Subs))
producing:
I prefer this because the Symbolic Math Toolbox does not make the typical algebra errors that I frequently do. Also this is MATLAB Answers, so I use it frequently to derive such functions and expressions. (It saves me time and embarrassment.)
Thank you so much and for taking the time explaining everything. I seem to get the hang of it now which really makes using matlab more enjoyable.
By the way I think ?̈(0)=?̇(0)=?(0)=0 meaning all the curves should start from 0 which can be defined by setting:
[t,y] = ode45(diffeq3,[0 50],[0;0;0]);
instead of
[t,y] = ode45(diffeq3,[0 50],[0;-1;1]);
and this is my final code, (got a little help with legend from another user). Looking perfect now?
[t,y] = ode45(@diffeq3,[0 50],[0;0;0]);
plot(t,y)
legend({'y(t)', '$\dot{y}(t)$', '$\ddot{y}(t)$'}, 'Interpreter', 'latex')
function dy = diffeq3(t,y)
dy = zeros(3,1);
dy(1) = y(2);
dy(2) = y(3);
dy(3) = 2 - 2*y(3) - 3*y(2) - 4*y(1);
end
It looks good to me!
got a little help with legend from another user
I saw that. I could have helped you with it, since I did the same in my previous Comment.
Yeah just different ways of writing the script, thanks for all the help by the way!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!