Message Boards Message Boards

[✓] Plot the tangent of a function?

GROUPS:

In my mathematical programming class, I have to write a function in Mathematica that takes in a function f and a point pt and gives the graph of f and the tangent line of f at pt. This is probably a very simple task, however, I'm having some difficulty. This is what I have:

drawTangent[f_, {x0_, y0_}] := 
  Plot[{f, f'[x0]*(x - x0) + y0}, {x, x0 - 10, y0 + 10}, 
   PlotRange -> {y0 - 10, y0 + 10}];

I really think this should work. The problem is that instead of giving the tangent line it gives f[x]=y0. drawTangent[g[x], {-3, 9}], where g[x_]:=x^2, gives g[x^2] and f[x]=9. I think instead of f'[x]=-6 mathematica gives f'[x]=0. I checked and it does this for all other points on the graph. Yet, when I input g'[-3] in a different cell by itself i get -6. Can some explain what I did wrong? Should I put something else instead of f'[x0]?

POSTED BY: Mia Arnold
Answer
3 months ago

Mia,

This post should help: post

Regards

Neil

POSTED BY: Neil Singer
Answer
3 months ago

Thank you for your suggestion!

POSTED BY: Mia Arnold
Answer
3 months ago

Hi. I think that to draw a tangent a curve your point has to actually be on the curve.

I rewrote your function slightly. I used the following code to put a point inside the plot showing where the tangent line passes:

https://mathematica.stackexchange.com/questions/101660/how-to-put-single-points-inside-the-plot

$Version
fun[x_]:=Sin[x]
ptslope[x_,x0_]:= Plot[{fun[x],fun'[x0]*(x-x0)+fun[x0]},{x,x0-2,x0+2},
Epilog -> {Blue, PointSize@Large, Point[{x0, fun[x0]}]}]
ptslope[x,Pi/4]

enter image description here

Edit: See Dr. Singer's very well written post and also note that his solution takes greater care of watching for pathological behavior such as the non existence of a derivative at a particular point.

POSTED BY: v z
Answer
3 months ago

Thank you for your suggestion!

POSTED BY: Mia Arnold
Answer
3 months ago

Group Abstract Group Abstract