3
$\begingroup$

I am doing some biological system modeling and trying to use WhenEvent in the initial conditions section of my NDSolve code to allow me to periodically change the value of a parameter (which I've coded as a variable) like a squarewave function. The idea is that the parameter kxscribe (really a variable here) takes on a low value (a low rate) for the first half of the period (period set to 100 mins) and a higher value (higher rate) for the second half of the period. Right now kxscribe is only changing up to the high value but never back down to the lower value in the second period. Not sure what I'm doing wrong. Here's my code:

  params = {period -> 100 (* period of 100 minutes *), 
   kmRNAdeg -> 0.03 (* mRNA degradation rate in min^-1*)};


odes = {

   mRNA'[t] == kxscribe[t] - kmRNAdeg*mRNA[t],

   kxscribe'[t] == 0

   };


initConds = {
   mRNA[0] == 5, 
   kxscribe[0] == 0.25, 
   WhenEvent[Mod[t, period] <= period/2, kxscribe[t] -> 0.25], 
   WhenEvent[Mod[t, period] > period/2, kxscribe[t] -> 1.0]
 };


sol = NDSolve[

   {
     (*ODEs*)

     odes,


     (* init conditions *)

     initConds} /. params (*put in params *),

   {mRNA, kxscribe}, {t, 0, 1000}];
$\endgroup$

1 Answer 1

3
$\begingroup$

You don't have to use WhenEvent[ ] for direct dependencies on the independent variable:

params = {kmRNAdeg -> 0.03 };
period = 100;
initConds = {mRNA[0] == 5};
odes = {mRNA'[t] == kxscribe[t] - kmRNAdeg*mRNA[t]};
kxscribe[t_] := .25 /; Mod[t, period] <= period/2
kxscribe[t_] := 1 /; Mod[t, period] > period/2

sol = First@ NDSolveValue[{odes, initConds} /. params , {mRNA}, {t, 0, 1000}]

Plot[{sol[t], 30 kxscribe[t]}, {t, 0, 1000}, AxesOrigin -> {0, 0}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Great! Is the /; after the kxscribe[t_] a conditional? Also, can you tell me why you chose to use NDSolveValue here instead of NDSolve? Thanks! $\endgroup$
    – user13999
    Oct 7, 2015 at 19:41
  • $\begingroup$ @user13999 1) See Condition. 2) NDSolveValue[ ] returns a slightly more convenient form for plotting, no big deal $\endgroup$ Oct 7, 2015 at 20:04
  • $\begingroup$ Or, kxscribe[t_] := Rescale[SquareWave[x/period], {1, -1}, {0.25, 1}] $\endgroup$ Oct 10, 2015 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.