Python differentiation using numpy not creating expected result

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:
enter image description here

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.

+3
source share
1 answer

The docs don't mention it, but coordinate array support is very new, NumPy 1.13. In previous versions of NumPy, you can only specify a fixed scalar step value for each dimension.

NumPy 1.12 has a check to catch non-scalar steps, but the NumPy 1.11 you are on does not notice the input signal with the array and silently does the wrong thing, trying to treat the array as a step.

+5
source

Source: https://habr.com/ru/post/1202704/


All Articles