如果你想从两个数据点找到目标函数中的两个参数,这对于最小二乘拟合不一定是个问题:只需求解联立方程式y1 = b(x1 a)^ p和y2 = b( x2 a)^ p参数a和b:
import numpy as np
import matplotlib.pyplot as plt
def func(x, adj1,adj2):
return ((x+adj1) ** pw) * adj2
x = [0.5,0.85] # two given datapoints to which the exponential function with power pw should fit
y = [0.02,4]
pw = 15
A = np.exp(np.log(y[0]/y[1])/pw)
a = (x[0] - x[1]*A)/(A-1)
b = y[0]/(x[0]+a)**pw
xf=np.linspace(0,1,50)
plt.figure()
plt.plot(x, y, 'ko', label="Original Data")
plt.plot(xf, func(xf, a, b), 'r-', label="Fitted Curve")
plt.show()
当然,结果函数将完全通过两个点.