Message Boards Message Boards


[✓] Plot the tangent of a function?

Posted 9 months ago
4 Replies
0 Total Likes

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]?

4 Replies


This post should help: post



Posted 9 months ago

Thank you for your suggestion!

Posted 9 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:

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]}]}]

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 9 months ago

Thank you for your suggestion!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract