Difference between revisions of "Interpolation"
(→Implementation in Matlab) |
(→Cubic Spline Interpolation) |
||
Line 186: | Line 186: | ||
|+ Constraints for Cubic Splines | |+ Constraints for Cubic Splines | ||
|- | |- | ||
− | ! width=" | + | ! width="175px"|Description |
! Mathematical Statement | ! Mathematical Statement | ||
− | ! Number of Constraints | + | ! width="75px"|Number of Constraints |
|- valign="top" | |- valign="top" | ||
| At each data point, two splines meet. Here the splines must be equal to the data values. | | At each data point, two splines meet. Here the splines must be equal to the data values. | ||
Line 217: | Line 217: | ||
<br clear="all" /> | <br clear="all" /> | ||
− | === Example === | + | === Example 1 === |
Using the data in the table above, determine the density of air at the following temperatures: 60, 120, 180, 240, 300, 360, 420, 480, 540 °F. | Using the data in the table above, determine the density of air at the following temperatures: 60, 120, 180, 240, 300, 360, 420, 480, 540 °F. | ||
Line 250: | Line 250: | ||
Assume that we have four points, | Assume that we have four points, | ||
− | <math>(x_1,y_1),\,(x_2,y_2),\,(x_3,y_3),\,(x_4,y_4) | + | <math>(x_1,y_1),\,(x_2,y_2),\,(x_3,y_3),\,(x_4,y_4)</math>. This means that we have three cubic equations that form the cubic spline. We will write these equations as |
− | |||
− | {| border="1" cellpadding=" | + | :<math> \begin{align} |
+ | f_1(x) &= a_1 + b_1 x + c_1 x^2 + d_1 x^3 \\ | ||
+ | f_2(x) &= a_2 + b_2 x + c_2 x^2 + d_2 x^3 \\ | ||
+ | f_3(x) &= a_3 + b_3 x + c_3 x^2 + d_3 x^3 | ||
+ | \end{align} | ||
+ | </math> | ||
+ | |||
+ | From the table above, the equations that we must solve are given by | ||
+ | |||
+ | {| border="1" cellpadding="3" cellspacing="0" align="center" style="text-align:left" | ||
|+ Constraints for Cubic Splines | |+ Constraints for Cubic Splines | ||
|- | |- | ||
Line 279: | Line 287: | ||
|- valign="top" | |- valign="top" | ||
| Second derivatives must match at data points (where splines meet). | | Second derivatives must match at data points (where splines meet). | ||
+ | |<math> | ||
+ | \begin{array}{lcl} | ||
+ | f_1^{\prime\prime}(x_2) = f_2^{\prime\prime}(x_2) = 0 & \Rightarrow & 2c_1 + 6d_1 x_2 = 2c_2 + 6x_2 x_2 \\ | ||
+ | f_2^{\prime\prime}(x_3) = f_3^{\prime\prime}(x_3) = 0 & \Rightarrow & 2c_2 + 6d_2 x_3 = 2c_3 + 6x_3 x_3 \\ | ||
+ | \end{array} | ||
+ | </math> | ||
+ | |- valign="top" | ||
+ | | Second derivatives are zero at end points | ||
|<math> | |<math> | ||
\begin{array}{lcl} | \begin{array}{lcl} | ||
Line 301: | Line 317: | ||
0 & 0 & 2 & 6x_2 & 0 & 0 & 2 & 6x_2 & 0 & 0 & 0 & 0 \\ | 0 & 0 & 2 & 6x_2 & 0 & 0 & 2 & 6x_2 & 0 & 0 & 0 & 0 \\ | ||
0 & 0 & 0 & 0 & 0 & 0 & 2 & 6x_3 & 0 & 0 & 2 & 6x_3 \\ | 0 & 0 & 0 & 0 & 0 & 0 & 2 & 6x_3 & 0 & 0 & 2 & 6x_3 \\ | ||
− | 0 & 0 & 2 & 6x_1 & 0 & 0 & | + | 0 & 0 & 2 & 6x_1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ |
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 2 & 6x_3 \\ | 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 2 & 6x_3 \\ | ||
\end{array} \right] | \end{array} \right] | ||
Line 317: | Line 333: | ||
\end{array} \right) | \end{array} \right) | ||
</math></center> | </math></center> | ||
+ | |||
+ | <!-- jcs upload a matlab script to do cubic splines. --> | ||
== Lagrange Polynomial Interpolation == | == Lagrange Polynomial Interpolation == |
Revision as of 19:24, 1 September 2008
Contents
Linear Interpolation
A linear function may be written as Given any two data points, , and , we can determine a linear function that exactly passes through these points. We can do this by solving for and . Since there are two unknowns we require two equations. They are:
These may be written in matrix form as
Given values for , and , these equations may be easily solved in MATLAB. But since they are so simple, we can easily by hand to obtain a general solution,
This gives the coefficients, which may then be substituted into the original equation and simplified to obtain
This is a very convenient equation for performing linear interpolation.
Example
The density of air at various temperatures and atmospheric pressure is given in the following table
Temperature (°F) | 1.39 | 1.16 | 0.99 | 0.87 | 0.78 | 0.69 | 0.63 | 0.58 | 0.54 | 0.50 | 0.46 | 0.44 | 0.41 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Density (kg/m3) | -10 | 80 | 170 | 260 | 350 | 440 | 530 | 620 | 710 | 800 | 890 | 980 | 1070 |
Estimate the density of air at 32 °F. Using linear interpolation, we have . We define and . Now we can calculate
Implementation in Matlab
There are three ways to do interpolation in MATLAB.
- Solve the linear system.
T1=-9.7; T2=80; rho1=1.39; rho2=1.16; T = 32; A = [ 1 T1; 1 T2 ]; b = [ rho1; rho2 ]; a = A\b; rho = a(1) + a(2)*T;
- Use the general equation we derived above:
T1=-9.7; T2-80; rho1=1.39; rho2=1.16; T = 32; rho = (rho2-rho1)/(T2-T1) * (T-T1) + rho1;
- Use MATLAB's built-in interpolation tool
Ti = [-9.7 80]; rhoi = [1.39 1.16]; rho = interp1( Ti, rhoi, 32 );
All three of these methods provide exactly the same answer. However, using MATLAB's interp1 function allows you to use a vector for the points that you want to interpolate to. For example, if we wanted the density at 30, 50, 70, 10, and 110 °F, we could accomplish this by:
Ti = [-9.7 80 170 ];
rhoi = [1.39 1.16 0.995];
T = 30:20:110;
rho = interp1( Ti, rhoi, T );
Implementation in Excel
|
Polynomial Interpolation
Polynomial interpolation is a simple extension of linear interpolation. In general, an nth degree polynomial is given asIf n=1 then we recover a first-degree polynomial, which is linear. The formula gives .
In general, if we want to interpolate a set of data using an nth degree polynomial, then we must determine n+1 coefficients, . Therefore, we require n+1 points to interpolate using an nth degree polynomial.
The equations that must be solved are given as
Solving these equations provides the values for the coefficients, . Once we know these coefficients, we can evaluate the polynomial at any point.
Example
Using a third degree polynomial and the data in the table above, approximate the density of air at 50 °F and 300 °F.
We require 4 points to fit a third degree polynomial, . The equations to be solved are:
Once we have solved these equations we have the polynomial coefficients and we can then evaluate the resulting polynomial at any temperature that we wish.
We still must choose what 4 points in the table to use in the above equations. We will choose the 4 points in the table that are closest to the temperature that we wish to interpolate to. Note that if we want to evaluate the density at different temperatures, we may want to use different points in the table to determine the coefficients!
Our procedure to perform the interpolation is:
- Determine which points we need to use.
- Set up and solve the linear system to obtain the polynomial coefficients .
- Evaluate the polynomial to obtain the temperature.
Implementation in Matlab
The MATLAB code to obtain the density at 50 °F is
% Set the data points from the table.
Ti = [ -9.7 80 170 260 350 440 530 ...
620 710 800 890 980 1070 ]';
rhoi=[ 1.39 1.16 0.99 0.87 0.78 0.69 0.63 ...
0.58 0.54 0.50 0.46 0.44 0.41 ]';
% Set the temperature we want to evaluate the density at.
T = 50;
% determine the set of points that we will use to construct
% the interpolant. We want to use points that are closest
% to the temperature that we are interested in.
points = 1:4;
% set up the linear system
A = [ ones(4,1), ones(4,1).*Ti(points), ones(4,1)*Ti(points).^2, ones(4,1)*Ti(points).^3 ];
b = rhoi(points);
% solve for the polynomial coefficients
a = A\b;
% evaluate the density.
rho = a(0) + a(1)*T + a(2)*T^2 + a(3)*T^3;
For T=300 we would probably want to choose a different set of points to determine the polynomial coefficients. Therefore, we would repeat the above with the only change being:
T=300;
points=4:8;
Cubic Spline Interpolation
Cubic spline interpolation uses cubic polynomials to interpolate datasets. The concept is illustrated in the following figure:
The data points are connected with cubic functions, and on each interval the coefficients must be determined. If we have data points then we can define cubic functions, one between each data point. For each spline we have four coefficients to determine. Therefore, we require constraints to determine the coefficients.
The constraints may be summarized in the following table:
Description | Mathematical Statement | Number of Constraints |
---|---|---|
At each data point, two splines meet. Here the splines must be equal to the data values. |
|
|
At the first and last point, the function must equal the data. |
|
|
At each interior point the first derivatives of adjacent splines must be equivalent. | ||
At each interior point, the second derivatives of adjacent splines must be equivalent. | ||
We specify the curvature at the end points. By choosing the curvature to be equal to zero, we obtain a natural spline. |
|
Adding up all of the constraints, we see that we have constraints - precisely the number that we need. We could formulate equations for all of these constraints and solve them all to determine the coefficients for the cubic function on each interval.
Example 1
Using the data in the table above, determine the density of air at the following temperatures: 60, 120, 180, 240, 300, 360, 420, 480, 540 °F.
Implementation in Matlab
Matlab has built-in functions for cubic spline interpolation:
y = interp1( xi, yi, x, 'spline' );
- (xi,yi) are the points at which we have data defined.
- x is the point(s) where we want to interpolate.
- 'spline' tells Matlab to interpolate using cubic splines.
Using this built-in function, we may obtain the interpolated values as follows:
% Set the data points from the table.
Ti = [ -9.7 80 170 260 350 440 530 ...
620 710 800 890 980 1070 ]';
rhoi=[ 1.39 1.16 0.99 0.87 0.78 0.69 0.63 ...
0.58 0.54 0.50 0.46 0.44 0.41 ]';
% set the temperatures that we want to interpolate to.
T = 60:60:540;
% determine the density at the desired temperatures.
rho = interp1( Ti, rhoi, T, 'spline' );
Example 2
Assume that we have four points, . This means that we have three cubic equations that form the cubic spline. We will write these equations as
From the table above, the equations that we must solve are given by
Description | Equations |
---|---|
Splines must pass through the data points. | |
First derivatives must match at data points (where splines meet). | |
Second derivatives must match at data points (where splines meet). | |
Second derivatives are zero at end points |
These equations are linear with respect to the unknown coefficients and may be written in matrix form as
Lagrange Polynomial Interpolation
|
Given points the order Lagrange polynomial that interpolates these function values, are expressed as
where is the Lagrange polynomial given by
and . In other words, for an order interpolation, we require points.
The operator represents the continued product, and is analogous to the operator for summations. For example, .