NDSolve::svnder NDSolveValue::svnder ParametricNDSolve::svnder ParametricNDSolveValue::svnder
Examples
Basic Examples (2)
The highest-order derivatives cannot be set with WhenEvent directly:
NDSolve[{x'[t] == y[t], y'[t] + y[t] + 2 x[t] == 0, x[0] == 0, y[0] == 1, WhenEvent[Mod[t, 3] == 0, {x'[t], y'[t]} -> {4 y[t], x[t]}]}, {x, y}, {t, 0, 20}]As the message indicates, Method{"EquationSimplification""Residual"} is an alternative:
NDSolve[{x'[t] == y[t], y'[t] + y[t] + 2 x[t] == 0, x[0] == 0, y[0] == 1, WhenEvent[Mod[t, 3] == 0, {x'[t], y'[t]} -> {4 y[t], x[t]}]}, {x, y}, {t, 0, 20}, Method -> {"EquationSimplification" -> "Residual"}]In some cases, Method{"EquationSimplification""Residual"} is an not an alternative. Consider this system:
NDSolve[{w'[t] == 0.03 * w[t], k'[t] == 0.2 * w[t] + 0.03 * k[t], w[20] == 50.0, k[20] == 0.0, WhenEvent[t > 65, {k'[t] -> -70.0 + 0.03 k[t], w'[t] -> 0.0}]}, {w, k}, {t, 20, 100}];One way to modify the actual equations is to make use of DiscreteVariables to switch on or off components of the overarching equation:
solution = NDSolve[{w'[t] == 0.03 * w[t], k'[t] == α[t] * 0.2 * w[t] + 0.03 * k[t] + β[t], w[20] == 50.0, k[20] == 0.0, α[20] == 1, β[20] == 0, WhenEvent[t > 65, {α[t] -> 0, β[t] -> -70, w[t] -> 0.0}]}, {w, k}, {t, 20, 100}, DiscreteVariables -> {α, β}];Plot[Evaluate[{k[t]} /. solution], {t, 20, 100}, GridLines -> Automatic, ImageSize -> 400, AxesLabel -> {"t", "k[t]"}]A simpler way to proceed is to have NDSolve automatically insert the WhenEvent:
solution = NDSolve[{w'[t] == Piecewise[{{0.03 * w[t], t <= 65}}, 0.], k'[t] == Piecewise[{{0.20 * w[t] + 0.03 * k[t], t <= 65}}, -70.0 + 0.03 k[t]], w[20] == 50.0, k[20] == 0.0}, {k, w}, {t, 20, 200}];Plot[Evaluate[{k[t]} /. solution], {t, 20, 100}, GridLines -> Automatic, ImageSize -> 400, AxesLabel -> {"t", "k[t]"}]