# How to plot an implicit solution of a differential equation?

The question is to find and sketch the solution of the following initial value problem: $$(x-y)dx+(3x+y)dy=0$$ and for $$x=3$$ we have $$y=-2$$.

I was able to find an implicit solution using

DSolve[{(x - y[x]) + (3x + y[x]) y'[x] == 0, y == -2}, y[x], x]

Solve[Log[1 + y[x]/x] - 2/(1 + y[x]/x) == -6 - Log[x], y[x]]


Is it possible to plot the implicit solution? I tried using the general solution -

sol = DSolve[(x - y[x]) + (3*x + y[x])*y'[x] == 0, y[x], x]
expr = C /. Solve[sol[], C][] /. {y[x] -> y}


and then

ContourPlot[expr, {x, 0, 5}, {y, -5, 5}]


However, I want to plot

Solve[Log[1 + y[x]/x] - 2/(1 + y[x]/x) == -6 - Log[x], y[x]]

• ContourPlot[ Log[1 + y/x] - 2/(1 + y/x) + 6 + Log[x] == 0, {x, 0, 1}, {y, -2, 1}] ? – OkkesDulgerci Feb 26 at 16:26

The main issue we encounter here is that equation solvig functionality doesn't work quite seamlessly for various types of transcendental functions. However looking at the result of DSolve it appears convenient to introduce a new variable $$z=\log(1+\frac{y(x)}{x})$$.

z[x] == Log[1+ y[x]/x]


Now we can find the exact solution, setting up the initial condition in terms of z[x], i.e. z == -Log:

z[x]/.First @ DSolve[{0==(x - y[x]) + (3x + y[x])y'[x]/.{
y[x] -> x (Exp[z[x]] - 1),
y'[x] -> D[x(Exp[z[x]] - 1), x]}//Simplify,
z == -Log},
z[x], x] // Quiet

-6 - Log[x] + ProductLog[2 E^6 x]


that is Log[1+ y[x]/x] == -6 - Log[x] + ProductLog[2 E^6 x] i.e. the solution of our equation is y[x] == f[x] such, that

f[x_] := E^(-6 + ProductLog[2 E^6 x]) - x


because

FullSimplify[x (Exp[-6 - Log[x] + ProductLog[2 E^6 x]] - 1), x > 0]


 E^(-6 + ProductLog[2 E^6 x]) - x where W is the Lambert function. Now we can plot the graph of the solution. However since it looks like a straight line, we find an example of a linear function to compare the both graphs. We assume that the graphs intersect at x == 0 and x == 3 and so:

g[x_] = a x + b /. First @ Solve[{ f == b, f == 3 a + b}, {a, b}]

 1/E^6 - ((1 + 2 E^6) x)/(3 E^6)

Plot[{ f[x], g[x]}, {x, 0, 4}, PlotStyle -> {Thick, Dashed},
Epilog -> {Red, PointSize[0.02], Point[{3, -2}]}] The graph is very close to a linear function as could be seen from ContourPlot. It might be convenient to plot the difference of the both functions:

Plot[ Re @ f[x] - g[x], {x, -3, 5}, PlotStyle -> {Thick, Magenta},
Epilog -> {Red, PointSize[0.02], Point[{3, 0}]}] For completeness we plot the real and imaginary values of f[x] since it becomes complex for negative numbers (this is independendent from the physical (?) model discribed by the original equation)

Plot[ Flatten @ {ReIm @ f[x], g[x]}, {x, -20, 20},
PlotStyle -> {Thick, Thick, Dashed}, Evaluated -> True,
Epilog -> {Red, PointSize[0.01], Point[{3, -2}]}] 