Is this a bug of ode function

조회 수: 1 (최근 30일)
Liu Haoge
Liu Haoge 2019년 3월 6일
댓글: madhan ravi 2019년 3월 7일
I find it's interesting if I change the initial value h0, it will affect the accuracy of "ode45"
close all
tspan=0:0.001:60;
h0=0;%!!!!!!!!!!!!!
[t,h]=ode45(@TankLvl, tspan, h0);
plot(t,h)
xlabel('t')
ylabel('y')
hold on
h2 = h0 + 0.005.*tspan - 0.05.*cos(tspan) + 0.05;
plot(tspan, h2,'r-')
function dh=TankLvl(t,h0)
A=2;
q_in=0.51+0.1*sin(t);
q_out=0.5;
dh=1/A*(q_in-q_out);
end
For example, if I change h0 as:
h0=10;
then the result will be:
It seems that the "ode" function can't deal with the initial value quite well. In order to deal with this issue, I can only set a pseudo initial value h0_pse=0, and use it as the input for "ode", then add the real initial value h0 to the computed h. Shown as following:
h0_pse=0;
h0=10;
[t,h]=ode45(@TankLvl, tspan, h0_pse);
h=h+h0;
  댓글 수: 1
madhan ravi
madhan ravi 2019년 3월 6일
Did you notice something when defining the function input argument?

댓글을 달려면 로그인하십시오.

답변 (1개)

SandeepKumar R
SandeepKumar R 2019년 3월 6일
편집: SandeepKumar R 2019년 3월 6일
It was a tolerance issue. Please define your funtions appropriately. Not a bug in MATLAB
close all
tspan=0:0.1:90;
h0=0;%!!!!!!!!!!!!!
opts = odeset('RelTol',1e-6,'AbsTol',1e-6);
[t,h]=ode45(@(t,z)TankLvl(t,z), [0,90], h0,opts);
plot(t,h,'*')
xlabel('t')
ylabel('y')
hold on
h2 = h0 + 0.005.*t - 0.05.*cos(t) + 0.05;
plot(t, h2,'r')
function dh=TankLvl(t,z)
A=2;
q_in=0.51+0.1*sin(t);
q_out=0.5;
dh=1/A*(q_in-q_out);
end
  댓글 수: 1
madhan ravi
madhan ravi 2019년 3월 7일
also did you notice the OP’s function argument while defining the function?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by