11

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
dharmatech
  • 919
  • 2
  • 11
  • 19

2 Answers2

21

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
12

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

Silvia
  • 27,556
  • 3
  • 84
  • 164