【发布时间】:2016-01-16 03:59:42
【问题描述】:
我想用常数 pw 拟合指数函数 y=x ** pw 以适应两个数据点。 scipy curve_fit 函数应该优化 adj1 和 adj2。我已经尝试使用下面的代码,但无法让它工作。曲线不通过数据点。我该如何解决?
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(x, adj1,adj2):
return np.round(((x+adj1) ** pw) * adj2, 2)
x = [0.5,0.85] # two given datapoints to which the exponential function with power pw should fit
y = [0.02,4]
pw=15
popt, pcov = curve_fit(func, x, y)
xf=np.linspace(0,1,50)
plt.figure()
plt.plot(x, y, 'ko', label="Original Data")
plt.plot(xf, func(xf, *popt), 'r-', label="Fitted Curve")
plt.show()
【问题讨论】:
标签: python scipy curve-fitting