The Code for Interpolation in MATLAB
MATLAB is a sophisticated mathematical software program that can carry out arithmetic, calculus and hundreds of other math applications -- including linear interpolation, or the art of figuring a value indirectly based on other data. If a table only tells you that 40-degree air has a pressure of 70 units and 50-degree air has a pressure of 92 units, interpolation is how to find the air pressure at 44 degrees.
-
The Direct Method
-
The direct method of interpolation involves several calculation steps. Using the example from the introduction:
"dt = 50 - 40" is the range of temperatures from the table.
"dp = 92 - 70" is the range of pressures from the table.
"dt1 = (44-40)/dt" is the fraction up the range of temperatures for the temperature in question.
"answer = dt1*dp + 70" is the same fraction along the pressure range added to the lower pressure in the table; and it's the answer. In this example, it's 78.8 units of pressure.
MATLAB Linear Interpolation
-
MATLAB includes a function that can accomplish all four calculations from the previous section simultaneously. It's the "interp1" function and it will make your calculations simple. Using the example from before, provide the interp1 function the pressure range, the temperature range and the temperature in question and it will give you the pressure answer in one step. Type this code at MATLAB's prompt:
answer = interp1([40 50], [70 92], 44)
MATLAB will respond with "answer = 78.8000," which matches the previous section's result.
-
Nearest Neighbor Interpolation
-
MATLAB's interp1 function offers several methods for performing interpolation. Linear is the default method. However, assume that instead of just the numbers 40 and 50, you had temperature values for all integers between 40 and 50. Assume as well that you have corresponding integer-only pressure values for each temperature value in your table. If you use the "nearest neighbor" method, MATLAB won't give you an exact value - it instead tells you the nearest answer in the table. Code in the following format accomplishes this:
answer = interp1([temp values], [pressure values], 43.4, 'nearest')
Cubic Spline Interpolation
-
Cubic spline is another interpolation method the interp1 function or the standalone interpolation function "spline" can perform. Instead of giving a single value to the third argument in the function, give it a range of values inside the first range (temperatures in table). The spline function will return a corresponding range inside the second range (pressures in table). You can use code in the following format to extract the range of pressures corresponding to a sub-range of temperatures:
answer = spline([temp range], [pressure range], [temp sub-range])
-