The principle and implementation of polynomial curve fitting with least squares method

Source: Internet
Author: User


Concept

The least squares polynomial curve fitting, according to the given m points, does not require this curve to pass through these points precisely, but rather the approximate curve y=φ (x) of the curve y=f (x).

principle [principle part by individual according to the information on the Internet to summarize, hope to everyone can be practical]

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). Approximate curve at point Pi deviation δ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 the sum of squared deviations, and the method of fitting the curve by the two-item equation is called the 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 the qualifying a value, the peer to the 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. by representing these equations in the form of matrices, the following matrices can be obtained:

     

6. The matrix simplification of this vandermonde can be achieved by:

     

7. in other words 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:
    1. Python execution environment and editing environment;
    2. The Matplotlib.pyplot graphics library, which can be used for high-speed plotting of 2D charts, is similar to the plot command in MATLAB, and uses the same method.
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 want to reprint, please indicate the source: Http://blog.csdn.net/JairusChan.

If you have any comments or suggestions for this article, please contact the author (Jairuschan).


Least square polynomial polynomial curve fitting principle and implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.