标签:a* none 偏差 效果 网上 互联网 numpy space comm
概念
最小二乘法多项式曲线拟合,根据给定的m个点,并不要求这条曲线精确地经过这些点,而是曲线y=f(x)的近似曲线y= φ(x)。
原理
[原理部分由个人根据互联网上的资料进行总结,希望对大家能有用]
给定数据点pi(xi,yi),其中i=1,2,…,m。求近似曲线y= φ(x)。并且使得近似曲线与y=f(x)的偏差最小。近似曲线在点pi处的偏差δi= φ(xi)-y,i=1,2,...,m。
常见的曲线拟合方法:
1.使偏差绝对值之和最小

2.使偏差绝对值最大的最小

3.使偏差平方和最小

按偏差平方和最小的原则选取拟合曲线,并且采取二项式方程为拟合曲线的方法,称为最小二乘法。
推导过程:
1. 设拟合多项式为:

2. 各点到这条曲线的距离之和,即偏差平方和如下:



.......

4. 将等式左边进行一下化简,然后应该可以得到下面的等式:


.......

5. 把这些等式表示成矩阵的形式,就可以得到下面的矩阵:

6. 将这个范德蒙得矩阵化简后可得到:

7. 也就是说
运行前提:
- Python运行环境与编辑环境;
- Matplotlib.pyplot图形库,可用于快速绘制2D图表,与matlab中的plot命令类似,而且用法也基本相同。
代码:
-
- ‘‘
- import matplotlib.pyplot as plt
- import math
- import numpy
- import random
-
- fig = plt.figure()
- ax = fig.add_subplot(111)
-
- order=9
-
- x = numpy.arange(-1,1,0.02)
- y = [((a*a-1)*(a*a-1)*(a*a-1)+0.5)*numpy.sin(a*2) for a in x]
-
- i=0
- xa=[]
- ya=[]
- for xx in x:
- yy=y[i]
- d=float(random.randint(60,140))/100
-
- i+=1
- xa.append(xx*d)
- ya.append(yy*d)
-
- ‘‘
-
- ax.plot(xa,ya,color=‘m‘,linestyle=‘‘,marker=‘.‘)
-
-
- matA=[]
- for i in range(0,order+1):
- matA1=[]
- for j in range(0,order+1):
- tx=0.0
- for k in range(0,len(xa)):
- dx=1.0
- for l in range(0,j+i):
- dx=dx*xa[k]
- tx+=dx
- matA1.append(tx)
- matA.append(matA1)
-
- matA=numpy.array(matA)
-
- matB=[]
- for i in range(0,order+1):
- ty=0.0
- for k in range(0,len(xa)):
- dy=1.0
- for l in range(0,i):
- dy=dy*xa[k]
- ty+=ya[k]*dy
- matB.append(ty)
-
- matB=numpy.array(matB)
-
- matAA=numpy.linalg.solve(matA,matB)
-
- xxa= numpy.arange(-1,1.06,0.01)
- yya=[]
- for i in range(0,len(xxa)):
- yy=0.0
- for j in range(0,order+1):
- dy=1.0
- for k in range(0,j):
- dy*=xxa[i]
- dy*=matAA[j]
- yy+=dy
- yya.append(yy)
- ax.plot(xxa,yya,color=‘g‘,linestyle=‘-‘,marker=‘‘)
-
- ax.legend()
- plt.show()
运行效果:

最小二乘法多项式曲线拟合原理与实现 zz
标签:a* none 偏差 效果 网上 互联网 numpy space comm
原文地址:http://www.cnblogs.com/zhoug2020/p/7628668.html