使用lmfit拟合两个变量

问题描述 投票:0回答:1

我如何在python中使用lmfit将这个函数与两个输入变量拟合?函数f(x)= a *(x-b)** 2,a和b是变量,它们可以是随机数。

python-3.x curve-fitting nonlinear-optimization lmfit
1个回答
0
投票

您可以尝试这个吗?

import matplotlib.pyplot as plt
import numpy as np
from lmfit.models import ExpressionModel

# synthetic data
x = np.linspace(-10, 10, 100)
a,b = 0.9,1.9
y = a*(x-b)**2

# fit y using lmfit; with "guesses": a=1, b=2
gmod = ExpressionModel("a*(x-b)**2")
result = gmod.fit(y, x=x, a=1, b=2)

print(result.fit_report())

plt.plot(x, y, 'bo')
plt.plot(x, result.init_fit, 'k--', label='initial fit')
plt.plot(x, result.best_fit, 'r-', label='best fit')
plt.legend(loc='best')
plt.show()

输出:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.