4

This question comes from: Exact Higher-Order Equations.

5 y[x] + 5 x y'[x] - y''[x] + y'''[x]

Execution time is longer than several minutes.

Of course, its result of DSolve is...

...so prodigious large that my computer took 155.493 seconds to calculate it out.

ooo
  • 564
  • 3
  • 11

2 Answers2

6

Using withTimedIntegrate from Why Can't `DSolve` Find a Solution for this ODE?, I prefer putting a time constraint on Integrate. DSolve might call Integrate several times, and I only want to abort the long ones. Aborting all of them might yield an inferior solution.

withTimedIntegrate[
  DSolve[5 y[x] + 5 x y'[x] - y''[x] + y'''[x] == 0, y, x],
  1] // AbsoluteTiming
(*  {2.10949, {{y -> ... }}}  *)

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]];

See also:

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Q: Why MemberQ[%, _Integrate, ∞] gets False? What to get True? – ooo Jan 29 '18 at 16:18
  • 1
    @ooo Because to keep the integrals from evaluating, they are inactivated. Try MemberQ[sol, Inactive[Integrate][__], \[Infinity]]. You can also change the form with sol = sol /. Inactive[Integrate] -> Integrate. Since they're inside Function, they won't evaluate, at least until the Function is evaluated, at which point they will run forever. – Michael E2 Jan 30 '18 at 02:13
  • @MichaelE2 Congrats on hitting gold badges in differential-equations, calculus-and-analysis and equation-solving tags. You're the first one who win all of them. Best regards! – Artes Jan 30 '18 at 13:18
  • @Artes Thank you very much! – Michael E2 Jan 30 '18 at 16:23
2

Preventing Integrate from trying to find anti-derivatives speeds it up

res = Block[{Integrate}, Function[expr, expr /. Integrate -> Inactive[Integrate]][
                          DSolve[5 y[x] + 5 x y'[x] - y''[x] + y'''[x] == 0, y[x], x]]]

Or trying each integral for at most one second:

pos = SortBy[Drop[Position[res, Inactive[Integrate]], 0, -1], Minus@*Length];

Fold[MapAt[TimeConstrained[Hold[# // Activate // Evaluate], 1, #] /.
             Integrate -> Inactive[Integrate] &, #, #2] &, res, pos] /. Hold -> Identity
Coolwater
  • 20,257
  • 3
  • 35
  • 64