Reference no: EM1384111
Write a function that solves a system of linear equations or calculates the inverse of a matrix by Gauss-Jordan elimination. Your function should accept a matrix as an argument and return a matrix of the same dimension.
Note: Your function should be written to handle matrices of any size. Test your function with matrices of different sizes for which you know the solution to ensure that your function works correctly.
Use variable names that relate to their function and include comments that explain your program logic.
Do not use any built-in MATLAB functions except size(), input(), and zeros().
As example of what is expected from this function consider the following linear system:
3x + 2y + z = 11
2x + 3y +z = 13
x + y + 4z = 12
In this case you should pass into your function the following augmented matrix:
After performing Gauss-Jordan elimination, your function should return the following reduced system:
from which we can readily determine that x = 1, y = 3, z = 2.
Furthermore, if you pass a double augmented matrix such as
Your function should return
which solves the original equations
3x + 2y + z = 11
2x + 3y +z = 13
x + y + 4z = 12
as x = 1, y = 3, z = 2
and, at the same time, the equations
3x + 2y + z = -5
2x + 3y +z = -4
x + y + 4z = 9
as x = -2, y = -1, z = 3.
Additionally, if you pass to your function a square matrix on the left augmented by the identity matrix on the right, such as
your function should return the identity matrix on the left and the inverse of the original square matrix on the right.