Concept
The least squares polynomial curve fitting, according to the given m points, does not require this curve to pass through these points precisely. It is the approximate curve y=φ (x) of the curve y=f (x).
principle
[The principle is partly summarized by individuals based on information on the Internet.] Hope to be practical for everyone]
Given the data point Pi (xi,yi), i=1,2,..., m. The approximate curve y=φ (x) is obtained.
It also minimizes the deviation of the approximate curve from the y=f (x). The deviation of the approximate curve at the Point Pi δi=φ (xi)-Y. i=1,2,..., M.
Common Curve fitting methods:
1. Minimize the sum of the absolute deviations
2. Minimize the absolute maximum deviation
3. Minimizing the squared deviations
The fitting curve is selected according to the principle of squared deviation and minimum. The two-item equation is a method of fitting the curve, which is called least squares.
Derivation process:
1. set the fit polynomial to:
2. The sum of the distances from each point to this curve, that is, the sum of squared deviations such as the following:
3. in order to obtain a value that meets the criteria. The peer right side of the AI derivative, so we got :
.......
4. simplify the equation to the left, and then you should be able to get the following equation:
.......
5. represent these equations in the form of matrices. You will be able to get the following matrix:
6. The matrix simplification of this vandermonde can be achieved by:
7. that is x*a=y, then a = (X ' *x) -1*x ' *y, we get the coefficient matrix a, at the same time, We also get the fitting curve.
Realize
Implementation Prerequisites:
- Python execution environment and editing environment;
- Matplotlib.pyplot Graphics library for high-speed drawing of 2D charts. Similar to the plot command in Matlab, and the same approach is used.
Code:
# coding=utf-8 ' Author: Jairus Chan program: Polynomial curve fitting method ' import Matplotlib.pyplot as Pltimport mathimport numpyimport randomfig = Plt.figure () ax = Fig.add_subplot (111) #阶数为9阶order =9# generates individual points on the curve 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] #ax. Plot (x,y,color= ' R ', linestyle= '-', marker= ') #,label= "(a*a-1) * (a*a-1) * (a*a-1) + 0.5 "#生成的曲线上的各个点偏移一下, and put into Xa,ya i=0xa=[]ya=[]for xx in X:yy=y[i]d=float (Random.randint (60,140))/100#ax.plot ([Xx*d] , [yy*d],color= ' m ', linestyle= ', marker= '. ') I+=1xa.append (xx*d) ya.append (yy*d) "For I in Range (0,5): Xx=float (Random.randint ( -100,100))/100yy=float ( Random.randint ( -60,60))/100xa.append (xx) ya.append (yy) "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.0for K in range (0,len (XA)):d x=1.0for L in R Ange (0,j+i):d X=dx*xa[k]tx+=dxmata1.append (TX) mata.append (matA1) #print (len (XA)) #print (mata[0][0]) mata= Numpy.array (MatA) matb=[]for I in Range (0,order+1): Ty=0.0for K in RANge (0,len (XA)):d y=1.0for L in range (0,i):d y=dy*xa[k]ty+=ya[k]*dymatb.append (Ty) Matb=numpy.array (MATB) mataa= Numpy.linalg.solve (MATA,MATB) #画出拟合后的曲线 #print (mataa) xxa= numpy.arange ( -1,1.06,0.01) yya=[]for I in range (0,len (XXA)) : Yy=0.0for j in range (0,order+1):d y=1.0for K in range (0,j):d y*=xxa[i]dy*=mataa[j]yy+=dyyya.append (yy) Ax.plot (Xxa,yya , color= ' g ', linestyle= '-', marker= ') ax.legend () plt.show ()
Execution Effect:
All the posts in this blog are original for the author (Jairus Chan).
If you need to reprint. Please indicate the source: Http://blog.csdn.net/JairusChan.
This article assumes that you have any comments and suggestions. Please contact the author (Jairuschan).
At least the principle squared polynomial curve fitting and implementation