concept

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

 

The principle of

[The principle part is summarized by individuals according to the information on the Internet, I hope it can be useful to you]

 

 

Given data point PI (xi,yi), where I =1,2… , m. Find the approximate curve y= φ(x). And minimize the deviation of the approximate curve from y=f(x). Deviation of approximate curve at point PI δ I = φ(xi)-y, I =1,2… , m.

Common curve fitting methods:

1. Minimize the sum of absolute values of deviations

     \

 

2. Minimize the maximum deviation absolute value

\

 

3. Minimize the sum of squares of deviation

 

     

 

The method of selecting fitting curve according to the principle of minimum sum of squares of deviation and adopting binomial equation as fitting curve is called the least square method.

Derivation process:

1. Suppose the fitting polynomial is:

          \

2. The sum of the distances from each point to the curve, i.e. the sum of the squares of the deviations, is as follows:

          \

3. In order to obtain the value of A conforming to the conditions, take the partial derivative of ai on the right-hand side of the equation, so we get:

          

          

.

          

 

4. Simplify the left-hand side of this equation and you should get something like this:

          \

          

.

          

 

5. By expressing these equations in matrix form, the following matrix is obtained:

          \

6. After simplifying the Van der Monde matrix, we can get:

          \

If X*A=Y, then A= (X’*X)-1*X’*Y, then we get the coefficient matrix A, and we also get the fitting curve.

implementation

 

Operation premise:

  1. Python runtime environment and editing environment;
  2. The matplotlib. pyplot graph library is used to quickly draw 2D charts. It is similar to the plot command in MATLAB, and the usage is basically the same.

Code:

1 # coding= UTF-8 2 3 ''''' 4 programs: Polynomial curve fitting algorithm 5 "" 6 import matplotlib.pyplot as PLT 7 import math 8 import numpy 9 import Random 10 FIG 11 = PLT. Figure 12 ax = () FIG. Add_subplot (111) 13 14 15 order = 9 # order number is order 9 # 16 17 18 x = generate each point on the curve Numpy. Arange,1,0.02 (- 1) 19 y = [((a * a - 1) * (a * a - 1) * (a * a - 1) + 0.5) * numpy in sin (a * 2) for a x] in 20 # ax. The plot (x, y, color = 'r', graphics.linestyle = '-', marker = ' ') # 21, label = "(a * a - 1) * (a * a - 1) * (a * a - 1) + 0.5" 22 23 # of each point on the curve shifts, 25 I =0 25 ya=[] 26 ya=[] 27 for xx in x: 28 yy = y [I] 29 d = float (random. Randint (60140)) / 100 30 # ax. The plot (xx * [d], [d] yy *, color = 'm', graphics.linestyle = ', marker = '. ') 31 I + = 1 32 xa.append(xx*d) 33 ya.append(yy*d) 34 35 '''''for I in range(0,5): 36 xx=float(random.randint(-100,100))/100 37 YY =float(random.randint(-60,60))/100 38 xa.append(xx) 39 ya.append(yy) "" Plot (xa,ya,color='m',linestyle='',marker='.') 33 33 matA=[] 34 for I in range(0,order+1): 50 for k in range(0,len(xa)): 50 for k in range(0,len(xa)): 50 for k in range(0,len(xa)): 50 for l in range(0,j+ I): 53 dx=dx*xa[k] 54 tx+=dx 55 matA1.append(tx) 56 matA.append(matA1) 57 58 #print(len(xa)) 59 #print(matA[0][0]) 60 MatA =numpy. Array (matA) 62 matB=[] 63 for I in range(0,order+1): 64 ty=0.0 65 for k in range(0,len(xa)): Dy =1.0 for l in range(0, I): 68 dy=dy*xa[k] 69 ty+=ya[k]*dy 70 matB.append(ty) 71 72 matB=numpy.array(matB) 73 74 matAA=numpy.linalg.solve(matA,matB) 77 #print(matAA) 78 xxa= numpy. Arange (-1,1.06,0.01) 77 yya=[] 80 for I in range(0,len(xxa)): For j in range(0,order+1): 83 dy=1.0 84 for k in range(0,j): 85 dy*=xxa[i] 86 dy*=matAA[j] 87 yy+=dy 88 yya.append(yy) 89 ax.plot(xxa,yya,color='g',linestyle='-',marker='') 90 91 ax.legend() 92 plt.show()Copy the code

Operation effect:

 

 

 

This article is from **blog.csdn.net/JairusChan**