Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

8.4: Practice Curve Fitting Example

( \newcommand{\kernel}{\mathrm{null}\,}\)

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

f(x)=ax2+bx+c

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=a12+b1+c

or

2=a+b+c

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.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your answer to the above question here.
#Put your answer to the above question here.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

from urllib.request import urlretrieve

urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 
            'answercheck.py');
from urllib.request import urlretrieve

urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 
            'answercheck.py');

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

from answercheck import checkanswer

checkanswer.matrix(A,'1896041ded9eebf1cba7e04f32dd1069');
from answercheck import checkanswer

checkanswer.matrix(A,'1896041ded9eebf1cba7e04f32dd1069');

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

from answercheck import checkanswer

checkanswer.matrix(b,'01e594bb535b4e2f5a04758ff567f918');
from answercheck import checkanswer

checkanswer.matrix(b,'01e594bb535b4e2f5a04758ff567f918');
Question

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

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your answer to the above question here.
#Put your answer to the above question here.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

from answercheck import checkanswer

checkanswer.vector(x,'1dab22f81c2c156e6adca8ea7ee35dd7');
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?

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your answer to the above question here.
a = 0
b = 0
c = 0
#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:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

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')
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 R2) and calculate the coefficients a, b, c, and d so that the graph of f(x)=ax3+bx2+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.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

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)
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)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#put your answer to the above question here
#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).

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

# Put the answer to the above question above or copy and paste the above function and modify it in this cell. 
# 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 R2 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.

Support Center

How can we help?