Hw 1

Problem Set 1

1

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

u%*%v
##      [,1]
## [1,] -0.5

2

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

3

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

4

angle_uv <- acos(u%*%v / (length_u * length_v)) * 180/pi
angle_uv
##         [,1]
## [1,] 98.1301

Problem Set 2

gaus <- function(a, b) {
  #Combine Matrix and Results
  c <- cbind(a,b)
  
  #Make [2,1] = 0
  pivot1 <- c[2,1]/c[1,1]
  c[2,] <- c[2,] - (c[1,]*pivot1)
  
  #Make [3,1] = 0
  pivot2 <- c[3,1]/c[1,1]
  c[3,] <- c[3,] - (c[1,]*pivot2)
  
  #Make [3,2] = 0
  pivot3 <- c[3,2]/c[2,2]
  c[3,] <- c[3,] - (c[2,]*pivot3)
  
  #Solve for x1,x2, and x3
  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))
  
} 


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

gaus(M,y)
##       [,1]
## [1,] -1.55
## [2,] -0.32
## [3,]  0.95
solve(M,y)
## [1] -1.5454545 -0.3181818  0.9545455