6
$\begingroup$

I am trying to plot the solutions to a differential equation $$ y' = \frac{2 x + y + 2}{2x + y - 4}. $$ My code is

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$
8
  • $\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 Aug 3 at 20:57
  • $\begingroup$ screen shot of the above i.sstatic.net/JpfYPmk2.png $\endgroup$
    – Nasser
    Commented Aug 3 at 22:29
  • $\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 Aug 3 at 22:35
  • $\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 Aug 3 at 22:39
  • $\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 Aug 3 at 22:42

5 Answers 5

6
$\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$
11
  • $\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 Aug 3 at 23:24
  • 1
    $\begingroup$ A way to clobber Solve[] so that DSolve will return an implicit form: Internal`InheritedBlock[{Solve}, Unprotect@Solve; Solve[eq_, y[x]] := Solve[eq, y[x], Method -> None]; Protect@Solve; impl = Quiet[DSolve[eqn, y[x], x, GeneratedParameters -> c], Solve::method] // First ] $\endgroup$
    – Michael E2
    Commented Aug 4 at 15:26
  • $\begingroup$ This is surprising and confusing. Trace[(* code *), _Solve, TraceInternal -> True] shows if we take Solve[eq_, y[x]] := Solve[eq, y[x], Method -> None] away, the desired Solve[…, y[x]] isn't generated. $\endgroup$
    – xzczd
    Commented 2 days ago
  • $\begingroup$ @xzczd I'm not sure exactly what "the desired" one is in your mind. Possibly, it seems to be missing because of this: DSolve[] transforms the ODE, solves it, then transforms the solution. If Solve[..., y[x]] fails, then the Solve code is transformed into the implicit solution; otherwise, if it succeeds, the explicit solution of the transformed ODE is transformed into an explicit solution of the original ODE. (Of course, DSolve[] changes internally fairly often, so I'm not sure the hack in the comment is a good, long-term method.) $\endgroup$
    – Michael E2
    Commented yesterday
  • $\begingroup$ I mean the equation 4 + x + 2 Log[-1 + x + y[x]/2] == 2 c[1] + y[x] doesn't seem to be generated internally if we don't define Solve[eq_, y[x]] := Solve[eq, y[x], Method -> None]. DSolve doesn't fail on the equation, it simply never try solving the equation. $\endgroup$
    – xzczd
    Commented yesterday
6
$\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$
5
$\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$
2
  • $\begingroup$ I do not get what the epilog points should represent? $\endgroup$ Commented Aug 4 at 9:45
  • $\begingroup$ @azerbajdzan Each point lies on a stream line, so each must represent an initial condition for a solution with such a trajectory. By solving for the initial conditions, I was able to place the stream lines at regular intervals. This morning I though, oh, I should have solved for the nullclines. Ah well, exercise for the reader, perhaps. :) $\endgroup$
    – Michael E2
    Commented Aug 4 at 13:25
4
$\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$
2
$\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$
2
  • 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 Aug 3 at 22:14
  • 1
    $\begingroup$ @azerbajdzan: Yeah, the solution used in ParametricPlot here has been modified from the output in sols in ways that are unclear to me. In particular, I'm not sure what the s parameter is doing here. Note also that this method will be incapable of following an integral curve past a local maximum in y. $\endgroup$ Commented 2 days 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.