Skip to main content
Library homepage
 
Engineering LibreTexts

16.3: Linear Data Fitting and Interpolation

  • Page ID
    84350
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\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}}\)

    The main commands for this section are polyfit, polyval and interp1. Let’s begin with linear regression.

    Example 9.1.1: Linear Regression

    Plot a set of data:

    >> x=0:5;y=[12,10,9,6,2,0];
    >> plot(x,y,'o')

    clipboard_ee03763075158a9932e1c2f40d940b2b4.png

    The data certainly seems to have a (downward) linear trend. We can find the line of best fit as follows:
    >>coeffs=polyfit(x,y,1);
    >>besty=coeffs(1)*x+coeffs(2);

    The command polyfit returns the matrix [-2.4857 12.7143] of the slope and y-intercept of the line of best fit. The y values of this line correspond-
    ing to x (from 0 to 5) are stored in the variable
    besty. We plot them together using:

    >>plot(x,y,'o',x,besty)

    clipboard_e5c3cf7326ad558eecb662082d36cd67a.png

    We often use regression lines to guess y values for x values that are not included in the data set. For this, we use interpolation.

    Example 9.1.2: Interpolation

    Using the data from example 38, we would like to know the y value corresponding to the x value 3.5. For this, we use the command interp1 (that’s the number one on the end, not a lowercase for the letter L).

    >> interp1(x,y,3.5,'linear')
    ans =
    4

    We plot these together with the interpolated point indicated with a red tri-
    angle.

    plot(x,y,'o',x,besty,3.5,4,'r>')

    clipboard_e24ad79b49e49bb5a29e15164ee52179f.png

    Note that if you try to use the interp1 function for data that is outside the data set, you will get an error since you are trying to “extrapolate” instead of interpolate.

    >>interp1(x,y,10.5,'linear')

    gives

    ans =
    NaN

    This indicates that the answer is NaN or “Not a Number” (look it up in the Help files).


    This page titled 16.3: Linear Data Fitting and Interpolation is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Troy Siemers (APEX Calculus) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.