You are on page 1of 6

E E 338 Fall 2001

MATLAB Tutorial

1. The Basics

• MATLAB is an interactive software development tool intended to facilitate the


analysis and visualization of numerical data.
• The basic object in MATLAB is the data matrix and the manipulation of these objects
generally follows the rules of matrix algebra.
• Lines of program can be entered and executed one at a time in the Command Window
or as groups of commands from a script file.
• A MATLAB script file is just a text file with the .m extension and can be produced
with any word processor.
• In the Command Window, the prompt for input is >>.

To enter a matrix the square brackets [ ] are used and it is usually assigned to a variable
name. So, for example, if
é1 2 3 4 ù
A = êê5 6 7 8 úú
êë9 10 11 12 úû
We would proceed as follows:

>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]

The semicolon inside the brackets delimits the rows of the matrix.

Now if we type:

>> A

MATLAB would respond as:

A =

1 2 3 4
5 6 7 8
9 10 11 12

Now if we type:

>> A = [1 2; 3 4];
>> B = [1 1; 0 1];
>> C = A * B

1 of 6
MATLAB would respond as:

C =

1 3
3 7

>>

So that C is the matrix product of A and B. The semicolon at the end of a line is used to
suppress the output of the result of executing the command.

To input a row vector, the colon operator can be used:

>> x = [-5:15];

To convert x to a column vector, the transpose operator can be used:

>> y = x';

To do the dot product between two row vectors x and w, we could type:

>> z = x * w';

2. Variable Formats

• Numeric
>> x = 5.3; % scalars, vectors matrices etc.

• String
>> y = 'this is text'; % used to label axes, plots etc.

• Logical
>> x = [1 2 3];
>> y = [3 2 1+3j];
>> z = x == y

z = 0 1 0 % z is a logical variable

• Symbolic
>> x = sym('x') % the string x becomes symbolic
>> y = diff(cos(x))

y = -sin x % the derivative of cos x

2
3. Predefined Variables

A number of useful variables have been predefined in MATLAB.

ans, i, j, pi, inf, eps, nan

A column vector containing the values of these variables could be produced by typing

>>v = [ans; i; j; pi; inf; eps; nan];

Complex numbers can be entered by typing

>>r = 2 - j * y; % y must already be defined.


or
>>r = 2 - 3j; % a convenient short form.

4. Built-in Functions

MATLAB contains literally hundreds of built-in functions including the conventional


trigonometric, exponential functions, those that produce conventional plots and other
visuals displays and those that perform specialized operations such as Fourier and
Laplace transforms, Bode plots, statistical regression and so on. Generally the
elementary mathematical functions take matrices and complex numbers as arguments.

For example:

>> n = 0:99;
>> x = 2.54 * sin(.1*pi*n + pi/8);
>> subplot(2,2,1)
>> plot(n, x)
>> grid
>> m = 0:10;
>> z = -3.2 * exp(.1j*pi*m);
>> subplot(2,2,2)
>> stem(m, real(z), 'filled')
>> grid

3 4

2
2
1

0 0

−1
−2
−2

−3 −4
0 20 40 60 80 100 0 2 4 6 8 10

3
>> myprog % user created m file

To find out what arguments a function requires, type:

>>help stem

Elementary math functions.

Trigonometric.
sin - Sine.
sinh - Hyperbolic sine.
asin - Inverse sine.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosh - Hyperbolic cosine.
acos - Inverse cosine.
acosh - Inverse hyperbolic cosine.
tan - Tangent.
tanh - Hyperbolic tangent.
atan - Inverse tangent.
atan2 - Four quadrant inverse tangent.
atanh - Inverse hyperbolic tangent.
sec - Secant.
sech - Hyperbolic secant.
asec - Inverse secant.
asech - Inverse hyperbolic secant.
csc - Cosecant.
csch - Hyperbolic cosecant.
acsc - Inverse cosecant.
acsch - Inverse hyperbolic cosecant.
cot - Cotangent.
coth - Hyperbolic cotangent.
acot - Inverse cotangent.
acoth - Inverse hyperbolic cotangent.

Exponential.
exp - Exponential.
log - Natural logarithm.
log10 - Common (base 10) logarithm.
log2 - Base 2 logarithm.
pow2 - Base 2 power.
sqrt - Square root.
nextpow2 - Next higher power of 2.

4
Complex.
abs - Absolute value.
angle - Phase angle.
complex - Construct complex data.
conj - Complex conjugate.
imag - Complex imaginary part.
real - Complex real part.
unwrap - Unwrap phase angle.
isreal - True for real array.

Rounding and remainder.


fix - Round towards zero.
floor - Round towards minus infinity.
ceil - Round towards plus infinity.
round - Round towards nearest integer.
mod - Modulus (signed remainder).
rem - Remainder after division.
sign - Signum.

5. Operators

Arithmetic operators.
plus - Plus +
uplus - Unary plus +
minus - Minus -
uminus - Unary minus -
mtimes - Matrix multiply *
times - Array multiply .*
mpower - Matrix power ^
power - Array power .^
mldivide - Backslash or left matrix divide \
mrdivide - Slash or right matrix divide /
ldivide - Left array divide .\
rdivide - Right array divide ./

Relational operators.
eq - Equal ==
ne - Not equal ~=
lt - Less than <
gt - Greater than >
le - Less than or equal <=
ge - Greater than or equal >=

5
Logical operators.
and - Logical AND &
or - Logical OR |
not - Logical NOT ~
xor - Logical EXCLUSIVE OR
any - True if any element of vector is nonzero
all - True if all elements of vector are nonzero

6. Workspace Management Commands

who - List current variables


whos - List current variables with attributes
clear - Clear variables from memory
save - Save workspace variables to a file
load - Load workspace variables from a file
cd - change directory
dir - show current directory
pwd - path to current directory
quit - Quit MATLAB

7. Example from Continuous Systems

Suppose that we have the transfer function:


3s + 2
H ( s) =
s + 2.22 s 2 + 1.74 s + 0.33
3

Analysis of this system in MATLAB could proceed as:

>> b = [ 3 2 ]; % numerator polynomial coefficients


>> a = [ 1 2.22 1.74 0.33 ]; % denominator coefficients
>> w = 2*pi*[ 0: 0.01: 10 ]; % radial frequency vector
>> freqs(b, a, w) % produces a Bode plot

2
10

0
10
Magnitude

−2
10

−4
10
−1 0 1
10 10 10
Frequency (radians)

−50
Phase (degrees)

−100

−150

−200
−1 0 1
10 10 10
Frequency (radians)

You might also like