Hello all. Trying to find slope of function using numpy. Getting close, but results are a bit off. Hope someone out here can help.
import numpy as np def deriv(y): x = list(range(len(y))) x.reverse() # Change from [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] x = np.array(x) # to [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] y = np.array(y) # x.reverse() is used to put point 0 at end of list. z = np.polyfit(x, y, 2) print np.poly1d(z) # Returns: # 2 # 3.142 x - 18.85 x + 35.13 # 2 # Should be closer to 3.142 x - 6.283 + 10 ???????????????????? return [z[0] * 2, z[1]] if __name__=='__main__': # range(-6,5,1) # y = 3.141592 * x ** 2 - 6.283184 * x + 10 for x in range(-6, 5, 1) # 160.796416, 119.95572, 85.398208, 57.12388, 35.132736, 19.424776, 10.0, 6.858408, 10.0, 19.424776, 35.132736 # # y' = 6.283184 * x - 6.283184 for x in range(-6, 5, 1) # -43.982288, -37.699104, -31.41592, -25.132736, -18.849552, -12.566368, -6.283184, 0.0, 6.283184, 12.566368, 18.849552 # z = deriv([160.796416, 119.95572, 85.398208, 57.12388, 35.132736, 19.424776, 10.0, 6.858408, 10.0, 19.424776, 35.132736]) for x in range(-6,5,1): print str(w(x)) + ',' , # Returns: # -56.548656, -50.265472, -43.982288, -37.699104, -31.41592, -25.132736, -18.849552, -12.566368, -6.283184, -1.06581410364e-14, 6.283184 # Should be: # -43.982288, -37.699104, -31.41592, -25.132736, -18.849552, -12.566368, -6.283184, 0.0, 6.283184, 12.566368, 18.849552 # Note that the range is offset by 2 positions -- http://mail.python.org/mailman/listinfo/python-list