# DSolve gives wrong solution

eq = x^2 f''[x] + x f'[x] - 4 f[x] == 0;
bc = f[∞] == 0; (* boundary condition *)

sol = DSolve[ {eq, bc}, f[x], x]

(* {{f[x] -> 0}} *)


The correct solution is f[x]=c/x^2. Why does DSolve give a wrong answer?

• Where is the actual equation? I see no ODE here. – zhk Mar 8 '18 at 9:33
• I have an image there. The equation is x^2 f '' (x) + x f'(x) - 4 f(x) = 0 with boundary condition f(Infinity)=0. DSolve gives f(x)=0 but the correct solution is f(x) = c / x^2 – KDH Mar 8 '18 at 9:36
• Clearly, the answer is correct, trivially so. – mmeent Mar 8 '18 at 9:39
• f(x)=0 is a solution of the ODE but f(x)=c/x^2 is the general solution. DSolve missed the general solution. Why? – KDH Mar 8 '18 at 9:42

The boundary condition f[Infinity]==0is the problem:

You might solve your problem using the general solution

sol = DSolve[{x^2 f''[x] + x f'[x] - 4 f[x] == 0}, f, x][]
TrigToExp[ f[x] /. sol]
(* C/(2 x^2) + 1/2 x^2 C - (I C)/(2 x^2) + 1/2 I x^2 C *)


Alternatively to gwr's extension, one could examine the general solution from Ulrich Neumann symbolically:

sf = C/(2 x^2) + 1/2 x^2 C - (I C)/(2 x^2) + 1/2 I x^2 C
Limit[sf, x->Infinity]


(C + I C) Infinity

We need this expression to equal 0, so set C->I C:

sf /. C -> I C


C/x^2

To get to the heart of why this happens, I imagine it's because DSolve evaluates Infinity preemptively. Even after taking the limiting case specifically and seeing the term containing Infinity, a bit of a logical leap is required to say that the only way this will work is if (C + I C) is 0. Even after that leap that term is Indefinite, not 0 exactly.

Building upon Ulrich Neumann's answer you may approach the solution by including the boundary condition for arbitrarily large x:

eq = x^2 f''[x] + x f'[x] - 4 f[x] == 0;

sol = DSolve[ {eq, f[1.*^60] == 0}, f[x], x] // RightComposition[
First,
TrigToExp,
FullSimplify,
N
]


$\text{f}[x] \rightarrow 0. +\frac{1. \text{C}}{x^2}$

• +1 for introducing me to RightComposition! – Thies Heidecke Mar 8 '18 at 13:01
• @ThiesHeidecke You are welcome - I must admit that I tend to use it excessively -- feeling vindicated, though, by looking at more structured code ... ;-) – gwr Mar 8 '18 at 13:07
• Through my previous exposition to functional programming with Haskell i was already a fan of point free style and liked to use Composition a lot, it's good to know there are more options that feel more natural in some situations! – Thies Heidecke Mar 8 '18 at 13:14
• How is RightComposition here better than sol=... // First // TrigToExp // FullSimplify // N? – Ruslan Mar 8 '18 at 18:26
• @Ruslan Essentially, there is no difference but the block-writing with , makes for better readability and Composition[ g, h ] can be immediately treated as a function while ... // g // h can not. – gwr Mar 8 '18 at 19:00

Further to the other answers: Mathematica can find the general solution so long as you ask it to solve for the boundary condition $f(a) = 0$ at a finite (but arbitrary) $a$, and only then take the limit $a \to \infty$:

sol = DSolve[{x^2 f''[x] + x f'[x] - 4 f[x] == 0, f[a] == 0}, f[x], x][]
Limit[f[x] /. sol, a -> \[Infinity]]

(* {f[x] -> C Cosh[2 Log[x]] - C Coth[2 Log[a]] Sinh[2 Log[x]]} *)
(* C/x^2 *)


See also my answer to DSolve not satisfying initial conditions, which dealt with a similar problem of DSolve missing certain solutions.

Carrying the above solutions further:

eq = x^2 f''[x] + x f'[x] - 4 f[x] == 0;

DSolve[eq, f[x], x] // Flatten
(* {f[x] -> C*Cosh[2*Log[x]] + I*C*Sinh[2*Log[x]]} *)

f[x_] = f[x] /. % /. {C -> c1, C -> c2/I}
(* c1*Cosh[2*Log[x]] + c2*Sinh[2*Log[x]] *)

f[x] // TrigToExp // Collect[#, x^_] &
(* x^2*(c1/2 + c2/2) + (c1/2 - c2/2)/x^2 *)

f[x_] = % /. {c1/2 + c2/2 -> c1, c1/2 - c2/2 -> c2}
(* c1*x^2 + c2/x^2 *)


Applying the bc, for f to be 0 at x == infinity, c1 must be 0.