Why two methods give different results, and which is correct?

3 views (last 30 days)
I use ode15i and ode45 to solve an implicit function, however two methods give different results, which is correct?
clear
clc
close all
%% ode15i
f = @(t,y,yp)yp.^2-5/(0.5*yp-2./y);
[t0,y0,yp0,tspan] = deal(0,1.924,2.079,[0 1]);
[y0,yp0] = decic(f,t0,y0,1,yp0,0);
sol = ode15i(f,tspan,y0,yp0);
subplot(1,2,1)
plot(sol.x,sol.y)
title('ode15i')
%% ode45
tspan = [0 1]; % time interval
y0 = 1.924; % initial value
[t,y] = ode45(@(t,y)odefcn(t,y),tspan,y0); % ode45
subplot(1,2,2)
plot(t,y)
title('ode45')
% define function
function Dy = odefcn(t,y)
fun = @(Dy) Dy.^2 - 5/(0.5*Dy-2/y); % implicit function
Dy = fzero( fun,0); % fzero function to solve implict function
end
and results are plotted below:

Accepted Answer

Davide Masiello
Davide Masiello on 1 Apr 2022
Edited: Davide Masiello on 1 Apr 2022
Since you have an implicit ODE, the correct solution is given by ode15i.
The problem with ode45 is in the initial guess used to find the value of Dy. I used 5 instead of 0 in the exmple below and got the same result of ode15i.
clear
clc
close all
%% ode15i
f = @(t,y,yp)yp.^2-5/(0.5*yp-2./y);
[t0,y0,yp0,tspan] = deal(0,1.924,2.079,[0 1]);
[y0,yp0] = decic(f,t0,y0,1,yp0,0);
sol = ode15i(f,tspan,y0,yp0);
subplot(1,2,1)
plot(sol.x,sol.y)
title('ode15i')
%% ode45
tspan = [0 1]; % time interval
y0 = 1.924; % initial value
[t,y] = ode45(@(t,y)odefcn(t,y),tspan,y0); % ode45
subplot(1,2,2)
plot(t,y)
title('ode45')
% define function
function Dy = odefcn(t,y)
fun = @(Dy) Dy.^2 - 5/(0.5*Dy-2/y); % implicit function
Dy = fzero(fun,5); % fzero function to solve implict function
end

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!