So I have a rather non-trivial differential equation. I can't solve it analytically so I want to solve it numerically using NDSolve. But the thing is that the output just becomes what I had as input, the command does not execute.

So, I have a Lagrangian given by: L2 = r^3 (((1/Pi)*Sqrt[(1^4)*(Sin[x[r]]^4) + (Pi*(800) - x[r] + (1/2)*Sin[2*x[r]])^2])*(1 + ((1/2)*(x'[r]^2))) + (1/Pi)*(Pi*(800) - x[r] + (1/2) Sin[2*x[r]]))

With some help from below (<3) I get:

eq={D[D[L2, x'[r]], r] == D[L2, x[r]], x[0] == 0, x'[0] == 0}

Then I use NDSolve to get

NDSolve[eq, x, {r, 0, Infinity}]

Where the range of x[r] is from zero to Pi and range of r is from zero to infinity.

But I get some errors:

1: "Power:Infinite expression $\frac{1}{0^3}$ encountered." 2: "Infinity: Indeterminate expression 0.π ComplexInfinity encountered." 3: "NDSolve: Encountered non-numerical value for a derivative atr==0."

I know that some people have solved this Lagrangian numerically in a paper so should be possible.

Are there any workarounds I could do to these error messages? Many thanks in advance!

  • $\begingroup$ There are some extra commas in your second expression and your ( and [ don't seem to match up. I'll see if I can figure out how to fix but you might be able to fix it faster than I can. $\endgroup$ Dec 30, 2020 at 17:46
  • $\begingroup$ This works better but complains that it needs more initial conditions: NDSolve[D[D[L2[x[r], x'[r], r], x'[r]], r] == D[L2[x[r], x'[r], r], x[r]], {x[r], 0, Pi}, {r, 0, Infinity}] $\endgroup$ Dec 30, 2020 at 17:53
  • 1
    $\begingroup$ A second order ODE needs 2 initial conditions for a numerical solution. $\endgroup$ Dec 30, 2020 at 19:08
  • $\begingroup$ This doesn't seem to work either: NDSolve[{D[D[L2[x[r], x'[r], r], x'[r]], r] == D[L2[x[r], x'[r], r], x[r]], x[0] == 0, x'[0] == 0}, {x[r], 0, Pi}, {r, 0, Infinity}] $\endgroup$ Dec 30, 2020 at 20:28
  • $\begingroup$ True, I still don't get it to work with some conditions x[0] == something, x'[0] == something. The cell just evaluate to "True" and I get an error message saying that there should be an equation in the argument instead of "True"... @DanielHuber $\endgroup$ Jan 1, 2021 at 17:44

1 Answer 1


I assume that x[t] in the definition of L2 is a typo and should read x[r].

With this assumption, you can simply define L2 as:

L2 = r^3 ((V2[x[r], r])*(1 + ((1/2)*(x'[r]^2))) + (1/Pi)*(Pi*(800) - 
       x[r] + (1/2) Sin[2*x[r]]))

The equations then read:

eq={D[D[L2, x'[r]], r] == D[L2, x[r]], x[0] == 0, x'[0] == 0}

Furthermore, the syntax of NDSolveis wrong. {x[r], 0, Pi} is not needed as x[r] is the searched for solution and its range is given by the range of r.

Note further that the equations can not be solved without specifying V2.

The command to solve the ODE then reads:

NDSolve[eq, x, {r, 0, Infinity}]

But to solve it, you need V2

  • $\begingroup$ Thanks for this @Daniel Huber, very helpful! You are right. That was indeed a typo in the copy-pasting process. So I did do everything in your answer and substituted V2 explicitly in L2. I'm one step closer, but unfortunately, I get into a number of problems. 1: "Power: infinite expression encountered" 2: "Infinity: Indeterminate expression 0, Pi ComplexInfinity encountered." 3:" NDSolve: Encountered non-numerical value for a derivative at r==0." Are there any workarounds to this problem? May I ask how you knew to put x[0] == 0, x'[0] == 0? $\endgroup$ Dec 31, 2020 at 14:12
  • $\begingroup$ I only said you need 2 initial conditions. Their values depend on the problem at hand and is presumably not zero. $\endgroup$ Jan 1, 2021 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.