func_gen.py
876 字节
#!/usr/bin/env python
# encoding: utf-8
'''
@author: dongjie
@license: (C) Copyright 2013-2020, Node Supply Chain Manager Corporation Limited.
@contact: 503479457@qq.com
@software: garner
@file: func_gen.py.py
@time: 2021/7/8 9:12
@desc:
'''
import numpy as np
from scipy import optimize # 最小二乘法拟合
import matplotlib.pyplot as plt # python matplotlib 绘图
from mpl_toolkits.mplot3d import Axes3D # 3D 绘图
x = [10,20,30,40,50,60,70,80]
x = np.array(x)
print('x is :\n',x)
num = [174,236,305,334,349,351,342,323]
y = np.array(num)
print('y is :\n',y)
# f1 为各项的系数,3 表示想要拟合的最高次项是多少。
f1 = np.polyfit(x, y, 3)
# p1 为拟合的多项式表达式
p1 = np.poly1d(f1)
print('p1 is :\n',p1)
plt.plot(x, y, 's',label='original values')
plt.show()
yvals = p1(x) #拟合y值
plt.plot(x, yvals, 'r',label='polyfit values')