【问题标题】:How to use numerical methods to approximate the solution of an integral如何使用数值方法逼近积分的解
【发布时间】:2019-09-10 07:38:23
【问题描述】:

我有以下积分(更多详情: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(),但没有得到任何结果。

谢谢。

【问题讨论】:

    标签: python numerical-methods numerical-integration


    【解决方案1】:

    你可以使用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
    

    【讨论】:

      【解决方案2】:

      替代方案:使用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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多