# Using implicit differentiation to find a line that is tangent to a curve at a point

A few days ago I asked about using differentiation to find a line that is tangent to a curve at a given point. J.M. provided a very elegant way to solve these kinds of problems in Mathematica.

Now I'd like to find a line that is tangent to $x^2+xy+y^2=3$ at $(1,1)$ using implicit differentiation.

To find the slope at $(1,1)$ I used this code:

x^2 + x y[x] + y[x]^2 == 3;
D[%, x];
% /. x -> 1;
% /. y[1] -> 1;
Reduce[%, y'[1]]


which yields:

y'[1] == -1


Now to find the equation of the line:

y[x] - y[a] == y'[a] (x - a);
% /. a -> 1;
% /. y[1] -> 1;
% /. y'[1] -> -1;
Reduce[%, y[x]]


which yields:

y[x] == 2 - x


The answer is correct, but of course the method is much less elegant than for the case of non-implicit differentiation. Is there a better way than the one given above to solve the problem?

-

I'm not sure the implicit case allows for a solution as elegant as the explicit case; here is what I'd do, however:

eq = x^2 + x y + y^2 == 3; pt = {1, 1};
With[{m = (Dt[y, x] /. First[Solve[Dt[eq, x], Dt[y, x]]]) /.
Collect[m (x - First[pt]) + Last[pt], x, Simplify]]
2 - x


As you can see, this assumes a point pt on the implicitly-defined curve eq is already known; if not, a preliminary application of Solve[] is necessary.

Prompted by Michael's answer, I managed to figure out a second solution, based on the properties of the gradient of a function of two variables (here computed as D[f, {{x, y}}]):

eq = x^2 + x y + y^2 == 3; pt = {1, 1};
Expand[(D[Subtract @@ eq, {{x, y}}] /. Thread[{x, y} -> pt]).({x, y} - pt)] == 0
-6 + 3 x + 3 y == 0


This also requires that both coordinates of pt are already known, as in the first method.

Warning:

It must be noted, however, that both approaches given so far will fail spectacularly if the point being considered is in fact a singular point of the given curve (e.g. the (cru)node at the point $(0,0)$ of the folium of Descartes $x^3+y^3=3xy$). There are special techniques to attack cases like this (based on the clever evaluation of an appropriate limit), but I shall not say anything further about them in this answer.

-
Thank you @J.M.. I also decided to explore using a program to solve this sort of problem. I've posted a separate answer illustrating this approach. – dharmatech Feb 18 '12 at 4:47
"There are special techniques to attack cases like this..." - which I finally decided to demonstrate in my other answer. – J. M. Feb 19 '12 at 1:25

For giggles, here's a solution that a.) works only on algebraic curves, and b.) avoids calculus at the expense of having to solve simultaneous equations (via GroebnerBasis[] and Eliminate[]):

eq = x^2 + x y + y^2 == 3; pt = {1, 1};
fac = Apply[Times,
Power @@@
DeleteCases[FactorSquareFreeList[
First[GroebnerBasis[
Append[Thread[{x, y} == pt + {a, b} u], eq],
{u, a, b}, {x, y}]]],
{_?NumericQ, 1} | {u, _Integer}]] /. u -> 0;
ta = Eliminate[Append[Thread[{x, y} == pt + {a, b} u], fac == 0], {a, b, u}]

2 - y == x

ContourPlot[{eq, ta} // Evaluate, {x, -3, 3}, {y, -3, 3},
Epilog -> {AbsolutePointSize[5], Point[pt]}, PlotPoints -> 85]


Here's the method applied to finding the tangent of the trifolium $(x^2 + y^2)^2=y(3x^2 - y^2)$ at the point $\left(\dfrac{1-\sqrt{3}}{4},-\dfrac{1+\sqrt{3}}{4}\right)$:

eq = (x^2 + y^2)^2 == y (3 x^2 - y^2);
pt = {(1 - Sqrt[3])/4, -(1 + Sqrt[3])/4};
fac = Apply[Times,
Power @@@
DeleteCases[FactorSquareFreeList[
First[GroebnerBasis[
Append[Thread[{x, y} == pt + {a, b} u], eq],
{u, a, b}, {x, y}]]],
{_?NumericQ, 1} | {u, _Integer}]] /. u -> 0;
ta = Eliminate[Append[Thread[{x, y} == pt + {a, b} u], fac == 0], {a, b, u}]

1 - 2 Sqrt[3] + 8 y - 5 Sqrt[3] y == 11 x

ContourPlot[{eq, ta} // Evaluate, {x, -1, 1}, {y, -1, 1},
Epilog -> {AbsolutePointSize[5], Point[pt]}, PlotPoints -> 85]


Nicely enough, the method still works even at the singular points that I warned about in my previous answer:

eq = (x^2 + y^2)^2 == y (3 x^2 - y^2); pt = {0, 0};
fac = Apply[Times,
Power @@@
DeleteCases[FactorSquareFreeList[
First[GroebnerBasis[
Append[Thread[{x, y} == pt + {a, b} u], eq],
{u, a, b}, {x, y}]]],
{_?NumericQ, 1} | {u, _Integer}]] /. u -> 0;
ta = Eliminate[Append[Thread[{x, y} == pt + {a, b} u], fac == 0], {a, b, u}];
ContourPlot[{eq, ta} // Evaluate, {x, -1, 1}, {y, -1, 1},
Epilog -> {AbsolutePointSize[5], Point[pt]}, PlotPoints -> 85]


where as you can see, all three tangents at the point $(0,0)$ were found.

-
...and if you want to learn more, see this book. – J. M. Feb 19 '12 at 1:41
If you like J.M.'s approach for algebraic curves, you may like the MathOverflow question titled "Taking 'Zooming in on a point of a graph' seriously" and Jack Huizenga's answer. – Michael Wijaya Mar 18 '12 at 22:02

I'm not sure if you're aware of this, but if you have a curve of the form $f(x,y)=\text{constant}$, then the gradient of $f$ at some point $(x_0,y_0)$ on the curve will be perpendicular to the curve at that point. Therefore, the slope of the line tangent to x^2+x y+y^2==3 at the point pt={1,1} is equal to

pt = {1, 1};
eq = x^2 + x y + y^2;
slope = -D[eq, x]/D[eq, y] /. Thread[{x, y} -> pt];


And the tangent line is equal to

line[x_] := pt[[2]] + (x - pt[[1]]) slope


To show that the line is indeed tangent at {1,1}:

Show[Plot[line[x], {x, 0, 2}],
ContourPlot[f[x, y] == 3, {x, 0, 2}, {y, 0, 2}]]


-
This is essentially my second solution, but given in an explicit form. – J. M. Feb 18 '12 at 17:53

If you are satisfied with a function which applies only to algebraic curves, there is an alternative approach which starts by homogenizing the algebraic curve. See for example the Wikipedia article on "Tangent". To homogenize the equation $x^2+xy+y^2-3=0$, we can try

Numerator@Together@ReplaceAll[x^2 + x y + x^2 - 3, {x -> x/z, y -> y/z}]


So the homogeneous equation looks like x^2 + x y + y^2 - 3 z^2. Let this polynomial be g. One equation of the tangent line is given by

$g_{x}(x_{0},y_{0},1) \cdot x + g_{y}(x_{0},y_{0},1) \cdot y + g_{z}(x_{0},y_{0},1)= 0$

We can compute each of the partial derivatives using D, so for example $g_{x}(1,1,1)$ is given by

D[g, x] /. {x -> 1, y -> 1, z -> 1}


Putting everything together, we get one equation of the line passing through $(1,1)$ which is tangent to the curve $x^2+xy+y^2-3=0$:

$3x+3y-6=0$.

My original goal was to find an analog of J.M.'s elegant solution to an earlier version of the problem which eschews the use of Reduce. I have not been able to put together a nice, short code like J.M.'s using this alternative approach though.

-
What is a good way to take the derivative of a function so that the output is also a function? It would be nice if I could do something like D[g,x][1,1,1] and get the partial derivative evaluated at $(1,1,1)$. Using ReplaceAll just does not seem right. – Michael Wijaya Feb 18 '12 at 7:11
g = x^2 + x y + y^2 - 3 z^2; (Derivative[##][Function[{x, y, z}, Evaluate[g]]][1, 1, 1] & @@@ IdentityMatrix[3]).{x, y, 1} == 0 works nicely, I think. – J. M. Feb 18 '12 at 7:20
@J.M. I am amazed at how quickly you come up with such elegant codes. – Michael Wijaya Feb 18 '12 at 7:25
I upvoted your answer, BTW. :) I distinctly recall a more automatic method for performing such homogenizations on algebraic curves based on the properties of Gröbner bases, but I need to check my notes to be certain. – J. M. Feb 18 '12 at 7:28

I've written a program to solve this sort of problem:

Tangent[ax_, ay_, eq_, x_, y_] :=
Module[{rule, slope},
rule = {a -> ax, y[a] -> ay};
slope = ToRules[Reduce[D[eq, x] /. x -> a /. rule, y'[a] /. rule]];
Reduce[y[x] - y[a] == y'[a] (x - a) /. rule /. slope, y[x]]]


To solve the above:

Tangent[1, 1, x^2 + x y[x] + y[x]^2 == 3, x, y]


which yields:

y[x] == 2 - x


Tangent also appears to work on problems involving explicit differentiation.

The parameters to Tangent are as follows:

• ax and ay indicate the point at which the line is tangent to the curve.

• eq is the equation of the curve.

• x and y are the independent and dependent variables.

-