1. How to write a row vector? For example: \(\vec{a}=(1,2,3)\)
#library(matlib)
a<- matrix(c(1,2,3))
a
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
2. How to write a column vector in R? For example \(\vec{c}=[9,7,6]\)
c<- c(9,7,6)
c
## [1] 9 7 6
3. Can you transpose of \(\vec{a}=(1,2,3)\) ? What happens if you take the transpose twice?
t(a)
## [,1] [,2] [,3]
## [1,] 1 2 3
2*t(a)
## [,1] [,2] [,3]
## [1,] 2 4 6
4. Find the followings: A. Constant multiple of a vector: \(7\vec{a}=(1,2,3)\)
7*a
## [,1]
## [1,] 7
## [2,] 14
## [3,] 21
B. Addition of two vectors \(\vec{a}=(1,2,3)\) , \(\vec{b}=(4,5,6)\)
a<-c(1,2,3)
b<-c(4,5,6)
a+b
## [1] 5 7 9
5. Create a sytem of linear equations in R: A. Create the coefficient matrix A and the column vector b. B.Try the command in R:
library(matlib)
## Warning: package 'matlib' was built under R version 3.5.3
A1 <- matrix(c(1, 2, -1, 2), 2, 2)
b1 <- c(2,1)
showEqn(A1, b1)
## 1*x1 - 1*x2 = 2
## 2*x1 + 2*x2 = 1
6. Can you find the rank of the matrix?
c(R(A1), R(cbind(A1,b1)))
## [1] 2 2
7. Is the system of linear equations consistent?
all.equal(R(A1), R(cbind(A1,b1)))
## [1] TRUE
8. Can you graph the system of linear equation? Use the plotEqn() command.
plotEqn(A1,b1)
## x1 - 1*x2 = 2
## 2*x1 + 2*x2 = 1
9. Solve the system on linear equation and print the answers in fraction format.
Solve(A1, b1, fractions = TRUE)
## x1 = 5/4
## x2 = -3/4
10. Solve the sytem of linear equations with three unknowns.
A2 <- matrix(c(2, 1, -1,
3, 1, -2,
2, -1, -2), 3, 3, byrow=TRUE)
colnames(A2) <- paste0('x', 1:3)
b2 <- c(8, 11, 3)
showEqn(A2, b2)
## 2*x1 + 1*x2 - 1*x3 = 8
## 3*x1 + 1*x2 - 2*x3 = 11
## 2*x1 - 1*x2 - 2*x3 = 3
11. Solve the system of linear equation. Check the answers from https://www.wolframalpha.com or solve by yourself. Are you getting the same answer from R too?
solve(A2, b2)
## x1 x2 x3
## 2 3 -1
solve(A2) %*% b2
## [,1]
## x1 2
## x2 3
## x3 -1
12. Plot the system of linear equations with three unknowns in 3D. Use the command plotEqn3d().
plotEqn3d(A2,b2, xlim=c(0,4), ylim=c(0,4))
13. Consider the same 3x3 matrix A1. Find the determinant of that matrix. If det(A1) does not equal 0 then find the inverse of A1.
det(A2) %*% b2
## [,1] [,2] [,3]
## [1,] -8 -11 -3
inv(A2) %*% b2
## [,1]
## [1,] 2
## [2,] 3
## [3,] -1
14.
A2*inv(A2)
## x1 x2 x3
## [1,] 8 -3 -1
## [2,] -6 2 2
## [3,] 10 4 -2
inv(A2)*A2
##
## [1,] 8 -3 -1
## [2,] -6 2 2
## [3,] 10 4 -2