MATLAB Linear Systems Example
MATLAB Linear Systems Example
To enter matrix A in MATLAB, type:
A=[1 -2 -3; 1 2 –1; 2 4 –1]
This command generates a 3x3 matrix, which is displayed on your screen.
Then type
b=[1 2 3]’
to generate a column vector b (make sure you include the prime ’ at the end of the command).
In order to solve the system Ax=b using Gauss-Jordan elimination, you first need to generate the augmented matrix, consisting of the coefficient matrix A and the right hand side b:
Aaug=[A b]
You have now generated augmented matrix Aaug (you can call it a different name if you wish). You now need to use command “rref”, in order to reduce the augmented matrix to its reduced row echelon form and solve your system:
C = rref(Aaug)
Can you identify the solution of the system after you calculated matrix C?
You can also solve the same system in MATLAB using command
x= A\b
The symbol between matrix A and vector b is a “backslash”. This command will generate a vector x, which is the solution of the linear system.
(Can we always use this method to solve linear systems in MATLAB? Experiment with different systems.)
Command "help" is a command you should use frequently. It shows you how MATLAB commands should be used. For example, type:
help rref
and you will get information on the usage of "rref". To find out more about command "help", type
help help
Command "help" is useful when you know the exact command you want to use and you want to find out details on its usage. Sometimes we do not know the exact command we should use for the problem we need to solve. Then we can use command "lookfor". Type
lookfor echelon
and you will get as a result a number of MATLAB commands that have to do with row echelon forms.
You can also get help using command "doc". Type
help doc
for details.
To save your work, you can use command “diary”. Type
help diary
for more information on how to use the command.
A few more useful commands:
A’ is the transpose of A.
Command “inv” calculates the inverse of a matrix. Type:
inv(A)
Command “det” computes determinants (we will learn more about determinants shortly).
There are several MATLAB commands that generate special matrices.
Command “rand” generates matrices with random entries (rand(3,4) creates a 3x4 matrix with random entries). Command “eye” generates the identity matrix (try typing eye(3)).
Other such commands are “zeros” (for zero matrices) and “magic” (type help zeros and help magic for more information).