Escolar Documentos
Profissional Documentos
Cultura Documentos
Interpolation
Interpolation
Interpolation is a method of constructing
new data points from a discrete set of
known data points
Force f (lb) Deflection x
(in)
0 0
100 0.09
200 0.18
300 0 28
0.28
400 missing
500 0.46
600 0.55
700 0.65
2
1
Interpolation vs. Approximation
Interpolation:
Find a function that passes through a set of data points.
points
The function passes through each point.
Approximation:
Find a function that comes close to a set of data points.
The function might not pass through any of the points.
Interpolation
y linear Interpolation
y interpolation : estimate a variable's value between the
data points.
y extrapolation : estimate a variable's value outside of the
given data range.
y linear interpolation : equivalent to connecting the data
points
i t with
ith a li
linear ffunction
ti (a( straight
t i ht line)
li )
y Linear interpolation in MATLAB :
interp1, interp2, interpn
2
Interp1
One-dimensional data interpolation
First Method:
yi = interp1(x,Y,xi)
Description
returns vector yi containing elements corresponding to the
elements of xi and determined by interpolation within vectors x
and Y. The vector x specifies the points at which the data Y is
given. If Y is a matrix, then the interpolation is performed for
each column of Y and yi is length(xi)-by-size(Y,2).
5
Interp1
Example:
C id the
Consider h ffollowing
ll i set off data;
d
x=[0, 1, 2, 3, 4, 5, 6, 7];
y=[2.3, 5.8, 8.9, 12.5, 14.7, 18.7, 20.5, 25.8];
xi 0:0.5:10;
xi=0:0.5:10;
yi=interp1(x,y,xi)
plot(x,y,'rp',xi,yi,'bo:')
3
30
Interp1
25
20
15
10
The result is : 0
0 1 2 3 4 5 6 7 8 9 10
yi =
2.3000 4.0500 5.8000 7.3500 8.9000 10.7000 12.5000
13.6000 14.7000 16.7000 18.7000 19.6000 20.5000 23.1500
25.8000 NaN NaN NaN NaN NaN NaN 7
Interp1
Second Method:
yyi = interp1(x,Y,xi,method)
p ( , , , )
Description
interpolates using alternative methods:
'nearest' Nearest neighbor interpolation
'linear' Linear interpolation (default)
'spline' Cubic spline interpolation
'pchip' Piecewise cubic Hermite interpolation
'cubic' (Same as 'pchip')
'v5cubic' Cubic interpolation used in MATLAB 5 8
4
Interp1
Example:
C id the
Consider h ffollowing
ll i set off data;
d
x=[0, 1, 2, 3, 4, 5, 6, 7];
y=[2.3, 5.8, 8.9, 12.5, 14.7, 18.7, 20.5, 25.8];
xi 0:0.5:10;
xi=0:0.5:10;
yi=interp1(x,y,xi,'method')
plot(x,y,'rp',xi,yi,'bo:')
Compare your result using different interpolation methods.
9
30
'nearest'
25
20
15
10
The result is : 0
0 1 2 3 4 5 6 7 8 9 10
yi =
2.3000 5.8000 5.8000 8.9000 8.9000 12.5000 12.5000
14.7000 14.7000 18.7000 18.7000 20.5000 20.5000 25.8000
25.8000 NaN NaN NaN NaN NaN NaN 10
5
30
'linear'
25
20
15
10
The result is : 0
0 1 2 3 4 5 6 7 8 9 10
yi =
2.3000 4.0500 5.8000 7.3500 8.9000 10.7000 12.5000
13.6000 14.7000 16.7000 18.7000 19.6000 20.5000 23.1500
25.8000 NaN NaN NaN NaN NaN NaN 11
160
'spline'
140
120
100
80
60
40
20
The result is : 0
0 1 2 3 4 5 6 7 8 9 10
yi =
2.3000 4.2375 5.8000 7.2625 8.9000 10.8251 12.5000
13.5247 14.7000 16.7262 18.7000 19.7204 20.5000 22.1546
25.8000 32.5521 43.5268 59.8399 82.6072 112.9446 151.9679 12
6
40
'pchip'
35
30
25
20
15
10
The result is : 0
0 1 2 3 4 5 6 7 8 9 10
yi =
2.3000 4.1015 5.8000 7.3446 8.9000 10.7750 12.5000
13.5865 14.7000 16.7445 18.7000 19.5744 20.5000 22.6047
25.8000 29.4390 32.8746 35.4599 36.5479 35.4915 31.6437 13
40
'cubic'
35
30
25
20
15
10
The result is : 0
0 1 2 3 4 5 6 7 8 9 10
yi =
2.3000 4.1015 5.8000 7.3446 8.9000 10.7750 12.5000
13.5865 14.7000 16.7445 18.7000 19.5744 20.5000 22.6047
25.8000 29.4390 32.8746 35.4599 36.5479 35.4915 31.6437 14
7
30
'v5cubic'
25
20
15
10
The result is : 0
0 1 2 3 4 5 6 7 8 9 10
yi =
2.3000 4.1000 5.8000 7.3438 8.9000 10.7562 12.5000
13.5750 14.7000 16.7250 18.7000 19.5188 20.5000 22.7125
25.8000 NaN NaN NaN NaN NaN NaN 15
Interp1
Example(2):
C id the
Consider h ffollowing
ll i set off data;
d
x = 0:10;
y = sin(x);
xi = 0:.25:10;
yi = interp1(x,y,xi,'method');
plot(x,y,'rp',xi,yi, 'bo:')
Compare your result using different interpolation methods.
16
8
1
'nearest'
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
17
1
'linear'
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
18
9
1
'spline'
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
19
1
'pchip'
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
20
10
1
'cubic'
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
21
1
'v5cubic'
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
22
11
Spline
Cubic spline data interpolation
yi = spline(x,y,xi)
Description
uses cubic spline interpolation to find yi, the values of the
underlying
y g function y at the p points in the vector xi. The vector x
specifies the points at which the data y is given. If y is a matrix,
then the data is taken to be vector-valued and interpolation is
performed for each row of y. For this case, length(x) must equal
size(y,2), and yi is size(y,1)-by-length(xi).
23
Spline
Cubic spline data interpolation
Example(1):
x = 0:10;
y = sin(x);
xi = 0:.25:10;
yi = spline(x,y,xi);
plot(x,y,'rp',xi,yi, 'bo:')
24
12
1
Spline
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
25
Spline
Cubic spline data interpolation
Example(2):
x = -4:4;
y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0];
cs = spline(x,[0 y 0]);
xx = linspace(-4,4,20);
plot(x,y,'rp',xx,ppval(cs,xx),'bo:');
26
13
3
Spline
25
2.5
1.5
0.5
0
-4 -3 -2 -1 0 1 2 3 4
27
Spline
Example(3):
years census (millions)
The table represent 1900 75 995
75.995
the census years from
1910 91.972
1900 to 1990 and the
1920 105.711
corresponding United
1930 123.203
States population in
1940 131.669
millions of people.
1950 150.697
Use the cubic
c bic spline to
1960 179.323
extrapolate and
1970 203.212
predict the population
in the year 2020. 1980 226.505
1990 249.633
28
14
Spline
Example(3):
t = 1900:10:1990;
p = [ 75.995 91.972 105.711 123.203 131.669 ...
150.697 179.323 203.212 226.505 249.633 ];
spline(t,p,2020)
29
Cubic‐Spline
y Cubic‐Spline Interpolation
y the use of high‐order polynomials can exhibit undesired
the use of high order polynomials can exhibit undesired
behavior between the data points, and this behavior can
make high‐order polynomials unsuitable for interpolation.
y spline interpolation : using a lower‐order polynomial
between each pair of adjacent data points.
y Spline interpolation obtains an exact fit that is also smooth.
y cubic‐spline
b l interpolation
l
y the most common procedure
y use cubic polynomials, called cubic splines
30
15
Interp2
Two-dimensional data interpolation
First Method:
ZI = interp2(X,Y,Z,XI,YI)
Description
ZI = interp2(X,Y,Z,XI,YI) returns matrix ZI containing elements
corresponding
p g to the elements of XI and YI and determined by y
interpolation within the two-dimensional function specified by
matrices X, Y, and Z. X and Y must be monotonic, and have the
same format ("plaid") as if they were produced by meshgrid.
Matrices X and Y specify the points at which the data Z is given.
Out of range values are returned as NaNs.
31
Interp2
Two-dimensional data interpolation
Example:
[X,Y] = meshgrid(-3:.5:3);
Z = peaks(X,Y);
[XI,YI] = meshgrid(-3:.1:3);
ZI = interp2(X,Y,Z,XI,YI);
interp2(X Y Z XI YI);
mesh(X,Y,Z), hold, mesh(XI,YI,ZI+15)
hold off
axis([-3 3 -3 3 -5 20])
32
16
Interp2
33
Interp2
Two-dimensional data interpolation
S
Second
dMMethod:
th d
ZI = interp2(X,Y,Z,XI,YI,method)
Description
ZI = interp2(X,Y,Z,XI,YI,method) specifies an alternative
interpolation method:
'nearest' Nearest neighbor interpolation
'linear' Bilinear interpolation (default)
'spline' Cubic spline interpolation
'cubic' Bicubuc interpolation 34
17
Interp2
[X,Y] = meshgrid(-3:.5:3);
Z = peaks(X,Y);
[XI,YI] = meshgrid(-3:.1:3);
ZI = interp2(X,Y,Z,XI,YI,'method');
mesh(X,Y,Z), hold, mesh(XI,YI,ZI+15)
hold off
axis([-3 3 -3 3 -5 20])
pause(5)
contour(XI,YI,ZI)
35
'nearest'
-1
-2
-3
-3 -2 -1 0 1 2 3
36
18
'linear'
-1
-2
-3
-3 -2 -1 0 1 2 3
37
'spline'
-1
-2
-3
-3 -2 -1 0 1 2 3
38
19
'cubic'
-1
-2
-3
-3 -2 -1 0 1 2 3
39
Exercise
y Given x = [-3 -1 0 2 5 6]; y = [9 1 0 4 25 36];
1 Compute and plot the nearest, linear and cubic
1. Compute and plot the nearest, linear and cubic‐spline
spline
interpolation of the data points over the range xi =
[-3:0.05:6];
2.Compute the value of f(4) using nearest, linear and
cubic‐spline interpolation. Display the respective
errors when the answer is compared to the actual value
of f(4)
f( ) = 16?
y Plot the original points as black circles, 'nearest'
values with dotted line in red, 'linear' with dashed line
in blue, and 'spline' with a solid line in green
40
20
Solution
40
x = [-3 -1 0 2 5 6];
35
y = [9 1 0 4 25 36];
30
xi = [-3:0.05:6];
25
y1=interp1(x,y,xi,'nearest');
y2=interp1(x,y,xi,'linear');
20
y3=interp1(x,y,xi,'spline');
15
plot(
plot(x,x,'ok',xi,y1,'r:',xi,y2,'b--',xi,y3,'g-')
10
'ok' i 1 'r:' i 2 'b ' i 3 'g ')
5
-5
-3 -2 -1 0 1 2 3 4 5 6
41
21