MATLAB Lecture 1. Introduction To MATLAB
MATLAB Lecture 1. Introduction To MATLAB
MATLAB Lecture 1. Introduction To MATLAB
Make sure you understand the difference between the file produced by diary and the file made by save!
With diary we create a human readable text file, with save we make a binary mat-file.
MATLAB records the commands you typed in. You can scroll through the list of executed commands
with the arrow keys ↑ and ↓. The arrows ← and → enable to go to the location in the command where
edits are necessary. This is convenient to correct type errors in a command, or if you need to execute lots of
slightly different versions of the same instruction.
MATLAB has online help facilities. The command help displays information in the current window,
helpwin opens a new window, helpdesk launches your internet browser.
>> x = 0.65
x =
0.6500
Jan Verschelde, April 9, 2007 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 1
MCS 320 Introduction to Symbolic Computation Spring 2007
MATRICES These are defined row-wise. A square bracket [ means to MATLAB that you wish to enter a
matrix. The input of a matrix is terminated by a ]. You can enter a matrix in two ways.
For instance, you want
0 1 2
a= 3 4 5
6 7 8
then you either type
>> a = [ 0 1 2 ; 3 4 5 ; 6 7 8 ]
or
>> a = [ 0 1 2
3 4 5
6 7 8 ]
a =
0 1 2
3 4 5
6 7 8
Jan Verschelde, April 9, 2007 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 2
MCS 320 Introduction to Symbolic Computation Spring 2007
m =
1 2
4 5
To transpose a matrix, you type an accent after the matrix. For example >> b = a’ returns
b =
0 3 6
1 4 7
2 5 8
Jan Verschelde, April 9, 2007 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 3
MCS 320 Introduction to Symbolic Computation Spring 2007
>> x=a\b
>> [v,d]=eig(a)
The two matrices v and d on return have the following meaning: d is a diagonal matrix with the
eigenvalues of a on its diagonal, and v contains in its columns the eigenvectors of a. Without roundoff,
a*v == v*d.
1.2.7 Programs
You can collect a sequence of MATLAB commands in one file, which should have the ‘.m’ extension. This
sequence of commands is then executed when you type in the name of that file (without the extension).
To ensure that MATLAB will find your program, you may have to adjust MATLAB search path with the
command path Typing path displays MATLAB’s current search path. To append a directory to this path,
type path(path,’c:\temp\my files’) for instance.
Jan Verschelde, April 9, 2007 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 4
MCS 320 Introduction to Symbolic Computation Spring 2007
1.3 Assignments
1. Type tour in a MATLAB session, follow the Intro to MATLAB link and browse through the various
aspects that interest you.
2. Visit http://www.mathworks.com/matlabcenteral/fileexchange, this is the site of the MATLAB
Central File Exchange which contains user contributions. Download one file, and answer the following
two questions: (1) Why did you choose this type of application? (2) What did you learn from it?
3. Type in u = [2 3 1 4]; v = [4 3 2 1]; w = [1 2 3 4];
Give MATLAB commands to create a matrix a that has as rows u, v, and w.
The matrix a defines the augmented matrix of a 3x3 linear system with right hand size vector in its
fourth column. Compute its reduced row echelon form with rref.
What is the solution of the corresponding linear system?
Verify that the solution vector x makes b - a*x (almost) zero.
· ¸ · ¸
1 2 2
4. The linear system Ax = b is defined by A = and b = .
2 3 5
Give the MATLAB commands to enter A and b, to find x, the solution to Ax = b, and finally to
compute the norm of b − Ax.
½
2x1 + 3x2 = 1
5. Consider the linear system
−2x1 + x2 = 3.
Give all the MATLAB commands to define this linear system in the format Ax = b, with x = [x1 x2 ]T ,
i.e.: what are the commands to define A and b? What is the solution of this system?
6. Type a = [1 2 3; 4 5 6; 7 8 9], followed by [l, u] = lu(a). Test whether l ∗ u equals a. Notice that u is
upper triangular. Explain why l is not lower triangular. (hint: type help lu.)
7. Generate a random 6 × 6 matrix a and compute the LU-decomposition of a, storing the factors of the
LU-decomposition in the matrices l and u.
8. Create a random 3-by-3 matrix a and compute its eigenvectors and eigenvalues via [v, d] = eig(a).
(a) Verify the accuracy of the computed results via the norm of the matrix a ∗ v − v ∗ d.
Interpret the result.
(b) The product of a with the kth column of v is d(k, k) times the k column of v. Give the MATLAB
commands and the output to verify this property of eigenvectors and eigenvalues.
Jan Verschelde, April 9, 2007 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 5