Lec 1-2 Interpolation
Lec 1-2 Interpolation
Engineering
➢ Polynomial Interpolation,
4
Interpolation
6
The function f(x) is known at a finite set of discrete values of x.
However, a problem arises when the value of the function is needed at
any value of x between the discrete values in the table.
The actual function is not known and cannot be determined from the
tabular values.
1. Polynomials
2. Trigonometric functions
3. Exponential functions
7
Approximating functions should have the following properties:
1. The approximating function should be easy to determine.
2. It should be easy to evaluate, differentiate, and integrate.
An exact fit yields a polynomial that passes exactly through all of the
discrete points. This type of fit is useful for small sets of smooth data.
12
Interpolation
13
14
Interpolation
Example: Find the polynomial that interpolates
15
Interpolation
16
The main advantage of direct fit polynomials is that the explicit form of the
approximating function is obtained, and interpolation at several values of x
can be accomplished simply by evaluating the polynomial at each value of x.
The work required to obtain the polynomial does not have to be redone for
each value of x.
The main disadvantage of direct fit polynomials is that each time the degree
of the polynomial is changed, all of the work required to fit the new
polynomial must be redone. The results obtained from fitting other degree
polynomials are of no help in fitting the next polynomial. One approach for
deciding when polynomial interpolation is accurate enough is to interpolate
with successively higher-degree polynomials until the change in the result is
within an acceptable range. This procedure is quite laborious using direct fit
polynomials.
For a high-degree polynomial (n greater than about 4), the
system of equations can be ill-conditioned, which causes large errors in the
values of the coefficients. 17
18
19
20
Interpolation
function yint = Lagrange(x,y,xx)
% Lagrange: Lagrange interpolating polynomial
% yint = Lagrange(x,y,xx): Uses an (n - 1)-order
% Lagrange interpolating polynomial based on n data points
% to determine a value of the dependent variable (yint) at
% a given value of the independent variable, xx.
% input:
% x = independent variable
% y = dependent variable
% xx = value of independent variable at which the
% interpolation is calculated
% output:
% yint = interpolated value of dependent variable
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
21
Interpolation
s = 0;
for i = 1:n
product = y(i);
for j = 1:n
if i ~= j
product = product*(xx-x(j))/(x(i)-x(j));
end
end
s = s+product;
end
yint = s;
23
Neville’s Algorithm
Weighted average
on a straight line
24
25
The advantage of Neville’s algorithm over a Lagrange interpolating
polynomial, if the data are arranged in order of closeness to the
interpolated point, is that none of the work performed to obtain a specific
degree result must be redone to evaluate the next higher degree result.
26
Let’s interpolate for f(3.44) using Neville's linear, quadratic, and cubic
interpolation algorithm. Rearranging the data in order of closeness to the
point x = 3.44 yields the following data:
27
28
Interpolation
29
30
Interpolation
31
Interpolation
This entire process can be repeated again and again, and each
time higher interpolating polynomials
32
Interpolation
33
Interpolation
34
35
Interpolation
36
Interpolation
function yint = Newtint(x,y,xx)
% Newtint: Newton interpolating polynomial
% yint = Newtint(x,y,xx): Uses an (n - 1)-order Newton
% interpolating polynomial based on n data points (x, y)
% to determine a value of the dependent variable (yint)
% at a given value of the independent variable, xx.
% input:
% x = independent variable
% y = dependent variable
% xx = value of independent variable at which
% interpolation is calculated
% output:
% yint = interpolated value of dependent variable
% compute the finite divided differences in the form of a
% difference table
n = length(x);
if length(y)~=n, error('x and y must be same length');
end
37
b = zeros(n,n);
Interpolation
% assign dependent variables to the first column of b.
b(:,1) = y(:); % the (:) ensures that y is a column
vector.
for j = 2:n
for i = 1:n-j+1
b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end
% use the finite divided differences to interpolate
xt = 1;
yint = b(1,1);
for j = 1:n-1
xt = xt*(xx-x(j));
yint = yint+b(1,j+1)*xt;
end
39