Octave for Chapter 4
The templates in this section provide sample Octave code for matrix operations and -factorization. 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
Basic Operations
% Define matrices A, B, and C
A = [1 -1 0 0;
2 -2 1 2;
0 1 0 1];
B = [2 3 -1 4;
1 -1 2 -2;
1 -2 0 3];
C = [1 -2 1 4;
2 -2 0 1;
-2 1 1 0;
1 3 -2 -1];
% Find A+B
sum=A+B
% Find the transpose of A
A_trans=transpose(A)
% Find AC
product=A*C
% Find the inverse of C
inv_C=inv(C)
% 5 by 5 identity matrix
I_5=eye(5)
% 5 by 5 zero matrix
O_5=zeros(5)
% The zero vector in R^5
O_col=zeros(5,1)
% Row of zeros
O_row=zeros(1,5)
% Non-square zero matrix
O_block=zeros(2,3)
% Matrix filled with 1s
One_block=ones(2,3)
Using Loops
We first introduced loops in Octave for Chapter 1. Examples in this section and the corresponding exercises will allow you to practice using loops while also encouraging you to delve deeper into various ways of thinking about matrix multiplication.
Write a routine that uses this definition to compute the product of a user-defined matrix and vector .
- The user will enter matrix and vector at the top.
- We will use the size function determine the size of the user-defined matrix .
% Find the size of matrix A
[m, n] = size(A); % m is the number of rows, and n is the number of columns of A
- We will utilize the length function to find the length of .
% Find the length of vector x
L=length(x);
- We will check that the dimensions of and are compatable. If the number of
columns of doesn’t match the number of entries in , we will issue a warning.
if n ~= L
warning("Dimensions of A and x are incompatable.");
else
% insert product code here ...
end
Here is the complete code.
% User-defined matrix A
A=[4 -1 6 10 -2;
3 11 8 -3 4;
-5 2 14 6 -1;
3 2 -3 0 7];
% User-defined vector x
x=[-2; 4; 8; 2; 3];
% Find the size of matrix A
[m,n]=size(A); % m is the number of rows, and n is the number of columns of A
% Find the length of vector x
L=length(x);
if n ~= L
warning("Dimensions of A and x are incompatable.");
else
% if the dimensions are compatable, proceed with the multiplication
% Initialize the product matrix
A_x=zeros(m,1);
% Find the product Ax
for i=1:m
for j=1:n
A_x(i)=A_x(i)+A(i,j)*x(j);
end
end
% Print our answer
A_x
% Check our answer
correct_answer=A*x
end
Demonstrate this method for matrix-vector multiplication in Octave.
A(2,:) % this is row 2 of matrix AAlternatively, if we ever need to reference a single column of a matrix, we would use the following:
A(:,5) % this is column 5 of matrix A
Here is our code that utilizes the dot product to perform matrix-vector multiplication.
% Define matrix A
A=[4 -1 6 10 -2;
3 11 8 -3 4;
-5 2 14 6 -1;
3 2 -3 0 7];
% Define vector x
x=[-2; 4; 8; 2; 3];
% Find the product Ax using the dot product method
for i=1:4
A_x(i,1)=dot(A(i,:),x);
end
% Print our answer
A_x
% Check our answer
correct_answer=A*x
-factorization
Before we proceed with the code, recall that if the elementary matrices used to reduce the given matrix to row-echelon form are all lower triangular, then we can find an factorization. In general, the following theorem applies.
- (a)
- is the matrix obtained from by doing these interchanges (in order) to A.
- (b)
- has an factorization.
% Define matrix A
A=[2 2 -1 2;
-1 2 2 1;
1 2 0 -2;
1 1 -2 -4];
% Find LU factorization for A
[L, U, P] = lu (A)
% where L is a lower triangular matrix,
% U is an upper triangular matrix, and
% P is an appropriate permutation matrix
% Verify that this factorization is correct
productLU=L*U
productPA=P*A
- Observe that the permutation matrix is not the identity matrix. What elementary row operation does multiplication by induce?
- With the last two lines of our code we checked the correctness of our factorization. Is it true that ? Interpret the value in the product . What do you think caused this value to occur?
Octave Exercises
If you studied section Curve Fitting this matrix may look familiar to you.
Use loops to create a Vandermonde matrix for .
Text Source
Problem prob_oct_mat_lu was adapted from Problem 2.7.3(d) of Keith Nicholson’s Linear Algebra with Applications. (CC-BY-NC-SA)
W. Keith Nicholson, Linear Algebra with Applications, Lyryx 2018, Open Edition, p. 127
2024-09-28 14:48:19