I have a function, I want to get its integral function, something like this:
That is, instead of getting a single integration value at point x
, I need to get values at multiple points
.
For example:
Let's say I want the range at (-20,20)
def f(x):
return x**2
x_vals = np.arange(-20, 21, 1)
y_vals =[integrate.nquad(f, [[0, x_val]]) for x_val in x_vals ]
plt.plot(x_vals, y_vals,'-', color = 'r')
The problem
In the example code I give above, for each point, the integration is done from scratch. In my real code, the f(x)
is pretty complex, and it's a multiple integration, so the running time is simply too slow(Scipy: speed up integration when doing it for the whole surface?).
I'm wondering if there is any way of efficient generating the Phi(x)
, at a giving range.
My thoughs:
The integration value at point Phi(20)
is calucation from Phi(19)
, and Phi(19)
is from Phi(18)
and so on. So when we get Phi(20)
, in reality we also get the series of (-20,-19,-18,-17 ... 18,19,20)
. Except that we didn't save the value.
So I'm thinking, is it possible to create save points for a integrate function, so when it passes a save point
, the value would get saved and continues to the next point. Therefore, by a single process toward 20
, we could also get the value at (-20,-19,-18,-17 ... 18,19,20)