7
$\begingroup$

I wanted to find a basis for the set of solutions of the following ODE.

$$y^{''}+\frac{1}{x^2+1}y^{'}(x)+\left[-1-\frac{1}{x^2+1}\right]y(x)=0$$

But when I try to use DSolve as follows

ODE = 
  Derivative[2][y][x] + 1/(1 + x^2) Derivative[1][y][x] + (-1 - 1/(1 + x^2)) y[x] == 0
DSolve[ODE, y, x]

I get an endless evaluation!

As far as I know, there is not any analysis issues which may cause difficulties to find a solution. So I am wondering what is wrong?

Then I tried Maple and it just gave the solution as

enter image description here

$\endgroup$
7
  • 3
    $\begingroup$ Does the Maple result count as a solution since it's given in terms of an integral? $\endgroup$
    – QuantumDot
    Commented Jul 9, 2016 at 21:19
  • 2
    $\begingroup$ @QuantumDot: I think it is better than nothing! ;) $\endgroup$ Commented Jul 9, 2016 at 22:01
  • $\begingroup$ Getting stuck in a loop is something that can happen with any mathematical software when integration is involved, some software just automatically times out though. $\endgroup$
    – Feyre
    Commented Jul 9, 2016 at 22:01
  • 1
    $\begingroup$ @Quantum, to be fair, Mathematica is not above giving integral solutions as well: DSolve[{y'[x] == Sin[Sin[x]], y[0] == 0}, y, x] $\endgroup$ Commented Jul 10, 2016 at 14:34
  • 1
    $\begingroup$ @Artes You may want to take part in the discussion here :) : mathematica.meta.stackexchange.com/q/1219/1871 $\endgroup$
    – xzczd
    Commented Nov 9, 2020 at 3:04

2 Answers 2

13
$\begingroup$

When DSolve runs a long time, it's probably because either Integrate or Solve is chewing over a tough problem. Here's a way to make Integrate give up quicker, so that you can see if that is the problem.

Note/warning: The code returns Inactive[Integrate][...] instead of an unevaluated Integrate[...]. The latter seems preferable, but I was unable to figure out how to do it. Luckily, the inactive version seems to work.

Base code: The function withTimedIntegrate runs code with Integrate under a time constraint of tc seconds.

ClearAll[withTimedIntegrate];
SetAttributes[withTimedIntegrate, HoldFirst];
withTimedIntegrate[code_, tc_] := Module[{$in},
   Internal`InheritedBlock[{Integrate},
    Unprotect[Integrate];
    i : Integrate[___] /; ! TrueQ[$in] :=
     Block[{$in = True},
      TimeConstrained[i, tc, Inactivate[i, Integrate]]
      ];
    Protect[Integrate];
    code
    ]
   ];

OP's example:

withTimedIntegrate[{dsol} = DSolve[ode == 0, y, x], 1]; // AbsoluteTiming
dsol

Mathematica graphics

OP's example with an IVP:

withTimedIntegrate[
  {dsolIVP} = DSolve[{ode == 0, y[0] == 1, y'[0] == 0}, y, x], 
  1] // AbsoluteTiming
y[1] /. dsolIVP // Activate // N

Mathematica graphics

Check:

NDSolveValue[{ode == 0, y[0] == 1, y'[0] == 0}, y[1], {x, 0, 1}]
(*  1.83793  *)
$\endgroup$
6
  • $\begingroup$ (+1) Perfect illustration! :) $\endgroup$ Commented Jul 13, 2016 at 19:58
  • 1
    $\begingroup$ A slight shortening: TimeConstrained[i, tc] /. $Aborted :> Inactivate[i, Integrate] can be replaced with TimeConstrained[i, tc, Inactivate[i, Integrate]]. $\endgroup$ Commented Aug 5, 2017 at 7:58
  • $\begingroup$ @J.M. Thanks! . $\endgroup$
    – Michael E2
    Commented Aug 5, 2017 at 11:05
  • 1
    $\begingroup$ @ooo Re Q1: It's now called the Villegas-Gayley trick. It's used several times on the site, which you can search for. $\endgroup$
    – Michael E2
    Commented Jan 30, 2018 at 12:47
  • 2
    $\begingroup$ Re Q2: 1. Match Integrate[___] /; ! TrueQ[$in]; $in is undefined (or should be), so TrueQ[$in] is False and ! TrueQ[$in] is True; so this definition matches any Integrate[..]. 2. Then Block is execute which sets $in = True. 3. Then TimeConstrained, which executes the original Integrate command (represented by i) under a time constraint. 4. Now $in is True, so this def. does not match and the regular Integrate is executed. 5 If Integrate finishes in time, the result is returned; if not, the inactivated Integrate command is returned. $\endgroup$
    – Michael E2
    Commented Jan 30, 2018 at 12:52
9
$\begingroup$

With

$Version
(* 10.4.1 for Microsoft Windows (64-bit) (April 11, 2016) *)

the code in the question produces the desired answer, although slowly,

(* {{y -> Function[{x}, E^x*C[1] + E^x*C[2]*Integrate[E^(-ArcTan[K[1]] - 2*K[1]), 
     {K[1], 1, x}]]}} *)

Computation time, as measured by AbsoluteTiming, is about 40 minutes on my PC.

Addendum

As is so often the case, DSolve performs much better when given some help. Begin with the substitution, y[x] -> Exp[x] z[x].

Unevaluated[D[y[x], {x, 2}] + D[y[x], {x, 1}]/(1 + x^2) - 
    y[x] (1 + 1/(1 + x^2))] /. y[x] -> Exp[x] z[x];
Simplify[% Exp[-x]] // Apart
(* ((3 + 2*x^2)*Derivative[1][z][x])/(1 + x^2) + Derivative[2][z][x] *)

One might think that DSolve could solve this greatly simplified ode in seconds, but in fact it takes 36 minutes! (Perhaps, DSolve is searching for a solution that does not involve Integrate.)

DSolve[% == 0, z[x], x]
(* {{z[x] -> C[2] + Integrate[E^(-ArcTan[K[1]] - 2*K[1])*C[1], {K[1], 1, x}]}} *)

The obvious substitution z'[x] -> w[x] finally allows DSolve to proceed quickly.

%% /. {z''[x] -> w'[x], z'[x] -> w[x]};
DSolve[% == 0, w[x], x]
(* {{w[x] -> E^(-2 x - ArcTan[x]) C[1]}} *)

Back substitution and an additional integration then yield the desired result.

$\endgroup$
7
  • $\begingroup$ So it seems to be a system issue? I was using 10.4.0 for Microsoft Windows (32-bit) $\endgroup$ Commented Jul 10, 2016 at 8:15
  • 1
    $\begingroup$ @H.R. I just added the computation time on my PC to the answer, almost 40 minutes. Did you wait that long? If so, it must be that the newest version is more capable. I do know that a lot of improvements were made to DSolve in version 10. $\endgroup$
    – bbgodfrey
    Commented Jul 10, 2016 at 12:29
  • $\begingroup$ I really appreciate your detailed answer and attention. Many thanks for that. It seems that DSolve needs some improvement in the cases where solution can be represented in an integral form! :) $\endgroup$ Commented Jul 10, 2016 at 15:31
  • $\begingroup$ It seems that you just did the reduction of order method with the help of Mathematica for computations. $\endgroup$ Commented Jul 10, 2016 at 17:01
  • 1
    $\begingroup$ @H.R. Yes, I wanted to obtain some insight into why DSolve took so long. $\endgroup$
    – bbgodfrey
    Commented Jul 10, 2016 at 17: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.