DSolve gives complex function although the solution is a real one

First, you can't say the solution is a real one, because in the generale case it is not. Take this function:

f[x_] := I*E^(-((a^(1/4) x)/Sqrt[2]))*Sin[(a^(1/4) x)/Sqrt[2]]

It so happens that it is not real (it's pure imaginary), and it is a solution to your differential equation:

enter image description here

Yes, the general form of the solution is thus complex. If you are only interested in real solutions, you have two options:

  1. add some (real) initial conditions, so you get a fully specified real solution after some simplification:

    enter image description here

  2. analyse the general solution to find out how it can be factor into real and complex parts:

    enter image description here

    The first part of that expression is the general equation of the real solutions to your differential equation.


Here is the reply I got from the Mathematica support team:

Thank you for your prompt reply.

Please find attached a small notebook which demonstrates using FullSimplify directly on the output of your first DSolve expression to obtain a symbolic real-valued formula.

The formula itself does contain imaginary values, but these zero each other out as seen when the formula is evaluated numerically. Unfortunately, the corresponding "hidden zero" in the imaginary component cannot be eliminated by either of two typical techniques:

(1) increasing the internal precision available to the N function when evaluating the formula numerically, or

(2) using FullSimplify which is sometimes successful in removing hidden zeros from symbolic formulas.

The recommended approach in cases like this is to use the Chop function, which will eliminate near-zero numerical values in any given expression.

For more information, please see the Possible Issues subsection of the $MaxExtraPrecision page.

For details on the Chop function and how its behavior can be customized, please see its corresponding Documentation Center page.

Please let me know if you have any questions. Thank you.

{sol} = DSolve[{y''''[x] + a y[x] == 0, y''[-c] == ic0, y''[c] == ic0,
 y'''[-c] == ic1, y'''[c] == -ic1}, y[x], x];

val = FullSimplify[sol, a > 0 && {ic0, ic1, c, x} \[Element] Reals]

N[val]
% // Chop

Block[{$MaxExtraPrecision = 10000}, N[val]]
% // Chop

I think that some of your statements depend on the details. For instance some of the parameters C[] can be complex numbers if you choose a<0 :

parS = Solve[{y1''[-c] == ic0, y1''[c] == ic0, y1'''[-c] == ic1, 
    y1'''[c] == -ic1}, {C[5], C[6], C[7], C[8]}] // Simplify;
parFS = Solve[{y1''[-c] == ic0, y1''[c] == ic0, y1'''[-c] == ic1, 
    y1'''[c] == -ic1}, {C[5], C[6], C[7], C[8]}] // FullSimplify

parS /. {a -> -2, c -> 10, ic0 -> 1, ic1 -> -1} // N
parFS /. {a -> -2, c -> 10, ic0 -> 1, ic1 -> -1} // N

(* {{C[5] -> -0.35876 - 2.498*10^-15 I,  C[6] -> -0.35876 - 2.498*10^-15 I, 
     C[7] -> 2.27596*10^-15 - 0.358762 I, C[8] -> -2.27596*10^-15 + 0.358762 I}}

   {{C[5] -> -0.35876 + 5.10703*10^-15 I, C[6] -> -0.35876 + 5.10703*10^-15 I, 
     C[7] -> 2.35922*10^-15 - 0.358762 I, C[8] -> -2.19269*10^-15 + 0.358762 I}} *)

Besides this point you can get the solution to your problem in one line and indeed it seems a real function (apart from numerics) :

sol[a_, ic0_, ic1_, c_, x_] = y[x] /. DSolve[{y''''[x] + a y[x] == 0, y''[-c] == ic0, 
     y''[c] == ic0, y'''[-c] == ic1, y'''[c] == -ic1}, y[x], x][[1]] ;

Plot[Im[sol[-2.0, 1.0, -1.0, 10., x]], {x, -10., 10.}]
Plot[Re[sol[-2.0, 1.0, -1.0, 10., x]], {x, -10., 10.}]

Im Re