How to use else if with ODE45 solver?

조회 수: 1 (최근 30일)
Skye Cameron
Skye Cameron 2021년 4월 9일
댓글: Walter Roberson 2021년 4월 9일
%define ODEs
%DOX conc in blood
if t<Tiv
diffeqs(1,1) = (-k_clr_DOX*C_DOX_Bl)+ R_DOX
else
R_DOX = 1
R_DOX = 0
end
%BEV conc in blood
if t<Tiv
diffeqs(2,1) = (-k_clr_BEV*C_BEV_Bl)+ R_BEV
else
R_BEV = 1
R_BEV = 0
end
This is what I have and it is showing an error message when I try to solve this?

답변 (1개)

Walter Roberson
Walter Roberson 2021년 4월 9일
The mathematics used in the RK methods such as ode45(), require that for any one call the function be continuous and its first two derivatives are continuous. If the second derivative is not continuous, the most likely result is that the function will return incorrect results without noticing. If the first derivative is not continuous, sometimes it will notice and and abort the integration and other times it will not notice and will continue producing wrong results without noticing.
If for any one call you cross the time boundary between < Tiv and >= Tiv, then your function is not continuous, and if you are lucky you will get a message about it; if you are unlucky then it will return an answer without telling you that the answer is wrong.
We notice that if in the case where t >= Tiv, you do not assign anything to diffeqs(1,1) or diffeqs(2,1), which could potentially be a problem (depending what else you do.)
What you should do is run the code once with time span 0 to Tiv, and then take the final conditions from that and use them as the input boundary conditions for running a second call with time span Tiv to whatever.
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 4월 9일
Suppose I wrote
if a < 5
disp('outcome 1')
else
disp('outcome 2')
else
disp('outcome 3')
end
then under what circumstances would you expect outcome 3 to be what is displayed?
Walter Roberson
Walter Roberson 2021년 4월 9일
to help control thr 'infusion'
If you are adding more drug within the time span of any one ode45() call, then you will have problems, unless you can match derivatives. The easiest approach is to have any one ode45() call cover only the intervals between injections. For example if you inject every 6 hours then
curvar = var0;
hours_6 = 6*60*60;
for K = 1 : 4
[t{K},var{K}] = ode45(@ode_sys, (K-1:K)*hours_6, curvar);
curvar = var{K}(end,:) + amounts_to_inject;
end

댓글을 달려면 로그인하십시오.

카테고리

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