import scipy.integrate as spi
import numpy as np
print('Single integral computed by SciPy quad')
print('Example 1-01 quad')
print('Integral of 2xe^-x from x=1 to x=5')
integrand = lambda x : 2 * x * np.exp(-x)
a = 1.
b = 5.
result, error = spi.quad(integrand, a, b)
print('Result is ', result, 'with error ', error)
输出:
Single integral computed by SciPy quad
Example 1-01 quad
Integral of 2xe^-x from x=1 to x=5
Result is 1.3906624006967436 with error 1.54394541673402e-14
用 fixed_quad 计算积分
下面是使用 SciPy 库的 fixed_quad 函数计算积分的 Python 代码示例:
import scipy.integrate as spi
import numpy as np
print('Single integral computed by SciPy fixed_quad')
print('Example 1-01 fixed_quad')
print('Integral of 2xe^-x from x=1 to x=5')
integrand = lambda x : 2 * x * np.exp(-x)
a = 1.
b = 5.
result, none = spi.fixed_quad(integrand, a, b, n=5)
print('Result is ', result)
输出:
Single integral computed by SciPy fixed_quad
Example 1-01 fixed_quad
Integral of 2xe^-x from x=1 to x=5
Result is 1.39066368239563