PROBLEM SET 1

  1. Calculate the dot product u.v where u = [0.5; 0.5] and v = [3;−4]
library(geometry)
u <- c(0.5, 0.5)
v <- c(3, -4)
u.v <- dot(u,v,d = NULL)
u.v
## [1] -0.5

\[ u = \left[ \begin{array}{cccc} 0.5 & 0.5 \end{array} \right] v = \left[ \begin{array}{cccc} 3 & -4 \end{array} \right] \]

\[ u.v = (0.5 \times 3) + (0.5 \times -4) = -0.5 \]

  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 definition.
len_u <- sqrt(sum(u^2))
len_u
## [1] 0.7071068
len_v <- sqrt(sum(v^2))
len_v
## [1] 5

Solving using pythagorean theorem:

\[ ||\overrightarrow{u}|| = \sqrt{0.5^2 + 0.5^2} = 0.7071 \] \[ ||\overrightarrow{v}|| = \sqrt{3^2 + -4^2} = 5 \] 3. What is the linear combination: 3u−2v?

lc <- 3*u - 2*v
lc
## [1] -4.5  9.5

\[ \overrightarrow{u} = \left[ \begin{array}{cccc} 0.5 & 0.5 \end{array} \right] \]

\[ 3\overrightarrow{u} = \left[ \begin{array}{cccc} 1.5 & 1.5 \end{array} \right] \] \[ \overrightarrow{v} = \left[ \begin{array}{cccc} 3 & -4 \end{array} \right] \]

\[ 2\overrightarrow{v} = \left[ \begin{array}{cccc} 6 & -8 \end{array} \right] \]

\[ 3\overrightarrow{u} - 2\overrightarrow{v} = \left[ \begin{array}{cccc} 1.5 & 1.5 \end{array} \right] - \left[ \begin{array}{cccc} 6 & -8 \end{array} \right] \]

\[ (1.5 - 6) = -4.5 \] \[ (1.5 + 8) = 9.5 \]

\[ 3\overrightarrow{u} - 2\overrightarrow{v} = \left[ \begin{array}{cccc} -4.5 & 9.5 \end{array} \right] \]

  1. What is the angle between u and v
angle <- acos( dot(u, v) / ( sqrt(dot(u, u)) * sqrt(dot(v, v)) ) )
angle_degree <- angle * 180 / pi
angle_degree
## [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.

Please test it with the system below and it should produce a solution x = [−1.55,−0.32,0.95]

\[ 3\overrightarrow{u} - 2\overrightarrow{v} = \left[ \begin{array}{cccc} 1 & 1 & 3 \\ 2 & -1 & 5 \\ -1 & -2 & 4 \\ \end{array} \right] \left[ \begin{array}{cccc} x1 \\ x2 \\ x3 \\ \end{array} \right] = \left[ \begin{array}{cccc} 1 \\ 2 \\ 6 \\ \end{array} \right] \]

# Function to check for upper triangular matrix
is_upper_triangular <- function(A) {
  is_ut_matrix = TRUE
  
  for(row in 1:nrow(A)) {
    for(col in 1:ncol(A)) {
      # If row# is greater than col# then they should be 0 (below the main diagonal) 
      if (row > col) {
        if (A[row, col] != 0) {
          is_ut_matrix = FALSE
        }
      }
    }
  }
  return(is_ut_matrix)
}
func_sol_set2 <- function(A, b)
{
  A <- cbind(A, b)
  m <- A[2,1]/A[1,1]*(A[1,])
  A[2,] = A[2,]-m

  print("Showing steps of operation")
  print("Step 1")  
  print(A)
  print(" ")
  
  m = (A[3,1]/A[1,1]*(A[1,]))
  A[3,] = A[3,]-m
  
  print("Step 2")  
  print(A)
  print(" ")
  
  m = (A[3,2]/A[2,2]*(A[2,]))
  A[3,] = A[3,]-m

  print("Step 3")  
  print(A)
  print(" ")
  print("Upper Triangular Check")
  print(is_upper_triangular(A))
  
  x3 <- (A[3,4]/A[3,3])
  x2 = (A[2,4]-A[2,3]*x3)/A[2,2]
  x1 = (A[1,4]-A[1,3]*x3 - A[1,2]*x2)/A[1,1]
  
  return(c(x1, x2, x3))
}

A <-  matrix(
  c(1, 2, -1, 1, -1, -2, 3, 5, 4), 
  nrow=3, 
  ncol=3)

b <- matrix(
  c(1, 2, 6),
  nrow = 3,
  ncol = 1)
# Tests
print(is_upper_triangular(A))
## [1] FALSE
r <- func_sol_set2(A, b)
## [1] "Showing steps of operation"
## [1] "Step 1"
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    0   -3   -1    0
## [3,]   -1   -2    4    6
## [1] " "
## [1] "Step 2"
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    0   -3   -1    0
## [3,]    0   -1    7    7
## [1] " "
## [1] "Step 3"
##      [,1] [,2]      [,3] [,4]
## [1,]    1    1  3.000000    1
## [2,]    0   -3 -1.000000    0
## [3,]    0    0  7.333333    7
## [1] " "
## [1] "Upper Triangular Check"
## [1] TRUE
# Show results
print(r)
## [1] -1.5454545 -0.3181818  0.9545455