如何使用Scipy curve_fit用某些参数值求解方程已知

发布于 2025-02-12 05:18:43 字数 681 浏览 1 评论 0原文

我尝试使用Scipy Curve_fit求解方程,以获取一些未知参数的估计值。我有自变量(x)和因变量(y)和一个已知的参数(e),现在我需要找到a,b,c和d的估计值。

我使用以下代码,不确定A,B,C和D是否是使用此方法的“正确”估计值。任何建议将不胜感激。

import pandas as pd
import numpy as np
from scipy.optimize import curve_fit

np.random.seed(0)

x = np.random.randint(0, 100, 100) # known independent variable
y = np.random.randint(0, 100, 100) # known dependent variable
e = np.random.randint(0, 100, 100) # know parameter 

def cubic(x, a, b, c, d, e ):
    return a * x**3 + b * x**2 + c * x + d + e


(a, b, c, d, e), _ = curve_fit(cubic, x, y)

print((a, b, c, d ))
(0.00010514483118750917, -0.00810393624233341, -0.10316706291775657, -24200081.18055175) 

I try to use scipy curve_fit to solve an equation in order to get the estimated value for some unknown parameters. I have the independent variable (x) and dependent variable(y) and one parameter (e) known, now I need to find the estimated value for a, b, c and d.

I used the following code and not quite sure if a, b, c, and d are the "correct" estimated value by using this approach. Any advice would be greatly appreciated.

import pandas as pd
import numpy as np
from scipy.optimize import curve_fit

np.random.seed(0)

x = np.random.randint(0, 100, 100) # known independent variable
y = np.random.randint(0, 100, 100) # known dependent variable
e = np.random.randint(0, 100, 100) # know parameter 

def cubic(x, a, b, c, d, e ):
    return a * x**3 + b * x**2 + c * x + d + e


(a, b, c, d, e), _ = curve_fit(cubic, x, y)

print((a, b, c, d ))
(0.00010514483118750917, -0.00810393624233341, -0.10316706291775657, -24200081.18055175) 

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2025-02-19 05:18:43

创建一个仅接受a,b,c,d的函数,并通过e的固定值传递:

(a, b, c, d), _ = curve_fit(lambda x, a, b, c, d: cubic(x, a, b, c, d, e), x, y)

您可以通过使用**来使事情变得更加明确。 Args和初始猜测:

def func(*args):
    return cubic(*args, e)
(a, b, c, d), _ = curve_fit(func, x, y, p0=np.zeros(4))

Create a function that only accepts a, b, c, d and passes in the fixed value of e:

(a, b, c, d), _ = curve_fit(lambda x, a, b, c, d: cubic(x, a, b, c, d, e), x, y)

You can make things a bit more explicit by using *args and an initial guess:

def func(*args):
    return cubic(*args, e)
(a, b, c, d), _ = curve_fit(func, x, y, p0=np.zeros(4))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文