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 am using ListLinePlot to draw a number of points to a graph:

ListLinePlot[{{5 10^-3, 0}, {4.65 10^-3, 100}, {4.32 10^-3, 200}, {3.74 10^-3, 400}, 
    {3.23 10^-3, 600}, {2.6 10^-3, 900}, {2.09 10^-3, 1200}, {1.68 10^-3, 1500}}]

How can I also draw the tangent to the line in a point?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

You can use Interpolation[] to reckon out tangent lines to your data, as in the following:

data = {{5 10^-3, 0}, {4.65 10^-3, 100}, {4.32 10^-3, 200},
        {3.74 10^-3, 400}, {3.23 10^-3, 600}, {2.6 10^-3, 900},
        {2.09 10^-3, 1200}, {1.68 10^-3, 1500}};

dint = Interpolation[data];
slope = dint';

With[{x = .003 (* where to draw the tangent *), h = 800 (* length of tangent *)},
 y = dint[x]; dir = Normalize[{1, slope[x]}]/2;
 ListLinePlot[data, Epilog -> Line[{{x, y} - h dir, {x, y} + h dir}]]]

data with tangent line


If multiple tangents are desired, here's one way to go about it:

With[{pos = {.002, .003, .0045} (* where to place tangents *),
      h = 400 (* length of tangent *)},
 ListLinePlot[data, Axes -> None, Epilog -> 
   Table[With[{y = dint[x], dir = Normalize[{1, slope[x]}]/2},
              {AbsoluteThickness[2], Line[{{x, y} - h dir, {x, y} + h dir}]}],
         {x, pos}], Frame -> True, PlotStyle -> AbsoluteThickness[1]]]

multiple tangents

share|improve this answer
    
If I want multiple tangents, how do I do that? –  Tyilo Aug 29 '12 at 9:58
    
I've edited my answer. –  J. M. Aug 29 '12 at 10:08

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.