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

2 ビュー (過去 30 日間)
warnerchang
warnerchang 2022 年 4 月 1 日
コメント済み: warnerchang 2022 年 4 月 1 日
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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by