Is this a bug of ode function

3 visualizaciones (últimos 30 días)
Liu Haoge
Liu Haoge el 6 de Mzo. de 2019
Comentada: madhan ravi el 7 de Mzo. de 2019
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 comentario
madhan ravi
madhan ravi el 6 de Mzo. de 2019
Did you notice something when defining the function input argument?

Iniciar sesión para comentar.

Respuestas (1)

SandeepKumar R
SandeepKumar R el 6 de Mzo. de 2019
Editada: SandeepKumar R el 6 de Mzo. de 2019
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 comentario
madhan ravi
madhan ravi el 7 de Mzo. de 2019
also did you notice the OP’s function argument while defining the function?

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by