Is this a bug of ode function
2 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207121/image.jpeg)
For example, if I change h0 as:
h0=10;
then the result will be:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207122/image.jpeg)
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;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207123/image.jpeg)
1 个评论
回答(1 个)
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 个评论
另请参阅
类别
在 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!