Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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);
    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

share|improve this question

marked as duplicate by Artes, Sjoerd C. de Vries, Yves Klett, Michael E2, Dr. belisarius Oct 19 '13 at 2:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Inset/Text? – Kuba Oct 18 '13 at 5:02
Dear Tang, your Mma skills seem improved a lot :-) – mm.Jang Oct 18 '13 at 7:09
@mm.Jang,Thanks,can I know your QQ number? – Shutao TANG Oct 18 '13 at 9:15
@Tangshutao evaluate Uncompress@"1:eJxTTMoPyvSrvqUDAA/NA30=" in mathematica, then you get my qq. – mm.Jang Oct 18 '13 at 12:52
up vote 4 down vote accepted

Like that?

f[x_] := Sin@x;
l[x_, a_] := f[a] + f'[a] (x - a);
  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.

share|improve this answer
,Dear Kuba,I use your method of Inset,it works.However,the answer you give is perfect.Thanks sincerely! – Shutao TANG Oct 18 '13 at 5:14
@Tangshutao That's great you like it :) Good luck. – Kuba Oct 18 '13 at 6:04

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.

  Plot[{Sin[u], tangent[u, x]}, {u, 0, 2 π},
    AspectRatio -> Automatic,
    Epilog -> {
      {Red, PointSize[Large], Point[{x, Sin[x]}]},
            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))]


share|improve this answer

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