1. ## Getting gradient vector from f(x,y)?

I am trying to draw arrows on a graph to show the normal gradient of a function. By gradient, I mean the arrows should follow the surface of the function, not being perpendicular to it.

For example the function $f(x, y) = 10sin(y - x) => z = 10sin(y - x)$. This is the graph I got

The arrows are coming very flat (on the x-y plane), and not really following the surface of the function.
This is what the function looks like

The problem is its not respecting the z-axis I think.

The way I generated the $(x,y,z)$ coordinates and the $(u,v,w)$ (this is the direction vector for each arrow) coordinates are:

Use the function above to get the gradient vector formula (using partial derivatives) which is

$q(x,y,z) = (-10cos(x - y), 10cos(x - y), -1) = (u,v,w)$

1. get your $x,y,z,u,v,w$
2. normalize $u,v,w$
3. draw the line from $(x,y,z)$ to $(x,y,z)+(u,v,w)*length$

Here I notice (I'm not sure if this is the problem) that the z component is always -1. Which is why the arrows appear to be flat on the x-y plane.

Does anyone see whats wrong here?

Thanks

2. ## Re: Getting gradient vector from f(x,y)?

never mind I figured it out. its supposed to be

(u,v,w) = (cos(theta), sin(theta), (df/dx)(x,y) * cos(theta) + (df/dy)(x,y) * sin(theta))