MATLAB Summary for Math 25

Bard Ermentrout

January 9, 1996

Commands are entered in MATLAB at the command prompt. Case is important so that A and a are different. To supress output add a semi-colon to the end of a statement. Use the arrow keys to scroll back to previous commands and left and right to edit statements.

  1. Entering matrices

A matrix is entered by (i) explictly listing elements, (ii) using defining statements (iii) disk files.

Two examples of entering them yourself:

A= [1 2 3; 4 5 6; 7 8 9]
B = [
     1 3
     5 7 ]
create the obvious matrices. The semicolon ; separates rows. Commas can be used insetad of spaces. Do not use spaces between parts of a number, e.g. 2.34e-9 is valid but 2.34 e-9 will be treated as two elements.

Complex numbers are allowed by appending i:

A= [1 2;3 4] + i* [5 6;7 8]
A= [1+5i 2+6i;3+7i 4+8i]
produce the same matrix. Don't write 2 + 3i as that will produce two numbers. You can use i or j as the number sqrt(-1).

  2. Other ways to create matrices

  1. rand(n,m) eye(n,m) zeros(n,m) ones(n,m) respectively produce an n x m matrix or random numbers between 0 an 1, identity matrix, zero matrix, or matrix of 1's. If you only put one argument in these they produce square arrays, eye(5) is a 5 x 5 identity matrix.
  2. diag(A), triu(A), tril(A) extract the diagonal, upper triangular, and lower triangular part of a matrix. The diagonal part returns a column vector. If A is a vector then diag(A) is the square matric with A down the diagonal.
  3. Block definitions. For example, if A is 3-by-3 then B = [ A , zeros(3,2) ; zeros(2,3), eye(2)] produces a 5-by-5 matrix.

  3. Operations

The following matrix operations are allowed:

+ - : addition and subtraction
* : multiplication
** ^ : power operator
/ \ : right and left division. A/b solves x A = b while A\b : solves A x = b .
' : is the matrix transpose

For powers, multiplication and division, the operators .* ./ .^ all perform elementwise multiplication. A.^ 2 squares each element in A while A^ 2 is the square of A.

  4. Functions

Scalar functions

These functions operate elementwise on matrices

sin  cos  tan  asin acos atan
exp  log  abs  sqrt sign rem (remainder)

Vector functions

These functions operate on vectors to produce a scalar or on a column-by-column fashion to produce a row vector. Use the transpose to get row-by-row operation.

max   min   sum   prod   median  
sort  std   any   all    mean
max(max(A)) give the maximum entry in A.

Matrix functions

eig : eigenvalues and eigenvectors. x=eig(A) returns eigenvalues of A in x but [v,x]=eig(A) gives the eigenvectors in v and the eigenvalues as entries in a diagonal matrix, x.
inv : inverse
rref : row-reduced echelon form
expm : matrix exponential
sqrtm : matric square root
poly : coefficients of the characteristic polynomial, highest first.
det : determinant
trace : trace
size : size of the matrix
rank : rank of the matrix

  5. Submatrices and colon notation

  6. Output format

MATLAB lets you output in various formats. Amond them are the following:

format short Fixed point with 4 decimal places
format long Fixed point with 14 decimal places
format short e Scientific with 4 places
format long e Scientific with 14 places
format rat rational approximation

  7. Graphics

This is just a small subset of all the graphics MATLAB can do.

plot(x,y) This plots the vector y as a function of x. Thus, the following will plot sin(x):

and the following plots sin(x) and cos(x) on the same plot:
Alternatively you could write the last line as plot(x,y1,y2).

The command

print bob
will produce a file of the plot for printing called ``''

  8. Recording your work

Type diary filename and everything you type and that MATLAB outputs is recorded in the file called filename. Type diary off to turn it off and diary to turn it back on. Once done with MATLAB, you can print the output.

Return to Math 25 Information