如何在Python中定义一个函数,该函数包含一个确定的积分,其自由参数在两个固定值之间运行?

发布于 2025-01-20 05:57:55 字数 644 浏览 4 评论 0原文

我想定义一个具有三个参数的Python函数:C,PSI和M,但实际上它具有基于另一个称为ETA的变量的积分。

def func(x: np.float64, C: np.float64, psi: np.float64, m: np.float64) -> np.float64:
    return C * (1-psi) * quad(np.power(1-eta, psi) * np.power(m/x, C/eta+1)/eta, 0, np.inf, args=(C, psi, m, x))[0]

由于我想将此功能拟合到数据集,因此我不能选择ETA作为拟合的免费参数,因此它不应在功能签名中作为参数出现。但是,Python不知道内部ETA是定义的变量。

File "c:\Users\username\Desktop\Project\my_script.py", line 277, in func
    np.power(1 - eta, psi) * np.power(m / x, C/eta + 1) / eta, 0, np.inf,
NameError: name 'eta' is not defined

我如何让函数知道它不会期望用参数馈送,但是内部积分可以通过集成来照顾它。

- -谢谢,

I would like to define a python function that has three parameters: C, psi, and m but it actually internally has an integral that is running based on another variable called eta.

def func(x: np.float64, C: np.float64, psi: np.float64, m: np.float64) -> np.float64:
    return C * (1-psi) * quad(np.power(1-eta, psi) * np.power(m/x, C/eta+1)/eta, 0, np.inf, args=(C, psi, m, x))[0]

Since I would like to fit this function to a dataset, I cannot choose eta to be a free parameter for fitting and hence it should not appear as a parameter in the function signature. But, python doesn't know the internal eta as a defined variable.

File "c:\Users\username\Desktop\Project\my_script.py", line 277, in func
    np.power(1 - eta, psi) * np.power(m / x, C/eta + 1) / eta, 0, np.inf,
NameError: name 'eta' is not defined

How can I let the function know that it is not expecting to be fed with a parameter but the inside integral takes care of that through integration.

---Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文