write a function in Octave OR 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 test it with the system below and it should produce a solution x = [-1.55, -0.33, 0.95]
1 1 3
2 -1 5
-1 -2 4
x1
x2
x3
1
2
6
gaussian.elimination <- function(eq,sol)
{
n <-nrow(eq)
(eq.sol <-cbind(eq,sol))
eq.sol[1,] <-eq.sol[1,]/eq.sol[1,1]
for (i in 2:n)
{
for (j in i:n)
{
eq.sol[j, ] <- eq.sol[j, ] - eq.sol[i-1,] * eq.sol[j,i-1]
}
eq.sol[i,] <-eq.sol[i,]/eq.sol[i,i]
}
for (i in n:2)
{
for(j in i:2-1)
{
eq.sol[j,] <-eq.sol[j,] -eq.sol[i,] *eq.sol[j,i]
}
}
eq.sol
eq.sol[,4]
}
gaussian.elimination(matrix(c(1,1,3,2,-1,5,-1,-2,4),byrow=T,nrow=3,ncol=3),matrix(c(1,2,6),nrow=3,ncol=1))
## [1] -1.5454545 -0.3181818 0.9545455