I want to solve an ode with a time dependent parameter.
cA
should be 10000 if t
is >=10
and <=11
else it should have the value of 0. cA
is then used in an differential equation to calculate cB
.
See the code:
function dcB = myode(t,y)
cB=y(1,:);
if t>=10 && t<=11
cA=10000
else
cA=0
end
dcB=(cA-cB)*100/1750;
[t,y]=ode45(@myode,[tdown tup],0);
Fallowing problems show up:
- if I print
cA
it has not the correct values at the specified times. - if
tup
is e.g. 20cB
has values, iftup
is e.g. 100cB
is zero.
myode
so that it prints botht
andcA
each time the function is called, then call it using[t,y]=ode45(@myode,[0 20],0);plot(t,y);
then the numbers printed to the MATLAB Command Window appear to be correct, and the plot shows the expected discontinuity at 10 and 11 seconds. – Phil Goddardmyode
integration function with theif
statement is a bad idea. User-supplied ODE functions should not have discontinues. It will only lead to inefficient computation and inaccuracies. See my answers to this question and this one for the proper way to accomplish what you're trying to do. If you want a "time-dependent parameter", it should vary continuously. – horchleroptions = odeset('MaxStep',0.01);
or some other suitable step size and then[t,y]=ode45(@myode,[0 100],0,options);plot(t,y);
– Phil Goddard