วันเสาร์ที่ 30 พฤษภาคม พ.ศ. 2563

ฟิตเคิร์ฟโดยโพลิโนเมียล ติวเตอร์เรียล 13

Tutorial 13 – Polynomial Curve Fitting

A typical experiment collects data related to one parameter (say x) and the corresponding value of a dependent variable (say y). These observations can be stored in two vectors, namely, x and y. Using least square regression, it is possible to compute the coefficients of a polynomial function, of some selected degree, for y in terms of x. The equation for a polynomial of degree n can be expressed as: y x=a1a2 xa3 x 2 ⋯an1 x n =∑i=1 n1 ai x i−1 where ai ,i=1to n1 are unknown coefficients which can be computed by solving the following set of linear simultaneous equations: [ ∑ x 0 ∑ x ∑ x 2 ⋯ ∑ x n ∑ x ∑ x 2 ∑ x 3 ⋯ ∑ x n1 ∑ x 2 ∑ x 3 ∑ x 4 ⋯ ∑ x n2 ⋮ ⋮ ⋮ ⋱ ⋮ ∑ x n ∑ x n1 ∑ x n2 ⋯ ∑ x 2n ]{ a1 a2 a3 ⋮ an1} = { ∑ y ∑ xy ∑ x 2 y ⋮ ∑ x n y} or [ X ]{a}={b} We can solve for the unknown coefficients as follows: a=[ X ] −1 b Once the coefficients are determined, the predicted values of y for the observed values of x as follows: y x=∑i=1 n1 ai x i−1 Let us consider the sample data given below: x -1 0 1 2 3 5 7 9 y -1 3 2.5 5 4 2 5 4 Let us fit a fourth order polynomial equation to the above data, of the form y=a1a2 xa3 x 2 a4 x 3 a5 x 4 The required simultaneous equations that must be solved are as follows: [ 8 ∑ x ∑ x 2 ∑ x 3 ∑ x 4 ∑ x ∑ x 2 ∑ x 3 ∑ x 4 ∑ x 5 ∑ x 2 ∑ x 3 ∑ x 4 ∑ x 5 ∑ x 6 ∑ x 3 ∑ x 4 ∑ x 5 ∑ x 6 ∑ x 7 ∑ x 4 ∑ x 5 ∑ x 6 ∑ x 7 ∑ x 8]{ a1 a2 a3 a4 a5} = { ∑ y ∑ xy ∑ x 2 y ∑ x 3 y ∑ x 4 y} The elements of the above matrix equation are listed in the table on the next page. Tutorial 13 – Polynomial Curve Fitting | 37 ∑ x 0 ∑ x 1 ∑ x 2 ∑ x 3 ∑ x 4 ∑ x 5 ∑ x 6 ∑ x 7 ∑ x 8 8 26 170 1232 9686 79256 665510 5686952 49208966 Similarly, the right hand vector is ∑ y ∑ xy ∑ x 2 y ∑ x 3 y ∑ x 4 y 24.5 106.5 676.5 5032.5 39904.5 The simultaneous equations are therefore as follows: [ 8 26 170 1232 9686 26 170 1232 9686 79256 170 1232 9686 79256 665510 1232 9686 79256 665510 5686952 9686 79256 665510 5686952 49208966]{ a1 a2 a3 a4 a5} = { 24.5 106.5 676.5 5032.5 39902.5} Solving these equations gives a1=2.6855 , a2=2.3015 , a3=−1.2326 , a4=0.2169 and a5=−0.0118 The fourth order polynomial equation is thus y=2.68552.3015 x−1.2326 x 2 0.2169 x 3 −0.0118 x 4 In Scilab, it is not necessary to use loops to compute the elements of the coefficient matrix and right hand vector. Instead, they can be computed through the following matrix multiplication: -->xx = [x.^0 x x.^2 x.^3 x.^4]; -->X = xx' * xx; -->b = xx' * y; -->a = inv(X) * b a = 2.6855895 2.3014597 -1.2326305 0.2168915 -0.0118197 The predicted values of y based on this polynomial equation can be computed as follows: -->y(:,2) = xx * a; You can plot the graph of x against observed and predicted values with the following: -->plot(x, y) Now that we know how this calculation is to be done, let us write a function to automate this. Let the function interface be as follows: Interface: [a, yf] = polyfit(x, y, n) Input Parameters: x = column vector of observed values of the independent variable, y = column vector of observed values of dependent variable, Tutorial 13 – Polynomial Curve Fitting | 38 n = degree of the polynomial fit Output parameters: a = column vector of coefficients of the polynomial of order n that minimizes the least squares error of the fit, ai ,i=1, 2,... , n1 where ai is the coefficient of the term x i−1 . a is a column vector. yf = a column vector of calculated values of y for a polynomial of order n. The function polyfit() can be written in SciPad and then loaded into Scilab workspace: function [a, yf] = polyfit(x, y, n) xx = zeros(length(x), n+1); for i = 1:n+1 xx(:,i) = x.^(i-1); end X = xx' * xx; b = xx' * y; a = inv(X) * b; yf = xx * a; endfunction Once the function is successfully loaded into the Scilab workspace, to fit a fourth order polynomial, you can call it as follows: -->[a4, y(:,2)] = polyfit(x, y, 4); -->[a5, y(:,3)] = polyfit(x, y, 5); -->plot(x, y, x, yf); xtitle('POLYNOMIAL CURVE FITTING', 'x', 'y'); The first line calculates the coefficients a of the polynomial fit and the calculated values yf for a 4th order polynomial fit and the second line does the same for a 5th order polynomial fit. The calculated values of y for each are stored in the 2nd and 3rd columns of y. The third line plots the graph of raw data and fitted values and adds a title for the graph and labels for the x and y axes. Fig. 13.1 Polynomial curve fitting by least square method

ไม่มีความคิดเห็น:

แสดงความคิดเห็น