如何使用数值方法逼近积分的解
How to use numerical methods to approximate the solution of an integral
我有以下积分(更多详情:https://math.stackexchange.com/questions/3193669/how-to-evaluate-the-line-integral-checking-stokes-theorem)
C_3 可以用三角函数计算。那么你可以这样解决:
import sympy as sp
t = sp.symbols('t')
sp.Integral((1-sp.cos(t)-sp.sin(t))**2 * sp.exp(1-sp.cos(t)-sp.sin(t)) * (sp.sin(t)-sp.cos(t)), (t, 0, 2*sp.pi))
问题是 C_1 和 C_2。这些不能用技巧来评估。那我就得用数值方法了
你有什么建议? 我一直在尝试 N()
但一无所获。
谢谢。
您可以使用scipy.integrate.quad
函数:
from scipy.integrate import quad
from numpy import cos, sin, exp, pi
f1 = lambda t: (1 + sin(t))*exp(1+cos(t))*(-sin(t))
f2 = lambda t: ((1 + cos(t))**2 + exp(1+cos(t)))*cos(t)
C1, err1 = quad(f1, 0, 2*pi)
C2, err2 = quad(f2, 0, 2*pi)
print("C1 = ", C1, ", estimated error: ", err1)
print("C2 = ", C2, ", estimated error: ", err2)
输出:
C1 = -9.652617083240306, estimated error: 2.549444932020608e-09
C2 = 15.93580239041989, estimated error: 3.4140955340600243e-10
编辑:
您还可以通过参数指定精度:epsrel
:相对误差,epsabs
:绝对误差。但这有点棘手(参见 this):
我们将绝对误差目标指定为零。此条件无法满足,因此相对误差目标将决定积分何时停止。
C1, err1 = quad(f1, 0, 2*pi, epsrel=1e-10, epsabs=0)
print("C1 = ", C1, ", estimated error: ", err1)
输出:
C1 = -9.652617083240308 , estimated error: 1.4186554373311127e-13
备选方案:使用 quadpy(我的一个项目):
import quadpy
from numpy import cos, sin, exp, pi
c1, err1 = quadpy.quad(
lambda t: (1 + sin(t)) * exp(1 + cos(t)) * (-sin(t)), 0.0, 2 * pi,
)
c2, err2 = quadpy.quad(
lambda t: ((1 + cos(t)) ** 2 + exp(1 + cos(t))) * cos(t), 0.0, 2 * pi,
)
print("C1 = ", c1, ", estimated error: ", err1)
print("C2 = ", c2, ", estimated error: ", err2)
C1 = -9.652617076333142 , estimated error: 1.3725463615061705e-09
C2 = 15.9358023895608 , estimated error: 6.646678031309946e-11
我有以下积分(更多详情:https://math.stackexchange.com/questions/3193669/how-to-evaluate-the-line-integral-checking-stokes-theorem)
C_3 可以用三角函数计算。那么你可以这样解决:
import sympy as sp
t = sp.symbols('t')
sp.Integral((1-sp.cos(t)-sp.sin(t))**2 * sp.exp(1-sp.cos(t)-sp.sin(t)) * (sp.sin(t)-sp.cos(t)), (t, 0, 2*sp.pi))
问题是 C_1 和 C_2。这些不能用技巧来评估。那我就得用数值方法了
你有什么建议? 我一直在尝试 N()
但一无所获。
谢谢。
您可以使用scipy.integrate.quad
函数:
from scipy.integrate import quad
from numpy import cos, sin, exp, pi
f1 = lambda t: (1 + sin(t))*exp(1+cos(t))*(-sin(t))
f2 = lambda t: ((1 + cos(t))**2 + exp(1+cos(t)))*cos(t)
C1, err1 = quad(f1, 0, 2*pi)
C2, err2 = quad(f2, 0, 2*pi)
print("C1 = ", C1, ", estimated error: ", err1)
print("C2 = ", C2, ", estimated error: ", err2)
输出:
C1 = -9.652617083240306, estimated error: 2.549444932020608e-09
C2 = 15.93580239041989, estimated error: 3.4140955340600243e-10
编辑:
您还可以通过参数指定精度:epsrel
:相对误差,epsabs
:绝对误差。但这有点棘手(参见 this):
我们将绝对误差目标指定为零。此条件无法满足,因此相对误差目标将决定积分何时停止。
C1, err1 = quad(f1, 0, 2*pi, epsrel=1e-10, epsabs=0)
print("C1 = ", C1, ", estimated error: ", err1)
输出:
C1 = -9.652617083240308 , estimated error: 1.4186554373311127e-13
备选方案:使用 quadpy(我的一个项目):
import quadpy
from numpy import cos, sin, exp, pi
c1, err1 = quadpy.quad(
lambda t: (1 + sin(t)) * exp(1 + cos(t)) * (-sin(t)), 0.0, 2 * pi,
)
c2, err2 = quadpy.quad(
lambda t: ((1 + cos(t)) ** 2 + exp(1 + cos(t))) * cos(t), 0.0, 2 * pi,
)
print("C1 = ", c1, ", estimated error: ", err1)
print("C2 = ", c2, ", estimated error: ", err2)
C1 = -9.652617076333142 , estimated error: 1.3725463615061705e-09
C2 = 15.9358023895608 , estimated error: 6.646678031309946e-11