Author: Romerl Elizes

Problem Set 1

  1. Calculate the dot product u:v where u = [0.5; 0.5] and v = [3; 4]
u <- c(0.5,0.5)
v <- c(3,4)
dot <- u %*% v
dot
##      [,1]
## [1,]  3.5

Answer: 3.5 Reference: [DOT]

  1. What are the lengths of u and v? Please note that the mathematical notion of the length of a vector is not the same as a computer science de nition.
len_u <- sqrt(sum(u*u))
len_v <- sqrt(sum(v*v))
len_u
## [1] 0.7071068
len_v
## [1] 5

Answer: length of u is 0.7071068 and length of v is 5

  1. What is the linear combination: 3u - 2v?
(3*u)-(2*v)
## [1] -4.5 -6.5

Answer: -4.5 -6.5

  1. What is the angle between u and v?
acos( sum(u*v) / (sqrt(sum(u^2))*sqrt(sum(v^2))) )
## [1] 0.1418971
  norm.u <- norm(u,type="2")
  norm.v <- norm(v,type="2")
  theta <- acos(dot / (norm.u * norm.v))
  as.numeric(theta)
## [1] 0.1418971

Answer: 0.1418971 Reference: [ANG]

Problem Set 2

My solution approach was using brute force. The algorithm is not robust, but it does solve the problem set. I used 4 examples to make sure the problem and available solution were expected. The basic algorithm is highlighted below using comments.

Algorithm Function

findX <- function(A,b) {
   tempA <- A
   tempb <- b
   
   # PART A - Develop Upper Triangular Matrix
   # Take care of 1 in tempA[1,1]: R1 * 1/A[1,1] => R1
  if (tempA[1,1] != 1) {
     tmpval <- 1/(tempA[1,1])
     tempA[1,] <- tempA[1,] * tmpval
     tempb[1] <- tempb[1] * tmpval
     # DEBUG Print
     # print(tempA)
     # print(tempb)
  }
  # Take care of 0 in tempA[2,1]: -A[2,1] * R1 + R2 => R2
  if (tempA[2,1] != 0) {
     tmpVal2 <- -1 * tempA[2,1]
     tmpRow1 <- tempA[1,] * tmpVal2
     tempA[2,] = tempA[2,] + tmpRow1
     tempb[2] = (tempb[1] * tmpVal2) + tempb[2]
     # DEBUG Print
     # print(tempA)
     # print(tempb)
  }
  # Take care of 0 in tempA[3,1]: -A[3,1] * R1 + R3 => R3
  if (tempA[3,1] != 0) {
     tmpVal2 <- -1 * tempA[3,1]
     tmpRow1 <- tempA[1,] * tmpVal2
     tempA[3,] = tempA[3,] + tmpRow1
     tempb[3] = (tmpVal2 *tempb[1]) + tempb[3]
     # DEBUG Print
     # print(tempA)
     # print(tempb)
  }
  # Take care of 1 in tempA[2,2]: R2 * 1/A[2,2] => R2
  if (tempA[2,2] != 1) {
     tmpval <- 1/(tempA[2,2])
     tempA[2,] <- tempA[2,] * tmpval
     tempb[2] <- tempb[2] * tmpval
     # DEBUG Print
     # print(tempA)
     # print(tempb)
  }
  # Take care of 0 in tempA[3,2]: A[3,2]*R2 - R3 => R3
  if (tempA[3,2] != 0) {
     tmpVal2 <-  tempA[3,2]
     tmpRow1 <- tempA[2,] * tmpVal2
     tempA[3,] = tmpRow1 - tempA[3,]
     tempb[3] = (tmpVal2 * tempb[2]) - tempb[3]
     # DEBUG Print
     # print(tempA)
     # print(tempb)
  }
  # Take care of 1 in tempA[3,3]: R3 * 1/A[3,3] => R3
  if (tempA[3,3] != 1) {
     tmpval <- 1/(tempA[3,3])
     tempA[3,] <- tempA[3,] * tmpval
     tempb[3] <- tempb[3] * tmpval
     # DEBUG Print
     # print(tempA)
     # print(tempb)
  }

  # DEBUG Print
  # print(tempA)
  # print(tempb)
   
  # PART B - Solve for X using back substitution
  tempX3 = tempb[3]
  tempX2 = tempb[2] - (tempX3 * tempA[2,3])
  tempX1 = tempb[1] - (tempX2 * tempA[1,2]) - (tempX3 * tempA[1,3])
  candX = c(tempX1,tempX2,tempX3)

  return(candX)
}

Testing of Algorithm Using 4 Examples

testA1 <- matrix(c(1,1,3,2,-1,5,-1,-2,4), nrow=3, ncol=3, byrow=TRUE)
testb1 <- c(1,2,6)
testX1 <- findX(testA1,testb1)
testX1
## [1] -1.5454545 -0.3181818  0.9545455
testA2 <- matrix(c(1,1,-1,0,1,3,-1,0,-2), nrow=3, ncol=3, byrow=TRUE)
testb2 <- c(9,3,2)
testX2 <- findX(testA2,testb2)
testX2
## [1]  0.6666667  7.0000000 -1.3333333
#Reference: [SOL1]
testA3 <- matrix(c(1,2,2,3,-2,1,2,1,-1), nrow=3, ncol=3, byrow=TRUE)
testb3 <- c(5,-6,-1)
testX3 <- findX(testA3,testb3)
testX3
## [1] -1  2  1
#Reference: [SOL2]
testA4 <- matrix(c(1,3,-2,0,-2,6,3,4,-5), nrow=3, ncol=3, byrow=TRUE)
testb4 <- c(-1,0,11)
testX4 <- findX(testA4,testb4)
testX4
## [1]  6 -3 -1

References

[ANG] Angle between Two Vectors in R. Retrieved from website: https://stackoverflow.com/questions/1897704/angle-between-two-vectors-in-r

[DOT] Matrix Multiplication. Retrieved from website: https://stat.ethz.ch/R-manual/R-devel/library/base/html/matmult.html

[SOL1] Solving a 3x3 System of Equations Using the Inverse. Problem Example and Solution. Retrieved from website: https://www.youtube.com/watch?v=hu6B1d3vvqU

[SOL2] Solving a System 3x3 Using Matrices. Problem Examle and Solution. Retrieved from website: https://www.youtube.com/watch?v=oCygbOvQqtw