{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

37. Interpolation

# 37. Interpolation - Interpolation For an introduction to...

This preview shows pages 1–5. Sign up to view the full content.

©2009 by L. Lagerstrom Interpolation For an introduction to the basic concepts, see the associated video clip Linear interpolation Multiple values and out-of-range values Multiple data sets Cublic spline interpolation Which is better: linear or cubic spline? Extrapolation Nearest neighbor interpolation Versions of interp1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Matlab code Command window display ©2009 by L. Lagerstrom Linear Interpolation %Linear interpolation: Given a %set of (x,y) data, we can estimate %the y value for a certain x value %that's not part of the data set %by connecting the two nearest data %points with a straight line. %Some sample data x = 0:5 y = [0 20 60 68 77 110] %Interpolate a y value for x = 2.6 %using the interp1 function. %(Note: the "1" in interp1 indicates %one variable; Matlab also has an %interp2 function for functions of %two variables, but we won't cover %it.) yi = interp1(x,y,2.6) x = 0 1 2 3 4 5 y = 0 20 60 68 77 110 yi = 64.80
Matlab code Command window display ©2009 by L. Lagerstrom Multiple Values and Out-of-Range Values %We can also interpolate for %multiple x values at the same time %by using a row vector: %Redisplay x and y for reference x = 0:5 y = [0 20 60 68 77 110] %Interpolate for x = 2.6 and 4.9 xi = [2.6 4.9] yi = interp1(x,y,xi) %Note that interpolation cannot %be done outside the range of the %given data set. The max value in %x is 5, so interpolating for 5.1 %is undefined ("not a number"): yi = interp1(x,y,5.1) x = 0 1 2 3 4 5 y = 0 20 60 68 77 110 xi = 2.60 4.90 yi = 64.80 106.70 yi = NaN

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Matlab code Command window display ©2009 by L. Lagerstrom Multiple Data Sets %The data in y may also be an array, %in which case the interp1 function %will interpolate a value for each %column of data. That is, each %column represents a set of data %points for the same set of x %values. Consider, for example, %three sets of temperature readings %at one second intervals: %Time (s) Temp 1 Temp 2 Temp 3 % 0 0 0 0 % 1 20 25 52 % 2 60 62 90 % 3 68 67 91 % 4 77 82 93 %Store the time and temperature data t = (0:4)'; T(:,1) = [0 20 60 68 77]'; T(:,2) = [0 25 62 67 82]'; T(:,3) = [0 52 90 91 93]'; %Interpolate for the time value 2.6 temps = interp1(t,T,2.6) temps = 64.8000 65.0000 90.6000
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}