Problem Set I

(1) Calculate the dot product u.v where u = [0.5; 0.5] and v = [3; -4]
u = c(0.5, 0.5)
u
## [1] 0.5 0.5
v = c(3,-4)
v
## [1]  3 -4
plot(c(-5,5),c(-5,5))
arrows(0,0,0.5,0.5)
arrows(0,0,3,-4)

dot.prod = u[1]*v[1] + u[2]*v[2]
dot.prod
## [1] -0.5
(2) What are the lengths of u and v?
len_u = sqrt(u[1]**2+u[2]**2)
len_u
## [1] 0.7071068
len_v = sqrt(v[1]**2+v[2]**2)
len_v
## [1] 5
(3) What is the linear combination: 3u - 2v?

3u - 2v = 3u + (-2v)

3*u + (-2*v)   
## [1] -4.5  9.5
(4) What is the angle between u and v?

Formula - angle theta = Arccosine (dot product (u and V)/(lenght of u*length of v)

theta = acos(dot.prod/(len_u*len_v)) 
theta
## [1] 1.712693

Problem Set II

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]

1 1 3 x 1
2 -1 5 y 2
-1 -2 4 z 6

Construction of an Upper Triangular Matrix

UTM <- function (M)
{ 
sw = 1 
while (sw < 4)
{
  if (A[1,1] != 0)
  {
     mult = A[2,1]/A[1,1]
     A[2,1] = A[2,1] - (A[1,1] * mult)          # zero!
     A[2,2] = A[2,2] - (A[1,2] * mult)
     A[2,3] = A[2,3] - (A[1,3] * mult)
     A[2,4] = A[2,4] - (A[1,4] * mult)
     mult = A[3,1]/A[1,1]
     A[3,1] = A[3,1] - (A[1,1] * mult)          # zero! 
     A[3,2] = A[3,2] - (A[1,2] * mult)
     A[3,3] = A[3,3] - (A[1,3] * mult)
     A[3,4] = A[3,4] - (A[1,4] * mult)
     sw = 4
  }
  else          #pivot is 0, need to swap rows
  {
    if (sw < 3)   #can only switch 2 times (rows 2 and 3)
    {
      sw = sw + 1
      t = A[1,] 
      A[1,] = A[sw,]
      A[sw,] = t
    }
    else
    {
      sw = 4
    }
  }
}
A
sw = 2 
while (sw < 4)
{
  if (A[2,2] != 0 )
  {
     mult = A[3,2]/A[2,2]
     A[3,2] = A[3,2] - (A[2,2] * mult)          # zero!
     A[3,3] = A[3,3] - (A[2,3] * mult)
     A[3,4] = A[3,4] - (A[2,4] * mult)
     sw = 4
  }
  else          #pivot is 0, need to swap rows
  {
    if (sw < 3)   #can only switch 1 time (row 3)
    {
      sw = sw + 1
      t = A[2,] 
      A[2,] = A[sw,]
      A[sw,] = t
    }
    else
    {
      sw = 4
    }
  }
}
 A
#Apply Back Substitution To Obtain Solution
z = A[3,4]/A[3,3]

y = (A[2,4] - z*A[2,3])/A[2,2]

x = (A[1,4] - z*A[1,3] - y*A[1,2])/A[1,1] 

s = c(x, y, z)
s
return(s)

}
A = matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4, 1, 2, 6), nrow=3, ncol=4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    2   -1    5    2
## [3,]   -1   -2    4    6
s <- UTM(A)
s
## [1] -1.5454545 -0.3181818  0.9545455