2

I am constructing a diagram that consists of a sine curve and its tangent line at a given argument. However, I want to show the coordinates of the point of tangency and the equation of the tangent line dynamically.

My work so far:

f[x_] := Sin@x;
l[x_, a_] := f[a] + f'[a] (x - a);
Manipulate[
   Show[
    Plot[Sin@x, {x, 0, 2 \[Pi]},
      PlotRange -> {-2, 2}, AxesStyle -> Arrowheads[0.03], 
      AxesLabel -> {"x", "Sin[x]"}, AspectRatio -> Automatic, 
      PlotLabel -> f[a] <> "+" <> f'[a] ("x-" <> a)], 
    Plot[l[x, a], {x, a - 2, a + 2}, PlotStyle -> Pink],
    Graphics[{PointSize[Medium], Red, Point[{a, Sin@a}]}]],
  {a, 0, 2 \[Pi]}]

Unfortunately, what I've done doesn't work.

So my question is: how do I get the information I need to make it work.

enter image description here

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
xyz
  • 605
  • 4
  • 38
  • 117

2 Answers2

3

Like that?

f[x_] := Sin@x;
l[x_, a_] := f[a] + f'[a] (x - a);
Manipulate[
  Show[Plot[Sin@x, {x, 0, 2 \[Pi]}, PlotRange -> {-2, 2}, AxesStyle ->Arrowheads[0.03],              
                                     AspectRatio -> Automatic], 
       Plot[l[x, a], {x, a - 2, a + 2}, PlotStyle -> Pink], 
       Graphics[{PointSize[Medium], Red, Point[{a, Sin@a}]}],
       Epilog -> {Text[StringForm["A = (``,``)", a, Sin@a], Scaled[{.7, .7}]], 
                  Text[StringForm["y = ``", Expand@l[x, a]], Scaled[{.7, .6}]]}]
   , {a, 0, 2 \[Pi]}]

enter image description here

Of course you can add Round or NumberForm etc. to reduce the precision of displaying. Also PolynomialForm[#, TraditionalOrder->True]& can help if one want to display line equation in form y = ax+b.

Kuba
  • 136,707
  • 13
  • 279
  • 740
3

This is more of a graphics design problem than a coding problem, which means it is open to an unlimited number of answers. Further, many of the layout decisions will be more a matter of taste than anything else. My own taste runs to showing the textual information inside a panel placed at the at the top-right corner of the plot. I use a panel along Column and Row because doing so puts a lot of formatting tools at my disposal.

Manipulate[
  Plot[{Sin[u], tangent[u, x]}, {u, 0, 2 π},
    AspectRatio -> Automatic,
    Epilog -> {
      {Red, PointSize[Large], Point[{x, Sin[x]}]},
      Inset[
        Panel[
          Column[{
            Row[{TraditionalForm[Graph of Sin["x"]]}],
            Row[{"Point of tangency: ", Chop@{x, Sin[x]}}],
            Row[{"Tangent line: y = ", Expand@tangent["x", x]}]}, 
            Background -> White],
          FrameMargins -> 5],
        {Right, Top}, {Right, Top}]},
    PlotRange -> {-3.25, 3.25},
    ImageSize -> Medium ],
  {{x, N[π]}, 0, N[2 π], Appearance -> "Labeled"},
  Initialization :> (tangent[u_, x_] := Sin[x] + Cos[x] (u - x))]

plot.png

m_goldberg
  • 107,779
  • 16
  • 103
  • 257