Why Can't `DSolve` Find a Solution for this ODE?
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
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
Check:
NDSolveValue[{ode == 0, y[0] == 1, y'[0] == 0}, y[1], {x, 0, 1}]
(* 1.83793 *)
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.