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
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.
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.
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).
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:
- single scalar to specify a sample distance for all dimensions.
- N scalars to specify a constant sample distance for each dimension. i.e. dx, dy, dz, …
- 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
- 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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With