Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
1
Polynomial Interpolation
jm
ba
sil
la
Recall that the general interpolation problem is as follows:
Suppose that X is a vector space of functions real valued functions on some domain D, generated by the set {φ1 , φ2 , . . . , φn } over R
and we are given some data points (xi , yi ), i = 1, 2, . . . , n. Determine
g = r1 φ1 + r2 φ2 + · · · + rn φn such thatg(xi ) = yi , for i = 1, 2, . . . , n.
Sometimes, a complicated function is given in place of the data
(xi , yi ), i = 0, 1, . . . , n. For example, suppose that f (x) is a compliRb
cated function and we want to compute a f (x) dx. As we have
seen in nonformal calculus(i.e. Math 64/Math 54), the integrand can
make the problem not solvable analytically; hence the need for numerical methods. A common approach for numerical methods is to
replace f (x) by a simpler function g(x), where f (x) and g(x) agrees
on values on nodes. For instance, the trapezoidal rule for computing
the area of the region under the curve f (x), above the y-axis and
between the lines x = a and x = b replaces the function f (x) by a
piecewise-defined function where the pieces are linear functions.
In this lecture notes, we restrict the space X in the general interpolation problem to the collection of polynomials of degree at most
n. Hence, the problem may be stated as follows:
Given (xi , yi ), i = 0, 1, 2, . . . , n determine a polynomial p(x) of
degree at most n such that p(xi ) = yi for i = 0, 1, 2, . . . , n.
1
Monomial Basis
We note that the collection of polynomials of degree at most n with
real coefficient forms a vector space of dimension n + 1 over R.
Hence a basis for the vector space of polynomial of degree at most
n contains n + 1 elements. An obvious basis for this vector space
is {1, x, x2 , . . . , xn }, called monomial basis. The basis matrix for the
jbasilla\20130207PolynomialInterpolation\February 15, 2013
Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
2
monomial basis is given by
xn1
xn2
···
···
. . . ...
n
· · · xn
sil
la
1 x1 x21
1 x x2
2
A = .. .. ..2
. . .
1 xn x2n
jm
ba
This matrix is called the Vandermonde matrix and is known to be
nonsingular if the x0i s are distinct. This is done by computing the determinant using a clever Gauss-Jordan Elimination technique, transforming the matrix into an upper/lower triangular matrix. It will be
clear then that the product of the diagonal of the resulting triangular matrix is nonzero if the xi ’s are distinct.
Hence, the linear system Ax = y, where y = [y0 , y1 , y2 , . . . , yn ]0 has
a unique solution. Below is the scilab code for implementing the
algorithm.
function A=MonomialBasisMatrix(x);
n= length(x);
A = zeros(n,n);
A(:,1) =ones(n,1);
for i=1:n-1
A(:,i+1)=A(:,i).*x;
end
endfunction
function yy=MonomialPolynomialInterp(x,y,t)
A=MonomialBasisMatrix(x);
p=A\y;
n=length(p);
m=length(t);
B=zeros(m,n);
B(:,1)=ones(m,1);
for i=1:n-1
B(:,i+1)=B(:,i).*t;
end
jbasilla\20130207PolynomialInterpolation\February 15, 2013
Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
3
yy=B*p;
endfunction
jm
ba
-->x=linspace(-1,1,6)'
x =
- 1.
- 0.6
- 0.2
0.2
0.6
1.
sil
la
Since, scilab starts indexing a vector or a matrix at 1 and not
0, there has been a shift in index in implementing the algorithm in
scilab. We will be using this often and at most care should be exercise.
1
Let us consider the function f (x) =
on [−1, 1]. Consider
1 + 25x2
the nodes
Our goal is to find a quintic(degree=5) polynomial g(x) which agrees
with f (x) at the nodes x = [−1, −0.6, −0.2, 0.2, 0.6, 1]. That is, the values
of g(x) on x are
-->
y=1 ./ (1+25*x.^2)
y =
0.0384615
0.1
0.5
0.5
0.1
0.0384615
respectively. We want to find a quintic polyomial y = g(x) whose
graph passes through the points (−1., 0.0384615),(−0.6, 0.1), (−0.2, 0.5),
(0.2, 0.5), (0.6, 0.1) and (1., 0.0384615).
jbasilla\20130207PolynomialInterpolation\February 15, 2013
Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
4
The following scilab code computes the polynomial g(x):
jm
ba
sil
la
t=(-1:0.01:1)'; // finer node for graphing the functions
yy=1 ./ (1+25*t.^2); // compute the value of f(x) at t for
graphing purposes
y1=MonomialPolynomialInterp(x,y,t); //compute the value of
g(x) at t for graphing purposes
plot2d(x,y,-5) //plots the 6 data points given in the problem
plot2d(t,[yy,y1]) //plots f(x) and g(x)
The graph above shows y = f (x)(black) and y = g(x)(blue) and the
agreement of f (x) and g(x) at the data points.
2
Lagrange Polynomials
As most linear algebra text or course would teach us, problem involving vector spaces can be made simpler by using the appropriate basis. This is the unifying justificiation for learning Gauss-Jordan
Elimination, Change of basis theorems, Diagonalization of matrices
and Gram-schmidth orthogonalization/orthonormalization procedures.
Accompanying a Linear algebra problem is a matrix and choosing
the appropriate basis makes this matrix ”simpler”. The same holds
true for our basis matrix [φj (xi )]. Perhaps there is a basis {φ0 , φ1 , φ2 , . . . , φn }
jbasilla\20130207PolynomialInterpolation\February 15, 2013
Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
5
sil
la
for the space of polynomials of degree at most n with real coefficients which makes [φj (xi )] simpler, say [φj (xi )] becomes the identity
matrix. By a clever application of Factor Theorem, we see that there
are indeed polynomials φj (x) such that φj (xi ) = δij , the kronecker
delta δij = 1 when i = j and δij = 0 when i 6= j. These are called the
Lagrange Polynomials and is given by
n
Y
x − xi
φj (x) =
.
x
−
x
j
i
i=0
i6=j
jm
ba
If the basis matrix is the identity, then the solution of Ax = y is
clearly x = y. Thus, we obtain the Lagrange Interpolating polynomial
n
n
n
X
X
Y
x − xi
L(x) =
yj φj (x) =
yj
.
x
−
x
j
i
i=1
j=0
j=1
i6=j
An scilab code for computing the lagrange polynomial is as follows
function y= Lagrange(j,x,t)
//evaluates the jth Lagrange polynomial
at the vector t
y=ones(t);
for i=1:j-1
y=y.*(t-x(i));
y=y ./(x(j)-x(i));
end
for i=j+1:n
y=y.*(t-x(i));
y=y ./(x(j)-x(i));
end
endfunction
function yy=LagrangeInterpolation(x,y,t)
//evaluates the Lagrange Interpolating function at t
jbasilla\20130207PolynomialInterpolation\February 15, 2013
Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
6
3
Newton Interpolation
sil
la
yy=zeros(t);
n=length(x);
for j=1:n
yy =yy + y(j)*Lagrange(j,x,t);
end
endfunction
Newton proposed using a basis {φ1 , φ2 , . . . , φn } which makes the basis
matrix [φj (xi )] lower triangular. He proposed using the basis
jm
ba
φ0 (x) = 1,
φ1 (x) = (x − x0 ),
φ2 (x) = (x − x1 )(x − x1 ),
.. ..
.=.
φn (x) = (x − x1 )(x − x2 ) · · · (x − xn−1 ).
The interpolating polynomial is now given by
g(x) = a0 +a1 (x−x0 )+a2 (x−x0 )(x−x1 )+· · ·+an (x−x1 )(x−x2 ) · · · (x−xn−1 ).
It follows that
a0 = y0 = f (x0 )
y1 − y0
f (x1 ) − f (x0 )
a1 =
=
x1 − x0
x1 − x0
..
.
It would be convenient to introduce a notation. For a given function
jbasilla\20130207PolynomialInterpolation\February 15, 2013
Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
7
f (x) and an increasing sequence (x0 , x1 , x2 , . . . , xn ), we define
f [x0 , x1 , . . . , xn ] =
sil
la
f [x0 ] = f (x0 )
f [x1 ] − f [x0 ]
f [x0 , x1 ] =
x1 − x0
f [x1 , x2 ] − f [x0 , x1 ]
f [x0 , x1 , x2 ] =
x2 − x0
..
.
f [x1 , x2 , . . . , xn ] − f [x0 , x1 , . . . , xn−1 ]
.
xn − x0
It can be shown that aj in the Newton’s interpolating polynomial is
given by aj = f [x0 , x1 , . . . , xj ]. Scilab can compute the interpolating
polynomial as follows
jm
ba
function B=NewtonBasis(t)
//
evaluate the basis matrix for the newton divided difference formula
n=length(t);
B=zeros(n,n);
B(:,1)=ones(n,1);
for j=2:n
for i=j:n
B(i,j)=B(i,j-1)*(t(i)-t(j-1))
end
end
endfunction
function yy =NewtonInterpolation(t,y,tt)
B=NewtonBasis(t);
p = B\y;
n=length(t);
yy=p(1)*ones(tt);
n1=length(tt);
currentterm = ones(n1,1)
for i=2:n
jbasilla\20130207PolynomialInterpolation\February 15, 2013
Math 171 - Numerical Analysis
TF
Instructor: J.M.Basilla
Lecture 1
12 February 2013
8
currentterm =currentterm.*(tt-t(i-1));
yy = yy + p(i)*currentterm;
jm
ba
sil
la
end
endfunction
jbasilla\20130207PolynomialInterpolation\February 15, 2013
© Copyright 2026 Paperzz