# Is there a way to plot the derivative of an implicit function in mathematica?

After having read the manuals and searched extensively, I could not find a way to do the following.

Suppose you have an implicit equation of the form

$y^4 + y^5 = x$, and $y$ is a function of $x$.

Now, using ContourPlot, one can easily graph y against x. However, I would like to know if there is a method to graph y' against x (i.e. $dy/dx$ against x). I haven't come across any such thing yet.

• You can get the solution with something like sol[x_] = y /. Solve[y^4 + y^5 == x, y] and solp[x_] = D[#, x] & /@ sol[x]. Commented May 28, 2013 at 14:18

I suppose you wanted something like this:

ContourPlot[Evaluate[
Eliminate[{y^4 + y^5 - x == 0, Dt[y^4 + y^5 - x == 0, x]} /.
Dt[y, x] :> yp, y]],
{x, -7, 7}, {yp, -7, 7}, PlotPoints -> 75]


The key here is the use of Dt[] to differentiate the implicit Cartesian equation, and Eliminate[] to eliminate y; in this case, we have an algebraic equation and Eliminate[] had no difficulty, but this technique will be less successful once transcendental functions are involved.

• Well, this is not plotting anything for me! Commented May 28, 2013 at 14:31
• Really? Did you copy the code into Mathematica? What version/OS are you on? Commented May 28, 2013 at 14:32
• Windows 7 Pro, Mathematica 9. i copied the exact code. Commented May 28, 2013 at 14:35
• Very peculiar. No error messages whatsoever? Commented May 28, 2013 at 14:36
• Well now that you mention it, I typed it and got the graph! Thanks! Commented May 28, 2013 at 14:51

One easy way would be to use the ParametricPlot. Indeed, since y'(x)=1/x'(y) and

 x[y_] := y^4 + y^5;


one finds that y' is expressed as

 1/D[x[y], y]


yielding

1/(4 y^3 + 5 y^4)


Than one can plot as follows. Evaluate it:

    ParametricPlot[{y^4 + y^5, 1/(4 y^3 + 5 y^4)}, {y, 0.3, 1.2},
AxesLabel -> {"x", "y'"}]


You can take the ContourPlot of the equation and replace all the y coordinates with the value of the derivative. This will be a fast transformation of the ContourPlot, but the main drawback is that the adaptive plotting algorithms are applied to the original equation, not to the derivative. Rapid changes in the derivative and exclusions will not be handled automatically.

The basic idea can be achieved like this.

f = y^4 + y^5 - x;
dydx[f_] := dydx[f] = -D[f, x]/D[f, y] /. {x -> #1, y -> #2} &;
pl = ContourPlot[f == 0, {x, -3, 3}, {y, -3, 3}];
pl /. GraphicsComplex[pts_, rest___] :> GraphicsComplex[{#1, dydx[f][##]} & @@@ pts, rest]


One might also wish to automate this. One problem above is that it might happen that D[f, y] is zero. One might catch this and give back a very large derivative. There is an option showFunction to plot the original equation along with the derivative and another derivativeStyle to set the style of the dy/dx plot. (Note: The syntax coloring complains of a scope conflict in the definition of dfplot; changing Block[..] to Block @@ Hold[..] makes the annoying coloring go away. Both ways seem to work.)

ClearAll[dydx, dfplot];

dydx[f_, x_, y_] := With[{dfx = D[f, x], dfy = D[f, y]},
dydx[f, x, y] = If[(dfy /. {x -> #1, y -> #2}) == 0,
\$MaxMachineNumber, -dfx/dfy /. {x -> #1, y -> #2}] &];

SetAttributes[dfplot, HoldAll];
Options[dfplot] = {showFunction -> False,
derivativeStyle -> Directive[ColorData[1][2]]} ~Join~ Options[ContourPlot];
dfplot[eqn_, domain1 : {x_, __}, domain2 : {y_, __},
opts : OptionsPattern[]] := Block[{x, y},
With[{f = eqn[[1]] - eqn[[2]],
plot = ContourPlot[eqn, domain1, domain2,
Evaluate@FilterRules[{opts}, Options[ContourPlot]]]},
Show[
If[OptionValue[showFunction], plot, {}],
plot /. GraphicsComplex[pts_, prim_, rest___] :>
GraphicsComplex[{#1, dydx[f, x, y][##]} & @@@ pts,
prim /. Tooltip[e_, label_] :> Tooltip[e, -D[f, x]/D[f, y]] /.
l_Line :> {OptionValue[derivativeStyle], l}, rest]
]
]
]


The OP's example:

dfplot[y^4 + y^5 == x, {x, -3, 3}, {y, -3, 3}, showFunction -> True]


Another example for which J.M.'s eliminate method took too long:

dfplot[x^2 y - x^3 y^3 + 3 y^4 == 3, {x, -5, 7}, {y, -3, 3},
showFunction -> True, PlotPoints -> 50]


• For sufficiently complicated algebraic functions, I'd probably replace Eliminate[] with GroebnerBasis[]: GroebnerBasis[{x^2 y - x^3 y^3 + 3 y^4 == 3, Dt[x^2 y - x^3 y^3 + 3 y^4 == 3, x] /. Dt[y, x] -> yp}, {x, yp}, y, MonomialOrder -> EliminationOrder] Commented May 29, 2013 at 2:14