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.

Octave Exercises

Given the code below, write out the system of equations and the corresponding solution(s).
% 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)  
    

Link to code.

Solve the following system using Octave. \begin{equation} \begin{array}{ccccccccccc} x_1 &- &x_2&+&2x_3&&&+&x_5&= &-1 \\ -x_1&&&-&x_3&+&x_4&&&=&1\\ 2x_1&+&2x_2&&&-&x_4&+&2x_5&=&1\\ x_1&&&&&+&2x_4&-&x_5&=&0 \end{array} \end{equation}
Solve the following system using Octave. \begin{equation} \begin{array}{ccccccccccc} 3x_1&+&x_2&+&4x_3&-&x_4&+&2x_5&= &10 \\ x_1&&&+&2x_3&-&x_4&+&x_5&=&3\\ &&x_2&+&x_3&+&x_4&+&3x_5&=&2\\ 2x_1&-&x_2&-&x_3&&&+&3x_5&=&-2\\ 4x_1&+&x_2&-&5x_3&+&2x_4&+&x_5&=&1 \end{array} \end{equation}
Suppose the graph of function of the form passes through points , , and . Set up a system of linear equations to find the coefficients , , and . Update the coefficients in the GeoGebra interactive below to sketch the graph.
You need to find coefficients such that , , , . For more information about this topic, see Curve Fitting.

The following code generates a random matrix whose entries are integers , , and , and a random vector whose components are integers , , and .
        % 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)  
    

Link to code.

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 ?

The rank can be any positive integer less than or equal to . The rank can be any positive integer less than or equal to . The rank is always equal to . The rank can be any non-negative integer less than or equal to .

What are the possibilities for the number of solutions of such ?

This system always has a unique solution. This system may have no solutions, infinitely many solutions or a unique solution. This system will always have infinitely many solutions. This system will always be infeasible.

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.

Acknowledgements

The inspiration for Example ex:rrefFail came from this Stack Overflow comment.

2024-09-15 22:52:27