How to use else if with ODE45 solver?

2 views (last 30 days)
Skye Cameron
Skye Cameron on 9 Apr 2021
Commented: Walter Roberson on 9 Apr 2021
%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?

Answers (1)

Walter Roberson
Walter Roberson on 9 Apr 2021
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 Comments
Walter Roberson
Walter Roberson on 9 Apr 2021
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 on 9 Apr 2021
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

Sign in to comment.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!