Created by
Ketan B. Kulkarni
Rensselaer Polytechnic Institute
Matlab refers to Matrix Laboratory. As it suggests, everything is defined as matrices or vectors. All the basic matrix algebra functions are built into it. So all you need to do is define your matrices and perform the operations using matlab functions. Let’s go over
Matlab GUI.
Matlab Windows
Workspace
Command
Window
Command
1. Workspace: Here you can see all the active variables, their type, and their size.
Double clicking on the variable names will open a new window which will show all the entries in that variable. You can change the entries in that variable dynamically.
2. Command Window: You can type any matlab commands here operating on the variables. It will show the output of your operations and any script you are running; in the same window. If you want help on any command you can type in command windows as follows. help command name
3. Command History: Command history records all the commands typed in command window. It keeps the log of all the commands typed previously. You can clear it by clicking Edit>Clear Command History
Note:
You can write your program in a file and save it as “.m” file. Also all the variables in the workspace or all of the workspace can be saved as “.mat” file, so next time you open the MATLAB again, you can reload it.
How to define matrices?
1. Matrix: Try following in the matlab window
>> a = [1 2; 3 4] a =
1 2
3 4
As you can see, you start entering row wise with spaces between each entry in a pair of square brackets. For every new row, type a semicolon “ ; ” and start typing as earlier row.
If you put a semicolon at the end of square bracket, matlab will not show any output.
e.g. a = [1 2; 3 4];
2. Vectors: Vectors can be entered as follows.
>> b = [1 2 3 4] b =
1 2 3 4
Putting spaces between entries defined it as a row vector.
>> b = [1; 2; 3; 4] b =
1
2
3
4
Putting semicolon between entries defines it as a column vector.
Note:
If you don’t know the size of the matrices or vectors created by any program, issue following command.
>> size(a) ans = 2 2
This means matrix
“a” has 2 rows and 2 columns
Basic Matrix Operations:
1. Addition:
>> a+a ans =
2 4
6 8
2. Subtraction:
>> a-a ans =
0 0
0 0
3. Multiplication:
>> a*a ans =
7 10
15 22
>> a*b ans =
5
11
4. Transpose of a matrix:
>> a=a' a =
1 3
2 4
5. Determinant of a matrix:
>> det(a)
ans =
-2
Determinant is useful to determine if the matrix in invertible or not. If the determinant is zero or close to zero; we say matrix is singular and can not be inverted.
Methods for solving systems of linear equations:
Square Systems:
The most common situation involves a square coefficient matrix “A” and a single righthand side column vector “b”.
Nonsingular Coefficient Matrix:
If the matrix A is nonsingular, the solution, x = A\b, is then the same size as b. For example,
A = pascal(3); u = [3; 1; 4]; x = A\u x =
10
-12
5
It can be confirmed that A*x is exactly equal to u.
If A and B are square and the same size, then X = A\B is also that size.
B = magic(3);
X = A\B
X =
19 -3 -1
-17 4 13
6 0 -6
It can be confirmed that A*X is exactly equal to B.
Both of these examples have exact, integer solutions. This is because the coefficient matrix was chosen to be pascal(3), which has a determinant equal to one. A later section considers the effects of round off error inherent in more realistic computations.
Singular Coefficient Matrix:
A square matrix A is singular if it does not have linearly independent columns. If A is singular, the solution to AX = B either does not exist, or is not unique. The backslash operator, A\B, issues a warning if A is nearly singular and raises an error condition if it detects exact singularity.
If A is singular and AX = b has a solution, you can find a particular solution that is not unique, by typing
P = pinv(A)*b
P is a pseudo inverse of A. If AX = b does not have an exact solution, pinv(A) returns a least-squares solution.
For example matrix
A = [ 1 3 7
-1 4 4
1 10 18 ]
is singular, as you can verify by typing det(A) ans =
0
Note For information about using “pinv” to solve systems with rectangular coefficient matrices, see Pseudo inverses.
Exact Solutions :
For b =[5;2;12] , the equation AX = b has an exact solution, given by pinv(A)*b ans =
0.3850
-0.1103
0.7066
You can verify that pinv(A)*b is an exact solution by typing
A*pinv(A)*b ans =
5.0000
2.0000
12.0000
Least Squares Solutions:
On the other hand, if b = [3;6;0], then AX = b does not have an exact solution. In this case, pinv(A)*b returns a least squares solution. If you type
A*pinv(A)*b ans =
-1.0000
4.0000
2.0000 you do not get back the original vector b.
You can determine whether AX = b has an exact solution by finding the row reduced echelon form of the augmented matrix [A b]. To do so for this example, enter rref([A b]) ans =
1.0000 0 2.2857 0
0 1.0000 1.5714 0
0 0 0 1.0000
Since the bottom row contains all zeros except for the last entry, the equation does not have a solution. In this case, pinv(A) returns a least-squares solution.
The function rref(A):
Reduced row echelon form
Syntax
R = rref(A)
[R,jb] = rref(A)
[R,jb] = rref(A,tol)
Description
R = rref(A) produces the reduced row echelon form of A using Gauss Jordan elimination with partial pivoting. A default tolerance of (max(size(A))*eps *norm(A,inf)) tests for negligible column elements.
[R,jb] = rref(A) also returns a vector “jb” such that: r = length(jb) is this algorithm's idea of the rank of A. x(jb) are the pivot variables in a linear system Ax = b.
A(:,jb) is a basis for the range of A. R(1:r,jb) is the r-by-r identity matrix.
[R,jb] = rref(A,tol) uses the given tolerance in the rank tests.
Round off errors may cause this algorithm to compute a different value for the rank than rank, orth and null.
Note The demo rrefmovie(A) enables you to sequence through the iterations of the algorithm.
Examples
Use rref on a rank-deficient magic square:
A = magic(4), R = rref(A)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
R =
1 0 0 1
0 1 0 3
0 0 1 -3
0 0 0 0
Trigonometric functions: sin
Sine of an argument in radians
Syntax
Y = sin(X)
Description
The sin function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians.
Y = sin(X) returns the circular sine of the elements of X.
Format of variables:
Set display format for output
Graphical Interface
As an alternative to format, use preferences. Select Preferences from the File menu in the
MATLAB desktop and use Command Window preferences.
Syntax
format format type format('type')
Description
Use the format function to control the output format of numeric values displayed in the
Command Window.
Note: The format function affects only how numbers are displayed, not how MATLAB computes or saves them.
“format” by itself, changes the output format to the default appropriate for the class of the variable currently being used. For floating-point variables, for example, the default is format short (i.e., 5-digit scaled, fixed-point values). format type changes the format to the specified type. format('type') is the function form of the syntax.
Note: Inbuilt help in MATLAB is one of the best you can find. You can search quarries by topic or keyword. It will show in detail syntax and examples for each command and at the bottom there will be a link to related commands.
Happy exploring MATLAB!!!!!!!