Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

It seems quite embarrassing for me, but somehow I can't draw a tangent line to a surface in a given direction. Assume that I want to visualize directional derivative of the function, say, $(x,y)\mapsto x^2+y^2$, at the point, say, $(1,0.5)$. I tried this:

F[x_, y_] := x^2 + y^2

Manipulate[
 Show[{Plot3D[F[x, y], {x, -2, 2}, {y, -2, 2}]},
      {ParametricPlot3D[{Cos[phi]*t,Sin[phi]*t,
         ((D[F[x, y], x] /. x -> 1 /. y -> 0.5)*Cos[phi]
         + (D[F[x, y], y] /. x -> 1 /. y -> 0.5)*Sin[phi])*t},
      {t, 0, 2}, PlotStyle -> Thick]}], {phi, 0, 2*Pi, Pi/4}]

to no avail. (I am a bit afraid that I did make some stupid mathematical error; unfortunately, Mathematica's syntax is a bit "perlish" to me: I'm learning to write in it, but have serious difficulties reading it...)

My question is twofold: (1) what is wrong with the above code and (2) what is a "canonical" (read: elegant and possibly fast) way of doing this?

share|improve this question
    
To clarify: you want a tangent line, not a tangent plane? –  J. M. Aug 16 '12 at 17:05
    
Yes, as I wrote: I want to visualize directional derivatives, so a tangent line seemed quite appropriate for me;). –  mbork Aug 16 '12 at 17:10
2  
There are tones of cases to get inspired by and downlaod source code from here: bit.ly/MAF6D6 and here: bit.ly/NJjKzF –  Vitaliy Kaurov Aug 16 '12 at 17:17
    
@VitaliyKaurov: thank you, I'll look at them; still, I'd like to know why my code didn't work... –  mbork Aug 16 '12 at 17:21
1  
OK, so it seems that either it's not only me who can't read Mathematica code, or my code is really bad (probably the latter). Given a function $f$ from (a subset of) $\mathbb{R}^2$ into $\mathbb{R}$, a point $(a,b)$ in its domain and a direction $\phi$ on the plane, I want to draw a ray starting at $(a,b,f(a,b))$ tangent to the graph of $f$ (provided that anything like that does exist) and such that its projection onto the plane $z=0$ has direction $\phi$. –  mbork Aug 16 '12 at 22:06
show 5 more comments

2 Answers

Try this variation. I didn't really change much, just spelled things out in a way I find to be more clear. It does seem to show directional derivative lines. Also I made the items in Show to be a flat list in case having two lists was causing trouble.

Manipulate[
  Module[{dirderiv, x, y, base},
    dirderiv = ({D[F[x, y], x], D[F[x, y], y]} /. {x -> pt[[1]], y -> pt[[2]]}
               ).{Cos[phi], Sin[phi]};
    base = {pt[[1]], pt[[2]], F[pt[[1]], pt[[2]]]};
    Show[{
       Plot3D[F[x, y], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 50], 
       ParametricPlot3D[
           base + t*{Cos[phi], Sin[phi], dirderiv}, {t, 0, 2}, 
           PlotStyle -> Thick
       ]
    }]
  ], 
  {phi, 0, 2*Pi, Pi/4}, 
  {pt, {-2, -2}, {2, 2}}
]
share|improve this answer
add comment

Answer to (2): As I said in the comment, there are tons of cases to get inspired by and download source code from here:

Answer to (1): too many { } inside Show. To make your code work without paying attention to mathematics do this:

F[x_, y_] := x^2 + y^2

Manipulate[

 Show[

  Plot3D[F[x, y], {x, -2, 2}, {y, -2, 2}, PlotStyle -> Opacity[.5], 
   Mesh -> False],

  ParametricPlot3D[{Cos[phi]*t, 
    Sin[phi]*
     t, ((D[F[x, y], x] /. x -> 1 /. y -> 0.5)*
        Cos[phi] + (D[F[x, y], y] /. x -> 1 /. y -> 0.5)*Sin[phi])*
     t}, {t, 0, 2}, PlotStyle -> Thick]

  ] , {phi, 0, 2*Pi, Pi/4}]

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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