Problem set 1

(1) Calculate the dot product u:v where u=[0:5;0:5] and v=[3;-????4]

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

lengthU <- sqrt(sum(u^2))
lengthU
## [1] 0.7071068
lengthV <- sqrt(sum(v^2))
lengthV
## [1] 5

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

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

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

angle <- acos(uvDotproduct/(lengthU*lengthV))
angleuv <- angle * 180/pi
angleuv
##         [,1]
## [1,] 98.1301

Problem set 2

(1) 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.

gaus <- function(a, b) {
    c <- cbind(a,b)
  
  pivot1 <- c[2,1]/c[1,1]
  c[2,] <- c[2,] - (c[1,]*pivot1)
  
  pivot2 <- c[3,1]/c[1,1]
  c[3,] <- c[3,] - (c[1,]*pivot2)
  
  pivot3 <- c[3,2]/c[2,2]
  c[3,] <- c[3,] - (c[2,]*pivot3)
  

  x3 <- c[3,4] / c[3,3]
  x2 <- (c[2,4] - (c[2,3]*x3)) / c[2,2]
  x1 <- (c[1,4] - (c[1,3]*x3) - (c[1,2]*x2)) / c[1,1]
  
  x <- matrix(c(x1, x2, x3), nrow = 3)

  return(round(x,2))
  
} 


a <- matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4), nrow=3, ncol=3) 
b <- c(1, 2, 6)

gaus(a,b)
##       [,1]
## [1,] -1.55
## [2,] -0.32
## [3,]  0.95