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)