Problem Set 1

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

0.5 * 3 + 0.5 * -4 = -0.5

0.5 * 3 + 0.5 * -4 
## [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.

u = [0.5; 0.5] and v = [3; -4]

length(u) = sqrt(0.5^2 + 0.5^2) = 0.7071068 length(v) = sqrt(3^2 + -4^2) = 5

length_u <- sqrt(0.5^2 + 0.5^2)
length_v <- sqrt(3^2 + (-4)^2)
length_u
## [1] 0.7071068
length_v
## [1] 5

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

u = [0.5; 0.5] and v = [3; -4]

3[0.5; 0.5] - 2[3; -4] = [1.5; 1.5] - [6; -8] = [-4.5, 9.5]

u <- c(0.5, 0.5)
v <- c(3, -4)
3 * u
## [1] 1.5 1.5
2 * v
## [1]  6 -8
3 * u - 2 * v
## [1] -4.5  9.5

(4) What is the angle between u and v

u = [0.5; 0.5] and v = [3; -4]

Do product of u.v from (1) is 0.5 * 3 + 0.5 * -4 = -0.5. Lengths of u and v from (2) are 0.7071068 and 5 respectively.

cos theta = (dot product of u.v)/(product of lengths of u and v) cos theta = -0.5 / (0.7071068 * 5) = -0.1414214 theta = 1.712693

cos_theta <- -0.5 / (0.7071068 * 5)
theta <- as.numeric(acos(cos_theta))
theta
## [1] 1.712693

Below is a function I saw from https://stackoverflow.com/questions/1897704/angle-between-two-vectors-in-r The manual calculations I did above has the same answer as the function below.

angle <- function(x, y){
  dot.prod <- x%*%y 
  norm.x <- norm(x,type="2")
  norm.y <- norm(y,type="2")
  theta <- acos(dot.prod / (norm.x * norm.y))
  as.numeric(theta)
}

x <- c(0.5, 0.5)
y <- c(3, -4)
angle(x,y)
## [1] 1.712693

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.

Below are functions to solve system of equations with 3x3 variables.

#Main function that solves system of equations with 3 variables
#A is an AUGMENTED matrix 
my_solve <- function(A){
col_limit <- ncol(A)-1
print(A)
for(j in 1:col_limit) {
  for( i in 1:nrow(A)){
    if(j!=i){
      #make zero
      ref_row = i
      ref_col = j
      ref_number = A[ref_row, ref_col]
      #print(paste("row is ", ref_row, " and col is ", ref_col))
      selected_row <- select_row(A, ref_row, ref_col)
      #print(paste("selected row is ", selected_row))
      if(selected_row < 0){
          print("This function is unable to process this matrix.")
          break
      }
      mult <- get_multiplier(A, selected_row, ref_row, ref_col)
      #print(paste("multiplier is ", mult))
      A[ref_row, ] <-  mult * A[selected_row,] + A[ref_row, ]
      #print(A)
    }
    #j=i
    else{
      m <- (1/A[i,j])
      #print(paste("m is", m))
      A[i, ] <-  m * A[i, ]
    }
  }
}
  return(A)
}

#Selects which row to multiple to zero out a specific position
select_row <- function(A, ref_row, ref_col){
  for(i in 1:nrow(A)){
    #print(paste("looking at row: ", i, " and col ", ref_col))
    #Case when ref_row and current row are the same or the value at A[i, ref_col] is zero.
    if(i==ref_row | A[i,ref_col]==0) next #skip to next iteration
    
    if(ref_col > 1)
      if(A[i,ref_col-1]!= 0) next #skip to next iteration
    
    return(i)
  }
  return(-1)#we did not find a row we can use
}

#Returns the multiplier to be used to generate a zero 
get_multiplier <- function(A, selected_row, ref_row, ref_col){
  mult <- (-1)* A[ref_row, ref_col] * (1/A[selected_row, ref_col])
  return(mult)
}
#multiplies diagonal numbers so that they equal 1

Use my_solve function to solve a system linear equations with 3 variables.

The my_solve function does not do any swapping. Zero pivots were not considered. Not tested with many 3x3 matrices. Written with intention of solving 3x3 matrices that row reduce to identity matrix.

Five augmented matrices were used to test the my_solve function.

Augmented matrix A_test is the test linear equation specified in the homework. The function my_solve produced the expected result.

Augmented matrices A1, A3, A4 have single solutations and gave the same solution as the rref function of the pracma library.

Augmented matrix A2 has infite solutions, and the my_solve function (as expected) did not produce any solution.

#A is an AUGMENTED matrix 
A_test <- matrix(c(1,1,3,1,2,-1,5,2,-1,-2,4,6),nrow=3, ncol=4, byrow = TRUE) # single solution: test reference
A1 <- matrix(c(1,2,2,4,1,3,3,5,2,6,5,6), nrow=3, ncol=4, byrow = TRUE) # single solution
A2 <- matrix(c(2,1,5,10,1,-3,-1,-2,4,-2,6,12), nrow=3, ncol=4, byrow = TRUE) # infinite solution 
A3 <- matrix(c(2,-1,1,3,5,2,-3,1,2,1,-1,2), nrow=3, ncol=4, byrow = TRUE) # single solution
A4 <- matrix(c(1,2,-1,4,2,1,1,-2,1,2,1,2), nrow=3, ncol=4, byrow = TRUE) # single solution

Run my_solve on the 5 augmented matrices.

my_solve(A_test)
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    2   -1    5    2
## [3,]   -1   -2    4    6
##      [,1] [,2] [,3]       [,4]
## [1,]    1    0    0 -1.5454545
## [2,]    0    1    0 -0.3181818
## [3,]    0    0    1  0.9545455
my_solve(A1)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    2    4
## [2,]    1    3    3    5
## [3,]    2    6    5    6
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    2
## [2,]    0    1    0   -3
## [3,]    0    0    1    4
my_solve(A2)
##      [,1] [,2] [,3] [,4]
## [1,]    2    1    5   10
## [2,]    1   -3   -1   -2
## [3,]    4   -2    6   12
## [1] "This function is unable to process this matrix."
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    2    4
## [2,]    0    1    1    2
## [3,]    0    0    0    0
my_solve(A3)
##      [,1] [,2] [,3] [,4]
## [1,]    2   -1    1    3
## [2,]    5    2   -3    1
## [3,]    2    1   -1    2
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0 1.25
## [2,]    0    1    0 3.75
## [3,]    0    0    1 4.25
my_solve(A4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2   -1    4
## [2,]    2    1    1   -2
## [3,]    1    2    1    2
##      [,1] [,2] [,3]      [,4]
## [1,]    1    0    0 -1.666667
## [2,]    0    1    0  2.333333
## [3,]    0    0    1 -1.000000

Compare solutions of my_solve to rref function of the pracma library.

library(pracma)
## 
## Attaching package: 'pracma'
## The following object is masked _by_ '.GlobalEnv':
## 
##     angle
rref(A1)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    2
## [2,]    0    1    0   -3
## [3,]    0    0    1    4
rref(A2)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    2    4
## [2,]    0    1    1    2
## [3,]    0    0    0    0
rref(A3)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0 1.25
## [2,]    0    1    0 3.75
## [3,]    0    0    1 4.25
rref(A4)
##      [,1] [,2] [,3]      [,4]
## [1,]    1    0    0 -1.666667
## [2,]    0    1    0  2.333333
## [3,]    0    0    1 -1.000000
rref(A_test)
##      [,1] [,2] [,3]       [,4]
## [1,]    1    0    0 -1.5454545
## [2,]    0    1    0 -0.3181818
## [3,]    0    0    1  0.9545455