The general solution is not real valued. Try setting an initial condition:
FullSimplify[
DSolve[{y'[x] == y[x]^2 - 2 x^2*y[x] + x^4 + 2 x + 4, y[0] == 1},
y[x], x]
]
yielding
{{y[x] -> -2 I + (4 + 8 I)/((2 - I) + (2 + I) E^(4 I x)) + x^2}}
which is not real valued (almost everywhere). However, for a different initial condition
FullSimplify[
DSolve[{y'[x] == y[x]^2 - 2 x^2*y[x] + x^4 + 2 x + 4, y[0] == 0},
y[x], x]
]
{{y[x] -> x^2 + 2 Tan[2 x]}}
the solution is real valued.
We can use a symbolic initial condition
FullSimplify[
DSolve[{y'[x] == y[x]^2 - 2 x^2*y[x] + x^4 + 2 x + 4, y[0] == c},
y[x], x]
]
{{y[x] -> -2 I + (8 - 4 I c)/(-2 I - c + (-2 I + c) E^(4 I x)) + x^2}}
and see that this complex valued behaviour is generic, but can be hidden with particular choices of the initial condition, c
. Note that we can give the initial condition at a different value of the independent variable, and get different behaviour altogether. In fact, providing an initial condition at x=1
gives a real valued generic solution.
FullSimplify[
DSolve[{y'[x] == y[x]^2 - 2 x^2*y[x] + x^4 + 2 x + 4, y[1] == c},
y[x], x]
]
{{ y[x] -> ( 2 (-1 + c + x^2) Cos[2 - 2 x] + (-4 + (-1 + c) x^2) Sin[2 - 2 x] )/
( 2 Cos[2 - 2 x] + (-1 + c) Sin[2 - 2 x] ) }}
Plot[Table[y[x] /. %[[1]], {c, -2, 2}], {x, -2, 2}]

Starting over in full generality, we can get an unintelligible plot.
genSol = FullSimplify[ ComplexExpand[
y[x] /. DSolve[{
y'[x] == y[x]^2 - 2 x^2*y[x] + x^4 + 2 x + 4,
y[d] == c},
y[x], x][[1]]
]];
Plot[Flatten[
Table[genSol, {c, -20, 20, 10}, {d, -2, 2, 1}
], 1], {x, -4, 4}]

Here we have applied the generic initial condition $y(d) = c$ (via y[d] == c
) so that we have labels for the parts of a point of a particular solution. Since we only want real solutions, we want $c$, $d$, and $x$ to be real. Applying ComplexExpand[]
treats all the variables as if they are real, yielding real solutions passing through the point $(c,d)$. (The largest effect here is that the complex exponentials are rewritten in terms of sine and cosine.)
y[x]
noty
in the ODE itself. $\endgroup$Range[-3.3]
supposed to beRange[-3,3]
? $\endgroup$