How could I define a time varying parameter when using ODE45?

8 views (last 30 days)
Hello,
I have a set of ODEs. I am using them to simulate a PK model. The parameter of one of those equations k1 is a time varying variable, when time is within a specific time range, the value of k7 is 100, otherwise is 1. How could I express this in code?
Here are some code I have written:
global k1 k2 k3 k4 k5 k6 k7 tspan;
k1=1;
k2=1;
k3=0.75;
k4=0.108;
k5=1;
k6=3;
for i=1:length(tspan)
if tspan(i)>8 & tspan(i)<24
k7=100;
else
k7 = 1;
end
end
y_0=zeros(1,9);
y_0(1)=(5/1000)/472.54*1000000000;
tspan=0:2:168;
[t,y]=ode45(@derivatives, tspan, y_0);

Accepted Answer

Star Strider
Star Strider on 17 Jun 2020
I have no idea what the ‘derivatives’ function is or how you call it, since you did not post the complete code.
First, using global variables is rarely (if ever) necessary. It can lead to code that is extremely difficult to debug. See the documentation on Passing Extra Parameters for the correct way to do that.
Second, a much better way to implement that condition on what appears to be the time variable is in my Answer to: ODE45 specific time point settings.
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!