代码之家  ›  专栏  ›  技术社区  ›  Ishigami

在python scipy中使用fsolve和quad求解积分方程组

  •  0
  • Ishigami  · 技术社区  · 11 月前

    我试图求解以下具有未知数theta1、theta2、theta3的积分方程组: enter image description here

    其中Phi和Phi分别是使用scipy的fsolve和积分得到的标准正态分布的cdf和pdf。这是我的代码:

    import numpy as np
    import math
    import scipy.integrate as integrate
    from scipy import integrate
    from scipy.stats import norm
    from scipy.optimize import fsolve
    
    
    def function(xi, thetai, thetaj, thetak):
      return (1 - norm.cdf(xi - thetaj)) * (1 - norm.cdf(xi - thetak)) * norm.pdf(xi - thetai)
    
    def pi_i(thetai, thetaj, thetak):
      return integrate.quad(function, -np.inf, np.inf)[0]
    
    def equations(p):
        t1, t2, t3 = p
        return (pi_i(t1,t2,t3) - 0.5, pi_i(t2,t1,t3) - 0.3, pi_i(t3,t1,t2) - 0.2)
    
    t1, t2, t3 =  fsolve(equations, (1, 1, 1))
    
    print(equations((t1, t2, t3)))
    

    但是,当我运行代码时,会弹出以下错误:

    TypeError: function() missing 3 required positional arguments: 'thetai', 'thetaj', and 'thetak'
    
    1 回复  |  直到 11 月前
        1
  •  2
  •   vht981230    11 月前

    这是因为你需要包含函数的参数 function 通话时 integrate.quad 使用 args param

    def pi_i(thetai, thetaj, thetak):
      return integrate.quad(function, -np.inf, np.inf, args=(thetai, thetaj, thetak))[0]
    

    注意:您还需要通过以下方式修复该问题 theta 函数中的变量 功能 。它没有在函数参数中提供,也不是全局变量,所以我不确定它来自哪里