Link to this document's Jupyter Notebook

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 asynchronously, 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: Determinants

Depiction of Cramer's Rule with two equations and two variables

Image from:http://www.mathnstuff.com/

Agenda for today's class (80 minutes)

  1. (20 minutes) Review Pre-class Assignment
  2. (30 minutes) Algorithm to calculate the determinant
  3. (30 minutes) Using Cramer's rule to solve $Ax=b$

1. Review Pre-class Assignment


2. Algorithm to calculate the determinant

Consider the following recursive algorithm (algorithm that calls itself) to determine the determinate of a $n\times n$ matrix $A$ (denoted $|A|$), which is the sum of the products of the elements of any row or column. i.e.:

$$i\text{th row expansion: } |A| = a_{i1}C_{i1} + a_{i2}C_{i2} + \ldots + a_{in}C_{in} $$$$j\text{th column expansion: } |A| = a_{1j}C_{1j} + a_{2j}C_{2j} + \ldots + a_{nj}C_{nj} $$

where $C_{ij}$ is the cofactor of $a_{ij}$ and is given by:

$$ C_{ij} = (-1)^{i+j}|M_{ij}|$$

and $M_{ij}$ is the matrix that remains after deleting row $i$ and column $j$ of $A$.

Here is some code that tries to implement this algorithm.

The following code generates an $n \times n$ matrix with random values from 0 to 10.
Run the code multiple times to get different matrices:

DO THIS: Use the randomly generated matrix ($A$) to test the above mydet function and compare your result to the numpy.linalg.det function.

QUESTION: Are the answers to mydet and numpuy.linalg.det exactly the same every time you generate a different random matrix? If not, explain why.

Put your answer here

QUESTION: On line 26 of the above code, you can see that algorithm calls itself. Explain why this doesn't run forever.

Put your answer here


3. Using Cramer's rule to solve $Ax=b$

Let $Ax = b$ be a system of $n$ linear equations in $n$ variables such that $|A| \neq 0$. the system has a unique solution given by:

$$x_1 = \frac{|A_1|}{|A|}, x_2 = \frac{|A_2|}{|A|}, \ldots, x_n = \frac{|A_n|}{|A|}$$

where $A_i$ is the matrix obtained by replacing column $i$ of $A$ with $b$. The following function generates $A_i$ by replacing the $i$th column of $A$ with $b$:

DO THIS: Create a new function called cramersRule, which takes $A$ and $b$ and returns $x$ using the Cramer's rule. Note: Use numpy and NOT mydet to find the required determinants. mydet is too slow.

QUESTION: Test your cramersRule function on the following system of linear equations:

$$ x_1 + 2x_2 = 3$$$$3x_1 + x_2 = -1$$

QUESTION: Verify the above answer by using the np.linalg.solve function:

QUESTION: Test your cramersRule function on the following system of linear equations and verify the answer by using the np.linalg.solve function:

$$ x_1 + 2x_2 +x_3 = 9$$$$ x_1 + 3x_2 - x_3 = 4$$$$ x_1 + 4x_2 - x_3 = 7$$

QUESTION: Cramer's rule is a $O(n!)$ algorithm and the Gauss-Jordan (or Gaussian) elimination is $O(n^3)$. What advantages does Cramer's rule have over elimination?

Put your answer 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 asynchronously, 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.