How to find the timestep in ode45?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm using ode45 to calculate these 2 diff eqs, which simulate drug moving through two compartments in the body:

p(t) represents injection into the body at a rate of 2mg/min.
So p(t) depends on the difference between the current and previous timestep. I tried solving it like this:
time=100; %minutes
options=odeset('RelTol',1e-3,'AbsTol',[1e-2 1e-2]);
%initial conditions
x0=0;
y0=0;
[T,Y]=ode45(@hw3q5,[0:time],[x0 y0],options);
%the function
function dy=hw3q5(t,y)
dy=zeros(2,1);
%constants
ke=0.024; %1/min
kf=0.066; %1/min
kr=0.038; %1/min
if length(t)==1
p=2*t;
end
if length(t)>1
delta_t=t(end)-t(end-1);
p=2*delta_t;
end
dy(1)=p+(kr*y(2))-(y(1)*(ke+kf));
dy(2)=(kf*y(1))-(kr*y(2));
the issue is that length(t) seems to always equal one, giving me a larger p value than I want in each step. Is there a way to access previous timesteps to get a delta_t in ode45? Thanks in advance.
0 comentarios
Respuestas (2)
KSSV
el 19 de Oct. de 2022
Alreay you have time in T. When you gave [0:time], the timestep is 1. You may provide your required time difference dt, using:
dt = 0.1 ;
time=100; %minutes
options=odeset('RelTol',1e-3,'AbsTol',[1e-2 1e-2]);
%initial conditions
x0=0;
y0=0;
[T,Y]=ode45(@hw3q5,[0:time],[x0 y0],options);
plot(T,Y)
%the function
function dy=hw3q5(t,y)
dy=zeros(2,1);
%constants
ke=0.024; %1/min
kf=0.066; %1/min
kr=0.038; %1/min
if length(t)==1
p=2*t;
end
if length(t)>1
delta_t=t(end)-t(end-1);
p=2*delta_t;
end
dy(1)=p+(kr*y(2))-(y(1)*(ke+kf));
dy(2)=(kf*y(1))-(kr*y(2));
end
0 comentarios
Torsten
el 19 de Oct. de 2022
Editada: Torsten
el 19 de Oct. de 2022
p(t) represents injection into the body at a rate of 2mg/min.
So p(t) depends on the difference between the current and previous timestep.
No, p(t) is constantly 2mg/min independent of t. If x is in mg and time is in minutes, your equations simply read
time=100; %minutes
%initial conditions
x0=0;
y0=0;
[T,Y]=ode45(@hw3q5,[0:time],[x0 y0]);
plot(T,Y)
function dy=hw3q5(t,y)
dy=zeros(2,1);
%constants
ke=0.024; %1/min
kf=0.066; %1/min
kr=0.038; %1/min
p = 2;
dy(1)=p+(kr*y(2))-(y(1)*(ke+kf));
dy(2)=(kf*y(1))-(kr*y(2));
end
If p depended on the concentration of x and/or y or if p changed with time, this would have been a different thing.
0 comentarios
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

