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

enter description of image here

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}]]]}]

enter description of image here

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}]]]}]

enter description of image here

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:
enter description of image here

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.

enter description of image here

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 <t leq 2 \
1 & 2 <t leq 4 \
text {Undetermined} & text {True}
end {cases} $

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 <t leq 2 \
frac {3-t} {2} & 2 <t leq 3 \
0 & 3 <t leq 4 \
text {Undetermined} & text {True}
end {cases} $

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?

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?

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

z (t)
p (t)

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.

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]

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

enter the description of the image here

problems occur if I change the simulation time tsim to 1.5 (example):

enter the description of the image here

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!