- Polynomial Interpolation
- Piecewise Linear Interpolation
- Cubic Spline Interpolation
Data points from the example Now, let's define a general polynomial equation. The following is the n-th order polynomial: If we extend the general polynomial, it looks like the following as, And, in matrix form, we may arrange these equations nicely as follows, For our example problem, we have 4 data points, which means that we will have four equations. The right side vector b is for the vertical (y-axis) data points. The left side nxn matrix is for the horizontal (x-axis) data points. But, there is a question. The horizontal data points are not in a squared matrix, specifically, it is now in 1x4 form; so, how could we represent the data as in a 4x4 matrix for our example? The following figure shows the current configuration: We can write a simple MATLAB program to make that 4x4 matrix. The following codes will generate a square matrix from a single row matrix. % Generation of a Polynomial Matrix x=[1 2 3 4]; % Sample x data points n = length(x); A = zeros(n,n); for i=1:n % Creating polynomial matrix for j=1:n % nth degree polynomial passing through (n-1) points A(i,j) = x(i).^(j-1); end end disp(A) After implementing the above program, we will have the following refined equations, which we can solve by Gaussian elimination to find the value of the unknown coefficients, a. Now, using the Gaussian elimination, the unknown parameters are, So, the polynomial equation that will pass through all the four points is, If we plot, the above equation fits and passes through all the points. So far, the mathematics behind polynomial interpolation has been discussed. Good news! MATLAB has a built-in polynomial interpolation feature. We can use the feature easily. You can access the built-in function from the command prompt shown as below, >> x = [1; 2; 3; 4]; >> y =[2.1; 4.6; 5.7; 9.8]; >> polyfit(x,y,3) ans = -6.2 12.667 -5.10 0.7333 'polyfit' is the built-in command in MATLAB that uses the same technique shown earlier. Piecewise Linear Interpolation: When we have more data points to fit, polynomial interpolation does generate too much oscillations in curve fitting. In this case, we may use the MATLAB built-in command 'interp1' for one dimensional piecewise linear interpolation. For our four data points example, we may write few lines in MATLAB command prompt to access this feature. >> x1 = [1;2;3;4]; >> y1 =[2.1; 4.6; 5.7; 9.8]; >> xx = 0:0.1:5; yy = interp1(x1,y1,xx); plot(x1,y1,'o',xx,yy) Cubic Spline Interpolation: An improvement to one dimensional piecewise linear interpolation is the cubic spline interpolation. We may use the MATLAB built-in command 'spline' for it. For our four data points example, we may write few lines in MATLAB command prompt to access this feature. >> x1 = [1;2;3;4]; >> y1 =[2.1; 4.6; 5.7; 9.8]; >> xx = 0:0.1:5; yy = spline(x1,y1,xx); plot(x1,y1,'o',xx,yy) #Interpolation #PiecewiseLinear #CubicSpline #Matlab #Polynomial #Blog #Blogger |