How to draw a tangent to a surface in a given direction?

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?

• To clarify: you want a tangent line, not a tangent plane? – J. M. will be back soon 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: 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
• 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.

Manipulate[
Module[{dirderiv, x, y, base},
dirderiv = ({D[F[x, y], x], D[F[x, y], y]} /. {x -> pt[], y -> pt[]}
).{Cos[phi], Sin[phi]};
base = {pt[], pt[], F[pt[], pt[]]};
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}}
]

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