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?

  • $\begingroup$ To clarify: you want a tangent line, not a tangent plane? $\endgroup$ Aug 16, 2012 at 17:05
  • $\begingroup$ Yes, as I wrote: I want to visualize directional derivatives, so a tangent line seemed quite appropriate for me;). $\endgroup$
    – mbork
    Aug 16, 2012 at 17:10
  • 2
    $\begingroup$ There are tones of cases to get inspired by and downlaod source code from here: bit.ly/MAF6D6 and here: bit.ly/NJjKzF $\endgroup$ Aug 16, 2012 at 17:17
  • $\begingroup$ @VitaliyKaurov: thank you, I'll look at them; still, I'd like to know why my code didn't work... $\endgroup$
    – mbork
    Aug 16, 2012 at 17:21
  • 1
    $\begingroup$ 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$. $\endgroup$
    – mbork
    Aug 16, 2012 at 22:06

2 Answers 2


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}}

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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