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.

I'm trying to do this:

enter image description here

In this graph, the secant points are aproximated in order to become the tangent, it seems I need some kind of function which plots a line based on two points and it's points do not limit the end of the line.

This is what I could do, as you can evaluate (see?), you'll see the line ends at the proposed points.

  Plot[{15 - 2 x^2}, {x, -1, 3}],
  Graphics[Line[{{1, 13}, {2, 7}}]]

I've read the reference on line but I still have no idea on how to do it, I imagine there are two ways of doing it, (1) the way I did, with Line; (2) With the Plot function.

share|improve this question
A Line[] object always has two ends, by its very nature (so it is properly a line segment as opposed to a line)... what you can do is to have your Line[] extend past the PlotRange of your plot... –  Guess who it is. Sep 15 '12 at 10:21
This is a standard sort of thing folks do with Mathematica. Why not look at versions published to the Wolfram Demonstrations project, e.g.: demonstrations.wolfram.com/SecantApproximations –  murray Sep 15 '12 at 14:35

4 Answers 4

up vote 11 down vote accepted

A little bit more general (for any function, interval, starting and final points...) and implementing most of your labels:

appDeriv[f_, x0_, xinit_, pRange_List] := Module[{xseq, tan, tan1, l, plot, absPR},
   xseq      = Table[x0 + Exp[-n/2] (xinit - x0), {n, 0, 10}]; (* exploration points *)
   tan       = D[f@x, x] /. x -> x0;                           (* real derivative *)
   tan1[x_] := (f[x] - f[x0])/(x - x0);                        (* approx derivative  *)
   l[t_]    := Line[Transpose[{pRange, pRange t + (-t x0 + f@x0)}]]; (*line w/given angle*)
   plot = Plot[f@x, {x, pRange[[1]], pRange[[2]]}, PlotStyle->Thick, PlotLabel->InputForm@f];
   absPR = AbsoluteOptions[plot, PlotRange][[1, 2]];     (*get actual prange for the plot*)
    Thick, PointSize[Large],
    Green, l[tan], Red, l[tan1[p]], Black,  (* The two lines *)
    Point[{x0, f@x0}],Point[{p,AbsoluteOptions[plot, AxesOrigin][[1,2,2]]}], Point[{p, f@p}],
    Inset[ "x =" <> ToString@N@p, {p, (absPR[[2, 1]] 9 + absPR[[2, 2]])/10}],
    Inset[Framed@Grid[{{"Exact ", tan}, {"Calculated", N@tan1[p]}}], {Left,Center}, 
          {Left, Center}, Background -> Yellow]}]], 
 {p, xseq}] ];
GraphicsGrid[{{appDeriv[Sin@# + 1 &, 0, 2, {-2, 2}],appDeriv[2 # + 2 # # &, 1, 2, {-2, 2}]}}]

Mathematica graphics

enter image description here

share|improve this answer
This is nice! One suggestion would be to use a variable instead of a pure function in the plot title (only the title, not in the code). If it's being used as a classroom material, that would be easier to understand than # and &. –  The Toad Sep 16 '12 at 18:34
@R.M This is a starting point, but I think it isn't ready for classroom usage in several ways. The yellow label can occlude the curves, the point legend at the axis can be cluttered by the curve or lines, and I couldn't find an easy way to show the point's legends along the curve consistently out of the curve's path. All those require considerable work. –  belisarius Sep 16 '12 at 18:45
line[x_] := Solve[{a + b == 13, a x + b == 15 - 2 x^2}, {a, b}] // Quiet
f[x_, x0_] := {15 - 2 x^2, (a x + b) /. line[x0]}

Animate[ Plot[{f[x, x0], -4 x + 17}, {x, -2, 3}, PlotRange -> {0, 18}, PlotStyle -> Thick,
               Evaluated -> True, Epilog -> {PointSize[0.025], 
               Point[{{1, 13}, {x0, 15 - 2 x0^2}}]}],           {x0, -2, 2.5}]

enter image description here

Adding any formulae has been left up to you.

share|improve this answer
f[x_] := 15 - 2 x^2

x0 = 1;
tangentLine[x_] = Normal[Series[f[x], {x, x0, 1}]]

secantLine[h_, x_] := InterpolatingPolynomial[{{1, f[1]}, {h, f[h]}}, x]

Animate[Plot[{f[x], tangentLine[x], secantLine[h, x]}, {x, -1, 3}, 
  AxesLabel -> {"x", "y"}, 
  Epilog -> {AbsolutePointSize[7], Point[{{1, 0}, {1, f[1]}, {h, 0}, {h, f[h]}}]}, 
  PlotRange -> {-4, 20}, PlotStyle -> {Red, Blue, Green}],
  {h, Range[2., 1.1, -.1] ~Join~ Range[1.09, 1.01, -.01] ~Join~ Range[1.009, 1.001, -.001]}]

tangent line animation

Adding the labels is up to you (hint: modify the setting of Epilog by adding Text[] primitives).

share|improve this answer

It seems to me that you are asking for a way to extend a line segment specified by two points. Here is one method you might use.

extend[x:{a_, b_}, d_] := (d/Norm[a - b]) (x - {b, a}) + x

This will extend a line segment a b by distance d in either direction. Example:

DynamicModule[{pt = {{1, 1}, {2, 1}}},
 LocatorPane[Dynamic @ pt,
   {Red, Line @ Dynamic @ extend[pt, 5]},
   PlotRange -> {{-10, 10}, {-10, 10}},
   Axes -> True,
   GridLines -> Automatic

Mathematica graphics

share|improve this answer

Your Answer


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.