4
$\begingroup$

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?

$\endgroup$
6
$\begingroup$

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

$\endgroup$
  • $\begingroup$ If I want multiple tangents, how do I do that? $\endgroup$ – Tyilo Aug 29 '12 at 9:58
  • $\begingroup$ I've edited my answer. $\endgroup$ – J. M. is away Aug 29 '12 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.