Top Banner
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
21

08 Matlab Interpolation

Nov 18, 2014

Download

Documents

Omed Ghareb

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
Page 1: 08 Matlab Interpolation

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

Page 2: 08 Matlab Interpolation

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

Page 3: 08 Matlab Interpolation

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

Page 4: 08 Matlab Interpolation

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

13.6000 14.7000 16.7000 18.7000 19.6000 20.5000 23.1500

25.8000 NaN NaN NaN NaN NaN NaN 7

Interp1Second Method:

yi = interp1(x,Y,xi,method)y 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

Page 5: 08 Matlab Interpolation

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,'method')

plot(x,y,'rp',xi,yi,'bo:')

Compare your result using different interpolation methods.9

25

30'nearest'

10

15

20

0 1 2 3 4 5 6 7 8 9 100

5

The result is :

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

Page 6: 08 Matlab Interpolation

6

25

30'linear'

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

13.6000 14.7000 16.7000 18.7000 19.6000 20.5000 23.1500

25.8000 NaN NaN NaN NaN NaN NaN 11

140

160'spline'

40

60

80

100

120

0 1 2 3 4 5 6 7 8 9 100

20

40

The result is :

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

Page 7: 08 Matlab Interpolation

7

35

40'pchip'

10

15

20

25

30

0 1 2 3 4 5 6 7 8 9 100

5

10

The result is :

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

35

40'cubic'

10

15

20

25

30

0 1 2 3 4 5 6 7 8 9 100

5

10

The result is :

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

Page 8: 08 Matlab Interpolation

8

25

30'v5cubic'

10

15

20

0 1 2 3 4 5 6 7 8 9 100

5

The result is :

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

Example(2):

C id h f ll i f d

Interp1

Consider the following set of data;

x = 0:10;

y = sin(x);

xi = 0:.25:10;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

Page 9: 08 Matlab Interpolation

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

Page 10: 08 Matlab Interpolation

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

Page 11: 08 Matlab Interpolation

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

Page 12: 08 Matlab Interpolation

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

Page 13: 08 Matlab Interpolation

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

Page 14: 08 Matlab Interpolation

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

Page 15: 08 Matlab Interpolation

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

Page 16: 08 Matlab Interpolation

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

Page 17: 08 Matlab Interpolation

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

Page 18: 08 Matlab Interpolation

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

Page 19: 08 Matlab Interpolation

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

Page 20: 08 Matlab Interpolation

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

Page 21: 08 Matlab Interpolation

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

15

20

25

30

plot(x,x,'ok',xi,y1,'r:',xi,y2,'b--',xi,y3,'g-')

-3 -2 -1 0 1 2 3 4 5 6-5

0

5

10

41