So, I am working on some numerical calculations. I calculated about 100,000 function points ( return_times ) only numerically, and now I want it to be obtained using numpy.gradient . As far as I understand ( doc ), for f (x) I can give the following arguments: numpy.gradient(arr_of_fx_datapoints, arr_of_their_x_values) to make it work. And this is what I (intended) to do.
Except that it does not work. The result is almost (but not exactly) zero everywhere. The bug is reproduced by this abstract of my code below (sin ^ 2 (x) has one form in its original function):
import matplotlib.pyplot as plt import numpy as np def find_times(t_arr): return np.power(np.sin(t_arr), 2) t_0 = 0 t_max = np.pi-1E-10 datapoints = 100000 dt = (t_max - t_0) / datapoints t_points = np.arange(t_0, t_max, dt, dtype=np.float64) return_times = find_times(t_points) gd = np.gradient(return_times, t_points) plt.plot(t_points, gd) plt.plot(t_points, return_times) plt.show()
The result is disappointing:

If I type gd , it shows that it really is not absolutely null:
[ inf 6.28318530e-05 6.28318529e-05 ..., -1.25666419e-09 -6.28326813e-10 -3.14161265e-10]
So: What did I miss? What is the maximum correct way for numerical conversion in Python?
Enviroment: Linux Mint 18.2 OS, Geany Editor, NumPy 1.11.0.
source share