Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-uniform spacing with numpy.gradient

I'm not sure how to specify non-uniform spacing when using numpy.gradient.

Here's some example code for y = x**2.

import numpy as np
import matplotlib.pyplot as plt

x = [0.0, 2.0, 4.0, 8.0, 16.0]
y = [0.0, 4.0, 16.0, 64.0, 256.0]
dydx = [0.0, 4.0, 8.0, 16.0, 32.0] # analytical solution

spacing = [0.0, 2.0, 2.0, 4.0, 8.0] #added a zero at the start to get length matching up with y

m = np.gradient(y, spacing)

plt.plot(x, y, 'bo',
         x, dydx, 'r-', #analytical solution
         x, m, 'ro')    #calculated solution
plt.show()

The length of the spacing array will always be one less than the array I want to calculated the gradient of. Adding in a zero to get the lengths matching up (like in the example code above) gives incorrect answers, with an infinite gradient for one point.

I can't understand / follow the numpy.gradient documentation for non-uniform spacing (https://docs.scipy.org/doc/numpy/reference/generated/numpy.gradient.html)

How should I specify the spacing between points? Is there an alternative way of doing this?

Numpy version 1.9.2

like image 223
Aaron Avatar asked Aug 10 '18 06:08

Aaron


People also ask

How does NumPy calculate gradient?

The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array.

How do you find the gradient of a function in Python?

The gradient of a function simply means the rate of change of a function. We will use numdifftools to find Gradient of a function. Examples: Input : x^4+x+1 Output :Gradient of x^4+x+1 at x=1 is 4.99 Input :(1-x)^2+(y-x^2)^2 Output :Gradient of (1-x^2)+(y-x^2)^2 at (1, 2) is [-4.

What is gradient Python?

gradient is the function or any Python callable object that takes a vector and returns the gradient of the function you're trying to minimize. start is the point where the algorithm starts its search, given as a sequence (tuple, list, NumPy array, and so on) or scalar (in the case of a one-dimensional problem).


1 Answers

The API of the function is quite confusing. For non-uniformly spaced sample points, the gradient function takes the coordinates of the point rather than the spacings:

varargs : list of scalar or array, optional

Spacing between f values. Default unitary spacing for all dimensions. Spacing can be specified using:

  1. single scalar to specify a sample distance for all dimensions.
  2. N scalars to specify a constant sample distance for each dimension. i.e. dx, dy, dz, …
  3. N arrays to specify the coordinates of the values along each dimension of F. The length of the array must match the size of the corresponding dimension
  4. Any combination of N scalars/arrays with the meaning of 2. and 3.

I slightly modified your example:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(10)
x.sort()
y = x**2
dydx = 2*x

dydx_grad = np.gradient(y, x)

plt.plot(x, dydx, 'k-', label='analytical solution')
plt.plot(x, dydx_grad, 'ro', label='calculated solution')
plt.legend(); plt.xlabel('x'); plt.ylabel('dy / dx'); plt.show(); 
like image 65
xdze2 Avatar answered Nov 15 '22 04:11

xdze2