You are on page 1of 11

University of Electronic Science and Technology of China

Submitted by: Armah Gabriel Kofi School of Computer Science and Engineering Course Title: Numerical Analysis

Assignment 3 : Interpolation

Submitted To: He Guoliang Date :5th June , 2012

REQUIREMENT 1
Use the given information, to come up with the best for Lagrange interpolation, piecewise linear and piecewise spline interpolation for the given data. Given the following data ( x y 1.00 0 0.9211 0.3894 0.6967 0.7174 0.3624 0.9320 -0.0292 0.9996 -0.4161 0.9093 -0.7374 0.6755 -0.9422 0.3350 -0.9983 -0.0584 -0.8968 -0.4425 -0.6536 -0.7568 -0.3073 -0.9516 0.0875 -0.9962 0.4685 -0.8835 0.7756 -0.6313 0.9602 -0.2794 Table 1 : Given Data )

The data is first divided into two data sets taking x values in ascending order. Then we apply interpolation methods for each data set and get interpolating values between them. Dividing the data set, x2 -0.9983 -0.8968 -0.6536 -0.3073 0.0875 0.4685 0.7756 0.9602 1.0000 y2 -0.0584 -0.4425 -0.7568 -0.9516 -0.9962 -0.8835 -0.6313 -0.2794 0.0000

Table 2: Data to be plotted For best results, we generated 100 data points for the upper and 100 data points for the lower part of the circle for x to which we used for the interpolation. And for each of the 2

x1 -0.9983 -0.9422 -0.7374 -0.4161 -0.0292 0.3624 0.6967 0.9211 1.0000

y1 -0.0584 0.3350 0.6755 0.9093 0.9996 0.9320 0.7174 0.3894 0.0000

interpolation methods, we generated an equivalent number of y values to be used to plot the graphs. Using these data sets, we plotted the follows graphs :

1. Piecewise Interpolation
The red colored graph in Fig 1 is drawn from the given data point, whereas the black graph is drawn from the piecewise interpolation. It can be seen, using 100 data points, the piecewise interpolation method maps into the graph of the given data points.

Fig1: Piecewise Interpolation

2. Lagrange Interpolation
3

The blue colored graph in Fig 2 is drawn from Lagrange Interpolation. It can be seen that unlike the piecewise interpolation graph, this graph is smooth.

Fig 2: Lagrange Interpolation

3. Spline Interpolation
The green colored graph in Fig 3 represents Spline Interpolation. This graph also is smooth, however, it can be observed that some sections of the graph tend to deviate quite noticeably from a general smooth circular curve.

Fig 3: Spline Interpolation

4. All Methods
Fig 4 displays all the graphs superimposed on the graph of the given data. Some portions of these graphs clearly share values hence mapping onto same points while at some points, each graph takes on distinctly different points. This can be explained interms of error in the error analysis that is discussed in the second part of this report.

Fig 4: Spline Interpolation

REQUIREMENT 2
Using the right norm to check the error between the functions and the data. In order to come up with an error analysis on the different interpolation methods that were used, we chose the one norm. This norm may not be the most suitable to perform this analysis, but we chose it due to its simplicity. To be able to do this we used the formula

The generate the values of y (see values in the excel worksheet attached) for the 100 data points that we had generated for the interpolation process using the formula We used these data points to calculate the error margin using the one norm formula *| |+

Values obtained from equation three for the three methods of interpolation are given in Table 3.

Lagrange Maxerror
0.098979899

Piecewise
0.128679899

Spline
0.105679899

Table 3: Maxerror Using One Norm From the results obtained, (See the excel worksheet attached to this report for the analysis) it can be concluded that Lagrange Interpolation approach gave the least max error. However, we may not conclude that Lagrange outperformed the other two methods hence recommended as a method of choice for interpolation since, using one norm to perform this error analysis, may not be the most suitable way to conduct the error analysis.

APPENDICES
1. Code to perform all methods together
%Main program for Interpolation by all the different methods clear all; clc; %Given Data %x = [ 1.00,0.9211,0.6967,0.3624,-0.0292,-0.4161,-0.7374,0.9422,-0.9983,-0.8968,-0.6536,0.3073,0.0875,0.4685,0.7756,0.9602,1.0] %y = [ 0,0.3894,0.7174,0.9320,0.9996,0.9093,0.6755,0.3350,0.0584,-0.4425,-0.7568,-0.9516,-0.9962,-0.8835,-0.6313,0.2794,0] %Breaking data into two sets x as x1 & x2 and y as y1 & y2 x1 = [-0.9983, -0.9422, -0.7374, -0.4161,-0.0292, 0.3624, 0.6967, 0.9211, 1.0] x2 = [-0.9983, -0.8968, -0.6536, -0.3073, 0.0875, 0.4685, 0.7756, 0.9602, 1.0]; y1 = [-0.0584, 0.3350, 0.6755, 0.9093, 0.9996, 0.9320, 0.7174, 0.3894, 0] y2 = [-0.0584,-0.4425,-0.7568,-0.9516,-0.9962,-0.8835,0.6313,-0.2794, 0]; % plot the original data set plot(x1,y1,'r','linewidth',2); hold on; plot(x2,y2,'r','linewidth',2) hold on; %interpolating data (test data) % 100 data points for upper half of circle. % 100 data points for lower half of circle. xi1 = linspace( 1,-1); xi2 = linspace(-1, 1); %Interplation methods %Lagrange Interpolation yil1 = Function_LagrangeInterpolation(x1,y1,xi1); plot(xi1,yil1 ,'b','LineWidth',1); yil2 =Function_LagrangeInterpolation(x2,y2,xi2); hold on plot(xi2,yil2 ,'b','LineWidth',1); %Piecewise Linear Interpolation

yip1 = Function_piecewiseLinear(x1,y1,xi1) hold on; plot(xi1,yip1,'k','LineWidth',1); yip2 = Function_piecewiseLinear(x2,y2,xi2) hold on; plot(xi2,yip2,'k','LineWidth',1); %Spline Interpolation yis1 = Function_splineInterpolation(xi1,x1,y1) hold on; plot(xi1,yis1,'g','LineWidth',1) yis2 = Function_splineInterpolation(xi2,x2,y2) hold on; plot(xi2,yis2,'g','LineWidth',1) x1=x1' x2=x1' y1=y1' y2=y2' xi1=xi1' xi2=xi2' yil1=yil1' yil2=yil2' yip1=yip1' yip2=yip2' yis1=yis1' yis2=yis2'

2. Code to per the necessary functions a. Function_Lagrange function yi = Function_LagrangeInterpolation(x,y,xi) % Function_LagrangeInterpolation % x,y - row-vectors of (n+1) data values (x,y) % xi - a row-vector of x-values, where interpolation is to be found (could be a single value) % yi - a row-vector of interpolated y-values n = length(x) - 1; % the degree of interpolation polynomial ni = length(xi); % the number of x-values, where interpolation is to be found

L = ones(n+1,ni); % the matrix for Lagrange interpolating polynomials L_(n,k)(x) % has (n+1) rows for each polynomial at k = 0,1,...,n % has ni column for each x-value of xi % Note: the algorithm uses the MATLAB capacities for matrix handling! % The two nested loops below are designed to compute in parallel % the values of Lagrange interpolating polynomials at each xvalue of xi ! for k = 0 : n % start the outer loop through the data values for x for kk = 0 : (k-1) % start the inner loop through the data values for x (if k = 0 this loop is not executed) L(kk+1,:) = L(kk+1,:).*(xi - x(k+1))/(x(kk+1)x(k+1)); % see the Lagrange interpolating polynomials end % end of the inner loop for kk = k+1 : n % start the inner loop through the data values (if k = n this loop is not executed) L(kk+1,:) = L(kk+1,:).*(xi - x(k+1))/(x(kk+1)-x(k+1)); end % end of the inner loop end % the end of the outer loop % Now multiply the values for Lagrange interpolating polynomials by the data values for y yi = y * L; % use matrix multiplication of row-vector y and the matrix L, the output is the row-vector b. Function_Piecewise function v =Function_piecewiseLinear(x,y,u) %Piecewise linear interpolation. % v = Function_piecewiseLinear(x,y,u) finds the piecewise linear L(x) % with L(x(j)) = y(j) and returns v(k) = L(u(k)). % First divided difference delta = diff(y)./diff(x); % Find subinterval indices k so that x(k) <= u < x(k+1) n = length(x); k = ones(size(u)); for j = 2:n-1 k(x(j) <= u) = j; end % Evaluate interpolant s = u - x(k);

10

v = y(k) + s.*delta(k);

c. Function_Spline function S = Function_splineInterpolation(xi,x,y) % Function_splineInterpolation Cubic spline interpolation with not-a-knot end conditions. % Input: % xi evaluation points for the interpolant (vector) % x interpolation nodes (vector, length n+1) % y interpolation values (vector, length n+1) % Output: % S values of the cubic spline interpolant (vector) x = x(:); y = y(:); % ensure column vectors n = length(x)-1; h = diff(x); H1 = diag(h); H2 = diag(h.^2); H3 = diag(h.^3); D = toeplitz( [1;zeros(n-2,1)], [1 -1 zeros(1,n-2)] ); % Interpolation conditions (n rows) A1 = [ H3 H2 H1 ]; rhs1 = diff(y); % Continuity of S' and S'' (n-1 rows each) A2 = [ 3*H2(1:n-1,:) 2*H1(1:n-1,:) D ]; rhs2 = zeros(n-1,1); A3 = [ 3*H1(1:n-1,:) D zeros(n-1,n) ]; rhs3 = zeros(n-1,1); % Not-a-knot end conditions (2 rows) NAK = [ [1 -1 zeros(1,3*n-2)]; [zeros(1,n-2) 1 -1 zeros(1,2*n)] ]; rhs4 = [0;0]; % Assemble and solve system coeff = [ A1;A2;A3;NAK ] \ [rhs1;rhs2;rhs3;rhs4]; coeff = reshape( coeff, [n,3] ); coeff(:,4) = y(1:n); % known constant term in cubic S = zeros(size(xi)); for k=1:n inside = (xi>=x(k)) & (xi<=x(k+1)); S(inside) = polyval( coeff(k,:), xi(inside)-x(k) ); end

11

You might also like