# How to constrain the solutions of ODE in Reals form?

(V 11.3)

For example:

ODE:

y'[x] == (x + y[x])^2

the solution in textbook:

y[x]==-x+Tan[x+C]

However, the Mathematica solutions are in Complexes form:

Clear[eqn, sol, x, y, a];
eqn = y'[x] == (x + y[x])^2;
sol = DSolve[eqn, y[x], x]

(*{{y[x] -> -I - x + 1/(-(I/2) + E^(2 I x) C[1])}}*)


The implicit solutions are also in Complexes form:

Clear[eqn, eq, sol, x, y];
eq = y'[x] == (x + y[x])^2;
Solve[Equal @@ DSolve[eq, y[x], x][[1, 1]], C[1]]

(*{{C[1] -> (E^(-2 I x) (1 + I x + I y[x]))/(2 (I + x + y[x]))}}*)


How to constrain the solutions of ODE in Reals form?

• I get a noncomplex answer in V13: i.sstatic.net/95Eng.png Commented Feb 7, 2022 at 5:55
• Try DSolve[eqn, y[x], x] /. {C[1] -> -((-1 - 4 C[1]^2)/(8 C[1])) - (1 - 4 C[1]^2)/(8 C[1])} // Simplify Commented Feb 7, 2022 at 6:16
• as Michael says, it gives the book answer in V 13. Try V 13, as DSolve there is now much better. Commented Feb 7, 2022 at 6:58
• Mathematica v12.2(Windoews10) gives non-complex solution (as desired) Commented Feb 7, 2022 at 7:01
• OK, thank you for your comments. Commented Feb 7, 2022 at 7:27

ComplexExpand general solution and get conditions for parameter, restricting to real solutions.

Clear[eqn, sol, x, y, a];
eqn = y'[x] == (x + y[x])^2;
ysol[c1_] = y /. First@DSolve[eqn, y, x] /. C[1] -> c1

ysim[c1_][x_] =
ComplexExpand[Im[ysol[c1][x]], c1, TargetFunctions -> {Re, Im}]

fsy = FullSimplify@Numerator[Together[ysim[c1][x]]]

solim = Reduce[fsy == 0, c1]

fi = FindInstance[solim, {Re[c1], Im[c1]}, 2]

Plot[Evaluate[ysol[Re[c1] + I Im[c1]][x] /. fi], {x, -5, 5}]

ysol[Re[c1] + I Im[c1]][x] /. fi // Together //
ComplexExpand[#, c1, TargetFunctions -> {Re, Im}] & // Simplify


Edit

How to transform between the diffentent forms of solution for different versions of MMA. (I call solution of v. 11.3 yss)

yss[c2_][x_] = -x + Tan[x + c2];

eqn = y'[x] == (x + y[x])^2;
ysol[c1_] = y /. First@DSolve[eqn, y, x] /. C[1] -> c1

(*   Function[{x}, -I - x + 1/(-(I/2) + E^(2 I x) c1)]   *)

diffy = (ysol[c1][x] - yss[c2][x] // TrigToExp // Together //
Numerator)

solc1 = Solve[ForAll[x, x \[Element] Reals, 0 == diffy], c1]

(*   {{c1 -> -(1/2) I E^(2 I c2)}}   *)

solc2 = Solve[ForAll[x, x \[Element] Reals, 0 == diffy], c2]

(*   {{c2 -> ConditionalExpression[-(1/2) I (2 I \[Pi] C[1] + Log[2 I c1]),
C[1] \[Element] Integers]}}   *)


ysol in the form of yss

ysol[c1][x] /. First@solc1 // ExpToTrig // FullSimplify

(*   -x + Tan[c2 + x]   *)


yss in the form of ysol

yss[c2][x] /. First@solc2 // Simplify[#, C[1] \[Element] Integers] & //
TrigToExp // Together // FullSimplify

(*   -I + 1/(-(I/2) + c1 E^(2 I x)) - x   *)

• Thank you for your answer. But why is the result obtained with this code not "y [x] = = - x + Tan [x + C]"? (v 11.3) The purpose of my question is that the solution I hope to get does not contain imaginary units I. Commented Feb 8, 2022 at 8:57
• @lotus2019 , please see my edit. Commented Feb 8, 2022 at 10:19
• Thank you very much! @Akku14 Commented Feb 9, 2022 at 4:09