智能仪器 第3版 吉林大学 程德福、林君 机械工业出版社 第124页 表4-1
对表格中数据用曲线拟合法,连续函数直线拟合,进行非线性校正。并指出最大误差点。
from functools import reduce import numpy import matplotlib.pyplot as plt x = [0, 0.4, 0.8, 1.2, 1.61, 2.02, 2.44, 2.85, 3.27, 3.68, 4.1, 4.51, 4.92, 5.33, 5.73, 6.14, 6.54, 6.94, 7.34, 7.74, 8.14, 8.54, 8.94, 9.34, 9.75, 10.15, 10.56, 10.97, 11.38, 11.8, 12.21, 12.62, 13.04, 13.46, 13.87, 14.29, 14.71, 15.13, 15.55, 15.97, 16.4, 16.82, 17.24, 17.67, 18.09, 18.51, 18.94, 19.36, 19.79, 20.21] y = range(0,500,10) A = sum(x) #x的和 B = reduce(lambda x, y: x + y, (map(lambda x: x ** 2, x))) #x的平方和 C = sum(y) #y的和 D = sum([a*b for a,b in zip(x,y)]) #x*y的和 E = numpy.array([[50,A],[A,B]]) F = numpy.array([[C],[D]]) [a0,a1] = numpy.linalg.solve(E,F) #linalg线性代数包,solve求矩阵函数E*[a0,a1]=Y Y = a0+a1*x error = Y-y v=max(error) #最大误差 print(a0,a1) print(v) #作图 plt.title('0-490°C的镍铬-镍铬热电偶分度图') # 折线图标题 plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示汉字 plt.xlabel('温度/°C') # x轴标题 plt.ylabel('电动势/mV') # y轴标题 plt.plot(x, y) # 绘制折线图 plt.plot(x, Y) plt.legend(['真实值', '预测值']) # 设置折线名称 plt.show() # 显示折线图分度图如下: