溫馨提示×

python如何擬合函數(shù)

九三
311
2021-02-25 13:49:10
欄目: 編程語言

python如何擬合函數(shù)

在python中使用curve_fit方法擬合函數(shù),具體方法如下:

mport 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) #擬合y值

print('popt:', popt)

print('系數(shù)a:', a)

print('系數(shù)b:', b)

print('系數(shù)c:', c)

print('系數(shù)pcov:', pcov)

print('系數(shù)yvals:', yvals)

0