Skip to main content
Mathematics LibreTexts

8.4: Practice Curve Fitting Example

  • Page ID
    64250
  • \( \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}}\)

    Consider the following polynomial with constant scalars \(a\), \(b\), and \(c\), that falls on the \(xy\)-plane:

    \[f(x) = ax^2 + bx + c \nonumber \]

    Question

    Is this function linear? Why or why not?

    Assume that we do not know the values of \(a\), \(b\) and \(c\), but we do know that the points (1,2), (-1,12), and (2,3) are on the polynomial. We can substitute the known points into the equation above. For eample, using point (1,2) we get the following equation:

    \[2 = a1^2 + b1 + c \nonumber \]

    \[\text{or} \nonumber \]

    \[2 = a + b + c \nonumber \]

    Question

    Generate two more equations by substituting points (-1,12) and (2,3) into the above equation:

    Question

    If we did this right, we should have three equations and three unknowns (\(a\),\(b\),\(c\)). Note also that these equations are linear (how did that happen?). Transform this system of equations into two matrices \(A\) and \(b\) like we did above.

    #Put your answer to the above question here.
    from urllib.request import urlretrieve
    
    urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 
                'answercheck.py');
    from answercheck import checkanswer
    
    checkanswer.matrix(A,'1896041ded9eebf1cba7e04f32dd1069');
    from answercheck import checkanswer
    
    checkanswer.matrix(b,'01e594bb535b4e2f5a04758ff567f918');
    Question

    Write the code to solve for \(x\) (i.e., (\(a\),\(b\),\(c\))) using numpy.

    #Put your answer to the above question here.
    from answercheck import checkanswer
    
    checkanswer.vector(x,'1dab22f81c2c156e6adca8ea7ee35dd7');
    Question

    Given the value of your x matrix derived in the previous question, what are the values for \(a\), \(b\), and \(c\)?

    #Put your answer to the above question here.
    a = 0
    b = 0
    c = 0

    Assuming the above is correct, the following code will print your 2nd order polynomial and plot the original points:

    import numpy as np
    import matplotlib.pylab as plt
    x = np.linspace(-3,3)
    y = a*x**2 + b*x + c
    
    #plot the function. (Transpose is needed to make the data line up).
    plt.plot(x,y.transpose())
    
    #Plot the original points
    plt.scatter(1, 2)
    plt.scatter(-1, 12)
    plt.scatter(2, 3)
    plt.xlabel('x-axis')
    plt.ylabel('y-axis')
    Question

    The following program is intended to take four points as inputs (\(p1\), \(p2\), \(p3\), \(p4\) \( \in R^2 \)) and calculate the coefficients \(a\), \(b\), \(c\), and \(d\) so that the graph of \(f(x) = ax^3 + bx^2 + cx + d\) passes smoothly through the points. Test the function with the following points (1,2), (-1,6), (2,3), (3,2) as inputs and print the values for \(a\), \(b\), \(c\), and \(d\).

    def fitPoly3(p1,p2,p3,p4):
        A = np.matrix([[p1[0]**3, p1[0]**2, p1[0], 1],
                       [p2[0]**3, p2[0]**2, p2[0], 1],
                       [p3[0]**3, p3[0]**2, p3[0], 1],
                       [p4[0]**3, p4[0]**2, p4[0], 1]])
        
        b = np.matrix([p1[1],p2[1],p3[1],p4[1]]).T
    
        X = np.linalg.solve(A, b)
        a = X[0]
        b = X[1]
        c = X[2]
        d = X[3]
        #Try to put your figure generation code here for the next question 
        #####Start your code here #####
        
        
        #####End of your code here#####       
        return (a,b,c,d)
    #put your answer to the above question here
    Question

    Modify the above fitpoly3 function to also generate a figure of the input points and the resulting polynomial in range x=(-3,3).

    # Put the answer to the above question above or copy and paste the above function and modify it in this cell. 
    Question

    Give any four \(R^2\) input points to fitPoly3, is there always a unique solution? Explain your answer.


    This page titled 8.4: Practice Curve Fitting Example is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.