How could I define a time varying parameter when using ODE45?
26 views (last 30 days)
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;
if tspan(i)>8 & tspan(i)<24
k7 = 1;
[t,y]=ode45(@derivatives, tspan, y_0);
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.