In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asyncronously, turn in your assignment using D2L no later than 11:59pm on the day of class. See links at the end of this document for access to the class timeline for your section.

In-Class Assignment: Linear Algebra and Python

Simple xy graph with a high order polynomial representing a curve. In this assignment we will be covering curve fitting an the figure is just intended as a viaual motivation


1. Quiz Review

Today we will go over our quiz from last week.

# Here are some libraries you may need to use
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
import math
sym.init_printing()

2. Solving Systems of Linear Equations

Remember the following set of equations from the mass weight example:

Image showing two balanced beams, each with three weights. In the top beam is unknown weight A is a distance of 40 to the left of the fulcrum, unknown weight B is a distance of 15 to the left of the fulcrum and a weight of 2 is 50 to the right of the fulcrum. In the bottom beam is the same unknown weights.  Weight A is now a distance of 50 to the right of the fulcrum, weight B is a distance of 25 to the left of the fulcrum and the weight of 2 is a distance of 25 to the right of the fulcrum. $$40A + 15B = 100$$ $$25B = 50 + 50A$$

As you know, the above system of equations can be written as an Augmented Matrix in the following form:

$$ \left[ \begin{matrix} 40 & 15 \\ -50 & 25 \end{matrix} \, \middle\vert \, \begin{matrix} 100 \\ 50 \end{matrix} \right] $$

**QUESTION:** Split apart the augmented matrix ($ \left[ \begin{matrix} A \end{matrix} \, \middle\vert \, \begin{matrix} b \end{matrix} \right] $) into it's left size ($2 \times 2$) matrix $A$ and it's right ($2x1$) matrix $b$. Define the matrices $A$ and $b$ as numpy matrices:

#Put your code here
from answercheck import checkanswer

checkanswer.matrix(A,'f5bfd7c52824d5ac580d0ce1ab98fe68');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-dff0ef3a714c> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(A,'f5bfd7c52824d5ac580d0ce1ab98fe68');

NameError: name 'A' is not defined
from answercheck import checkanswer

checkanswer.matrix(b,'c760cd470439f5db82bb165edf4dc3f8');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-8570aaa38044> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(b,'c760cd470439f5db82bb165edf4dc3f8');

NameError: name 'b' is not defined

**QUESTION:** solve the above system of equations using the np.linalg.solve function and store the value in a vector named x:

# Put your code to the above question here
from answercheck import checkanswer

checkanswer.vector(x,'fc02fe6d0577c4452ee70252e1e17654');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-36be936a4706> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(x,'fc02fe6d0577c4452ee70252e1e17654');

NameError: name 'x' is not defined

3. Practice - Curve Fitting Example

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

$$f(x) = ax^2 + bx + c$$

**QUESTION:** Is this function linear? Why or why not?

Put your answer to the above question here.

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$$$$\text{or}$$$$2 = a + b + c$$

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

Put your answer to the above question here. (See if you can use latex as I did above)

**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 answercheck import checkanswer

checkanswer.matrix(A,'1896041ded9eebf1cba7e04f32dd1069');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-129e722a1d05> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(A,'1896041ded9eebf1cba7e04f32dd1069');

NameError: name 'A' is not defined
from answercheck import checkanswer

checkanswer.matrix(b,'01e594bb535b4e2f5a04758ff567f918');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-3033aef4f89b> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(b,'01e594bb535b4e2f5a04758ff567f918');

NameError: name 'b' is not defined

**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');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-f0ec53df8366> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(x,'1dab22f81c2c156e6adca8ea7ee35dd7');

NameError: name 'x' is not defined

**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:

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')
Text(0, 0.5, '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.

Put your answer to the above question here.


Congratulations, we're done!

If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending remote, turn in your assignment using D2L.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.