Professional Documents
Culture Documents
MATLAB Tutorial
1. The Basics
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
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.
>> x = [-5:15];
>> 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))
2
3. Predefined Variables
A column vector containing the values of these variables could be produced by typing
4. Built-in Functions
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
>>help stem
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.
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
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)