Message Boards Message Boards

0
|
30745 Views
|
2 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Drawing tangent line on a curve

Posted 11 years ago
 Hi All !

 I am new to Mathematica...
 Could someone pl. suggest how to draw a tangent line on a curve... a demo would be great
 I am working on finding instanteous rate of change of at a point ...

 Thanx
POSTED BY: tmehmoodraza
2 Replies
Posted 11 years ago
Here are approaches for single variable: tangent line and two variable functions (tangent plane).
  1. Single variable function:
    tg[f_, x_, p_] := (f'[x] /. x -> p) (x - p) + f[p]
    Here the argument f is function, x is symbol for differentiation and p is the x-value of point the line will be tangent to.
    1. Example using a pure function:
      q = #^3 - 3 #^2 &;
      Manipulate[
      Plot[{q[x], tg[q, u, m] /. u -> x}, {x, -3, 3}, PlotRange -> {-5, 5},
         Epilog -> {Red, PointSize[0.02], Point[{m, q[m]}]}], {m, -1, 1,
        0.01}]
    2. Visualizing:

  2. Tangent plane
    tangent[f_, arg_,
      pt_] := (Grad[f @@ arg, arg] /. Thread[arg -> pt]).(arg - pt) +
      f @@ pt


    This is essentially a generalization of the previous code exploiting the gradient function and dot product.
    1. Example:
      w[x_, y_] := Sin[x] Cos[y];
    2. Visualizing:
       ListAnimate[
         Table[
           Show[Plot3D[w[x, y], {x, -3, 3}, {y, -3, 3},
             ViewVector -> {10, 10, 10}, PlotStyle -> Opacity[0.7]],
            Graphics3D[{Red, PointSize[0.03],
              Point[{Join[{j, j}, {Sin[j] Cos[j]}]}]}],
            Plot3D[Evaluate[(tangent[w, {x, y}, {j, j}] /. {x -> u,
                 y -> v})], {u, j - 0.5, j + 0.5}, {v, j - 0.5, j + 0.5},
             Mesh -> False, PlotStyle -> Green]], {j, -3, 3, 0.1}]];



POSTED BY: Mark Dooris
Here is a solution. I've picked a function that I am fond of, but you can adapt to your own purposes:

function which takes the name of function of one argument, a position to evaluate the derivative and a parameter that will be used for the tangent.
tangentLine[func_, loc_, param_] :=
Module[
  {deriv, x},
  deriv = D[func[x], x] /. x -> loc;
  deriv (param - loc) + func[loc]
  ]

For example:
f[t_] := t^2 + Cos[t]
tangentLine[f, 2, z]

Here it is with a function that I have defined within a module, others might have put the function definition in an Initialization, but I think this is easier to read.
 Module[
  {
   myFunction,
   tangent,
   t
   },
  myFunction[0] = 0;
  myFunction[1] = 0;
  myFunction[x_] := 3 x (1 - x) + (x Log[x] + (1 - x) Log[1 - x]);
Manipulate[
  tangent = tangentLine[myFunction, where, t];
  Plot[myFunction[z], {z, 0, 1},
   Epilog -> {Red,
     Line[{{0, tangent /. t -> 0}, {1, tangent /. t -> 1}}]}
   ],
  {{where, 0.25}, 0, 1}
  ]
]

Example result:
POSTED BY: W. Craig Carter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract