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

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

 Show[{Plot3D[F[x, y], {x, -2, 2}, {y, -2, 2}]},
         ((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
There are tones of cases to get inspired by and downlaod source code from here: and here: – 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
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

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.

  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]]]};
       Plot3D[F[x, y], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 50], 
           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

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



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

     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

Your Answer


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.