ASSIGNMENT 1

Data 605

1. Problem set 1

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

u <- c(.5,.5)
v <- c(3, -4)

dotuv <- u %*% v
dotuv
##      [,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.

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

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

(3*u) - (2*v)
## [1] -4.5  9.5

(4) What is the angle between u and v

ang <-acos(dotuv/(ul*vl))
ang
##          [,1]
## [1,] 1.712693
ang * 180/pi
##         [,1]
## [1,] 98.1301

2. Problem set 2

gausjor <- function(a, b){
  X <- cbind(a,b)
  
  gj1 <- X[2,1]/X[1,1]
  X[2,] <- X[2,]-gj1*X[1,]
  
  gj2 <- X[3,1]/X[1,1]
  X[3,] <- X[3,]-gj2*X[1,]
  
  gj3 <- X[3,2]/X[2,2]
  X[3,] <- X[3,]-gj3*X[2,]
  
  gj4 <- X[2,3]/X[3,3]
  X[2,] <- X[2,] - gj4*X[3,]
  
  gj5 <- X[1,2]/X[2,2]
  X[1,] <- X[1,] - gj5*X[2,]
  
  gj6 <- X[1,3]/X[3,3]
  X[1,] <- X[1,] - gj6*X[3,]
  
  y <- matrix(c(X[1,4]/X[1,1], X[2,4]/X[2,2], X[3,4]/X[3,3]), nrow = 3, ncol = 1)
  return(round(y,2))
}

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

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