How to solve third order equation using ode45

1 view (last 30 days)
Álvaro Recalde
Álvaro Recalde on 16 May 2022
Answered: Sam Chak on 16 May 2022
Hi, I was wondering if you could help me, I'm trying to solve the following third order equation, using ode45 for the range from [0,5]. Apart from that, I'd need to plot the different solutions of y', y'' and y.
My function is this one:
y'''-y'' * y +1 = 0
y(0) =1
y'(0)=0
y''(0)=0.1
I don't know how to apply ode45 for this equation, I'd gladly accept any help.
Thank you very much.
  1 Comment
Torsten
Torsten on 16 May 2022
Setting y(1) = y, y(2) = y' and y(3) = y'', your differential eqation can be written as a system of equations:
dy(1)/dt = y(2)
dy(2)/dt = y(3)
dy(3)/dt = y(3)*y(1) - 1
with initial conditions
y(1)(0) = 1
y(2)(0) = 0
y(3)(0) = 0.1
Now look at the page of ODE45 on how to set up this system:

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 16 May 2022
I let the Symbolic Math Toolbox do everything —
syms y(t) T Y
Dy = diff(y);
D2y = diff(Dy);
D3y = diff(D2y);
Eq = D3y - D2y * y + 1
Eq(t) = 
[VF,Sbs] = odeToVectorField(Eq)
VF = 
Sbs = 
yfcn = matlabFunction(VF, 'Vars',{T,Y})
yfcn = function_handle with value:
@(T,Y)[Y(2);Y(3);Y(1).*Y(3)-1.0]
ics = [0 0 0];
[t,y] = ode45(yfcn, [0 5], ics);
figure
plot(t,y)
grid
legend(string(Sbs), 'Location','best')
.

More Answers (1)

Sam Chak
Sam Chak on 16 May 2022
@Álvaro Recalde, I'll show an example from
function dydt = odefcn(t, y)
dydt = zeros(3,1);
dydt(1) = y(2); % y' = ...
dydt(2) = y(3); % y'' = ...
dydt(3) = - 3*y(3) - 3*y(2) - y(1); % y''' = ...
end
and run ode45 to solve it
tspan = [0 10];
y0 = [1 0.5 0];
[t, y] = ode45(@odefcn, tspan, y0);

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!