1 Interpolation Introduction to Matlab 8 Omed Ghareb Abdullah Sulaimani University Sulaimani University College of Sciences College of Sciences Physics Department Physics Department 1 Interpolation Interpolation is a method of constructing new data points from a discrete set of Force f (lb) Deflection x (in) 0 0 100 0.09 200 0.18 300 0 28 new data points from a discrete set of known data points 300 0.28 400 missing 500 0.46 600 0.55 700 0.65 2
Lecture (8): Matlab Interpolation - Sulaimani University - College of Science - Physics Department
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Interpolation
Introduction to Matlab 8
Omed Ghareb AbdullahSulaimani UniversitySulaimani UniversityCollege of SciencesCollege of SciencesPhysics DepartmentPhysics Department
1
InterpolationInterpolation is a method of constructingnew data points from a discrete set of
Force f (lb) Deflection x (in)
0 0
100 0.09
200 0.18
300 0 28
new data points from a discrete set ofknown data points
300 0.28
400 missing
500 0.46
600 0.55
700 0.65
2
2
Interpolation:Find a function that passes through a set of data points
Interpolation vs. Approximation
Find a function that passes through a set of data 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.
In engineering & science one often has a number of data points,as obtained by sampling or some experiment, and tries toconstruct a function which closely fits those data points.
This is called CURVE FITTING.
Interpolation is a specific case of curve fitting, in which thefunction must go exactly through the data points 3
Interpolation
linear Interpolationlinear Interpolationinterpolation : estimate a variable's value between the data points.extrapolation : estimate a variable's value outside of the given data range.linear interpolation : equivalent to connecting the data
i t ith li f ti ( t i ht li )points with a linear function (a straight line)Linear interpolation in MATLAB :
interp1, interp2, interpn
4
3
Interp1One-dimensional data interpolation
First Method:
yi = interp1(x,Y,xi)
Description
returns vector yi containing elements corresponding to thereturns 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
Example:
C id h f ll i f d
Interp1
Consider the following set of data;
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:')
6
4
25
30Interp1
10
15
20
0 1 2 3 4 5 6 7 8 9 100
5
The result is :
yi =2.3000 4.0500 5.8000 7.3500 8.9000 10.7000 12.5000
Compare your result using different interpolation methods.16
9
0.8
1
'nearest'
-0.2
0
0.2
0.4
0.6
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
17
0.8
1
'linear'
-0.2
0
0.2
0.4
0.6
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
18
10
0.8
1
'spline'
-0.2
0
0.2
0.4
0.6
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
19
0.8
1
'pchip'
-0.2
0
0.2
0.4
0.6
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
20
11
0.8
1
'cubic'
-0.2
0
0.2
0.4
0.6
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
21
0.8
1
'v5cubic'
-0.2
0
0.2
0.4
0.6
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
22
12
SplineCubic spline data interpolation
yi = spline(x,y,xi)
Description
uses cubic spline interpolation to find yi, the values of the underlying function y at the points in the vector xi. The vector x y g y pspecifies 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
SplineCubic 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
13
0.8
1
Spline
-0.2
0
0.2
0.4
0.6
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
25
SplineCubic 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
14
2 5
3
Spline
1
1.5
2
2.5
-4 -3 -2 -1 0 1 2 3 40
0.5
1
27
SplineExample(3):The table represent
years census (millions)1900 75 995
the census years from 1900 to 1990 and the corresponding United States population in millions of people. Use the c bic spline to
1900 75.9951910 91.9721920 105.7111930 123.2031940 131.6691950 150.697Use the cubic spline to
extrapolate and predict the population in the year 2020.
1960 179.3231970 203.2121980 226.5051990 249.633
28
15
SplineExample(3):
t = 1900:10:1990;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)
The result is ans =
298.1270
29
Cubic‐SplineCubic‐Spline Interpolation
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.spline interpolation : using a lower‐order polynomial between each pair of adjacent data points.Spline interpolation obtains an exact fit that is also smooth.
b l lcubic‐spline interpolationthe most common procedureuse cubic polynomials, called cubic splines
30
16
Interp2Two-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 to the elements of XI and YI and determined by p g yinterpolation 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
Interp2Two-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);ZI = 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
17
Interp2
33
Interp2
S d M th dTwo-dimensional data interpolation
Second Method:
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
18
Interp2[X,Y] = meshgrid(-3:.5:3);
Z = peaks(X,Y);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-3
-2
-1
0
36
19
'linear'
1
2
3
-3 -2 -1 0 1 2 3-3
-2
-1
0
37
'spline'
1
2
3
-3 -2 -1 0 1 2 3-3
-2
-1
0
38
20
'cubic'
1
2
3
-3 -2 -1 0 1 2 3-3
-2
-1
0
39
Exercise
Given x = [-3 -1 0 2 5 6]; y = [9 1 0 4 25 36];
1 Compute and plot the nearest, linear and cubic‐spline1. Compute and plot the nearest, linear and cubic splineinterpolation 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) = 16?f( )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
21
Solution
x = [-3 -1 0 2 5 6]; y = [9 1 0 4 25 36];35
40
y = [9 1 0 4 25 36];xi = [-3:0.05:6];y1=interp1(x,y,xi,'nearest');y2=interp1(x,y,xi,'linear');y3=interp1(x,y,xi,'spline');plot( 'ok' i 1 'r:' i 2 'b ' i 3 'g ')10