## scope – Using local variables in WhenEvent

Using `WhenEvent` inside `Module` is quite strange. Local variables are not evaluated in `WhenEvent`. for example

``````eq = Module({time1 = 200, time2 = 400},
{WhenEvent(t > time1, a(t) -> 1),
WhenEvent(t > time2, a(t) -> 0)})
``````

Return

``````{WhenEvent(t > time1$$1064, a(t) -> 1), WhenEvent(t > time2$$1064, a(t) -> 0)}
``````

I guess it has something to do with the attribute of the `WhenEvent`?

``````Attributes(WhenEvent)
(* {HoldAll, Protected} *)
``````

Is there anyway to force local variables into `WhenEvent` To be evaluated?

## event detection – Editing the equation using WhenEvent in NDSolve

I want to modify part of a set of equations using `WhenEvent` in `NDSolve`. I have made an example of much less complicated minimum work as follows:

``````sol = x /. First@NDSolve[{
x''
If[x
50 x
16 50 x
x'[0] == 0,
x[0] == 1
},
{x}, {t, 0, 2}];
Plot[sol
``````

It doesn’t use `WhenEvent` but changes a term in the equation. Now i'm trying to present a `WhenEvent`. I also introduce a module that contains both versions of the term in the equation.

``````ClearAll[ff];
ff[status_, t_?NumericQ, x_?NumericQ] :=
If[x < 0,
Sow[{t, x, -1, status}, tag1]; 50 x ,
Sow[{t, x,   1, status}, tag1]; 16 50 x ];

status = "More";
a = Reap[
res = Reap[
sol = x /. First@NDSolve[{
x''
ff[status, t, x
x'[0] == 0, x[0] == 1,

WhenEvent[x
If[x
Sow[{t, x
{x}, {t, 0, 2}];,
tag1];,
tag2];
Plot[sol
Epilog -> {Line[res[[2, 1, All, {1, 3}]]], Red, PointSize[0.01],
Point[a[[2, 1, All, {1, 2}]]]}]
``````

It also worked and both `Sow` the functions collect various values ​​which show that everything works. I have introduced a state value which indicates which of the two states I am. I would like to use the state value to control the switching of the term in the differential equation. So I modify the function `ff` so the status is examined rather than the value of the variable. It does not work.

``````ClearAll[ff];
ff[status_, t_?NumericQ, x_?NumericQ] :=
If[status == "Less",
Sow[{t, x, -1, status}, tag1]; 50 x ,
Sow[{t, x,   1, status}, tag1]; 16 50 x ];

status = "More";
a = Reap[
res = Reap[
sol = x /. First@NDSolve[{
x''
ff[status, t, x
x'[0] == 0, x[0] == 1,

WhenEvent[x
If[x
Sow[{t, x
{x}, {t, 0, 2}];,
tag1];,
tag2];
Plot[sol
Epilog -> {Line[res[[2, 1, All, {1, 3}]]], Red, PointSize[0.01],
Point[a[[2, 1, All, {1, 2}]]]}]
``````

Why it does not work? How can I fix it?

## differential equations – WhenEvent when using NDSolve does not work properly

``````sol = NDSolve({x''(t) == -x(t)*t + a(t)*x(t), x(0) == 1, x'(0) == 0,
WhenEvent(x(t) == 0, Tb(t) -> t),
WhenEvent(t > Tb(t) + 0.5, a(t) -> 1),
WhenEvent(t <= Tb(t) + 0.5, a(t) -> 0), Tb(0) == 0, a(0) == 0}, {x,
Tb, a}, {t, 0, 10}, DiscreteVariables -> {Tb, a})
``````

I am trying to solve a differential equation with some discrete components (Tb (t), a (t)). Tb (t) is supposed to be a stair function that records the moments at which x (t) = 0. Tb (t) works as expected:

a (t) however, does not work as expected. a (t) had to go from 0 to 1 each time (t- (Tb (t) +0.5)) is equal to zero, but as we can see on the images below, a (t) does not makes the first shift correctly, then stagnates at 1 when it should be moving back and forth.

How can I make this work properly.

## differential equations – Trigger WhenEvent after WhenEvent

I'm trying to solve a quasi-simple differential equation with events:

``````there
{y[0] == 0, y & # 39;[0] == 0, y & # 39;
When event[T==1y&#39;[T==1y'[t==1y'[t==1y'
When event[T==2y&#39;[T==2y'[t==2y'[t==2y'
When event[y[y[y[y
When event[There&#39;[Y'[y'[y'
there
``````

The result is not what I expected, the `y & # 39;`

``` $$begin {cases} 0 & 0 leq t leq 1 \ t-1 & 1 I can get around this condition with a hack that includes changing the y & # 39; there {y[0] == 0, y & # 39;[0] == 0, y & # 39; When event[T==1y&#39;[T==1y'[t==1y'[t==1y' WhenEvent[t == 2, y' When event[y[y[y[y there $$begin {cases} 0 & 0 leq t leq 1 \ frac {t-1} {2} & 1 That's the answer I was waiting for. Is there a way to make the first differential equation specification work like the second, and if it does not, should it be considered a bug? ```
``` jQuery(document).ready(function() {jQuery.ajax({type: "POST",url : rating_form_script.ajaxurl,data : { action : "display_rating_form_ajax", args : {"id":1,"post_id":101116,"comment_id":0,"custom_id":"0","user_id":0,"term_id":0,"title":false,"score":true,"total":true,"stats":true,"user_stats":false,"tooltip":true,"result":true,"rich_snippet":true,"is_widget":false,"state":"","before_content":"","after_content":"","rates":"rating,ratings","txt_score":"%1\$s\/%2\$s"} }, success : function(data) { jQuery("body").find("[data-id=\"RFR1P101116\"]").html(data); }});}); { "@context": "http://schema.org", "@type": "Article", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://newproxylists.com/differential-equations-trigger-whenevent-after-whenevent/" }, "headline": "differential equations - Trigger WhenEvent after WhenEvent", "url": "https://newproxylists.com/differential-equations-trigger-whenevent-after-whenevent/", "author": { "@type": "Person", "name":"Admin" }, "publisher": { "@type": "Organization", "name": "New and Fresh Private + Public Proxies Lists Everyday!", "logo": { "@type": "ImageObject", "url": " ", "width": "", "height": "" } }, "datePublished": "2019-06-09T16:48:39+03:00", "dateModified": "2019-06-09T16:48:39+03:00", "aggregateRating": { "@type": "AggregateRating", "bestRating": "5", "worstRating": "1", "ratingValue": "5", "ratingCount": "18" } } ```
``` Author AdminPosted on June 9, 2019Tags differential, equations, trigger, WhenEvent ```
``` Differential Equations – WhenEvent Function versus If for ODEs The following equation works for ParametricNDSolve PKmodel = { Amt1 & # 39; Yes[0 < Mod[t, [Tau]]<InfusionTime, Dose / InfusionTime, 0]- CL / V1 Amt1 Vmax Amt1 Amt2 & # 39; AMT1[0] == 0, AMT2[0] == 0 }; However, the following code did not work for ParametricNDSolve PKmodel = { Amt1 & # 39; Vmax Amt1 Amt2 & # 39; AMT1[0] == 0, AMT2[0] == 0, When event[0 < Mod[t, [Tau]] < InfusionTime, Amt1' So, whenEvent could not work for an entry over a period of time? A reason for that? Secondly, using the If function, ParametricNDSolve works to resolve ODEs, but I have a numeric problem when executing the following function, although mathematica still gives me some results. AUClist = NIntegrate[ Evaluate[Table[(Amt1[Dose] doselistug}]], {t, 0, tmax}](* ug / mL * day *) Any suggestions for solving this? jQuery(document).ready(function() {jQuery.ajax({type: "POST",url : rating_form_script.ajaxurl,data : { action : "display_rating_form_ajax", args : {"id":1,"post_id":91687,"comment_id":0,"custom_id":"0","user_id":0,"term_id":0,"title":false,"score":true,"total":true,"stats":true,"user_stats":false,"tooltip":true,"result":true,"rich_snippet":true,"is_widget":false,"state":"","before_content":"","after_content":"","rates":"rating,ratings","txt_score":"%1\$s\/%2\$s"} }, success : function(data) { jQuery("body").find("[data-id=\"RFR1P91687\"]").html(data); }});}); { "@context": "http://schema.org", "@type": "Article", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://newproxylists.com/differential-equations-whenevent-function-versus-if-for-odes/" }, "headline": "Differential Equations - WhenEvent Function versus If for ODEs", "url": "https://newproxylists.com/differential-equations-whenevent-function-versus-if-for-odes/", "author": { "@type": "Person", "name":"Admin" }, "publisher": { "@type": "Organization", "name": "New and Fresh Private + Public Proxies Lists Everyday!", "logo": { "@type": "ImageObject", "url": " ", "width": "", "height": "" } }, "datePublished": "2019-05-14T04:57:21+03:00", "dateModified": "2019-05-14T04:57:21+03:00", "aggregateRating": { "@type": "AggregateRating", "bestRating": "5", "worstRating": "1", "ratingValue": "5", "ratingCount": "18" } } Author AdminPosted on May 14, 2019Tags differential, equations, function, ODEs, WhenEvent Differential Equations – Using WhenEvent to Limit the Derivative In the system below, would like to keep z Maybe a user error, but I could not identify it. df = 4. (0.07 z of = {p & # 39; ic = {p[0] == 140., z[0] == 0.5}; events = {WhenEvent[z[z[z[z z & # 39; eqs = flatten[{de, ic, events}]; solODE = NDSolve[eqs, {p, z}, {t, 0, 20}]; Ground[p[p[p[p jQuery(document).ready(function() {jQuery.ajax({type: "POST",url : rating_form_script.ajaxurl,data : { action : "display_rating_form_ajax", args : {"id":1,"post_id":37866,"comment_id":0,"custom_id":"0","user_id":0,"term_id":0,"title":false,"score":true,"total":true,"stats":true,"user_stats":false,"tooltip":true,"result":true,"rich_snippet":true,"is_widget":false,"state":"","before_content":"","after_content":"","rates":"rating,ratings","txt_score":"%1\$s\/%2\$s"} }, success : function(data) { jQuery("body").find("[data-id=\"RFR1P37866\"]").html(data); }});}); { "@context": "http://schema.org", "@type": "Article", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://newproxylists.com/differential-equations-using-whenevent-to-limit-the-derivative/" }, "headline": "Differential Equations - Using WhenEvent to Limit the Derivative", "url": "https://newproxylists.com/differential-equations-using-whenevent-to-limit-the-derivative/", "author": { "@type": "Person", "name":"Admin" }, "publisher": { "@type": "Organization", "name": "New and Fresh Private + Public Proxies Lists Everyday!", "logo": { "@type": "ImageObject", "url": " ", "width": "", "height": "" } }, "datePublished": "2019-01-06T01:21:07+02:00", "dateModified": "2019-01-06T01:21:07+02:00", "aggregateRating": { "@type": "AggregateRating", "bestRating": "5", "worstRating": "1", "ratingValue": "5", "ratingCount": "18" } } Author AdminPosted on January 6, 2019Tags Derivative, differential, equations, limit, WhenEvent differential equations – WhenEvent detects only the first event my code is based on the suggestion of this discutionby @Michael E2 And when I use the same definitions the When event function only locates the first time the event occurs. slope[Alpha] = 0.01; Tend = 10; L = 1; we[m_, P_] : = (1 + Tanh[m (P)]) / 2; [Alpha]1 = 1; [Alpha]2 = 8; elementNum = 25; [CapitalDelta]Psnap = 0.25; maxval[if_: InterpolatingFunction[___][x_]]: = Max[if /. {x -> "ValuesOnGrid"}]; new[if : InterpolatingFunction[___][x_]]: = ConstantArray[1, Length[if /. x -> "Grid"]]; opts = Method -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "MinPoints" -> (elementNum - 1)}}; Pup = 0.7; t0 = 1; [Alpha]1 = 1; [Alpha]2 = 8; sol1 = NDSolve[{D[P[x, t], t]-[Alpha][x, t] re[P[P[P[P[x, t], {x, 2} == 0, P[x, 0] == 0, P[0, t] == UnitStep[t - t0], P[L, t] == 0, [Alpha][x, 0] == [Alpha]2, D[[[[[Alpha][x, t], t]== 0, WhenEvent[maxval[P[maxval[P[maxval[P[maxval[P[x, t]]> = Puppy, print ]}, {P[x, t][Alpha][x, t]}, {t, 0, Tend}, {x, 0, L}, opts]; Manipulate[Ground[{P[Plot[{P[Terrain[{P[Plot[{P[x, t] /. sol1} /. {t -> tt}, {x, 0, L}, PlotRange -> {{0, L}, {0, 1}}, Image Size -> Medium], {tt, 0, Tend}] and the output of printing is 2.20493 although the value of $$P[x,t]$$ remains higher than $$Pup$$ for all the time. Can not find the reason for this problem. Thank you so much, Ofek. jQuery(document).ready(function() {jQuery.ajax({type: "POST",url : rating_form_script.ajaxurl,data : { action : "display_rating_form_ajax", args : {"id":1,"post_id":36671,"comment_id":0,"custom_id":"0","user_id":0,"term_id":0,"title":false,"score":true,"total":true,"stats":true,"user_stats":false,"tooltip":true,"result":true,"rich_snippet":true,"is_widget":false,"state":"","before_content":"","after_content":"","rates":"rating,ratings","txt_score":"%1\$s\/%2\$s"} }, success : function(data) { jQuery("body").find("[data-id=\"RFR1P36671\"]").html(data); }});}); { "@context": "http://schema.org", "@type": "Article", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://newproxylists.com/differential-equations-whenevent-detects-only-the-first-event/" }, "headline": "differential equations - WhenEvent detects only the first event", "url": "https://newproxylists.com/differential-equations-whenevent-detects-only-the-first-event/", "author": { "@type": "Person", "name":"Admin" }, "publisher": { "@type": "Organization", "name": "New and Fresh Private + Public Proxies Lists Everyday!", "logo": { "@type": "ImageObject", "url": " ", "width": "", "height": "" } }, "datePublished": "2019-01-04T12:47:24+02:00", "dateModified": "2019-01-04T12:47:24+02:00", "aggregateRating": { "@type": "AggregateRating", "bestRating": "5", "worstRating": "1", "ratingValue": "5", "ratingCount": "18" } } Author AdminPosted on January 4, 2019Tags detects, differential, equations, event, WhenEvent Differential Equations – Why WhenEvent does not work in this case? You can not change the definition of the function yb[a] in an event when! Try eqns = {x & # 39;[s] == - sgn[s] yb[s], sgn[0] == + 1, WhenEvent[X&#39;[X'[X'[x'[s] > 0, {sgn[s] -> -sgn[s] }], X & # 39;[0] == 0} {X, SGN} = NDSolveValue[eqns, {x, sgn} , {s, -50, 50}, DiscreteVariables -> sgn] jQuery(document).ready(function() {jQuery.ajax({type: "POST",url : rating_form_script.ajaxurl,data : { action : "display_rating_form_ajax", args : {"id":1,"post_id":36270,"comment_id":0,"custom_id":"0","user_id":0,"term_id":0,"title":false,"score":true,"total":true,"stats":true,"user_stats":false,"tooltip":true,"result":true,"rich_snippet":true,"is_widget":false,"state":"","before_content":"","after_content":"","rates":"rating,ratings","txt_score":"%1\$s\/%2\$s"} }, success : function(data) { jQuery("body").find("[data-id=\"RFR1P36270\"]").html(data); }});}); { "@context": "http://schema.org", "@type": "Article", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://newproxylists.com/differential-equations-why-whenevent-does-not-work-in-this-case/" }, "headline": "Differential Equations - Why WhenEvent does not work in this case?", "url": "https://newproxylists.com/differential-equations-why-whenevent-does-not-work-in-this-case/", "author": { "@type": "Person", "name":"Admin" }, "publisher": { "@type": "Organization", "name": "New and Fresh Private + Public Proxies Lists Everyday!", "logo": { "@type": "ImageObject", "url": " ", "width": "", "height": "" } }, "datePublished": "2019-01-04T00:27:06+02:00", "dateModified": "2019-01-04T00:27:06+02:00", "aggregateRating": { "@type": "AggregateRating", "bestRating": "5", "worstRating": "1", "ratingValue": "5", "ratingCount": "18" } } Author AdminPosted on January 4, 2019Tags Case, differential, equations, WhenEvent, work differential equations – stick-slip oscillator: Missed WhenEvent Using Mathematica, I'm trying to model the slippage effect of the stick as recommended in the documentation (WhenEvent). The initial condition starts from a state X[0]== 0, x & # 39;[0]== 0, F[0]== 0 with stick conditions fulfilled. Here is the model that seems to work Clear[X, HAFT, frast] F[t_]: = Sin[2 Pi 5 t]; frast = 0.5; Manipulate[ {X, HAFT} = NDSolveValue[{.1 x'' X[0] == 0, x & # 39;[0] == 0, round[0] == 1 , Apply[WhenEvent{x&#39;[WhenEvent{x'[WhenEvent{x'[WhenEvent{x' , Apply[WhenEvent, {Abs[ F , {x, haft}, {t, 0, tsim}, DiscreteVariables -> haft]; Ground[{X[{X[{X[{X problems occur if I change the simulation time tsim to 1.5 (example): I wonder why MMA seems to miss the first event (t ~ = 0.015, force == 0.5)? My question: Is it possible to make event detection more robust? Thank you! jQuery(document).ready(function() {jQuery.ajax({type: "POST",url : rating_form_script.ajaxurl,data : { action : "display_rating_form_ajax", args : {"id":1,"post_id":14260,"comment_id":0,"custom_id":"0","user_id":0,"term_id":0,"title":false,"score":true,"total":true,"stats":true,"user_stats":false,"tooltip":true,"result":true,"rich_snippet":true,"is_widget":false,"state":"","before_content":"","after_content":"","rates":"rating,ratings","txt_score":"%1\$s\/%2\$s"} }, success : function(data) { jQuery("body").find("[data-id=\"RFR1P14260\"]").html(data); }});}); { "@context": "http://schema.org", "@type": "Article", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://newproxylists.com/differential-equations-stick-slip-oscillator-missed-whenevent/" }, "headline": "differential equations - stick-slip oscillator: Missed WhenEvent", "url": "https://newproxylists.com/differential-equations-stick-slip-oscillator-missed-whenevent/", "author": { "@type": "Person", "name":"Admin" }, "publisher": { "@type": "Organization", "name": "New and Fresh Private + Public Proxies Lists Everyday!", "logo": { "@type": "ImageObject", "url": " ", "width": "", "height": "" } }, "datePublished": "2018-12-07T20:52:08+02:00", "dateModified": "2018-12-07T20:52:08+02:00", "aggregateRating": { "@type": "AggregateRating", "bestRating": "5", "worstRating": "1", "ratingValue": "5", "ratingCount": "18" } } Author AdminPosted on December 7, 2018Tags differential, equations, Missed, oscillator, stickslip, WhenEvent ```