I have a discontinuous function ($u(t)$, a square wave) and I would like WhenEvent
to trigger when the signal goes high/low, i.e. when the value of $u(t)$ changes. I was hoping to use the derivative of $u(t)$ to determine this.
squareWave[t_, period_, duty_] := UnitBox[Mod[t/period, 1.]/(2. duty)]
system = {x'[t] == u[t], WhenEvent[Mod[t, 0.5 τ],
u[t] -> squareWave[t, 2 τ, 0.2]], x[0] == 0, u[0] == 1,
WhenEvent[D[u[t]] > 0, Print[t]]};
params = {τ -> 1};
sol = NDSolve[system /. params, {x, u}, {t, 0, 10},
DiscreteVariables -> u];
Plot[Evaluate[{x[t], u[t], u'[t]} /. sol], {t, 0, 10},
PlotLegends -> {"x(t)", "u(t)", "u'[t]"}]
Unfortunately this does not seem to work? The output is:
UnitBox
withHeavisidePi
, but it still doesn't work. $\endgroup$