# NDSolve Encountered non-numerical value for a derivative at t == 0

I want to solve this no-linear differential equation system:

$$1.2 (1 + 20(r(t)^2 + 2^2 + 4 r(t) \sin(\phi(t)))^2) \ddot\phi(t) + \dot \phi(t) + \phi(t) = 50 \sin(4 t)$$

$$\textbf{If}$$ ($$|-r(t)\dot\phi^2(t)+9.8\sin\phi(t)|$$<$$0.7\times 9.8\cos\phi(t)$$)$$\Rightarrow$$ $$\ddot r(t)=0$$

$$\textbf{If}$$ ($$-r(t)\dot\phi^2(t)=<9.8\sin\phi(t)$$)$$\Rightarrow$$ $$\ddot r(t)-r(t)\dot\phi^2+9.8(\sin\phi(t)-0.7\cos\phi(t))=0$$

$$\textbf{If}$$ ($$-r(t)\dot\phi^2(t)>9.8\sin\phi(t)$$)$$\Rightarrow$$ $$\ddot r(t)-r(t)\dot\phi^2+9.8(\sin\phi(t)+0.7\cos\phi(t))=0$$

Besides, When $$r(t)>1\Rightarrow\dot r(0)=0$$ and $$r(t)=1$$ or $$r(t)<-1\Rightarrow\dot r(0)=0$$ and $$r(t)=-1$$.

So to solve this differential equation system, I use NDSolve method. Nevertheless, i am getting the following error: NDSolve: Encountered non-numerical value for a derivative at t == 0 and i do not know Where it is the error. The code used is below:

 Clear["Global*"]
sol = NDSolve[{If[
Abs[-r[t] phi'[t]^2 + 9.8 Sin[phi[t]]] <= 9.8*0.7 Cos[phi[t]],
r''[t], If[-r[t] phi'[t]^2 + 9.8 Sin[phi[t]] >= 0,
r''[t] - r[t] phi'[t]^2 + 9.8 (Sin[phi[t]] - 0.7*Cos[phi[t]]),
r''[t] - r[t] phi'[t]^2 +
9.8 (Sin[phi[t]] + 0.7*Cos[phi[t]])]] == 0,
1.2 (1 + 20*(r[t]^2 + 2^2 + 4 r[t] Sin[phi[t]])^2) phi''[t] +
phi'[t] + phi[t] == 50 Sin[4 t],
r[0] == phi[0] == r'[0] == phi'[0] == 0,
WhenEvent[r[t] > 1, {r[t] -> 0.99998, r'[t] -> 0}],
WhenEvent[r[t] < -1, {r[t] -> -0.99998, r'[t] -> 0}]}, {r,
phi}, {t, 0, 100}]

Plot[Evaluate[{r'[t], r[t]} /. sol], {t, 0, 100}, PlotRange -> All,
PlotStyle -> {Blue, Black}, PlotLegends -> {"Speed", "Position"}]


If I remove the if condition and i put the following code r''[t] - r[t] phi'[t]^2 + 9.8 (Sin[phi[t]] - 0.7*Cos[phi[t]]) That it is the then condition of the second If ti works...

Thank you for you time

• The three If that you see in the post it is a piecewise function, that is it depends of what condtion it is true, i will have one equation so the total number of equation it will be $2$. I do not know where you see three equation rather than two, Have i written wrong the code? Commented Oct 15, 2021 at 10:13

NDSolve cannot handle the first ode. If you separate r''[t] out of your If-construct it works as expected:

sol = NDSolve[{r''[t] +
If[Abs[-r[t] phi'[t]^2 + 9.8 Sin[phi[t]]] <= 9.8*0.7 Cos[phi[t]],
0, If[-r[t] phi'[t]^2 + 9.8 Sin[phi[t]] >= 0,
0 - r[t] phi'[t]^2 + 9.8 (Sin[phi[t]] - 0.7*Cos[phi[t]]),
0 - r[t] phi'[t]^2 + 9.8 (Sin[phi[t]] + 0.7*Cos[phi[t]])]] ==
0, 1.2 (1 + 20*(r[t]^2 + 2^2 + 4 r[t] Sin[phi[t]])^2) phi''[t] +
phi'[t] + phi[t] == 50 Sin[4 t],
r[0] == phi[0] == r'[0] == phi'[0] == 0,
WhenEvent[r[t] > 1, {r[t] -> 0.99998, r'[t] -> 0}],
WhenEvent[r[t] < -1, {r[t] -> -0.99998, r'[t] -> 0}]}, {r,
phi}, {t, 0, 100}]

Plot[Evaluate[{r'[t], r[t]} /. sol], {t, 0, 100}, PlotRange -> All,
PlotStyle -> {Blue, Black}, PlotLegends -> {"Speed", "Position"}]


• Thanks a lot!! So the thing that was wrong was that in NDSolve, outside of If statatement, must be some function? Commented Oct 15, 2021 at 10:23
• NDSolve` probably fails when dissolving for the highest derivative of the ode. Commented Oct 15, 2021 at 11:32