Contents
- System of equations
- Coefficient matrix
- Right-hand side values
- Augmented matrix
- Swap rows 1 and 3
- Eliminate values in rows 2 and 3
- Divide through second row
- Row operation
- Divide through third row
- Add rows
- Add scaled rows
- Matlab can computer determinants automatically
- Matlab can calculate the row reduced echelon form
- Matrix division
System of equations
In linear algebra a system of equations can be represented by a coefficient matrix. MATLAB can solve all types of linear equations automatically. We will demonstrate how MATLAB matrix manipulation commands can be used to perform row operations on a system of linear equations.
Consider the system:
Coefficient matrix
We define a standard coefficient matrix.
A = [4 3 1; 5 4 2; 1 -1 1]
A = 4 3 1 5 4 2 1 -1 1
Right-hand side values
These are the values from the right hand side of the equations.
C = [10;4;15]
C = 10 4 15
Augmented matrix
Row reduction is done on the augmented matrix.
AA = [A C]
AA = 4 3 1 10 5 4 2 4 1 -1 1 15
Swap rows 1 and 3
The syntax manipulates two rows of the matrix at the same time by indexing them in a matrix. The colon indicates all columns.
AA([1 3], :) = AA([3 1], :)
AA = 1 -1 1 15 5 4 2 4 4 3 1 10
Eliminate values in rows 2 and 3
We use a trick to manipulate two rows at the same time. The final term in the command is a 2x1 matrix multiplied by a 1x4 matrix. This produces two copies of the first row, each scaled differently.
AA([2 3],:) = AA([2 3],:) - [5;4]*AA(1,:)
AA = 1 -1 1 15 0 9 -3 -71 0 7 -3 -50
Divide through second row
AA(2,:) = AA(2,:) ./ 9
AA = 1.0000 -1.0000 1.0000 15.0000 0 1.0000 -0.3333 -7.8889 0 7.0000 -3.0000 -50.0000
Row operation
AA(3,:) = AA(3,:) - 7 * AA(2,:)
AA = 1.0000 -1.0000 1.0000 15.0000 0 1.0000 -0.3333 -7.8889 0 0 -0.6667 5.2222
Divide through third row
AA(3,:) = AA(3,:) ./ -0.6667
AA = 1.0000 -1.0000 1.0000 15.0000 0 1.0000 -0.3333 -7.8889 0 0 1.0000 -7.8329
Add rows
AA(1,:) = AA(1,:) + AA(2,:)
AA = 1.0000 0 0.6667 7.1111 0 1.0000 -0.3333 -7.8889 0 0 1.0000 -7.8329
Add scaled rows
AA([1 2], :) = AA([1 2], :) + [-0.6667; 0.3333] * AA(3,:)
AA = 1.0000 0 -0.0000 12.3333 0 1.0000 -0.0000 -10.4996 0 0 1.0000 -7.8329
MATLAB can computer determinants automatically
The "det" command will compute a determinant.
det(A)
ans = 6
MATLAB can calculate the row reduced echelon form
A much simpler approach is to just ask MATLAB to compute the row reduced echelon form of the augmented matrix.
rref(AA)
ans = 1.0000 0 0 12.3333 0 1.0000 0 -10.5000 0 0 1.0000 -7.8333
Matrix division
MATLAB supports a form of matrix division. In the equation:
Solving for X gives:
MATLAB can perform this calculation with "left division":
A\C
ans = 12.3333 -10.5000 -7.8333
Published with MATLAB® 7.4