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:
댓글 수: 0
채택된 답변
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 Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!