Walt Wells - 08.01-08.07.2016
- Using matrix operations, describe the solutions for the following family of equations:
# x + 2y - 3z = 5
# 2x + y - 3z = 13
# -x + y + 2z= -8
M <- matrix(c(1,2,-1,2,1,1,-3,-3,2),3,3)
B <- c(5,13,-8)
M
[,1] [,2] [,3]
[1,] 1 2 -3
[2,] 2 1 -3
[3,] -1 1 2
- Find the inverse of the above 3x3 (non-augmented) matrix.
- Solve for the solution using R.
- Modify the 3x3 matrix such that there exists only one non-zero variable in the solution set.
# a) find inverse of M
Inv <- solve(M)
Inv
[,1] [,2] [,3]
[1,] -0.8333333 1.1666667 0.5
[2,] 0.1666667 0.1666667 0.5
[3,] -0.5000000 0.5000000 0.5
# test
M %*% Inv
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
# b) find the solution
x <- Inv %*% B
x
[,1]
[1,] 7
[2,] -1
[3,] 0
# c) Modify the 3x3 matrix such that there exists only one non-zero variable in the solution set.
# not sure best way to do programmatically, but if we make one col the solution (or = 1), allows for other cols to be 0.
M2 <- matrix(c(1,2,-1,5,13,-8,-3,-3,2),3,3)
M2
[,1] [,2] [,3]
[1,] 1 5 -3
[2,] 2 13 -3
[3,] -1 -8 2
round(solve(M2) %*% B, 4)
[,1]
[1,] 0
[2,] 1
[3,] 0
- Consider the matrix, q=matrix(c(3,1,4,4,3,3,2,3,2),nrow=3). Let b=c(1,4,5). Use Cramer’s rule and R to determine the solution, x, to qx=b, if one exists. Show all determinants.
q=matrix(c(3,1,4,4,3,3,2,3,2),nrow=3)
b=c(1,4,5)
# Cramer's rule
c1 <- c(3,1,4)
c2 <- c(4,3,3)
c3 <- c(2,3,2)
# calc determinants
D <- det(q)
Dx <- det(cbind(b, c2, c3))
Dy <- det(cbind(c1, b, c3))
Dz <- det(cbind(c1, c2, b))
D; Dx; Dy; Dz
[1] 13
[1] 19
[1] -33
[1] 44
#solve
D1 <- Dx / D
D2 <- Dy / D
D3 <- Dz / D
D1; D2; D3
[1] 1.461538
[1] -2.538462
[1] 3.384615
#test
solve(q) %*% b
[,1]
[1,] 1.461538
[2,] -2.538462
[3,] 3.384615