# Solving ODE having function which changes conditionally

I want to solve the following set of ODE with a piecewise function A[t] which changes if a certain condition is satisfied.

eq1 = y1''[t] + 0.5 y2'[t] + 0.7 y1[t] + A[t] == 0;

eq2 = y2''[t] + 0.3 y1'[t] + 1.3 y2[t] == 0;

A[t]=0 when y1[t]<=y2[t] for all t

A[t]=Sin[t] when y1[t]>y2[t] for all t

y1' == 1, y1 == 0, y2' == 1, y2 == 0


I have tried with WhenEvent inside NDSolve, but it only gives correct result if A[t] is purely discrete using 'DiscreteVariables' option. The following is the code for that.

ti = 0; tf = 30;

eq1 = y1''[t] + 0.5 y2'[t] + 0.7 y1[t] + A[t] == 0;

eq2 = y2''[t] + 0.3 y1'[t] + 1.3 y2[t] == 0;

sol = NDSolve[{eq1, eq2, y1'[ti] == 1, y1[ti] == 0, y2'[ti] == 1, y2[ti] == 0,
A[ti] == If[y1[ti] <= y2[ti], 0, 10], {WhenEvent[y1[t] <= y2[t],
A[t] -> 0], WhenEvent[y1[t] > y2[t], A[t] -> 10]}}, {y1, y2, A}, {t,
ti, tf}, DiscreteVariables -> A][];

Plot[Evaluate[{y1[t], y2[t], A[t]} /. sol], {t, ti, tf},
PlotRange -> All, PlotLegends -> Automatic]


So, please help and suggest if I want to use A[t] as mentioned earlier.

• Why not use Piecewise[]? – J. M. is away May 18 '15 at 13:23
• @Guesswhoitis. Piecewise[] is also not working. I tried with this:--- ti = 0; tf = 30; A[t_] := Piecewise[{0, y1[t] <= y2[t]}, {10*Sin[t], y1[t] > y2[t]}] eq1 = y1''[t] + 0.5 y2'[t] + 0.7 y1[t] + A[t] == 0; eq2 = y2''[t] + 0.3 y1'[t] + 1.3 y2[t] == 0; sol = NDSolve[{eq1, eq2, y1'[ti] == 1, y1[ti] == 0, y2'[ti] == 1, y2[ti] == 0}, {y1, y2, A}, {t, ti, tf}][]; Plot[Evaluate[{y1[t], y2[t]}], {t, ti, tf}, PlotRange -> All, PlotLegends -> Automatic] – Soumyajit Roy May 18 '15 at 13:38
• @Guesswhoitis. But, defining A[t] as piecewise function prior to NDSolve is not solving the issue, because there is no y1[t] and y2[t] values to check before solving the eqns. So, I think, the checking is to be done in every time step (t) within NDSolve. – Soumyajit Roy May 18 '15 at 13:46
• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey May 18 '15 at 14:09

## 1 Answer

Here is one way to use Piecewise, which might well be what Guess who it is. had in mind.

ti = 0; tf = 30;
eq1 = y1''[t] + 0.5 y2'[t] + 0.7 y1[t] + A[t] == 0;
eq2 = y2''[t] + 0.3 y1'[t] + 1.3 y2[t] == 0;

{sol} = NDSolve[{
eq1, eq2, y1'[ti] == 1, y1[ti] == 0, y2'[ti] == 1, y2[ti] == 0,
A[t] == Piecewise[{{Sin[t], y1[t] > y2[t]}}, 0]},
{y1, y2, A}, {t, ti, tf}];

Plot @@ {Through[{y1, y2, 10 A[#] &}[t]] /. sol, Flatten[{t, y1["Domain"] /. sol}]} • That's exactly it, yes. :D (Please settle for an upvote IOU for the time being…) – J. M. is away May 18 '15 at 16:22