Difference between revisions of "Interpolation"
Line 1: | Line 1: | ||
− | |||
− | |||
== Linear Interpolation == | == Linear Interpolation == | ||
+ | A linear function may be written as | ||
+ | <math>f(x) = a_0 + a_1 x.</math> | ||
+ | Given any two data points, <math>( x_1, y_1 )</math>, and <math>(x_2,y_2)</math>, we can determine a linear function that exactly passes through these points. We can do this by solving for <math>a_0</math> and <math>a_1</math>. Since there are two unknowns we require two equations. They are: | ||
+ | <center><math>\begin{align} | ||
+ | y_1 &= a_0 + a_1 x_1 \\ | ||
+ | y_2 &= a_0 + a_1 x_2 | ||
+ | \end{align}</math></center> | ||
+ | These may be written in [[LinearAlgebra#Linear_Systems_of_Equations|matrix form]] as | ||
+ | <center><math> | ||
+ | \left[ \begin{array}{cc} 1 & x_1 \\ 1 & x_2 \end{array} \right] | ||
+ | \left( \begin{array}{c} a_0 \\ a_1 \end{array} \right) | ||
+ | = | ||
+ | \left( \begin{array}{c} y_1 \\ y_2 \end{array} \right) | ||
+ | </math></center> | ||
+ | Given values for <math>( x_1, y_1 )</math>, and <math>(x_2,y_2)</math>, these equations may be easily [[Linear_Systems_in_Matlab|solved in MATLAB]]. But since they are so simple, we can easily by hand to obtain a general solution, | ||
+ | <center><math>\begin{align} | ||
+ | a_0 &= \frac{x_1 y_2 -x_2 y_1}{x_1-x_2} \\ | ||
+ | a_1 &= \frac{y_1-y_2}{x_1-x_2} | ||
+ | \end{align}</math></center> | ||
+ | This gives the coefficients, which may then be substituted into the original equation <math>f(x) = a_0 + a_1 x</math> and simplified to obtain | ||
+ | <center><math> | ||
+ | f(x) = \left( \frac{y_2-y_1}{x_2-x_1} \right) \left( x-x_1 \right) + y_1. | ||
+ | </math></center> | ||
+ | This is a very convenient equation for performing linear interpolation. | ||
=== Example === | === Example === | ||
+ | The density of air at various temperatures and atmospheric pressure is given in the following table | ||
+ | |||
+ | {| border="1" cellpadding="3" cellspacing="0" align="center" style="text-align:center" | ||
+ | |- | ||
+ | ! 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/m<sup>3</sup>) | ||
+ | | -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 <math>\rho(T)=a_0 + a_1 T</math>. | ||
+ | We define <math>(T_1,\rho_1)=(-9.7,1.39)</math> and <math>(T_2,\rho_2)=(80,1.161)</math>. Now we can calculate | ||
+ | |||
+ | :<math>\begin{align} | ||
+ | \rho(32) &\approx \frac{\rho_2 -\rho_1}{T_2-T_1}\left( T - T_1 \right) + \rho_1 \\ | ||
+ | &= \frac{1.16-1.39}{-9.7-80} \left( 32 + 9.7 \right) + 1.39 \\ | ||
+ | &= 1.28. | ||
+ | \end{align}</math> | ||
==== Implementation in Matlab ==== | ==== Implementation in Matlab ==== | ||
+ | There are three ways to do interpolation in MATLAB. | ||
+ | <ol> | ||
+ | |||
+ | <li> Solve the linear system. | ||
+ | <source lang="matlab"> | ||
+ | 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; | ||
+ | </source> | ||
+ | |||
+ | <li> Use the general equation we derived above: | ||
+ | <source lang="matlab"> | ||
+ | T1=-9.7; T2-80; | ||
+ | rho1=1.39; rho2=1.16; | ||
+ | T = 32; | ||
+ | rho = (rho2-rho1)/(T2-T1) * (T-T1) + rho1; | ||
+ | </source> | ||
+ | |||
+ | <li> Use MATLAB's built-in interpolation tool | ||
+ | <source lang="matlab"> | ||
+ | Ti = [-9.7 80]; | ||
+ | rhoi = [1.39 1.16]; | ||
+ | rho = interp1( Ti, rhoi, 32 ); | ||
+ | </source> | ||
+ | </ol> | ||
+ | |||
+ | 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: | ||
+ | <source lang="matlab"> | ||
+ | Ti = [-9.7 80 170 ]; | ||
+ | rhoi = [1.39 1.16 0.995]; | ||
+ | T = 30:20:110; | ||
+ | rho = interp1( Ti, rhoi, T ); | ||
+ | </source> | ||
==== Implementation in Excel ==== | ==== Implementation in Excel ==== | ||
+ | {{Stub}|section}} | ||
Line 13: | Line 92: | ||
== Polynomial Interpolation == | == Polynomial Interpolation == | ||
+ | Polynomial interpolation is a simple extension of linear interpolation. In general, an <var>n<sup>th</sup></var> degree polynomial is given as <center><math>f(x)=\sum_{i=0}^n a_i x^i.</math></center> | ||
+ | |||
+ | If <var>n=1</var> then we recover a first-degree polynomial, which is linear. The formula gives <math>f(x)=\sum_{i=0}^1 a_i x^i = a_0 + a_1 x</math>. | ||
+ | |||
+ | In general, if we want to interpolate a set of data using an <var>n<sup>th</sup></var> degree polynomial, then we must determine <var>n+1</var> coefficients, <math>a_0 \ldots a_n</math>. Therefore, we require <var>n+1</var> points to interpolate using an <var>n<sup>th</sup></var> degree polynomial. | ||
+ | |||
+ | The equations that must be solved are given as | ||
+ | <center><math> | ||
+ | \left[\begin{array}{ccccc} | ||
+ | 1 & x_{1} & x_{1}^2 & \cdots & x_{1}^{n}\\ | ||
+ | 1 & x_{2} & x_{2}^2 & \cdots & x_{2}^{n}\\ | ||
+ | \vdots & \vdots & \vdots & \cdots & \vdots \\ | ||
+ | 1 & x_{n+1} & x_{n+1}^2 & \cdots & x_{n+1}^{n}\end{array}\right]\left(\begin{array}{c} | ||
+ | a_{0}\\ | ||
+ | a_{1}\\ | ||
+ | \vdots\\ | ||
+ | a_{n}\end{array}\right) | ||
+ | = | ||
+ | \left(\begin{array}{c} | ||
+ | y_{1}\\ | ||
+ | y_{2}\\ | ||
+ | \vdots\\ | ||
+ | y_{n+1}\end{array}\right) | ||
+ | </math></center> | ||
+ | |||
+ | Solving these equations provides the values for the coefficients, <math>a_0 \ldots a_n</math>. Once we know these coefficients, we can evaluate the polynomial at any point. | ||
=== Example === | === Example === | ||
+ | |||
+ | Using a third degree polynomial, approximate the density of air at the following temperatures: 50, 300, 500 °F. | ||
+ | |||
+ | We require 4 points to fit a third degree polynomial, <math>a_0 \, a_1 \, a_2 \, a_3</math>. We will choose the 4 points closest to the temperature that we wish to interpolate to. | ||
==== Implementation in Matlab ==== | ==== Implementation in Matlab ==== | ||
+ | Our procedure is: | ||
+ | # Determine which points we need to use. | ||
+ | # Set up and solve the linear system to obtain the polynomial coefficients <math>a_0 \ldots a_3</math>. | ||
+ | # Evaluate the polynomial to obtain the temperature. | ||
+ | <ul><ul> | ||
+ | <source lang="matlab"> | ||
+ | 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 ]'; | ||
+ | T = 50; | ||
+ | points = 1:4; | ||
+ | A = [ ones(4,1), ones(4,1).*Ti(points), ones(4,1)*Ti(points).^2, ones(4,1)*Ti(points).^3 ]; | ||
+ | b = rhoi(points); | ||
+ | a = A\b; | ||
− | + | rho = a(0) + a(1)*T + a(2)*T^2 + a(3)*T^3; | |
+ | </source> | ||
+ | </ul> | ||
+ | For <var>T=300</var> we would repeat the above with the only change being: | ||
+ | <ul><source lang="matlab"> | ||
+ | T=300; | ||
+ | points=4:8; | ||
+ | </source></ul> | ||
+ | For <var>T=500</var> we would change | ||
+ | <ul><source lang="matlab"> | ||
+ | T=500; | ||
+ | points=5:9; | ||
+ | </source></ul> | ||
+ | </ul> | ||
== Cubic Spline Interpolation == | == Cubic Spline Interpolation == | ||
+ | {{Stub|section}} | ||
=== Example === | === Example === |
Revision as of 15:09, 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
{{Stub}|section}}
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, approximate the density of air at the following temperatures: 50, 300, 500 °F.
We require 4 points to fit a third degree polynomial, . We will choose the 4 points closest to the temperature that we wish to interpolate to.
Implementation in Matlab
Our procedure 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.
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 ]';
T = 50;
points = 1:4;
A = [ ones(4,1), ones(4,1).*Ti(points), ones(4,1)*Ti(points).^2, ones(4,1)*Ti(points).^3 ];
b = rhoi(points);
a = A\b;
rho = a(0) + a(1)*T + a(2)*T^2 + a(3)*T^3;
For T=300 we would repeat the above with the only change being:
T=300;
points=4:8;
For T=500 we would change
T=500;
points=5:9;
Cubic Spline Interpolation
|
Example
Implementation in Matlab
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, .