% Coefficient matrix A
A=[2 -4 1 1 3;
0 2 -1 4 -1;
2 -2 1 1 4];
% Column vector b
b=[-2; 1; 4];
% Augmented matrix A_b
A_b=[A b];
% Reduced row-echelon form
rref_A_b=rref(A_b)
Octave for Chapter 2
The templates in this section provide sample Octave code for solving systems of equations. You can access our code through the link at the bottom of each template. Feel free to modify the code and experiment to learn more!
You can write your own code using Octave software or online Octave cells. To access Octave cells online, go to the Sage Math Cell Webpage, select OCTAVE as the language, enter your code, and press EVALUATE.
To ”save” or share your online code, click on the Share button, select Permalink, then copy the address directly from the browser window. You can store this link to access your work later or share this link with others. You will need to get a new Permalink every time you modify the code.
Octave Tutorial
When solving systems by hand, you utilized Gauss-Jordan elimination to find rref. Having the reduced row echelon form available made it easy to write out the solution(s) or determine that the system is inconsistent. We can find rref and the rank of a matrix using Octave as follows.
(You may remember this system from Augmented Matrix Notation and Elementary Row Operations.) We will solve this system by finding the reduced row echelon form of the corresponding augmented matrix in Octave.
% Define the augmented matrix
% corresponding to the coefficient matrix A and vector b
A_b = [1 -1 0 0 0;
2 -2 1 2 4;
0 1 0 1 0;
0 0 2 1 5];
% Find rref of A_b
rref(A_b)
% Find the rank of A_b
rank(A_b)
If you start with a matrix and a separate vector , you can create an augmented matrix without re-typing as follows:
% Define the coefficient matrix A
A = [1 -1 0 0;
2 -2 1 2;
0 1 0 1;
0 0 2 1];
% Define vector b
b = [0;4;0;5];
% Create an augmented matrix
A_b=[A b]
% Define a matrixLink to code.
A=[2 -1 3 -1; 1 0 2 1; 1 -1 1 -2]
% Third row of A
row_3=A(3,:)
% Second column of A
col_2=A(:,2)
% Second row third entry of A
a_23=A(2,3)
Octave Exercises
% A is a randomly generated 20 by 3 matrix whose entries are -1, 0, 1.
A=randi([-1,1],20,3);
% b is a randomly generated column vector whose entries are -1, 0, 1.
b=randi([-1,1],20,1);
% Augmented matrix A_b
A_b=[A b];
% Reduced row-echelon form
rref_A_b=rref(A_b)
% Rank of A_b
rank(A_b)
Run the code several times to generate several random combinations of and and find the rank and the reduced row-echelon form of . Are you getting the answers you were expecting? Answer the following questions based on your theoretical knowledge.
What are the possibilities for the rank of such ?
What are the possibilities for the number of solutions of such ?
There is a good chance that your experiments have led you to believe that . Can you manually construct , satisfying the condition of the problem, with a smaller rank? How many solutions does your system have? Why do you think it is so rare to randomly generate a system of rank less than ?
Further Considerations
Issues related to computational aspects of linear algebra are outside the scope of this text. However, we want the reader to be aware of some potential problems that may arise.
% Use a 12 by 12 Hilbert matrix
A=hilb(12);
% Theoretical rank of A is 12, but the following computation shows a rank of 11
rank(A)
% rref of A should be the identity matrix, but the following result contains a row of zeros
rref(A)
Acknowledgements
The inspiration for Example ex:rrefFail came from this Stack Overflow comment.
2024-09-15 22:52:27