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

조회 수: 15 (최근 30일)
Yu Zhuo
Yu Zhuo 2020년 6월 17일
답변: Star Strider 2020년 6월 17일
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);

채택된 답변

Star Strider
Star Strider 2020년 6월 17일
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.
.

추가 답변 (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