1
$\begingroup$

(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?

$\endgroup$
6
  • 2
    $\begingroup$ I get a noncomplex answer in V13: i.sstatic.net/95Eng.png $\endgroup$
    – Michael E2
    Commented Feb 7, 2022 at 5:55
  • $\begingroup$ 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 $\endgroup$
    – Michael E2
    Commented Feb 7, 2022 at 6:16
  • 1
    $\begingroup$ as Michael says, it gives the book answer in V 13. Try V 13, as DSolve there is now much better. $\endgroup$
    – Nasser
    Commented Feb 7, 2022 at 6:58
  • 2
    $\begingroup$ Mathematica v12.2(Windoews10) gives non-complex solution (as desired) $\endgroup$ Commented Feb 7, 2022 at 7:01
  • $\begingroup$ OK, thank you for your comments. $\endgroup$
    – lotus2019
    Commented Feb 7, 2022 at 7:27

1 Answer 1

2
$\begingroup$

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   *)
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – lotus2019
    Commented Feb 8, 2022 at 8:57
  • $\begingroup$ @lotus2019 , please see my edit. $\endgroup$
    – Akku14
    Commented Feb 8, 2022 at 10:19
  • $\begingroup$ Thank you very much! @Akku14 $\endgroup$
    – lotus2019
    Commented Feb 9, 2022 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.