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

2 次查看(过去 30 天)
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:

采纳的回答

Davide Masiello
Davide Masiello 2022-4-1
编辑:Davide Masiello 2022-4-1
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by