Error using ODE45

조회 수: 7 (최근 30일)
Per-Kristian
Per-Kristian 2011년 11월 17일
I have an ode for density that is a function of mass flow, volume, density and the time derivative of the volume. Since the volume varies as a sine function I'm expecting the densities to oscillate around the initial value, but instead it increases. Does anyone know what is causing this error and what can be done to fix it?
D0=[70 80];
tspan=[0 50];
[t T]=ode45(@DensFunc, tspan, D0);
function Dx=DensFunc(t, D)
A=0.02;f=100;w=2*pi*f;
y=A*sin(w*t);v=w*A*cos(w*t);
a1=2e-3;
a2=1.5e-3;
VA=a1*(0.024+y);
VB=a2*(0.024-y);
dotVA=a1*v;
dotVB=-a2*v;
%setting the massflow equal to zero to find out whats wrong.
dotmA=0;
dotmB=0;
Dx=zeros(2,1);
Dx(1)=((dotmA-D(1)*dotVA)/(VA));
Dx(2)=((dotmB-D(2)*dotVB)/(VB));
end

채택된 답변

Andrew Newell
Andrew Newell 2011년 11월 18일
It's roundoff errors. For the large number of cycles you're integrating over, it's hard to avoid some drift; but you can lower the tolerances to get a pretty good result. Just replace
[t T]=ode45(@DensFunc, tspan, D0);
by
opts=odeset; opts=odeset(opts,'RelTol',1e-4,'AbsTol',1e-7);
[t T]=ode45(@DensFunc, tspan, D0,opts);

추가 답변 (0개)

카테고리

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