Reputation: 1281
I have been having a few issues using the quadrature function in python 2.7 (part of the scipy.integrate module). The equation I am trying to integrate is simply:
x/(d^2) - (x^2)
The integration is between limits a and b. However, I need to do the integration at 40 different values of d and am not sure how to pass in the second argument so as to loop the integration over the values of d. Any help would be much appreciated and is quadrature the best way to evaluate this problem.
Upvotes: 4
Views: 265
Reputation: 58721
If you want exact symbolic integration, you'll need to turn to SymPy. Try
import sympy
x = sympy.Symbol('x')
a = sympy.Symbol('a')
b = sympy.Symbol('b')
d = sympy.Symbol('d')
res = sympy.integrate(x / (d**2 - x**2), (x, a, b))
print(res)
This returns
log(a**2 - d**2)/2 - log(b**2 - d**2)/2
which you can most easily use to evaluate your data.
Upvotes: 0
Reputation: 13467
from numpy import arange
from scipy.integrate import quad
beg = 0.
end = 4.
res = []
for d in arange(1., 40.):
res.append(quad(lambda x: x/(d**2.)-(x**2.), beg, end))
You can then access the results by
print res[0]
or even
print res
Upvotes: 2
Reputation: 500157
In [9]: from scipy.integrate import quad
In [10]: a = 0
In [11]: b = 1
In [12]: [quad(lambda x, d: x/(d**2)-x**2, a, b, args=d) for d in range(2, 5)]
Out[12]:
[(-0.20833333333333334, 2.3717550132075781e-15),
(-0.27777777777777773, 3.0886887822595405e-15),
(-0.30208333333333337, 3.3546344203581545e-15)]
Change the for d in range(2, 5)
as required.
Upvotes: 4