# How to use conditional clause inside NDSolve

I am trying to solve a differential equation that changes its behaviour depending on the value of the dependent variable. I've tried as shown below, but the computer does not understand my If clause as an "equation or set of equations".

NDSolve[{If[x[t] > 0, x''[t] == -x[t], x''[t] == -2*x[t]], x[0] == 1,
x'[0] == 0}, x, {t, 0, 10}]


How can I make this work?

• You can surely use WhenEvent[] as @anders describes, but otherwise, your If[] is not properly structured: NDSolve[{x''[t] == If[x[t] > 0, -x[t], -2 x[t]], x[0] == 1, x'[0] == 0}, x, {t, 0, 10}] Feb 3, 2020 at 15:30

You can use WhenEvent (note the use of DiscreteVariables):
sol = NDSolve[{x''[t] == a[t]*x[t], x[0] == 1, x'[0] == 0, a[0] == -1,