Is this a bug of ode function

1 visualizzazione (ultimi 30 giorni)
Liu Haoge
Liu Haoge il 6 Mar 2019
Commentato: madhan ravi il 7 Mar 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 Commento
madhan ravi
madhan ravi il 6 Mar 2019
Did you notice something when defining the function input argument?

Accedi per commentare.

Risposte (1)

SandeepKumar R
SandeepKumar R il 6 Mar 2019
Modificato: SandeepKumar R il 6 Mar 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 Commento
madhan ravi
madhan ravi il 7 Mar 2019
also did you notice the OP’s function argument while defining the function?

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by