import matplotlib.pyplot as plt
import numpy as np
9.9. Linear Algebra Lab Solutions#
Put \(A\) into RREF.
Solution:
# Let's set up the matrix and row operation functions
A = np.array([[1,1,1,2],[2,3,1,3],[1,-1,-2,-11]])
# Swap two rows
def swap(matrix, row1, row2):
copy_matrix=np.copy(matrix).astype('float64')
copy_matrix[row1,:] = matrix[row2,:]
copy_matrix[row2,:] = matrix[row1,:]
return copy_matrix
# Multiple all entries in a row by a nonzero number
def scale(matrix, row, scalar):
copy_matrix=np.copy(matrix).astype('float64')
copy_matrix[row,:] = scalar*matrix[row,:]
return copy_matrix
# Replacing a row by the sum of itself and a multiple of another
def replace(matrix, row1, row2, scalar):
copy_matrix=np.copy(matrix).astype('float64')
copy_matrix[row1] = matrix[row1]+ scalar * matrix[row2]
return copy_matrix
A1 = replace(A, 1, 0, -2)
A2 = replace(A1, 2, 0, -1)
A3 = replace(A2, 2, 1, 2)
A4 = scale(A3, 2, -1/5)
A5 = replace(A4, 1, 2, 1)
A6 = replace(A5, 0, 1, -1)
A7 = replace(A6, 0, 2, -1)
A7
array([[ 1., 0., 0., -3.],
[ 0., 1., 0., 2.],
[-0., -0., 1., 3.]])
A7 is in RREF.
Find the solution set of the following system of equations
Solution:
We can change the system of equations into augmented matrix \(B\), and put it into RREF to solve.
B = np.array([[1,-1,3,0],[6,-1,4,1],[3,2,-4,2]])
B1 = replace(B, 1, 2, -2)
B2 = replace(B1, 2, 0, -3)
B3 = swap(B2, 1, 2)
B4 = replace(B3, 2, 1, 1)
B5 = replace(B4, 1, 2, 1)
B6 = scale(B5, 2, -1)
B7 = replace(B6, 1, 2, 14)
B8 = scale(B7, 1, 1/5)
B9 = replace(B8, 0, 1, 1)
B10 = replace(B9, 0, 2, -3)
B10
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 3.],
[-0., -0., 1., 1.]])
B10 is in reduced echelon form, and gives us the solutions \(x = 0\), \(y = 3\) and \(z = 1\).
Determine if the following vectors form a linearly independent or a linearly dependent set:
a.
b.
Solution:
a. A set of vectors form a linearly independent set if a matrix (\(C\)) made up of the vectors has only the trivial solution. Let’s find out the type of solution:
C = np.array([[1,3,4,0],[2,1,2,0],[0,5,3,0]])
C
array([[1, 3, 4, 0],
[2, 1, 2, 0],
[0, 5, 3, 0]])
C1 = replace(C, 1, 0, -2)
C1
array([[ 1., 3., 4., 0.],
[ 0., -5., -6., 0.],
[ 0., 5., 3., 0.]])
C2 = replace(C1, 2, 1, 1)
C2
array([[ 1., 3., 4., 0.],
[ 0., -5., -6., 0.],
[ 0., 0., -3., 0.]])
C2 gives us \(-3 = 0\), which is impossible and means the only solution is the trivial solution. The set of vectors is linearly independent.
b. By theorem 3, a set of 2 vectors is linearly dependent if and only if one is a multiple of another. The second vector is a multiple of the first by a factor of \(-3\), meaning the set of vectors is linearly dependent.
Write the general solution of the linear system corresponding to the augmented matrix \(D\).
Solution:
D = np.array([[2,3,0,1],[1,2,1,1],[2,4,2,2]])
D1 = replace(D, 0, 1, -1)
D1
array([[ 1., 1., -1., 0.],
[ 1., 2., 1., 1.],
[ 2., 4., 2., 2.]])
D2 = replace(D1, 2, 1, -2)
D2
array([[ 1., 1., -1., 0.],
[ 1., 2., 1., 1.],
[ 0., 0., 0., 0.]])
D3 = replace(D2, 1, 0, -1)
D3
array([[ 1., 1., -1., 0.],
[ 0., 1., 2., 1.],
[ 0., 0., 0., 0.]])
D4 = replace(D3, 0, 1, -1)
D4
array([[ 1., 0., -3., -1.],
[ 0., 1., 2., 1.],
[ 0., 0., 0., 0.]])
Since we are left with an empty row, we see that for any \(y \in \mathbb{R}\) we have a unique solution. A general solution for this system is: