PROBLEM SET 1

(1) Calculate the dot product u.v where u = [0.5; 0.5] and v = [3; −4]

Multiply corresponding elements of each vector.

u <- c(0.5, 0.5)
v <- c(3, -4)
u.v <- sum(u*v)
u.v
## [1] -0.5

(2) 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 definition.

Take the square root of the sum of the elements squared.

u_length <- sqrt(sum(u**2))
u_length
## [1] 0.7071068
v_length <- sqrt(sum(v**2))
v_length
## [1] 5

(3) What is the linear combination: 3u − 2v?

Multiply each term by the constant and add the results.

lc <- sum(3*u - 2*v)
lc
## [1] 5

(4) What is the angle between u and v?

Make use of the calculations up to this point to calculate angle = arccos[(xa * xb + ya * yb) / (√(xa2 + ya2) * √(xb2 + yb2))]

angle <- acos( sum(u*v) / (u_length * v_length) )
angle
## [1] 1.712693

PROBLEM SET 2

We’re going to zero out matrix positions below the diagonal, creating an upper triangular matrix, then we’re going to back substitute to solve for all x values.

ut_soln <- function(A,b){
  
  #Create an augmented matrix, Aug_A, based on input matrix A and vector b.
  Aug_A <- cbind(A,b)

  #Zero out matrix position 2,1.
  row1 <- Aug_A[1,]
  row2 <- Aug_A[2,]
  Aug_A[2,] <- row2 - row1 * (Aug_A[2, 1] / Aug_A[1, 1]) 
  
  #Zero out matrix position 3,1. Reset rows prior to operations.
  row1 <- Aug_A[1,]
  row3 <- Aug_A[3,]
  Aug_A[3,] <- row3 - row1 * (Aug_A[3, 1] / Aug_A[1, 1])
  
  #Zero out matrix position 3,2. Reset rows prior to operations.
  row2 <- Aug_A[2,]
  row3 <- Aug_A[3,]
  Aug_A[3,] <- row3 - row2 * (Aug_A[3, 2] / Aug_A[2, 2])
  
  
  #Solve for x values and back substitute.
  x3 <- Aug_A[3, 4] / Aug_A[3, 3]
  x2 <- (Aug_A[2, 4] - Aug_A[2, 3] * x3) / Aug_A[2, 2]
  x1 <- (Aug_A[1, 4] - Aug_A[1, 3] * x3 - Aug_A[1, 2] * x2) / Aug_A[1, 1]
  x <- c(x1, x2, x3)
  
  #Return our x vector with 2 decimal places.
  return(round(x, 2))
  
}

#Test the function with the provided system.
v <- c(1, 2, -1, 1, -1, -2, 3, 5, 4)
m <- matrix(v, nrow = 3, ncol = 3)
b <- c(1, 2, 6)

ut_soln(m, b)
##     b     b     b 
## -1.55 -0.32  0.95