# Interpolation

### From ComputingForScientists

## Contents |

# 1. Linear

Given two measurement points, linear interpolation gives an estimate of the value one would obtain at other points. Consider the following table of numbers
What is the expected value of If we assume that un-measured points between
Solving for the two unknowns gives 4 = 2(2 − In general, given the points
the
An alternative method for finding the unknown value |

# 2. Bi-Linear

In linear interpolation, one is given pairs of measurement points in the x-y plane and estimates of the y-value are made for a given x-value. For example, if x is position and y represents the measured height of an object, linear interpolation can be used to determine height values at positions for which the height was not measured.

In bi-linear interpolation, one is given values of x,y, and z and asked to estimate z values given x,y values. For example, if x and y represent the position of an object and z represents its measured height, bi-linear interpolation can be used to determine height values at x-y positions where no height measurement was made.

Bi-linear interpolation requires 3 interpolations. Consider the table of numbers x y z 0 0 2 1 0 1 1 1 0.5 0 1 0.5 Suppose that we want to estimate the value of The first interpolation finds a height The second interpolation finds a height The third interpolation finds a height The procedure for determining In general, the bi-linear interpolation expression for
where
and, for example, |

See also http://en.wikipedia.org/wiki/Bilinear_interpolation

# 3. Problems

## 3.1. Linear Interpolation

Draw the points in the table and use linear interpolation to find an estimate of `z`

for `a=99.5`

. Show your work using the system of equations method and the similar triangles method.

a z 90 20 95 22 100 33

Note that you may check your answer using MATLAB:

interp1([90,95,100],[20,22,33],99.5)

## 3.2. 1-D Interpolation

An estimate of speed at `x = 1.9`

given the following measurements

x speed 1 11 2 33

could be made using MATLAB's `interp1`

function:

Te = interp1([1,2],[11,33],1.9)

Sketch these measurements on a piece of paper, make the calculation of an estimate for `speed`

at `x=1.9`

by hand using only a calculator, and then compare your result with that from executing the above.

## 3.3. 1-D Interpolation

The following measurements were made

t v 1 2.1 2.1 -3.1

Use linear interpolation (pencil and paper) to estimate `v`

at `t=1.2`

.

Use `interp1`

to verify your answer.

Answer |
---|

t = [1, 2.1] v = [2.1, -3.1] a = interp1(t,v,1.2) |

## 3.4. Linear Interpolation

Given an two arrays of measurements `x`

and `y`

write a MATLAB program that, when copied below the code shown, will compute `yi`

, the linearly interpolated value of `y`

at point `xi`

. Your code should work for any value of `xi`

between 1.0 and 4.0.

xi = 1.5; x = [1.0,2.0,3.0,4.0]; y = [1.1,1.8,3.3,4.1];

## 3.5. Linear Interpolation

### 3.5.1. I

Given an two arrays of measurements `x`

and `y`

write a MATLAB program that, when copied below the code shown, will compute `yi`

, the linearly interpolated value of `y`

at point `xi`

. Your code should work for any value of `xi`

between 1.0 and 4.9.

xi = 1.5; x = [1.0,2.3,3.1,4.9]; y = [1.1,1.8,3.3,4.1];

### 3.5.2. II

Use the last pair of (x,y) values to provide an estimate of the value of `y`

at `x=5.0`

.

## 3.6. Linear Interpolation

### 3.6.1. Part I

Given an two arrays of measurements `x`

and `y`

write a MATLAB program that, when copied below the code shown, will compute `yi`

, the linearly interpolated value of `y`

at point `xi=1.5`

. Your code needs to only work the value of `xi=1.5`

.

Do this **without** using the function `interp1`

.

xi = 1.5; x = [1.0,2.1,2.8,3.8]; y = [2.1,2.8,3.3,8.1];

Write a program that works for any value of `xi`

between 1.0 and 3.8 (**without** using the function `interp1`

). If I copy the code onto the MATLAB after changing `xi`

, it should give the correct result.

Answer |
---|

xi = 1.5; x = [1.0,2.1,2.8,3.8]; y = [2.1,2.8,3.3,8.1]; % With the similar triangle method % ?/(2.8-2.1) = 0.5/(2.1-1.0) yi = 2.1 + 0.5*(2.8-2.1)/(2.1-1.0) % or, written using variables yi = y(1) + (xi-x(1))*(y(2)-y(1))/(x(2)-x(1)) % check answer yi = interp1(x,y,xi) |

### 3.6.2. Part II

Use the last pair of (x,y) values to provide an estimate of the value of `y`

at `x=9.0`

.

Answer |
---|

y = mx+b 3.3 = m*2.8+b 8.1 = m*3.8+b Two equations and two unknowns. Subtract two equations to eliminate b m = (8.1-3.3)/(3.8-2.8) % Gives 4.8 Substitute m into first equation and solve for b b = 3.3-2.8*4.8 % Gives -10.14 Equation of line is y = 4.8x-10.14 Plug in x=9.0: yi = 4.8*9.0 - 10.14 % Gives 33.06 |

## 3.7. Linear Interpolation

Derive

## 3.8. `meshgrid`

The function `meshgrid`

can be used to create matrices that are required for the input to `interp2`

.

For example, instead of writing

X = [1 2; 1 2]; Y = [1 1; 2 2];

one could write

[X,Y] = meshgrid([1:2],[1:2])

Use both methods to create a grid that corresponds to measurements made in the range of x=2-4 in increments of 0.5 and y=2-3 in increments of 0.5.

Both methods should give the same matrices `X`

and `Y`

.

Save your program as `HW5b.m`

.

Answer |
---|

x = [2,2.5,3,3.5,4;2,2.5,3,3.5,4;2,2.5,3,3.5,4] y = [2,2,2,2,2;2.5,2.5,2.5,2.5,2.5;3,3,3,3,3] [X,Y] = meshgrid([2:0.5:4],[2:0.5:3]) |

## 3.9. Bi-linear Interpolation I

Draw the points in the table and use linear interpolation to find an estimate of `z`

at `(x,y)=(0.25,0.75)`

.

x y z 0 0 2 1 0 1 1 1 0.5 0 1 0.5

Draw a plot for each of the three linear interpolations and show the calculations required to find `zA`

, `zB`

, and `zo`

.

Note that you can check your answer using MATLAB using:

[X,Y] = meshgrid([0:1],[0:1]) Z = [2.0 1.0;0.5 0.5] interp2(X,Y,Z,0.25,0.75)

## 3.10. Bi-linear Interpolation II

Suppose that you have the following set of measurements of `vx`

at four grid points and want to estimate `vx`

somewhere inside of the four points.

x y vx 1 1 9.9 1 2 10.10 2 1 11.11 2 2 12.12

Sketch out the measurements on a piece of paper and visually attempt to estimate `vx`

at (x,y) = (1.35,1.35).

Write a program that uses `interp2`

to estimate `vx`

at (x,y) = (1.35,1.35).

Save your program as `HW5c.m`

.

Answer |
---|

x = [1,2;1,2] y = [1,1;2,2] vx = [9.9,11.11;10.10,12.12] interp2(x,y,vx,1.35,1.35) |

## 3.11. Bi-linear Interpolation II

Suppose that you have the following set of measurements of Bx and By at four grid points and want to estimate Bx and By somewhere inside of the four points.

x y Bx By 1 1 1.1 0.4 1 2 2.2 0.5 2 1 3.3 0.6 2 2 4.4 0.7

Write a program that uses `interp2`

to estimate Bx and By at (x,y) = (1.35,1.35).

Answer |
---|

X = [1 2; 1 2]; Y = [1 1; 2 2]; %or %[X,Y] = meshgrid([1:2],[1:2]) Bx = [1.1 3.3; 2.2 4.4]; By = [0.4 0.6; 0.5 0.7]; Bxi = interp2(X,Y,Bx,1.35,1.35) Byi = interp2(X,Y,By,1.35,1.35) |

## 3.12. Bi-linear interpolation III

Derive the bi-linear interpolation expression for *z*_{i} = *f*(*x*_{i},*y*_{i}):

where

*a* = *f*(*x*_{1},*y*_{1})(*x*_{2} − *x*_{i})(*y*_{2} − *y*_{i})

*b* = *f*(*x*_{2},*y*_{1})(*x*_{i} − *x*_{1})(*y*_{2} − *y*_{i})

*c* = *f*(*x*_{1},*y*_{2})(*x*_{2} − *x*_{i})(*y*_{i} − *y*_{1})

*d* = *f*(*x*_{2},*y*_{2})(*x*_{i} − *x*_{1})(*y*_{i} − *y*_{1})

and, for example, *f*(*x*_{1},*y*_{1}) is *z* at point (*x*_{1},*y*_{1}).