Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

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.

share|improve this answer
    
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). –  dharmatech Feb 16 '12 at 3:30
    
So does this. At least, I assume that the geometric interpretation of the Taylor polynomial is still being taught in calculus classes... –  J. M. Feb 16 '12 at 4:31
    
Thanks again J.M. Great answer. And thanks for moderating this new stackexchange site. –  dharmatech Feb 16 '12 at 6:35
    
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. –  murray Sep 15 '12 at 14:30
    
+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. –  Silvia Sep 15 '12 at 19:30
show 3 more comments

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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