# Why does NDSolve run into problems here?

I am looking at a particle moving in a funny potential, described by the following differential equation. The solution should be just some kind of periodic trajectory, why is there a problem here?

Edit: here is the code:

k = NDSolve[{x'[t]^2 + 1/Cos[x[t]]^2 == 2, x[0] == 0},
x[t], {t, 0, 4}]

• Please provide your Mathematica code for this interesting problem! Aug 11, 2021 at 16:20
• Assuming x[t] to be real your ode evaluates negative x'[t]^2 for x[t]>= Pi/4 !!! That's why NDSolve stops for x[t]==Pi/4. Aug 11, 2021 at 16:52
• Both terms are squared, how can anything become negative? Aug 11, 2021 at 18:03
• @korni1990 It evaluates to false, because 1/Cos[x[t]]^2 will get larger than 2 and the other term is squared. Aug 11, 2021 at 18:33
• I see, thanks! But why is x[t] not simply decreasing after reaching the 'turning point' as it should? Is this not possible with NDSolve? Aug 11, 2021 at 18:36

Your differential equation has a singularity at $$x(t) = \pi/2 \approx 1.54$$, and that is why the integrator has troubles approaching that point. To solve this, use:

1. Initial value for the first derivative

$$x(0) = 0 \implies x'(0) = \pm 1$$

1. Method -> {"EquationSimplification" -> "Residual"}
k = NDSolve[{x'[t]^2 + 1/Cos[x[t]]^2 == 2, x'[0] == 1
}, x[t], {t, 0, 10},
Method -> {"EquationSimplification" -> "Residual"}]
Plot[Evaluate[x[t]] /. k, {t, 0, 10}]


• Cool thanks, that does the job indeed. Aug 11, 2021 at 19:11
• Your code in "12.3.1 for Mac OS X x86 (64-bit) (June 19, 2021)" gives an error: "NDSolve::ndcf: Repeated convergence test failure at t == 7.7854479164218; unable to continue." Are you using a different version, or did you do something not shown in the code? Aug 11, 2021 at 22:50
• @MichaelE2 No, I run exactly the same code in 12.3.0 for Microsoft Windows (64-bit) (May 10, 2021) without errors. Aug 12, 2021 at 11:47
• In my case, NDSolve is probably stepping slightly below -Pi/4, but I wonder what changed (assuming it’s version-related, not OS/CPU related). Aug 12, 2021 at 14:23

Your initial condition implies x[0] == 0 && x'[0] == 1.
Using the corresponding 2nd order equation works well:

eq = D[x'[t]^2 + 1/Cos[x[t]]^2 == 2, t];
sols = x[t] /. NDSolve[{eq, x[0] == 0, x'[0] == 1}, x[t], {t, 0, 5}];
Plot[sols, {t, 0, 5}, PlotRange -> All]
`