3
$\begingroup$

I have made this program to plot a tangent line to a graph, but the thing I don't know how to fix is that the tangent line is at a fixed length, when I move the slider it expands and shortens. I want the line to be like a segment.

Tangent[f_, x_] := Module[{},
  Manipulate[
   Show[
    Plot [f'[p] (x - p) + f[p], {x, p - 1, p + 1},
     PlotStyle -> {Thick, Orange},
     PlotRange -> {{-10, 10}, {-70, 70}}],
    Plot[f[x], {x, -10, 10}, PlotRange -> {{-10, 10}, {-70, 70}}, 
     PlotStyle -> {color}]
    ], {p, -10, 10, 
    0.2}, {color, {Purple -> "Purple", Blue -> "Blue"}}]
  ]

f[x_]:=x^2 Sin[x];

Tangent[f,x]

enter image description here

$\endgroup$
1
  • $\begingroup$ You're incrementing your $x$ value by $\pm 1$, and hence the length of the line segment will change. Instead, scale the $x$ value so the full length is constant. $\endgroup$ Mar 24, 2017 at 20:25

1 Answer 1

5
$\begingroup$

There are two things to take into account:

  1. The length of the tangent is proportional to $\sqrt{1+f'^2}$, and

  2. The aspect ratio will need to be accounted for in order tor the segment to appear of constant length on screen. The aspect ratio relates to the actual coordinates through the bounds on the plots.

Here is one way to go about it:

a = 10; 
b = 70;
aspect = 2/3; (* aspect ratio is relevant to the segment length *)
l = 200; (* sets the length of the tangent *)

Tangent[f_, x_] := Module[{},
  Manipulate[
   m := l/Sqrt[b^2 + aspect^2 a^2 f'[p]^2];
   Show[
    Plot[f[x], {x, -10, 10}, PlotRange -> {{-a, a}, {-b, b}},
     AspectRatio -> aspect],
    Plot[f'[p] (x - p) + f[p], {x, p - m, p + m}, 
     PlotStyle -> {Thick, Orange}, PlotRange -> {{-a, a}, {-b, b}}]
    ],
   {p, -a, a, 0.2}]]

f[x_] := x^2 Sin[x];

Tangent[f, x]

enter image description here

$\endgroup$
1
  • $\begingroup$ +1 I completely missed the aspect ratio part and was amazed why 1/Sqrt[1+f'] wasn't working lol $\endgroup$
    – BlacKow
    Mar 24, 2017 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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