You are on page 1of 37

Interpolación

Dr. Luis Sanchez


Introduction:
The Interpolation Problem
Temperature Viscosity
An experiment is used to determine the
(degree)
viscosity of water as a function of
temperature. The following table is generated:
0 1.792

5 1.519
Problem: Estimate the viscosity when the
temperature is 8 degrees. 10 1.308

15 1.140
Introduction
Estimation of intermediate values between precise data
points. The most common method is polynomial
interpolation:
f ( x)  a0  a1 x  a2 x    an x
2 n

 Polynomial interpolation is used when the point


determined are very precise. The curve representing
the behavior has to pass through every point.
 There is one and only one nth-order polynomial that
fits n+1 points
The Interpolation Problem
Given a set of n+1 points,

x0 , f ( x0 ), x1, f ( x1), ...., xn , f ( xn )


Find an nth order polynomial f n (x)
that passes through all points, such that:

f n ( xi )  f ( xi ) for i  0,1, 2,..., n


Introduction

n=3 n=4
n=2

First order (linear) 2nd order (quadratic) 3rd order (cubic)


Introduction
There are a variety of mathematical formats in which
this polynomial can be expressed:

 The Newton polynomial.


 The Lagrange polynomial.
Newton’s Divided-Difference
Interpolating Polynomials
Linear Interpolation/
• Is the simplest form of interpolation, connecting two data
points with a straight line.

𝑓1 (𝑥) − 𝑓(𝑥0 ) 𝑓(𝑥1 ) − 𝑓(𝑥0 )


=
𝑥 − 𝑥0 𝑥1 − 𝑥0
Slope and a
finite divided
𝑓1 (𝑥) − 𝑓(𝑥0 ) 𝑓(𝑥1 ) − 𝑓(𝑥0 )
= difference
𝑥 − 𝑥0 𝑥1 − 𝑥0 approximation to
1st derivative
𝑓(𝑥1 ) − 𝑓(𝑥0 )
𝑓1 (𝑥) = 𝑓(𝑥0 ) + (𝑥 − 𝑥0 ) Linear-interpolation
𝑥1 − 𝑥0 formula

f1(x) designates that this is a first-order interpolating polynomial.


Linear Interpolation
Given any two points, x0 , f ( x0 ), x1, f ( x1)
The line that interpolates the two points is:
f ( x1 )  f ( x0 )
f1 ( x)  f ( x0 )  x  x0 
x1  x0

𝑓1 (𝑥) = 𝑏0 + 𝑏1 𝑥 − 𝑥0

Example :
Find a polynomial that interpolates (1,2) and (2,4).

42
f1 ( x)  2  x  1  2 x
2 1
Quadratic Interpolation
• Given any three points: x0 , f ( x0 ), x1, f ( x1 ), and x2 , f ( x2 )
• The polynomial that interpolates the three points is:

f 2 ( x)  b0  b1 x  x0   b2  x  x0 x  x1 
where :
b0  f ( x0 )
f ( x1 )  f ( x0 )
b1  f [ x0 , x1 ] 
x1  x0
f ( x2 )  f ( x1 ) f ( x1 )  f ( x0 )

x2  x1 x1  x0
b2  f [ x0 , x1 , x2 ] 
x2  x0
General nth Order Interpolation
Given any n+1 points: x0 , f ( x0 ), x1 , f ( x1 ), ..., xn , f ( xn )
The polynomial that interpolates all points is:

f n ( x)  b0  b1 x  x0   b2 x  x0 x  x1   ...  bn x  x0 ...x  xn 1 


b0  f ( x0 )
b1  f [ x0 , x1 ]
....
bn  f [ x0 , x1 , ... , xn ]
Divided Differences
f [ x k ]  f ( xk ) Zeroth order DD
f [ x1 ]  f [ x0 ]
f [ x0 , x1 ]  First order DD
x1  x0
f [ x1 , x2 ]  f [ x0 , x1 ]
f [ x0 , x1 , x2 ]  Second order DD
x2  x0
............
f [ x1 , x2 ,..., xk ]  f [ x0 , x1 ,..., xk 1 ]
f [ x0 , x1 ,..., xk ] 
xk  x0
Divided Difference Table
x F[ ] F[ , ] F[ , , ] F[ , , ,]
x0 F[x0] F[x0,x1] F[x0,x1,x2] F[x0,x1,x2,x3]
x1 F[x1] F[x1,x2] F[x1,x2,x3]
x2 F[x2] F[x2,x3]
x3 F[x3]

 i 1 
x  x j 
n
f n ( x)   F [ x0 , x1 ,..., xi ] 
i 0  j 0 
Divided Difference Table
x F[ ] F[ , ] F[ , , ]
xi f(xi)
0 -5 2 -4
1 -3 6
0 -5
-1 -15 1 -3
-1 -15
Entries of the divided difference table are
obtained from the data table using simple
operations.
Divided Difference Table
x F[ ] F[ , ] F[ , , ] xi f(xi)
0 -5 2 -4 0 -5
1 -3 6 1 -3
-1 -15 -1 -15

The first two column of the


table are the data columns.
Third column: First order differences.
Fourth column: Second order differences.
Divided Difference Table

x F[ ] F[ , ] F[ , , ] xi yi
0 -5 2 -4 0 -5
1 -3 6
-1 -15 1 -3
-1 -15
 3  (5)
2
1 0
f [ x1 ]  f [ x0 ]
f [ x0 , x1 ] 
x1  x0
Divided Difference Table

x F[ ] F[ , ] F[ , , ] xi yi
0 -5 2 -4 0 -5
1 -3 6
-1 -15 1 -3
-1 -15
 15  (3)
6
1 1

f [ x2 ]  f [ x1 ]
f [ x1 , x2 ] 
x2  x1
Divided Difference Table

x F[ ] F[ , ] F[ , , ] xi yi
0 -5 2 -4 0 -5
1 -3 6
-1 -15 1 -3
-1 -15

6  (2)
 4
 1  (0)
f [ x1 , x2 ]  f [ x0 , x1 ]
f [ x0 , x1 , x2 ] 
x2  x0
Divided Difference Table

x F[ ] F[ , ] F[ , , ] xi yi
0 -5 2 -4 0 -5
1 -3 6
-1 -15 1 -3
-1 -15

f 2 ( x)  5  2( x  0)  4( x  0)( x 1)

f2(x)= F[x0]+F[x0,x1] (x-x0)+F[x0,x1,x2] (x-x0)(x-x1)


Two Examples
Obtain the interpolating polynomials for the two examples:

x y x y

1 0 2 3

2 3 1 0

3 8 3 8

What do you observe?


Two Examples
x Y
x Y
1 0 3 1 2 3 3 1
2 3 5 1 0 4
3 8 3 8

P2 ( x)  0  3( x  1)  1( x  1)( x  2) P2 ( x)  3  3( x  2)  1( x  2)( x  1)
 x2 1  x2 1

Ordering the points should not affect the


interpolating polynomial!!!!!!.

f [ x0 , x1 , x2 ]  f [ x1 , x2 , x0 ]  f [ x2 , x1 , x0 ]
Example
• Find a polynomial to x f(x)
interpolate the data.
2 3

4 5

5 1

6 6

7 9
Example

x f(x) f[ , ] f[ , , ] f[ , , , ] f[ , , , , ]
2 3 1 -1.6667 1.5417 -0.6750
4 5 -4 4.5 -1.8333
5 1 5 -1
6 6 3
7 9
f 4  3  1( x  2)  1.6667( x  2)( x  4)  1.5417( x  2)( x  4)( x  5)
 0.6750( x  2)( x  4)( x  5)( x  6)
Algoritmo computacional para el polinomio
de interpolación de Newton
Matlab Code:
function yint = Newtint(x,y,xx)
n = length(x);
if length(y)~=n
error('x and y must be same length');
end
b = zeros(n,n);
% 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
Lagrange Interpolating Polynomials

Problem: xi x0 x1 …. xn
Given
yi y0 y1 …. yn

Find the polynomial f n (x) such that:


f n ( xi )  f ( xi ) for i  0,1,..., n
n
f n ( x)   f xi   i ( x)
i 0
Lagrange Interpolation Formula:
x  x 
n
 i ( x)   x  x 
j 0, j i
j

i j
Lagrange Interpolating Polynomials

• The general form for n+1 data points is:

n
f n ( x)   Li ( x) f ( xi )
i 0
n x  xj
Li ( x)  
j 0 xi  x j
j i

designates the “product of”


Lagrange Interpolating Polynomials

• Linear version (n = 1):


Used for 2 points of data: (xo,f(xo)) and (x1,f(x1)),

x  x1 x  x0
f1 ( x)  f ( x0 )  f ( x1 )
x0  x1 x1  x0

Lo (x) L1 ( x)
Lagrange Interpolating Polynomials
• Second order version (n = 2):

f 2 ( x) 
 x  x1 x  x2 
f ( x0 ) Lo ( x) , j  0
x0  x1 x0  x 2 

 x  x0 x  x2 
f ( x1 ) L1 ( x) , j  1
x1  x0 x1  x 2 

 x  x0 x  x1 
f ( x2 ) L2 ( x) , j  2
x2  x0 x2  x1 
Lagrange Interpolating Polynomials -
Example

Use a Lagrange interpolating polynomial of the first


and second order to evaluate ln(2) on the basis of the
data:

x0  1 f ( x0)  ln(1)  0
x1  4 f ( x1)  ln(4)  1.386294
x2  6 f ( x2)  ln(6)  1.791760
Lagrange Interpolating Polynomials –
Example (cont’d)
• First order polynomial:

x  x1 x  x0
f 1( x)  f ( x 0)  f ( x1)
x0  x1 x1  x0

24 2 1
f 1(2)  0  1.386294  0.4620981
1 4 4 1
Lagrange Interpolating Polynomials –
Example (cont’d)

• Second order polynomial:

x  x1 x  x2 x  4 x  6
Lo ( x )    
xo  x1 xo  x2 0  4 0  6
x  xo x  x2 x  0 x  6
L1 ( x )    
x1  xo x1  x2 4  0 4  6
x  xo x  x1 x  0 x  4
L2 ( x )    
x2  xo x2  x1 6  0 6  4
Lagrange Interpolating Polynomials –
Example (cont’d)
n n
x  xj
f n ( x )   Li ( x ) f ( xi ) Li ( x )   ( j  i)
i 0 j 0 xi  x j

(2  4)(2  6)
f 2 (2)  0
(1  4)(1  6)
(2  1)(2  6)
 1.386294
(4  1)(4  6)
(2  1)(2  4)
 1.791760  0.5658444
(6  1)(6  4)
Lagrange Interpolating Polynomials –
Example (cont’d)
Pseudocode – Lagrange interpolation
Matlab Code:
function yint = Lagrange(x,y,xx)
n = length(x);
if length(y) ~= n
error('x and y must be same length');
end
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;
Summary

Next Class

You might also like