# Struggling with WhenEvent in NDSolve

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 *),

odes = {

kxscribe'[t] == 0

};

initConds = {
mRNA == 5,
kxscribe == 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}];


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

params = {kmRNAdeg -> 0.03 };
period = 100;
initConds = {mRNA == 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}] • 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! – user13999 Oct 7 '15 at 19:41
• @user13999 1) See Condition. 2) NDSolveValue[ ] returns a slightly more convenient form for plotting, no big deal – Dr. belisarius Oct 7 '15 at 20:04
• Or, kxscribe[t_] := Rescale[SquareWave[x/period], {1, -1}, {0.25, 1}] – J. M. will be back soon Oct 10 '15 at 2:01