1, Calculate the dot product u·v where

uv = u1v1 + u2*v2

u <- matrix(c(0.5,0.5), ncol=2)
v <- matrix(c(3,-4), ncol=2)
u <- c(0.5,0.5)
v <- c(3,-4)
dot_product <- u %*% v
dot_product
##      [,1]
## [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.

length_u <- sqrt(sum(u*u))
length_u
## [1] 0.7071068
length_v <- sqrt(sum(v*v))
length_v
## [1] 5

3, what is the linear combination: 3u - 2v?

linear_combination <- (3*u-2*v)
linear_combination
## [1] -4.5  9.5

4, What is the angle between u and v

angle <- acos(dot_product/(length_v * length_u))
angle
##          [,1]
## [1,] 1.712693
as.numeric(angle) * 180 /pi
## [1] 98.1301

Problem set 2

Set up a system of equations with 3 variables and 3 constraints and solve for x. Please write a function in R that will take two variables (matrix A & constraint vector b) and solve using elimination. Your function should produce the right answer for the system of equations for any 3-variable, 3-equation system. You don’t have to worry about degenerate cases and can safely assume that the function will only be tested with a system of equations that has a solution. Please note that you do have to worry about zero pivots, though. Please note that you should not use the built-in function solve to solve this system or use matrix inverses. The approach that you should employ is to construct an Upper Triangular Matrix and then back-substitute to get the solution. Alternatively, you can augment the matrix A with vector b and jointly apply the Gauss Jordan elimination procedure.

elimination_system_equations <- function(A, b){
  gj <- cbind(A, b)

  value <- (A[,1]/(-1*A[1,1]))
  multiply <- matrix(c(1, value[2], value[3], 0, 1, 0, 0, 0, 1), nrow=3, ncol=3) 
  transformation_1 <- multiply %*% gj
  
  value_2 <- -1*(transformation_1[3,2]/transformation_1[2,2])
  multiply_2 <- matrix(c(1, 0, 0, 0, 1, value_2, 0, 0, 1), nrow=3, ncol=3) 
  upper_triangular_matrix <- multiply_2 %*% transformation_1
  
  z = upper_triangular_matrix[3,4]/upper_triangular_matrix[3,3]
  y = (upper_triangular_matrix[2,4]-(upper_triangular_matrix[2,3]*z))/upper_triangular_matrix[2,2]
  x = (upper_triangular_matrix[1,4]-(upper_triangular_matrix[1,3]*z)-(upper_triangular_matrix[1,2]*y))/upper_triangular_matrix[1,1]
    
  result <- rbind(x, y, z)
  
  return (result)
}
matrix_3 <- matrix(
    c(1, 2, -1, 1, -1, -2, 3, 5, 4), 
    nrow=3, 
    ncol=3) 
constraint_3 <- matrix(
    c(1, 2, 6), 
    nrow=3, 
    ncol=1) 

elimination_system_equations(matrix_3, constraint_3)
##         [,1]
## x -1.5454545
## y -0.3181818
## z  0.9545455