Problem Set 1

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

dot product u.v = 3.5

u <- c(0.5, 0.5)
v <- c(3, 4)
sum(u * v)
## [1] 3.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.

2.a Mathematical Length of u = 0.707; Mathematical Length of v = 5

2.b Computer Science Length = 2, as in two dimensions x and y

length(u)
## [1] 2
length(v)
## [1] 2
lenu <- sqrt(0.5 * 0.5 + 0.5 * 0.5)
lenu
## [1] 0.7071068
lenv <- sqrt(3^2 + 4^2)
lenv
## [1] 5

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

3u - 2v = [-4.5; -6.5]

3 * u - 2 * v
## [1] -4.5 -6.5

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

If you have two unit vectors, then the dot-product between them is simply the cosine of the angle between them. When you don’t have unit vectors you need to normalize them, so …

\(x \cdot y = ||x|| * ||y|| \cos\theta\) , For our vectors u and v we get …

\(\cos\theta = \frac{u \cdot v}{||u|| * ||v||}\) , which is just the dot product of u and v divided by the lengths of u and v found above

\(\theta = \arccos(\frac{u \cdot v}{||u|| ||v||})\)

\(\theta = \arccos(\frac{3.5}{0.707 * 5})\)

\(\theta = \arccos(0.98995) = 0.1418935\)

Angle between u and v = 0.142 radians

theta <- acos(sum(u*v) / (sqrt(sum(u * u)) * sqrt(sum(v * v))))
theta
## [1] 0.1418971

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.

utm <- function(A, b) {
  # Upper Triangle Matrix soluion by elimination of a 3 X 3 Matrix
    
  # Run 1 or zero out (eliminate) position 2, 1
  colnames(A) <- c("x1", "x2", "x3")
  A <- cbind(A, b)
  pivot <- A[1, 1]
  multi <- A[2, 1]/pivot
  row1 <- A[1,]
  row2 <- A[2,]
  A[2,] <- row2 - row1 * multi
  
  # Run 2 or zero out position 3, 1
  pivot <- A[1, 1]
  multi <- A[3, 1]/pivot
  row1 <- A[1,]
  row3 <- A[3,]
  A[3,] <- row3 - row1 * multi
  
  # Run 3 or zero out position 3, 2
  pivot <- A[2, 2]
  multi <- A[3, 2]/pivot
  row2 <- A[2,]
  row3 <- A[3,]
  A[3,] <- row3 - row2 * multi
  
  # Solution
  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]
  x <- c(x1, x2, x3)
  return(round(x, 2))
}

vect <- c(1, 2, -1, 1, -1, -2, 3, 5, 4)
results <- c(1, 2, 6)
mat <- matrix(vect, 3, 3)

utm(mat, results)
##     b     b     b 
## -1.55 -0.32  0.95