Steve
Steve

Reputation: 13

Which dx do I choose for np.gradient argument?

I wont be to specific but I have a graph E vs T ( T being the independent quantity) I want the derivative of E with respect to T. I am unsure what dx spacing I should choose?

Details:

T = 10**(np.arange(-1,1.5,0.05)) (I.e the spacing is not equal) E is a function of T.

Questions:

Which spacing do I use?

My thoughts:

I think I take the spacing of T i.e np.gradient(Energy, dx = T) ??

Upvotes: 1

Views: 857

Answers (1)

tom10
tom10

Reputation: 69242

For non-uniform spacing, pass in an array of positional values (not differences) which gradient will to use to calculate dx for each point. That is, pass in the array of absolute positions, not differences. So in your case, just pass in T.

Here's an example, as a test, where the blue is the curve and red is the calculated gradients.

enter image description here

import numpy as np
import matplotlib.pyplot as plt

T = 10**(np.arange(-1,1.5,0.05))

E = T**2
gradients = np.gradient(E, T)

plt.plot(T, E, '-o') # plot the curve
for i, g in enumerate(gradients): # plot the gradients at each point
    plt.plot([T[i], T[i]+1], [E[i], E[i]+g], 'r')

Here's the line from the docs that's of interest:

  1. 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

Upvotes: 1

Related Questions