I solved this ode by hand and obtained the general solution. But not able to solve for the constants of the integrations given the initial conditions, as when I try, I get no solution.

Yet, Mathematica is able to do it, and would like to find how it did it. The problem is from text book

enter image description here

Mathematica gets the correct solution. This is what I did

ClearAll[y, x];
ode = y''[x] == -Exp[-2*y[x]]
(*just use one solution for now, but I tried both also*)
sol = y[x] /. First@DSolve[ode, y[x], x]  

Mathematica graphics

So now initial conditions

 y[3] == 0
 y'[3] == 1 

Need to be applied to the above solution in order to solve for C[1],C[2] and here is the problem. Setting up the two equations and asking Solve to Solve for C[1],C[2]

eq1 = 0 == Limit[sol, x -> 3] // Simplify
eq2 = 1 == Limit[D[sol, x], x -> 3] // Simplify

Mathematica graphics

Solve[{eq1, eq2}, {C[1], C[2]}]

Tried Reduce, SolveAlways, FindInstance and nothing works. Unable to find solution.

But Mathematica gets the correct solution:

ClearAll[y, x];
ode = y''[x] == -Exp[-2*y[x]]
ic = {y[3] == 0, y'[3] == 1}
sol = DSolve[{ode, ic}, y[x], x]  

Mathematica graphics

How did Mathematica solve for the constants of integration? What method did it use?


Figured how to solve it by hand. This looks like how DSolve did it, from the Trace shown by Michael below.

The whole idea it to solve for one constant early one (from the $y'$ and only then solve for $y$. This is easier to do by hand than program it in).

\begin{align*} y^{\prime\prime} & =-e^{-2y}\\ y\left( 3\right) & =0\\ y^{\prime}\left( 3\right) & =1 \end{align*} Let $p=y^{\prime}$, then $y^{\prime\prime}=\frac{dp}{dx}=\frac{dp}{dy} \frac{dy}{dx}=\frac{dp}{dy}p$. The ode becomes $$ p\frac{dp}{dy}=-e^{-2y} $$ This is first order ode. The solution is \begin{equation} p=\pm\sqrt{e^{-2y}+2c_{1}}\tag{1} \end{equation} Taking one solution for now (same for the other), then $p=\sqrt{e^{-2y}+2c_{1}}$. But from initial conditions $p\left( 3\right) =1,y\left( 3\right) =0$. Hence \begin{align*} 1 & =\sqrt{e^{-2\left( 0\right) }+2c_{1}}\\ 1 & =\sqrt{1+2c_{1}}\\ c_{1} & =0 \end{align*} The solution (1) becomes \begin{align*} p & =\sqrt{e^{-2y}}\\ & =e^{-y} \end{align*} Or since $p=\frac{dy}{dx}$ $$ \frac{dy}{dx}=e^{-y} $$ This is first order ode whose solution is (taking one of them now) \begin{equation} y=\ln\left( x+c_{2}\right) \tag{2} \end{equation} But $y\left( 3\right) =0$, hence $$ 0=\ln\left( 3+c_{2}\right) $$ Hence $3+c_{2}=1$ or $c_{2}=-2$. Therefore the solution (2) becomes $$ y=\ln\left( x-2\right) $$

DSolve is very good. Without doing this, and waiting for the end to solve both constants at same time (like I was doing), it would have got into trouble too. But it is much smarter than this.


1 Answer 1


Not sure what DSolve did, but I often try Sinc[z] when I see Sin[z]/z or Sinh[z]/z, because it gets around the need for Limit near z == 0. Since C[1] == 0 is excluded as a solution in the form of the solution returned by DSolve, this seemed worth trying.

dsol = DSolve[ode, y, x]; (* get Function form *)

   {y[3], y'[3]} == {0, 1} /. Last@dsol (* N.B. *)
   ] /.
  {Sinh[z_] :> z*Sinc[I z], Coth[z_] :> Cosh[z]/(z*Sinc[I z])},
 {C[1], C[2]}]

(* {{C[1] -> 0, C[2] -> -2}}  *)


I think DSolve does something like the following, based on tracing Solve during the computation:

  1. It solves the IVP for y'[x] by multiplying the ODE on both sides by y'[x] and integrating. It gets 1/2 y'[x]^2 == 1/2 E^(-2 y[x]), using the ICs to eliminate a constant of integration.

  2. It reduces to two equations y'[x] == ±E^-y[x].

  3. It then integrates these (they're separable), to get y[x] == Log[±x + C[1]].

  4. Again it uses the ICs to solve for C[1] and throw out the solution that does not satisfy the ICs.

The following shows the Solve[] commands. What DSolve is doing from step to step is just my guess. (For instance, the Solve[] with the long internal variable name is the equation one solves to get the constant of integration in step 1 above.)

   DSolve[{ode, ic}, y[x], x],
   s_Solve :> HoldForm[s] -> s,
   TraceInternal -> True] // Flatten // Column

Mathematica graphics

Solving for the constants in stages is much easier in this case, which can be verified by hand, too.

  • $\begingroup$ Thanks. I have feeling DSolve must have used more generic method, as it has to work for any other input? Otherwise, how would it know to do these transformation? I am also not sure why using these transformations made it solve it. I tried to use Wolfram alpha to see the steps, but it did not show these. $\endgroup$
    – Nasser
    Jul 30, 2022 at 5:09
  • $\begingroup$ @Nasser I'm meant to be doing something else, but tracing Solve[] wasn't too bad. See the update I knew the original answer was not what you were looking for, but it was easy to try. $\endgroup$
    – Michael E2
    Jul 30, 2022 at 5:37
  • $\begingroup$ Thanks. I just figured also how to solve it by hand. I think it might be similar to what you show. I just need to type the latex and post it at the end of my question so you can check if it looks OK. $\endgroup$
    – Nasser
    Jul 30, 2022 at 5:59
  • $\begingroup$ Yes, looking more at your trace, I think that is similar to what I did by hand also. I just have to figure how to code it myself. sometimes it is easier to do something by hand than on the computer. I need to learn how to use Trace more. I am not good at it. I know you are good with Traces. $\endgroup$
    – Nasser
    Jul 30, 2022 at 6:06

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.