Octave for Chapters 5-6
The templates in this section provide sample Octave code for finding subspaces associated with matrices as well as the kernel and image of a linear transformation. 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
Column Space of a Matrix (Image of a Linear Transformation)
Recall that the column space of a matrix is the span of the columns of the matrix. In Subspaces of Associated with Matrices we introduced the following procedure for finding the column space.
- (a)
- Find (or any row-echelon form of .)
- (b)
- Identify the pivot columns of (or ).
- (c)
- The columns of corresponding to the pivot columns of (or ) form a basis for .
We will implement this procedure in Octave.
We can find the reduced row-echelon form of using the rref function, and identify the pivot columns visually. We can also take advantage of the expanded version of the rref function to identify the pivot columns automatically, as shown below.
% Define A
A = [2 -1 1 -4 1;
1 0 3 3 0;
-2 1 -1 5 2;
4 -1 7 2 1];
% Compute rref of A
[rref_A, pivot_cols] = rref(A);
% the pivot column indices are...
pivot_cols
% Extract the pivot columns from A, using pivot column indices
colSpace_basis = A(:, pivot_cols)
When you study linear transformations, you will learn that the column space of a matrix is the image of the linear transformation induced by the matrix. So, Template temp:colSpace can be used to find the image of a linear transformation.
Null Space of a Matrix (Kernel of a Linear Transformation)
Recall the following definition of the null space of a matrix from Subspaces of Associated with Matrices.
We can use the reduced row-echelon form (rref function) to find the null space, as shown in Example ex:dimnull of Subspaces of Associated with Matrices. We will omit the details here.
We can also use the null function to find a basis for the null space of a matrix. This function finds an orthonormal basis for the null space. An orthonormal set of vectors consists of unit vectors that are pairwise orthogonal. We demonstrate the null function below.
% Define A
A = [2 -1 1 -4 1;
1 0 3 3 0;
-2 1 -1 5 2;
4 -1 7 2 1];
% Basis for null(A)
% Octave constructs an orthonormal basis, meaning that the basis elements are pairwise orthogonal unit vectors.
B=null(A)
% To find the dimension of the null space, find the number of columns in B
[rows, cols]=size(B)
The basis for the null space we found in Template temp:nullSpace looks different from what you would have found by hand using the rref function. In Problem prob_oct_Rn1 you will verify that in Template temp:nullSpace spans the null space of .
If you are familiar with linear transformations, you may recall that the kernel of a linear transformation induced by a matrix is the null space of the matrix. The null function gives us a way to compute the kernel.
Octave Exercises
For Problems prb:5.1 and prb:5.3, use Octave to find , , and their respective dimensions. Verify that the Rank-Nullity Theorem (th:matrixranknullity) holds for .
- (a)
- Verify that the product of with of each element of the basis is .
- (b)
- Use to find another basis for the null space.
- (c)
- The basis we found using the null function and the basis you found in part (b) look very different. How do we know that they span the same subspace?
- Given a matrix, find the product of the matrix with some vector in its null space. What do you observe? Why does this make sense?
- Formulate a conjecture about the relationship between the rows of a matrix and the elements of its null space.
- Prove your conjecture.
Bibliography
Matrices used in Problems prb:5.1-prb:5.34 come from the end of Chapter 4 of Ken Kuttler’s A First Course in Linear Algebra. (CC-BY)
Ken Kuttler, A First Course in Linear Algebra, Lyryx 2017, Open Edition, pp. 227–232.
2024-09-28 16:02:52