我想用指数函数 y=x ** pw
,其中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()