1. How to write a row (default) vector? For example: \(\vec{a}\) = (1,2,3).
veca = c(1,2,3)
veca
## [1] 1 2 3
  1. How to write a column vector in R? For example:

\[\vec{c} = \begin{bmatrix} 9 \\ 7 \\ 6 \end{bmatrix}\]

vecc = c(9,7,6)
dim(vecc) = c(3,1)
vecc
##      [,1]
## [1,]    9
## [2,]    7
## [3,]    6
  1. Can you take transpose of \(\vec{a}\)? What happens if you take the transpose twice?
t(veca)
##      [,1] [,2] [,3]
## [1,]    1    2    3
t(t(veca))
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3

The function t() takes the vector back to its original matrix, but if you use it twice, it takes the row and puts it into a column vector instead. 3 times would take it back to its original horizontal.

  1. Find the followings:
  1. Constant Multiple of a vector: \(7\vec{a}\)
7*veca
## [1]  7 14 21
  1. Addition of two vectors \(\vec{a} = (1,2,3), \vec{b} = (4,5,6)\)
vecb = c(4,5,6)
veca+vecb
## [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 coefficient matrix A and the column vector \(\vec{b}\) (You may need the library matlib)
A = matrix(c(1,-1,2,2), nrow = 2, ncol = 2, byrow=TRUE)
A
##      [,1] [,2]
## [1,]    1   -1
## [2,]    2    2
b = matrix(c(2,1), nrow = 2, ncol = 1, byrow=TRUE)
b
##      [,1]
## [1,]    2
## [2,]    1
  1. Try the command in R:
#install.packages("matlib")
library(matlib)
showEqn(A,b)
## 1*x1 - 1*x2  =  2 
## 2*x1 + 2*x2  =  1
  1. Can you find rank of the matrix?
c( R(A), R(cbind(A,b)))
## [1] 2 2
  1. Is the system of linear equation \(\begin{bmatrix} A & \vec{b}\end{bmatrix}\)
all.equal( R(A), R(cbind(A,b)))
## [1] TRUE
  1. Can you graph the system of linear equatins? 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.
all.equal(R(A), R(cbind(A,b)))
## [1] TRUE
solve(A,b)
##       [,1]
## [1,]  1.25
## [2,] -0.75
  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 \]

B = matrix(c(2, 1, -1, 3, 1, -2,2, -1, -2), nrow = 3, ncol = 3, byrow=TRUE)
B
##      [,1] [,2] [,3]
## [1,]    2    1   -1
## [2,]    3    1   -2
## [3,]    2   -1   -2
c = matrix(c(8,11,3), nrow=3, ncol=1, byrow=TRUE)
c
##      [,1]
## [1,]    8
## [2,]   11
## [3,]    3
solve(B,c)
##      [,1]
## [1,]    2
## [2,]    3
## [3,]   -1
  1. SOlve the system of linear equation \(A_1\vec{x} = \vec{b}\). Check the answers from https://www.wolframalpha.come or solve by yourself. Are you getting the same answer for R too?

We get the same answer on Wolfram as we do in R, 2, 3, -1:

https://www.wolframalpha.com/input/?i=2+x+%2B+1+y+-+1z+%3D+8,+3+x+%2B1+y+-2z+%3D+11,+2x+-+y+-2+z+%3D+3

  1. Plot the system of linear equations with three unknowns in 3D. Use the command plotEqn3d().
#plotEqn3d(B,c)
  1. Consider the same 3 x 3 matrix \(A_1\). Find the determinant of that matrix. If \(\text{det}(A_1) \neq 0\) then find the inverse of \(A_1\).
det(B)
## [1] -1
inv(B)
##      [,1] [,2] [,3]
## [1,]    4   -3    1
## [2,]   -2    2   -1
## [3,]    5   -4    1
  1. If you have found the inverse of \(A_1\), check \(A_1A_1^{-1} = I_3\) and \(A_1^{-1}A_1 = I_3\).
BI = matrix(c(4, -3, 1, -2, 2, -1,5, -4, -1), nrow = 3, ncol = 3, byrow=TRUE)
BI
##      [,1] [,2] [,3]
## [1,]    4   -3    1
## [2,]   -2    2   -1
## [3,]    5   -4   -1
B
##      [,1] [,2] [,3]
## [1,]    2    1   -1
## [2,]    3    1   -2
## [3,]    2   -1   -2
BI%*%B
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]   -4    2    5
B%*%BI
##      [,1] [,2] [,3]
## [1,]    1    0    2
## [2,]    0    1    4
## [3,]    0    0    5