Gradient calculation with python Gradient calculation with python numpy numpy

Gradient calculation with python


You need to give gradient a matrix that describes your angular frequency values for your (x,y) points. e.g.

def f(x,y):    return np.sin((x + y))x = y = np.arange(-5, 5, 0.05)X, Y = np.meshgrid(x, y)zs = np.array([f(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])Z = zs.reshape(X.shape)gx,gy = np.gradient(Z,0.05,0.05)

You can see that plotting Z as a surface gives:

sinxpy

Here is how to interpret your gradient:

gx is a matrix that gives the change dz/dx at all points. e.g. gx[0][0] is dz/dx at (x0,y0). Visualizing gx helps in understanding:

gx

Since my data was generated from f(x,y) = sin(x+y) gy looks the same.

Here is a more obvious example using f(x,y) = sin(x)...

f(x,y)enter image description here

and the gradients

g2

g1

update Let's take a look at the xy pairs.

This is the code I used:

def f(x,y):    return np.sin(x)x = y = np.arange(-3,3,.05)X, Y = np.meshgrid(x, y)zs = np.array([f(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])xy_pairs = np.array([str(x)+','+str(y) for x,y in zip(np.ravel(X), np.ravel(Y))])Z = zs.reshape(X.shape)xy_pairs = xy_pairs.reshape(X.shape)gy,gx = np.gradient(Z,.05,.05)

Now we can look and see exactly what is happening. Say we wanted to know what point was associated with the value atZ[20][30]? Then...

>>> Z[20][30]-0.99749498660405478

And the point is

>>> xy_pairs[20][30]'-1.5,-2.0'

Is that right? Let's check.

>>> np.sin(-1.5)-0.99749498660405445

Yes.

And what are our gradient components at that point?

>>> gy[20][30]0.0>>> gx[20][30]0.070707731517679617

Do those check out?

dz/dy always 0 check.dz/dx = cos(x) and...

>>> np.cos(-1.5)0.070737201667702906

Looks good.

You'll notice they aren't exactly correct, that is because my Z data isn't continuous, there is a step size of 0.05 and gradient can only approximate the rate of change.