Sei sulla pagina 1di 6

Saint Louis University

SCHOOL OF ENGINEERING AND ARCHITECTURE

LABORATORY MANUAL

ACTIVITY NO. 6
POLYNOMIAL INTERPOLATION

ACTIVITY OBJECTIVE:
At the end of the activity, the student should be able to write programs to determine the
interpolating polynomial for a given set of data and use polynomial interpolation to determine an
approximate polynomial for non-polynomial functions.

EQUIPMENT NEEDED:
PC with Scilab

LABORATORY ACTIVITIES:
Problem and Results:
1. Given the data set
x 0 0.5 1 1.5 2
f(x) 1.2 0.5 1.9 5.8 10.8

(a) Determine the interpolating polynomial p(x) for the data set.
p(x) = __1.2+(7/3)x – 0.1x2 + (62/15) x3 – (6/5) x4________
(b) Plot the data points and the interpolating polynomial for -0.2≤x≤2.2.

ELECTRONICS ENGINEERING DEPARTMENT ECE 427L: NUMERICAL METHODS LABORATORY


Saint Louis University
SCHOOL OF ENGINEERING AND ARCHITECTURE

LABORATORY MANUAL

(c) Estimate the value of f(x) at x=0.75, and x=1.4 using the interpolating polynomial.
f(0.75) ≈ p(0.75) f(1.4) ≈ p(1.4)
f(0.75) ≈ _________________________ f(1.4) ≈ _________________________

-x
2. Given the function f(x) = 5e sin(0.5πx)
(a) Determine a 2nd-order approximate polynomial p(x) for f(x) on the interval 0≤x≤2 using polynomial
interpolation with three equally spaced data points.
p(x) = ___________________________________________________________________________
(b) Determine a 4th-order approximate polynomial p(x) for f(x) on the interval 0≤x≤2 using polynomial
interpolation with five equally spaced data points.
p(x) = ___________________________________________________________________________

(c) Compute and tabulate the values of f(x) and p(x) as indicated in the given table.

p(x)
x f(x)
2nd-Order 4th-Order
0.2
0.8
1.2
1.8

(d) Plot the graph of f(x) and the 2nd-order p(x) for -0.5≤x≤2.5.

INSERT GRAPH HERE

Plot the graph of f(x) and the 4th-order p(x) for -0.5≤x≤2.5.

INSERT GRAPH HERE

(e) Answer the following questions based on the computed values and graphs.

Do the preceding results show that the interpolating polynomial and the non-polynomial function
coincide at the data points? ______

ELECTRONICS ENGINEERING DEPARTMENT ECE 427L: NUMERICAL METHODS LABORATORY


Saint Louis University
SCHOOL OF ENGINEERING AND ARCHITECTURE

LABORATORY MANUAL

Do the preceding results show that an interpolating polynomial with a higher degree is more accurate
in approximating a non-polynomial function than an interpolating polynomial with a lower degree?
_____

Do the preceding results show that an interpolating polynomial is a good approximation of a non-
polynomial function within the interval covered by the data points. _____

Do the preceding results show that an interpolating polynomial is a good approximation of a non-
polynomial function outside the interval covered by the data points. _____

SAMPLE PROGRAM:
-2x
Determine a 6th-order interpolating polynomial p(x) for f(x)=10xe on the interval 0≤x≤2.5.

//POLYNOMIAL INTERPOLATION
//Generate the values of x as a column vector
N=7; //no. of data points
a=0; //lower limit of the interval
b=2.5; //upper limit of the interval
dx=(b-a)/(N-1);
x=(a:dx:b)'

//Generate the values of f(x) on the data points


fx=10*x.*exp(-2*x)

//Solve for the coefficients of p(x) using Vandermonde matrix and Gauss-Jordan
elimination
AM=[x.^0, x.^1,x.^2,x.^3,x.^4, x.^5, x.^6,fx]

//Consider a pivot row starting from the first row until the the last row
[nrow,ncol]=size(AM);
for prow=1:nrow
//If the pivot element is zero, interchange the pivot row with a row below it
row=prow+1;

ELECTRONICS ENGINEERING DEPARTMENT ECE 427L: NUMERICAL METHODS LABORATORY


Saint Louis University
SCHOOL OF ENGINEERING AND ARCHITECTURE

LABORATORY MANUAL

while row<=nrow & abs(AM(prow,prow))<(10^-16)


if AM(row,prow)~=0 then
for col=prow:ncol
temp=AM(prow,col);
AM(prow,col)=AM(row,col);
AM(row,col)=temp;
end;
else
row=row+1;
end;
end;

//Normalize the pivot row


pivot=AM(prow,prow); //pivot - pivot element
for col=prow:ncol
AM(prow,col)=AM(prow,col)/pivot;
end;

//Make the elements above the pivot element zero


row=1;
while row<prow
multiplier=AM(row,prow);
for col=prow:ncol
AM(row,col)=AM(row,col)-multiplier*AM(prow,col);
end;
row=row+1;
end;
//Make the elements below the pivot element be zero
row=prow+1;
while row<=nrow
multiplier=AM(row,prow);
for col=prow:ncol
AM(row,col)=AM(row,col)-multiplier*AM(prow,col);
end;
row=row+1;
end;
end;

//Extract and display the coefficients of p(x).


p=AM(1:nrow,ncol) //coefficients of p(x)

//Plot the data points

ELECTRONICS ENGINEERING DEPARTMENT ECE 427L: NUMERICAL METHODS LABORATORY


Saint Louis University
SCHOOL OF ENGINEERING AND ARCHITECTURE

LABORATORY MANUAL

clf;plot2d(x,fx,-1); xgrid //plots each data point as a cross


//Plot p(x) as a smooth curve
xv=-0.2:0.01:2.7;
px=p(1)+p(2)*xv+p(3)*xv.^2+p(4)*xv.^3+p(5)*xv.^4+p(6)*xv.^5+p(7).*xv.^6;
plot2d(xv,px);xgrid
//Plot f(x) as a smooth curve
fxv=10*xv.*exp(-2*xv);
plot2d(xv,fxv);xgrid

//POLYNOMIAL INTERPOLATION
//Generate the values of x as a column vector
N=5; //no. of data points
a=0; //lower limit of the interval
b=2; //upper limit of the interval
dx=(b-a)/(N-1);
x=(a:dx:b)'

//Generate the values of f(x) on the data points


fx=1.2+((7/3)*x)-(0.1*x^2)+((62/15)*x^3)-((6/5)*x^4)

//Solve for the coefficients of p(x) using Vandermonde matrix and Gauss-Jordan elimination
AM=[x.^0, x.^1,x.^2,x.^3,x.^4,fx]

//Consider a pivot row starting from the first row until the the last row
[nrow,ncol]=size(AM);
for prow=1:nrow
//If the pivot element is zero, interchange the pivot row with a row below it
row=prow+1;
while row<=nrow & abs(AM(prow,prow))<(10^-16)
if AM(row,prow)~=0 then
for col=prow:ncol
temp=AM(prow,col);
AM(prow,col)=AM(row,col);
AM(row,col)=temp;
end;
else
row=row+1;
end;
end;

ELECTRONICS ENGINEERING DEPARTMENT ECE 427L: NUMERICAL METHODS LABORATORY


Saint Louis University
SCHOOL OF ENGINEERING AND ARCHITECTURE

LABORATORY MANUAL

//Normalize the pivot row


pivot=AM(prow,prow); //pivot - pivot element
for col=prow:ncol
AM(prow,col)=AM(prow,col)/pivot;
end;

//Make the elements above the pivot element zero


row=1;
while row<prow
multiplier=AM(row,prow);
for col=prow:ncol
AM(row,col)=AM(row,col)-multiplier*AM(prow,col);
end;
row=row+1;
end;
//Make the elements below the pivot element be zero
row=prow+1;
while row<=nrow
multiplier=AM(row,prow);
for col=prow:ncol
AM(row,col)=AM(row,col)-multiplier*AM(prow,col);
end;
row=row+1;
end;
end;

//Extract and display the coefficients of p(x).


p=AM(1:nrow,ncol) //coefficients of p(x)

//Plot the data points


clf;plot2d(x,fx,-1); xgrid //plots each data point as a cross
//Plot p(x) as a smooth curve
xv=-0.2:0.01:2.2;
px=p(1)+p(2)*xv+p(3)*xv.^2+p(4)*xv.^3+p(5)*xv.^4;
plot2d(xv,px);xgrid
//Plot f(x) as a smooth curve
fxv=1.2+((7/3)*xv)-(0.1*xv^2)+((62/15)*xv^3)-((6/5)*xv^4)

plot2d(xv,fxv);xgrid

ELECTRONICS ENGINEERING DEPARTMENT ECE 427L: NUMERICAL METHODS LABORATORY

Potrebbero piacerti anche