1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit
def func(x, a, b,c): return a*np.sqrt(x)*(b*np.square(x)+c)
x = [20,30,40,50,60,70] x = np.array(x) num = [453,482,503,508,498,479] y = np.array(num)
popt, pcov = curve_fit(func, x, y)
print(popt) a = popt[0] b = popt[1] c = popt[2] yvals = func(x,a,b,c) print('popt:', popt) print('系数a:', a) print('系数b:', b) print('系数c:', c) print('系数pcov:', pcov) print('系数yvals:', yvals)
plot1 = plt.plot(x, y, 's',label='original values') plot2 = plt.plot(x, yvals, 'r',label='polyfit values') plt.xlabel('x') plt.ylabel('y') plt.legend(loc=4) plt.title('curve_fit') plt.show()
|