1. How to write a row (default) vector? For example: \(\vec{a}\) = (1,2,3).
rbind(c(1,2,3))
##      [,1] [,2] [,3]
## [1,]    1    2    3
a <- rbind(c(1,2,3)) 
  1. How to write a column vector in R? For example: \(\vec{c}\) =
cbind(9,7,6)
##      [,1] [,2] [,3]
## [1,]    9    7    6
c <- cbind(9,7,6)
  1. Can you take the transpose of \(\vec{a}\)? What happens if you take the transpose twice?

Yes, you can take the transpose of \(\vec{a}\). The row vector would become a column vector. If you take the transpose twice, \(\vec{a}\) would go back to a row vector.

t(a)
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
t(t(a))
##      [,1] [,2] [,3]
## [1,]    1    2    3
  1. Find the followings:
  1. Constant multiple of a vector 7\(\vec{a}\)
7*a
##      [,1] [,2] [,3]
## [1,]    7   14   21
  1. Addition of two vectors \(\vec{a}\) = (1,2,3), \(\vec{b}\) = (4,5,6)
rbind(c(4,5,6))
##      [,1] [,2] [,3]
## [1,]    4    5    6
b <- rbind(c(4,5,6))

a + b
##      [,1] [,2] [,3]
## [1,]    5    7    9
  1. Create a system of linear equations in R:

x_{1} - x_{2} = 2 2x_{1} + 2x_{2} = 1

  1. Create the coefficent matrix A and the column vector \(\vec{b}\)
A <- matrix(c(1, -1, 2, 2), nrow = 2, ncol = 2, byrow = TRUE)


b <- c(2, 1)
  1. Try the command in R:
library(matlib)
## Warning in rgl.init(initValue, onlyNULL): RGL: unable to open X11 display
## Warning: 'rgl_init' failed, running with rgl.useNULL = TRUE
showEqn(A, b)
## 1*x1 - 1*x2  =  2 
## 2*x1 + 2*x2  =  1
  1. Can you find the rank of the matrix?
c(R(A), R(cbind(A, b)))
## [1] 2 2
  1. Is the system of linear equation [A \(\vec{b}\)] consistent? -Yes, it is.
all.equal(R(A), R(cbind(A, b)))
## [1] TRUE
  1. Can you graph the system of linear equations? Use the plotEqn() command.
plotEqn(A, b)
##   x1 - 1*x2  =  2 
## 2*x1 + 2*x2  =  1

  1. Solve the system of linear equation A\(\vec{x}\) = \(\vec{b}\) and print the answers in fraction format.
solve(A, b)
## [1]  1.25 -0.75
library(MASS)
fractions(solve(A, b))
## [1]  5/4 -3/4
  1. Solve the system of linear equations with three unknowns:

2x_{1} + x_{2} - x_{3} = 8 3x_{1} + x_{2} - 2x_{3} = 11 2x_{1} - x_{2} - 2x_{3} = 3

D <- matrix(c(2, 1, -1, 3, 1, -2, 2, -1, -2), nrow = 3, ncol = 3, byrow = TRUE)
d <- c(8, 11, 3)
solve(D, d)
## [1]  2  3 -1
  1. Solve the system of linear equation A_{1}\(\vec{x}\) = \(\vec{b}\)_{1} ([D, d]). Check the answers from https: //www.wolframalpha.com. Are you getting the same answers from R too?
solve(D)
##      [,1] [,2] [,3]
## [1,]    4   -3    1
## [2,]   -2    2   -1
## [3,]    5   -4    1
solve(D, d)
## [1]  2  3 -1

Yes, I am getting the same results as from the website.

  1. Plot the system of linear equations with three unknowns in 3D. Use the command plotEqn3d().
plotEqn3d(D, d)
  1. Consider the same 3 X 3 matrix A_{1} (D). Find the determinant of that matrix. If det(A_{1}/D) does not equal 0, then find the inverse of A_{1} (D).
inv(D) 
##      [,1] [,2] [,3]
## [1,]    4   -3    1
## [2,]   -2    2   -1
## [3,]    5   -4    1
(1/(inv(D)))
##       [,1]       [,2] [,3]
## [1,]  0.25 -0.3333333    1
## [2,] -0.50  0.5000000   -1
## [3,]  0.20 -0.2500000    1
DI <- cbind(D, diag(3)) 
DI   
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    2    1   -1    1    0    0
## [2,]    3    1   -2    0    1    0
## [3,]    2   -1   -2    0    0    1
library(matlib)
echelon(DI, verbose=TRUE, fractions=TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  2    1   -1    1    0    0  
## [2,]  3    1   -2    0    1    0  
## [3,]  2   -1   -2    0    0    1  
## 
## row: 1 
## 
##  exchange rows 1 and 2 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  3    1   -2    0    1    0  
## [2,]  2    1   -1    1    0    0  
## [3,]  2   -1   -2    0    0    1  
## 
##  multiply row 1 by 1/3 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1  1/3 -2/3    0  1/3    0
## [2,]    2    1   -1    1    0    0
## [3,]    2   -1   -2    0    0    1
## 
##  multiply row 1 by 2 and subtract from row 2 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1  1/3 -2/3    0  1/3    0
## [2,]    0  1/3  1/3    1 -2/3    0
## [3,]    2   -1   -2    0    0    1
## 
##  multiply row 1 by 2 and subtract from row 3 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1  1/3 -2/3    0  1/3    0
## [2,]    0  1/3  1/3    1 -2/3    0
## [3,]    0 -5/3 -2/3    0 -2/3    1
## 
## row: 2 
## 
##  exchange rows 2 and 3 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1  1/3 -2/3    0  1/3    0
## [2,]    0 -5/3 -2/3    0 -2/3    1
## [3,]    0  1/3  1/3    1 -2/3    0
## 
##  multiply row 2 by -3/5 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1  1/3 -2/3    0  1/3    0
## [2,]    0    1  2/5    0  2/5 -3/5
## [3,]    0  1/3  1/3    1 -2/3    0
## 
##  multiply row 2 by 1/3 and subtract from row 1 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0 -4/5    0  1/5  1/5
## [2,]    0    1  2/5    0  2/5 -3/5
## [3,]    0  1/3  1/3    1 -2/3    0
## 
##  multiply row 2 by 1/3 and subtract from row 3 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0 -4/5    0  1/5  1/5
## [2,]    0    1  2/5    0  2/5 -3/5
## [3,]    0    0  1/5    1 -4/5  1/5
## 
## row: 3 
## 
##  multiply row 3 by 5 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0 -4/5    0  1/5  1/5
## [2,]    0    1  2/5    0  2/5 -3/5
## [3,]    0    0    1    5   -4    1
## 
##  multiply row 3 by 4/5 and add to row 1 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0    0    4   -3    1
## [2,]    0    1  2/5    0  2/5 -3/5
## [3,]    0    0    1    5   -4    1
## 
##  multiply row 3 by 2/5 and subtract from row 2 
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  1    0    0    4   -3    1  
## [2,]  0    1    0   -2    2   -1  
## [3,]  0    0    1    5   -4    1
  1. If you have found the inverse of D_{1}, check D_{1}D_{1}^{-1} = I_{3} and D_{1}^{-1}D_{1} = I_{3}.
D%*%inv(D)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
inv(D)%*%D
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1