Integral Curve Question using Contour Plot

The ODE $$(xy-2)+(x^2-xy)y'=0$$ is NOT an exact differential equation. However, if I multiply both sides by an integrating factor $\mu=1/x$, the resulting equation $$\left(y-\frac2x\right)+(x-y)y'=0$$ is exact. My pencil and paper solution is: $$xy-2\ln|x|-\frac{y^2}{2}=0$$ I can write the original equation in the form $$y'=\frac{2-xy}{x^2-xy},$$ then define a function for the right hand side.

f[x_, y_] := (2 - x y)/(x^2 - x y)


Then I can set some options for a direction field.

SetOptions[VectorPlot,
VectorScale -> {0.03, 0.03, None},
Frame -> False, Axes -> True, AxesLabel -> {x, y}];


This allows me to easily create a direction field for the ODE.

vp = VectorPlot[{1, f[x, y]}, {x, -4, 4}, {y, -4, 4}]


Next, I can create a contour plot with my solution.

cp = ContourPlot[
x y - 2 Log[Abs[x]] - y^2/2, {x, -4, 4}, {y, -4, 4},
Contours -> 20,
ContourStyle -> Directive[Thick, Blue]
]


And I can superimpose my contour plot on my direction field.

Show[vp, cp]


Looks very nice. Now, let's try to use a little Mathematica to do the same thing.

sol = DSolve[(x y[x] - 2) + (x^2 - x y[x]) y'[x] == 0, y[x], x]

{{y[x] ->
x - Sqrt[-x^3 - x C[1] + 4 x Log[x]]/(Sqrt[-(1/x)] x)}, {y[x] ->
x + Sqrt[-x^3 - x C[1] + 4 x Log[x]]/(Sqrt[-(1/x)] x)}}


Now I can solve the first answer for C[1].

ieqn = Solve[y == sol[[1, 1, 2]], C[1]]


The output follows.

{{C[1] -> -2 x y + y^2 + 4 Log[x]}}


Now I create a contour plot with this result (although the missing absolute value is a concern).

cp1 = ContourPlot[ieqn[[1, 1, 2]], {x, -4, 4}, {y, -4, 4},
Contours -> 20,
ContourStyle -> Directive[Thick, Blue],
ImageSize -> 300
]


Smile, the right side again. But I am still concerned about the missing absolute value sign in the logarithm. Well, maybe the second solution provided by DSolve will give me the left hand side.

But when I try to solve the second solution for C[1], I get the same answer.

Solve[y == sol[[2, 1, 2]], C[1]]

{{C[1] -> -2 x y + y^2 + 4 Log[x]}}


So, when I try a contour plot, an expected result occurs (Can't take log of a negative number).

cp2 = ContourPlot[ieqn[[2, 1, 2]], {x, -4, 4}, {y, -4, 4},
Contours -> 20,
ContourStyle -> Directive[Thick, Blue],
ImageSize -> 300
]


But this doesn't exist, giving me the following error and a blank plot.

Part::partw: Part 2 of {{C[1]->-10.4508+12.5664 I}} does not exist. >>

Part::partw: Part 2 of {{C[1]->-6.49675+12.5664 I}} does not exist. >>

Part::partw: Part 2 of {{C[1]->-2.65539+12.5664 I}} does not exist. >>

General::stop: Further output of Part::partw will be suppressed during this calculation. >>


So, how can I use this approach to get the left hand side of my direction field covered in integral curves (contours)?

• Isn't sol = DSolve[{x y[x] - 2 + (x x - x y[x]) y'[x] == 0, y[a] == 1}, y[x], x] the solution for your original equation (using either a positive or negative a)? Feb 11, 2015 at 5:32
• C[1] is a constant. It cannot depend on x and y. Determine it by specifying the value of y at some point x. Feb 11, 2015 at 6:30

The right side of your plot actually needs both solutions. You can get the left side by inspection of your original equation observing that if y[x] is a solution then -y[-x] is also a solution.

 Show[{
Plot[ Table[
y[x] /. sol[[1]] /. C[1] -> c  , {c, -100, 100, 5}] , {x, 0, 4},
PlotRange -> {{-4, 4}, {-4, 4}}, PlotStyle -> Red],
Plot[ Table[
y[x] /. sol[[2]] /. C[1] -> c  , {c, -100, 100, 5}] , {x, 0, 4},
PlotRange -> {{-4, 4}, {-4, 4}}, PlotStyle -> Blue],
Plot[ Table[ ( -y[x] /. sol[[1]] /. x -> -xn ) /.
C[1] -> c  , {c, -100, 100, 5}] , {xn, -4, 0},
PlotRange -> {{-4, 4}, {-4, 4}}, PlotStyle -> Green],
Plot[ Table[ ( -y[x] /. sol[[2]] /. x -> -xn ) /.
C[1] -> c  , {c, -100, 100, 5}] , {xn, -4, 0},
PlotRange -> {{-4, 4}, {-4, 4}}, PlotStyle -> Black]}]


If you want to work at it you can get the left side directly by specifying appropriate complex C[1], eg:

 Plot[ y[x] /. sol  /. C[1] -> (4 I Pi - 1)  , {x, -4, 0},
PlotRange -> {{-4, 4}, {-4, 4}}, PlotStyle -> Red]


now realizing the trouble with your contour plot is the required C[1] on the left is complex you can sort of get at it like this:

 ContourPlot[ Norm[y (-2 x + y) + 4 Log[x]  ] , {x, -4, 4}, {y, -4, 4} ,
Contours -> 40, ContourShading -> None,
ContourStyle -> Directive[Thick, Blue]]


its asymmetric though because the real and complex C[1] on the left/right don't correspond in a meaningful way.