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,
ContourShading -> None,
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,
ContourShading -> None,
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,
ContourShading -> None,
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)?
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 negativea
)? $\endgroup$C[1]
is a constant. It cannot depend onx
andy
. Determine it by specifying the value ofy
at some pointx
. $\endgroup$