12.4: Interpolation
- Page ID
- 135907
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)In many real problems, we do not know the value of a quantity at every possible point. Instead, we collect measurements at a limited number of points. Interpolation is the process of estimating new values inside the range of known data points.
For example, suppose you measure the temperature in a room every hour. If you measured at 11:00 AM and 12:00 PM, but you want to estimate the temperature at 11:30 AM, interpolation is a reasonable approach.
Interpolation vs. Extrapolation
Interpolation estimates values inside the range of your data. Extrapolation estimates values outside the range of your data. Extrapolation is usually riskier because the pattern may not continue beyond the measured data.
Example Data: Office Temperature
Assume we measured the temperature in an office from 8:00 AM to 5:00 PM. The time is stored using a 24-hour clock, so 8 means 8:00 AM and 17 means 5:00 PM.
|
Time |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
|
Temp (F) |
43 |
58 |
63 |
75 |
80 |
82 |
81 |
77 |
75 |
71 |
Linear Interpolation with interp1
The simplest interpolation method is linear interpolation. MATLAB connects each pair of neighboring data points with a straight line and then estimates values along those lines. This is also called piecewise linear interpolation.
yNew = interp1(xData, yData, xNew);
Here, xData contains the known x-values, yData contains the known y-values, and xNew contains the new x-values where we want estimates.
Linear Inrterpolation.
clc; clear; clf;
% Known data points
timePoints = 8:1:17;
tempPoints = [43 58 63 75 80 82 81 77 75 71];
% Plot original measurements
plot(timePoints, tempPoints, '-o', 'MarkerFaceColor', 'g')
hold on
% Estimate temperature at 11:30 AM and 2:30 PM
newTimes = [11.5 14.5];
newTemps = interp1(timePoints, tempPoints, newTimes);
% Plot the estimated points
plot(newTimes, newTemps, 'bd')
xlabel('Time of Day')
ylabel('Temperature (F)')
title('Office Temperature: Linear Interpolation')
grid on
axis([8 17 40 90])
hold off
Solution

The blue diamond markers show the estimated temperatures at 11:30 AM and 2:30 PM. Because this method uses straight-line sections, the result is easy to understand and often good enough for simple data sets.
The xData and yData arrays must match. If you have 10 time values, you must also have 10 temperature values.
Cubic Spline Interpolation with spline
Linear interpolation is simple, but real data sometimes changes more smoothly than straight-line segments. The spline function uses cubic polynomials to estimate a smooth curve through the data points.
yNew = spline(xData, yData, xNew);
Cubic spline interpolation is widely used in computer graphics, image processing, animation, and motion planning because it creates smooth transitions between points.
Linear Inrterpolation.
clc; clear; clf;
% Known data points
timePoints = 8:1:17;
tempPoints = [43 58 63 75 80 82 81 77 75 71];
% Plot original measurements
plot(timePoints, tempPoints, 'o', 'MarkerFaceColor', 'g')
hold on
% Create many time values to draw a smooth curve
smoothTimes = linspace(8, 17, 50);
smoothTemps = spline(timePoints, tempPoints, smoothTimes);
% Plot spline curve
plot(smoothTimes, smoothTemps, '-r')
% Estimate temperature at selected times
newTimes = [11.5 14.5];
newTemps = spline(timePoints, tempPoints, newTimes);
plot(newTimes, newTemps, 'bd')
xlabel('Time of Day')
ylabel('Temperature (F)')
title('Office Temperature: Cubic Spline Interpolation')
grid on
axis([8 17 40 90])
hold off
Solution

The green circle markers represent the known data points.
The red line represents the estimated temperatures for 50 time points between 8:00 AM and 5:00 PM.
The blue diamond markers show the estimated temperatures at 11:30 AM and 2:30 PM.
Common Mistake
Do not assume that a smoother curve is always more accurate. A spline may look nicer, but the accuracy depends on how the real system behaves.

