You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Scipy fsolve与quad求解积分方程组遇参数缺失错误

问题与解决方案

问题背景

需要求解含未知量θ₁、θ₂、θ₃的积分方程组,其中Φ为标准正态分布的CDF,φ为标准正态分布的PDF。使用Scipy的fsolve和integrate模块编写代码后,出现TypeError: function() missing 3 required positional arguments: 'thetai', 'thetaj', and 'thetak'错误。

原代码如下:

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)))

错误原因

integrate.quad默认仅将积分变量(此处为xi)传递给被积函数function,但function还依赖thetai, thetaj, thetak三个额外参数,调用时未传递这些参数,导致参数缺失报错。

修正方案

在调用integrate.quad时,通过args参数将额外参数传递给被积函数,同时移除重复导入的模块以精简代码。

修正后的完整代码:

import numpy as np
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):
    # 通过args参数传递额外参数给被积函数
    return integrate.quad(function, -np.inf, np.inf, args=(thetai, thetaj, thetak))[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(f"求解得到θ₁={t1:.4f}, θ₂={t2:.4f}, θ₃={t3:.4f}")
print(f"方程组残差:{equations((t1, t2, t3))}")

关键说明

  • integrate.quad的args参数用于传递被积函数所需的额外参数,这些参数会在积分变量之后被传入function。
  • 移除了重复的import scipy.integrate as integrate语句,避免模块导入冗余。

内容的提问来源于stack exchange,提问作者Ishigami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 19:37:47