Borrar filtros
Borrar filtros

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

8 visualizaciones (últimos 30 días)
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:

Respuesta aceptada

Davide Masiello
Davide Masiello el 1 de Abr. de 2022
Editada: Davide Masiello el 1 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by