11
$\begingroup$

A common problem in the derivative section of calculus texts is "find the equation of the line that is tangent to the curve $y = \ldots$ at the point $P$."

To find the line that is tangent to $y = 2 x \sin x$ at $(\pi/2,\pi)$, I'd do something like this in Mathematica:

y[x_] := 2 x Sin[x]
y[x] == y'[x] x + b /. x -> Pi/2;
bRule = Solve[%, b][[1]];
y == y'[a] x + b /. bRule /. a -> Pi/2

which outputs y == 2 x.

Is this more or less idiomatic Mathematica code? Is there a better way?

$\endgroup$

2 Answers 2

21
$\begingroup$

Certainly, there is a better way:

y[x_] := 2 x Sin[x]; a = Pi/2;
Collect[Normal[Series[y[x], {x, a, 1}]], x, Simplify]

Recall that the formula for a Taylor polynomial looks a bit like this:

$$f(x)=\color{red}{f(a)+f^\prime (a)(x-a)}+\frac{f^{\prime\prime}(a)}{2}(x-a)^2+\cdots$$

and reconciling this with the geometric interpretation of the Taylor polynomial as the best one-point osculatory (agrees at function and derivative values) approximation of a function shows why the approach works. I believe this should be a standard way to look at Taylor polynomials in the textbooks, if it already isn't.


Here is an equivalent approach:

Collect[InterpolatingPolynomial[{{{a}, y[a], y'[a]}}, x], x, Simplify]

This is based on the fact that the tangent line is the unique Hermite interpolating polynomial of degree $1$.


Certainly, one could do the plodding, "traditional" (whatever that means) approach:

y[x_] := 2 x Sin[x]; a = Pi/2;
Collect[y[a] + y'[a] (x - a), x, Simplify]

In any event, Solve[] is definitely unnecessary here.

$\endgroup$
8
  • $\begingroup$ Thanks J.M.! The code I posted follows the approach taught in calculus texts. I wouldn't mind seeing an improved version of that approach as well (from anyone here). $\endgroup$
    – dharmatech
    Commented Feb 16, 2012 at 3:30
  • $\begingroup$ So does this. At least, I assume that the geometric interpretation of the Taylor polynomial is still being taught in calculus classes... $\endgroup$ Commented Feb 16, 2012 at 4:31
  • 1
    $\begingroup$ Thanks again J.M. Great answer. And thanks for moderating this new stackexchange site. $\endgroup$
    – dharmatech
    Commented Feb 16, 2012 at 6:35
  • $\begingroup$ A solution using a Taylor polynomial may not be appropriate for the level of the question or questioner: somebody beginning calculus, at least at taught at most U.S institutions, would certainly not know about Taylor polynomials. $\endgroup$
    – murray
    Commented Sep 15, 2012 at 14:30
  • $\begingroup$ +1 for Taylor expansion. But derivative against $x$ may not applicable for vertical tangent line. Parametric equation with the arc length parameter might be more general. $\endgroup$
    – Silvia
    Commented Sep 15, 2012 at 19:30
12
$\begingroup$

I'm recently doing something related, so here is my more general but some how too expensive method. I'll take the curve defined by

$$x^4-2 x^2+y^4-2 y^2+\frac{99}{100}=0$$

for example.

First we define the function and plot the curve:

exprFunc[x_, y_] := x^4 + y^4 - 2 x^2 - 2 y^2 + 99/100

exprgraph = 
 ContourPlot[exprFunc[x, y] == 0, {x, -2, 2}, {y, -2, 2}, 
  PlotRange -> {{-2, 2}, {-2, 2}}, AspectRatio -> Automatic]

Then we parametrize the curve with respect to natural parameter (i.e. the arc length $s$):

Fderiv = D[exprFunc[x, y], #] & /@ {x, y}

naturalderiv = 
 Reverse[{1, -1} Fderiv]/Sqrt[Fderiv.Fderiv] /. {x -> x[s], y -> y[s]}

(*find an initial point on the curve*)
xinit = x /. FindRoot[exprFunc[x, yinit = 0] == 0, {x, 2}]

1.04881

(*the natural parametric equation*)
naturalparaEq = With[{arcLength = 10},
    NDSolve[{
       x'[s] == naturalderiv[[1]],
       y'[s] == naturalderiv[[2]],
       x[0] == #[[1]], y[0] == #[[2]]
       }, {x, y}, {s, 0, arcLength}][[1]]  ]&@{xinit, yinit}

Now we can plot the tangent line any where and smoothly:

Manipulate[
 Show[{exprgraph,
   Graphics[{Black,
     Circle[{x[s], y[s]} /. naturalparaEq /. s -> svalue, .04],
     Lighter[Purple], Thickness[.005],
     Line@
      Table[{x[s], y[s]} + naturalderiv t /. naturalparaEq /. 
        s -> svalue,
       {t, {-3, 3}}]
     }]}],
 {svalue, 0, 10}]

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Hmm, this should be an answer to this question instead of this one, methinks. :) $\endgroup$ Commented Sep 15, 2012 at 20:36
  • $\begingroup$ @J.M. I missed that question. Anyway, they are from the same author :) $\endgroup$
    – Silvia
    Commented Sep 15, 2012 at 20:45
  • $\begingroup$ The "natrual" spelling is deliberate, I presume? $\endgroup$ Commented Sep 15, 2012 at 20:46
  • $\begingroup$ OH NO! I always made all kinds of spelling mistakes!! :( $\endgroup$
    – Silvia
    Commented Sep 15, 2012 at 20:54
  • $\begingroup$ @J.M. - well, it's pronounced as "natrual", so I think it's acceptable as alternative spelling :-) $\endgroup$
    – stevenvh
    Commented Sep 16, 2012 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.