5
$\begingroup$

I am trying to plot the solutions to a differential equation

ClearAll;
eqn = y'[x] == (2 x + y[x] + 2)/(2 x + y[x] - 4);
solution = DSolve[eqn, y[x], x]

which gives

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

and then plot it

sol1 = Table[ySol = (y[x] /. solution) /. C[1] -> Cval, {Cval, -20, 20, 0.5}];
sol2 = Table[ySol = (y[x] /. solution) /. C[1] -> Cval - Pi*I, {Cval, -20, 20, 0.5}];
sol = Append[sol1, sol2];
Plot[sol, {x, -4, 4}, PlotRange -> {-2, 8}]

which gives

Plot of solutions

The upper integral lines stop where the derivative becomes infinite, but they should continue (bending to the right)? How can I achieve this?

Also, is there really no way to get DSolve to produce an implicit solution to this differential equation?

New contributor
viehhaus is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
7
  • $\begingroup$ is there really no way to get DSolve to produce an implicit solution I've asked for this years ago. There is no such option in DSolve. In Maple there is. dsolve(ode,'implicit') it gives this x/3 - y(x)/3 + (2*ln(y(x) + 2*x - 2))/3 - c__1 = 0 you can translate to Mathematica and see. change ln to Log and c__1 to C[1] May be in version 15.0 of Mathematica such an option will be added. $\endgroup$
    – Nasser
    Commented 13 hours ago
  • $\begingroup$ screen shot of the above i.sstatic.net/JpfYPmk2.png $\endgroup$
    – Nasser
    Commented 11 hours ago
  • $\begingroup$ @Nasser If I am not mistaken Maple solution does not give the left hand side of the plot. And it should be Log[(y + 2*x - 2)^2] instead of 2*Log[(y + 2*x - 2)], then it produces the whole plot. So maybe a bug in Maple? $\endgroup$ Commented 11 hours ago
  • $\begingroup$ @azerbajdzan there is always chance of bug. But Maple does verify that its solution is correct using odetest. Screen shot i.sstatic.net/7AAOksFe.png $\endgroup$
    – Nasser
    Commented 11 hours ago
  • $\begingroup$ @Nasser Solution may be right but incomplete. Try to plot it in ContourPlot - first original solution and then replace 2*Log[(y + 2*x - 2)] with Log[(y + 2*x - 2)^2]. $\endgroup$ Commented 11 hours ago

5 Answers 5

5
$\begingroup$

Another way: Reverse the solution to get an implicit form:

Clear[c];
impl = Solve[
      First@DSolve[eqn, y[x], x, GeneratedParameters -> c] /. 
       Rule -> Equal, c[1]][[1, 1]] /. Rule -> Equal // Simplify // 
  Quiet

(*  c[1] == 1 + (3 x)/2 + Log[1/2 E^(1 - x - y[x]/2) (-2 + 2 x + y[x])]  *)

ContourPlot[
 Evaluate@
  Table[impl /. Log[z_] :> Log[Abs@z] /. y[x] -> y, {c[1], -10, 10, 
    1/2}], {x, -5, 5}, {y, -2, 8}, MaxRecursion -> 3]

stream lines of solutions

$\endgroup$
1
  • $\begingroup$ Maybe should point out that throwing in the Abs[] inside the logarithm is equivalent to adding Pi * I to the constant parameter c[1] when the argument is negative. $\endgroup$
    – Michael E2
    Commented 10 hours ago
5
$\begingroup$

Include the excluded branch alluded to by the warning message:

sol1 = Table[(y[x] /. solution) /. C[1] -> Cval, {Cval, -20, 20, 0.5}];
sol3 = Table[(y[x] /. solution) /. C[1] -> Cval /. 
    ProductLog -> (ProductLog[-1, #] &), {Cval, -20, 20, 0.5}];
sol2 = Table[(y[x] /. solution) /. C[1] -> Cval - Pi*I, {Cval, -20, 
    20, 0.5}];
sol = Join[sol1, sol2, sol3];
Plot[sol, {x, -4, 4}, 
 PlotStyle -> Table[ColorData[97][k], {k, 2 Length@sol1}], 
 PlotRange -> {-2, 8}]

streams of solutions

$\endgroup$
3
$\begingroup$

Equation for $y(x)$ can be converted into a set of equations for $x(t)$ and $y(t)$

eqs = {y'[t] == 1, x'[t] == (2 x[t] + y[t] - 4)/(2 x[t] + y[t] + 2)}
sols = DSolve[eqs, {y[t], x[t]}, t]

Then plot the solution

ParametricPlot[
 Table[{1/2 (-2 - t) + 2 (1 - s *ProductLog[E^(-1 + (3 t)/4 + c)]), 
    t}, {c, -30, 30, 5}, {s, -1, 1, 2}] // Flatten, {t, -30, 30}, 
 PlotRange -> {{-20, 20}, {-20, 20}}]

parametric plot involving product log

$\endgroup$
1
  • $\begingroup$ The plot does not seem to be same as in OP. Especially the left half. Also if Nasser's Maple solution is right then there is a discrepancy between his one-parameter solution and your two-parameter solution. $\endgroup$ Commented 12 hours ago
3
$\begingroup$
  • StreamPlot
StreamPlot[{1, (2 x + y + 2)/(2 x + y - 4)}, {x, -5, 5}, {y, -5, 5}]

enter image description here

  • ParametricNDSolveValue
Clear[sol,pts];
sol = ParametricNDSolveValue[{x'[t] == 1, 
    y'[t] == (2 x[t] + y[t] + 2)/(2 x[t] + y[t] - 4), x[0] == x0, 
    y[0] == y0}, {x[t], y[t]}, {t, 0, 100}, {x0, y0}];
pts = Join[
   RandomPoint[
    ImplicitRegion[Abs[2 x + y - 4] == 10^-2, {{x, -5, 5}, {y, -5, 5}}],
     300], RandomPoint[RegionBoundary@Rectangle[{-5, -5}, {5, 5}], 
    300]];
ParametricPlot[sol @@@ pts // Evaluate, {t, 0, 100}, PlotRange -> 5]

enter image description here

$\endgroup$
3
$\begingroup$

Two answers have gone the parametric route. I'd suggest separating the equations as follows, though:

eqs = {y'[t] == (2 x[t] + y[t] + 2), x'[t] == (2 x[t] + y[t] - 4)};
sols = DSolve[eqs, {y[t], x[t]}, t, GeneratedParameters -> c];
tbl = Flatten[
    Table[# /. {c[1] -> -2/3 + 2 b1 + a1, c[2] -> -2/3 - b1 - 2 a1}
     , {a1, -7, 6, 1}, {b1, -3, 3, 6}], 1] &;
ParametricPlot[
 tbl[{x[t], y[t]} /. sols[[1]]] // Evaluate
 , {t, -5, 5}, PlotRange -> {{-10, 10}, {-10, 10}},
 Epilog -> {Black, Point@tbl[2/3 + {c[1], c[2]}]}]

streams of solutions

The epilog shows the (ahem) Point of the funny Table[] substitution in tbl.

$\endgroup$
1
  • $\begingroup$ I do not get what the epilog points should represent? $\endgroup$ Commented 33 mins ago

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.