Está en la página 1de 33

INTERPOLATION

Bicubic interpolant to Data for the Lorry Student Center

5004

Elevation (ft)

5002 5000 4998

300 200 100 y (ft) 0 0 50 100 x (ft) 150 200

250

ONE DIMENSIONAL INTERPOLATION


General Concepts:
Given set of n+1 points (xk yk) with x0 < x1 < < xn, find a function f(x) whose graph interpolates the data points, i.e., f(xk) = yk, for k = 0, 1, , n.
6 5

0 0 2 4 6 8 10 12 14

Interpolation CIVE 3076 2

INTERPOLATION BY POLYNOMIALS
Assume now that the interpolating function is an algebraic polynomial pn(x) of degree at most n, where n = number of points of interpolation 1.

It is well known that the interpolating polynomial pn always exists and is unique
To determine the polynomial interpolation we use Newton's form. We shall describe briefly the Newton's method.

pn(x) = a0 + a1 (x x0) + a2 (x x0) (x x1) + + an(x x0)(x x1) (x xn1)

CIVE 3076

Interpolation 3

DIVIDED DIFFERENCES
Coefficients a0, a1, , an are called the divided differences and they can be computed recursively The k-th order divided difference based on points x0, xk, denoted by f[x0, , xk], s defined recursively as f[xm] = ym if k = 0 f[x0, , xk] = (f[x1, , xk] f[x0, , xk-1])/(xk x0) if k > 0 Coefficients {ak} and the divided differences are related in the following way ak = f[x0, , xk]
CIVE 3076 Interpolation 4

DIVIDED DIFFERENCE TABLE


xi f[xi ]=yi f[xi xi +1] f[xi xi +1,xi+2]
x
0

f[ , , , ]

f[xi xi +1,,xi+n,]

y0 f[x0, x1]

x
1

y1) f[x1, x2]

f[x0, x1, x2]

x
2

y2
f[x2, x3]

f[x1, x2, x3]

f[x0, x1, x2, , xn]

x
3

y3
CIVE 3076

f[x2, x3, x4]


Interpolation 5

EXAMPLE
From a table of values of y(xi) below y(1) = 1.5709 y(4) = 1.5727 y(6) = 1.5751 Find y(5), using the Newton method. xi 1 4 6 f[xi]=yi f[xi xi +1] f[xi xi +1,xi+2] 1.5709 0.0006 1.5727 0.0012 1.5751 0.00012

p2(x) = 1.5709 + 0.0006 (x 1) + 0.00012 (x 1) (x 4)


Replace x = 3.5 in the Newton polynomial form, p2(x) = 1.5709 + 0.0006 (2.5) + 0.00012 (2.5) (0.5) = 1.57225
CIVE 3076 Interpolation 6

DIVIDED DIFFERENCE FUNCTION


function a = divdiff(x, y) % Divided differences based on points stored in arrays x % and y. n = length(x); for k=1:n y(k+1:n) = (y(k+1:n) - y(k))./(x(k+1:n) - x(k)); end a = y(:); end

CIVE 3076

Interpolation

NEWTON INTERPOLATING POLYNOMIAL


function [yi, a] = Newtonpol(x, y, xi) % Values yi of the interpolating polynomial at the points xi. % coordinates of the points of interpolation are stored in % vectors x and y. Output parameter a holds coefficients % of the interpolating polynomial in Newton's form. a = divdiff(x, y); n = length(a); val = a(n); for m = n-1:-1:1 val = (xi - x(m)).*val + a(m); end yi = val(:); end
CIVE 3076 Interpolation 8

NEWTON INTERPOLATING POLINOMIAL


Function Newtonpol evaluates an interpolating polynomial at the user supplied points.
For the data of the previous example, we will evaluate Newton's interpolating polynomial of degree at most two, using function Newtonpol. Graph together with the points of interpolation will be plotted. >>xi=5; >> x=[1;4;6]; y=[1.5709; 1.5727; 1.5751]; >> [yi, a] = Newtonpol(x, y, xi); >> plot(x, y, 'o', xi, yi) >> title(Newton polynomial method')
CIVE 3076 Interpolation 9

NEWTON INTERPOLATING POLINOMIAL


Function Newtonpol evaluates an interpolating polynomial at the user supplied points. For the data of the previous example, we will evaluate Newton's interpolating polynomial of degree at most two, using function Newtonpol. Graph together with the points of interpolation will be plotted. >>xi=5; >> x=[1;4;6]; y=[1.5709; 1.5727; 1.5751]; >> [yi, a] = Newtonpol(x, y, xi); >> plot(x,y,'--'); >> hold on; >> plot(x,y,'o') >> plot(xi, yi, '*','color','r')
CIVE 3076 Interpolation 10

NEWTON INTERPOLATING POLINOMIAL


Newton polynomial method 1.575

1.574

1.573

1.572

1.571 1 2 3 4 5 6

CIVE 3076

Interpolation

11

INTERPOLATION BY CUBIC SPLINES


Problem with polynomial interpolation method: The concentration C and diffusion coefficient D(C) that make up the function f(C) to be approximated are nonnegative. But a Newton polynomial interpolant to f(C) is unsatisfactory.
C0 5 7.5 9.9 12.8 13.2 15.1 0.326 16.3 16.8

D 0.0240 0.0437 0.0797 0.1710 0.1990

0.8460 0.9720

p7(x) reproduces much negative values!


CIVE 3076 Interpolation 12

INTERPOLATION BY CUBIC SPLINES


Definition:

Given an interval [a, b]. A partition of the interval [a, b] with


the breakpoints {xl}, i = 1, , m is defined as = {a = x1 < x2 < < xm = b}, where m > 1.

Further, let k < 3, be nonnegative integers. Function s(x) is


said to be a cubic spline function (i.e. degree 3) with smoothness k if the following conditions are satisfied:

(i) On each subinterval [xi, xl+1] s(x) coincides with an algebraic polynomial of degree at most 3.
(ii) s(x) and its derivatives up to order k are all continuous on the interval [a, b]
CIVE 3076 Interpolation 13

INTERPOLATION BY CUBIC SPLINES


MATLAB function spline is designed for computations with the cubic splines that are twice continuously differentiable (k = 2) on the interval [x1, xm]. Clearly dim Sp(3, ) = m + 2. The spline interpolant s(x) is determined uniquely by the interpolatory conditions s(xl) = yi, i = 1, 2, , m and two additional boundary conditions, namely that s'''(x) is continuous at x = x2 and x = xm-1. These conditions are commonly referred to as the not-a-knot end conditions.

CIVE 3076

Interpolation

14

INTERPOLATION BY CUBIC SPLINES


Example 1: using cubic spline to interpolate function f(C)

given in the previous example and calculate f(6)


>> data=xlsread('Example.xlsx',-1); >> x=data(:,1); >> y=data(:,2); >> xi = 6; >> yi = spline(x, y, xi) >> plot(x, y, 'o); >> hold on >> plot(xi, yi,'*','color','r') >> Title('Cubic spline interpolant')
CIVE 3076 Interpolation

Co 5 7.5 9.9 12.8 13.2 15.1 16.3 16.8

D 0.024 0.0437 0.0797 0.171 0.199 0.326 0.846 0.972


15

INTERPOLATION BY CUBIC SPLINES


Cubic spline interpolant 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0

10

12

14

16

18

CIVE 3076

Interpolation

16

INTERPOLATION BY CUBIC SPLINES


Example 2: we will interpolate function g(x) = 1/(1 + x2) on the interval [0, 5] using six evenly spaced breakpoints >> x = [0; 1; 2; 3; 4; 5]; >> y = 1./(1 + x.^2); >> xi = linspace(0, 5); >> yi = spline(x, y, xi); >> plot(x, y, 'o', xi, yi), title('Cubic spline interpolant')

CIVE 3076

Interpolation

17

INTERPOLATION BY CUBIC SPLINES


Cubic spline interpolant 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0

0.5

1.5

2.5

3.5

4.5

CIVE 3076

Interpolation

18

MATLAB FUNCTION interp1


The general form of the function interp1 is yi = interp1(x, y, xi, method), where vectors x and y are the vectors holding the x- coordinates and the y- coordinates of points to be interpolated respectively, xi is a vector holding points of evaluation, i.e., yi = f(xi) method is an optional string specifying an interpolation

method.

CIVE 3076

Interpolation

19

MATLAB FUNCTION interp1


The following methods work with the function interp1 Nearest neighbor interpolation, method = 'nearest'. Produces a locally piecewise constant nterpolant. Linear interpolation method = 'linear'. Produces a piecewise linear interpolant.

Cubic spline interpolation, method = 'spline'. Produces a cubic spline interpolant.


Cubic interpolation, method = 'cubic'. Produces a piecewise cubic polynomial.

CIVE 3076

Interpolation

20

MATLAB FUNCTION interp1


In this example, the following points >> x = 0:pi/5:pi; >> y = sin(2.*x); are interpolated using the method of interpolating 'nearest The interpolant is evaluated at the following points >> xi = 0:pi/100:pi; >> yi = interp1(x, y, xi, 'nearest'); >> plot(x, y, 'o', xi, yi), >> title('Piecewise constant interpolant of y = sin(2x)')

CIVE 3076

Interpolation

21

MATLAB FUNCTION interp1


Piecewise constant interpolant of y = sin(2x) 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

0.5

1.5

2.5

3.5

CIVE 3076

Interpolation

22

MATLAB FUNCTION interp1


Linear constant interpolant of y = sin(2x) 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

0.5

1.5

2.5

3.5

CIVE 3076

Interpolation

23

MATLAB FUNCTION interp1


Spline constant interpolant of y = sin(2x) 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

0.5

1.5

2.5

3.5

CIVE 3076

Interpolation

24

MATLAB FUNCTION interp1


Cubic constant interpolant of y = sin(2x) 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

0.5

1.5

2.5

3.5

CIVE 3076

Interpolation

25

TWO DIMENSIONAL INTERPOLATION


General Concepts:
Given a rectangular grid {xk, yl} and the associated set of numbers zkl, for k = 0, 1, , m, l = 0, 1, , n, find a bivariate function z = f(x, y) that interpolates the data, i.e., f(xk. yl) = zkl for all values of k and l. The grid points must be sorted monotonically, x0 < x1 < < xn with a similar ordering of the y-ordinates.

MATLAB built-in function zi = interp2(x, y, z, xi, yi, 'method') generates a bivariate interpolant on the rectangular grids and evaluates it in the points specified in the arrays xi and yi. Sixth input parameter 'method' is optional and specifies a method of interpolation
CIVE 3076 Interpolation 26

TWO DIMENSIONAL INTERPOLATION


General Concepts: Available methods are:
'nearest' - nearest neighbor interpolation 'linear' - bilinear interpolation 'cubic' - bicubic interpolation Example 1: a bivariate function z = sin(x2 + y2) is interpolated on the square -1 x 1, -1 y 1 using the 'linear methods. >> [x, y] = meshgrid(-1:.25:1); >> z = sin(x.^2 + y.^2); >> [xi, yi] = meshgrid(-1:.05:1); >> zi = interp2(x, y, z, xi, yi, 'linear'); >> surf(xi, yi, zi); >> title('Bilinear interpolant to sin(x^2 + y^2)')
CIVE 3076 Interpolation 27

TWO DIMENSIONAL INTERPOLATION


Bilinear interpolant to sin(x 2 + y 2)

1 0.8 0.6 0.4 0.2 0 1 0.5 0 -0.5 -1 -1 -0.5 0.5 0 1

CIVE 3076

Interpolation

28

TWO DIMENSIONAL INTERPOLATION


Bicubic interpolant to sin(x 2 + y 2)

1 0.8 0.6 0.4 0.2 0 1 0.5 0 -0.5 -1 -1 -0.5 0.5 0 1

CIVE 3076

Interpolation

29

TWO DIMENSIONAL INTERPOLATION


Example 2: Basin Location Shape Reads x, y, z data from ShapeData.xlsx file then plot the 3-D surface of this basin location based on this data.

DATA = xlsread('ShapeData.xlsx','Input'); >> x=DATA(:,1); y=DATA(:,2); z=DATA(:,3); % Create a uniformly spaced grid >> xlin=linspace(min(x),max(x),100); >> ylin=linspace(min(y),max(y),100); >> [X,Y]=meshgrid(xlin,ylin); % Interpolate data for the grid created above >> Z=griddata(x,y,z,X,Y, binearest'); >> surf(Z) >> zlabel('Elevation (ft)'); >> title(Bilinear interpolant to Trapezoidal Basin');
CIVE 3076 Interpolation 30

TWO DIMENSIONAL INTERPOLATION


Binearest interpolant to Trapezoidal Basin

5004

Elevation (ft)

5002 5000 4998 4996 4994 400 300 200 100 y (ft) 0 0 50 100 x (ft) 150 200 250

CIVE 3076

Interpolation

31

TWO DIMENSIONAL INTERPOLATION


Bicubic interpolant to Trapezoidal Basin

5004

Elevation (ft)

5002 5000 4998 4996 4994 400 300 200 100 y (ft) 0 0 50 100 x (ft) 150 200 250

CIVE 3076

Interpolation

32

QUESTIONS?

CIVE 3076

Interpolation

33

También podría gustarte