Slobodan Pajic (WPI) MATLAB notes 1
What is MATLAB?
MATLAB (MATrix LABoratory) is an interactive program for scientific and engineering numeric calculation
and programming language
Getting help from within MATLAB
>> help <functionname> Shows help document for a give function
>> doc <functionname>
>> lookfor <keyword> Searches all the help documents for a given keyword
Variables
• Real/complex scalars
• Real/complex vectors
• Real/complex matrices
• Symbolic variables
Note: Variable Names are case sensitive
Math Functions
MATLAB does not perform trigonometric operations using units of degrees
Trigonometric function Description
cos(x)/acos(x) Cosine/Inverse cosine
sin(x)/asin(x) Sine/Inverse sine
tan(x)/atan(x) Tangent/Inverse tangent
cot(x)/acot(x) Cotangent/Inverse cotangent
cosh(x)/acosh(x) Hyperbolic cosine/Inverse hyperbolic cosine
sinh(x)/asinh(x) Hyperbolic sine/Inverse hyperbolic sine
tanh(x)/atanh(x) Hyperbolic tangent/Inverse hyperbolic tangent
coth(x)/acoth(x) Hyperbolic cotangent/Inverse hyperbolic cotangent
Exponential function Description
^ Power
exp Exponential
log Natural logarithm (ln)
log10 Base 10 logarithm
sqrt Square root
.* ./ .^ Element-by element operators
Slobodan Pajic (WPI) MATLAB notes 2
Note that for multiplying, dividing and exponentiating on a term-by-tem basis, you must precede
the operator with a period.
Complex Numbers
All the MATLAB arithmetic operations are available for complex operations. The imaginary unit
is predefined for two variables i = j = − 1 .
z=a+jb=|z|ejθ
Let’s define complex number: z = 1- j2 in MATLAB
MATLAB EXAMPLE: – complex number
>> z=1-2i
z=
1.0000 - 2.0000i
Calculating magnitude and phase angle
z = a + jb = z e jθ
z = Re{z} + Im{z} = a 2 + b 2
2 2
In our case: z = 12 + 2 2 = 5 = 2.236
Slobodan Pajic (WPI) MATLAB notes 3
⎛ Im{z} ⎞ ⎛b⎞
θ = tan −1 ⎜⎜ ⎟⎟ = tan −1 ⎜ ⎟
⎝ Re{z} ⎠ ⎝a⎠
⎛2⎞
In our case: θ = tan −1 ⎜ ⎟ = 1.10715
⎝1⎠
MATLAB EXAMPLE: – complex number
>>mag_z=abs(z) % Magnitude (absolute value) of complex number
mag_z =
2.2361
>> theta=angle(z) % angle(Z) returns the phase angle in radians
theta =
-1.1071
>> theta_deg=theta*180/pi % Phase angle in degrees
theta_deg =
-63.4349
Complex conjugate
MATLAB EXAMPLE: complex conjugate
>> z_conj=conj(1-2i) % conj(z) returns the complex conjugate of the z
z_conj =
1.0000 + 2.0000i
Complex number multiplication and division
MATLAB EXAMPLE – complex number multiplication/division
>> x=2-3i;
>> y=3-2i;
>> z=x*y
z=
Slobodan Pajic (WPI) MATLAB notes 4
0 -13.0000i
>> q=x/y
q=
0.9231 - 0.3846i
Useful complex number commands
real(Z) returns the real part of the elements of the complex array Z.
imag(Z) returns the imaginary part of the elements of array Z.
z*z=z^2
Vector operations
• In MATLAB, elements enclosed by brackets and separated by semicolons generate a
column vector.
• Transpose of a column vector results in a row vector, and vice versa. Transpose operation
in MATLAB is denoted by (‘)
MATLAB EXAMPLE – vector transpose operation
>> x=[1;2;3;4;5]
x=
1
2
3
4
5
>> y=x’
y=
1 2 3 4 5
There are many special utility matrices which are useful for matrix operations. A few examples
are:
eye(m,n) Generates an m×n identity matrix
zeros(m,n) Generates an m×n matrix of zeros
ones(m,n) Generates an m×n matrix of ones
Slobodan Pajic (WPI) MATLAB notes 5
diag(x) Produces a diagonal matrix with the elements of x on the diagonal
Ex. try x= [1 2 3 4] , diag(x)
For complete list and help on elementary matrices and matrix functions type help elmat and for
complete list on special matrices, type help specmat
MATLAB EXAMPLE – array merging
>> x=(1:5);
>> y=(6:9);
>> z=[x y]
z=
1 2 3 4 5 6 7 8 9
Comas or spaces are used to separate elements in a specific row, and semicolons are used to
separate individual rows
MATLAB EXAMPLE size of the matrix
>> H=[1 2;3 4;5 6]
H=
1 2
3 4
5 6
>>size(H)
ans =
3 2
>> [m,n] = size(H)
m=
3
n=
2
[m,n]=size(H) returns a size of each dimension of a matrix
m – number of rows
n – number of columns
Slobodan Pajic (WPI) MATLAB notes 6
r=size(H,1) returns the number of rows in H
c=size(H,2) returns the number of columns in H
n=length(H) returns max(size(H)) when H is a matrix, when H is a vector returns length of a
vector
Matrix multiplication
Recall: Multiplication of two matrices can be implemented if A∈ℜ(m×p) B∈ℜ(p×n) and
C=AB∈ℜ(m×n)
MATLAB EXAMPLE – matrix multiplication
>> A=[1 2 3;4 5 6]
A=
1 2 3
4 5 6
>> B=[1 2;3 4;5 6]
B=
1 2
3 4
5 6
>> C=A*B
C=
22 28
49 64
A(:,j)is the j-th column of A
A(i,:)is the i-th row of A
Slobodan Pajic (WPI) MATLAB notes 7
Solving system of equations
The most common situation involves square system of linear equations
− 4 x1 + 3x 2 − 6 x3 = 1
2 x1 − 8 x 2 − 3x3 = 2
− 2 x1 − x 2 − 5 x3 = 3
Consider following system of equations: Ax = b
Where:
⎛ − 4 3 − 6⎞ ⎛1⎞
⎜ ⎟ ⎜ ⎟
A = ⎜ 2 − 8 − 3⎟ b = ⎜ 2⎟
⎜ − 2 −1 − 5⎟ ⎜ 3⎟
⎝ ⎠ ⎝ ⎠
Solution process in MATLAB will look like
MATLAB EXAMPLE
>> A=[-4 3 -6;2 -8 -3;-2 -1 -5]
>> b=[1;2;3];
>> x=A\b
x=
-11.5000
-5.0000
5.0000
Compute the Integral
b
q = ∫ f ( x)dx
a
MATLAB: q = quad(fun,a,b)
Example:
0.01
q= ∫ 60 ⋅ cos(100t ) * sin (50t )dt
0
Slobodan Pajic (WPI) MATLAB notes 8
MATLAB EXAMPLE Compute the Integral
>> q=quad('(60*cos(100*t)).*sin(50*t)',0,0.01)
q=
0.1124
Polynomials
Let assume following polynomials
u = s 3 − 5s 2 + 8 s − 4
v = s 2 − 3s + 2
Polynomial multiplication
x = u v = s 5 − 8s 4 + 25s 3 − 38s 2 + 28s − 8
In MATLAB they can be defined as follows
u=[1 -5 8 -4]
v=[1 -3 2]
MATLAB EXAMPLE Polynomial multiplication/division
>> u=[1 -5 8 -4];
>> v=[1 -3 2];
>> x=conv(u,v) % polynomial multiplication
x=
1 -8 25 -38 28 -8
>> [q r]=deconv(u,v) % polynomial division (q- quotient; r- reminder)
q=
1 -2
r=
0 0 0 0
Slobodan Pajic (WPI) MATLAB notes 9
Finding the roots of the polynomial
u = s 3 − 5s 2 + 8s − 4 = (s − 2) (s − 1)
2
Given the roots of the polynomial it is also possible to construct the associated polynomial
MATLAB EXAMPLE roots of the polynomial
>> u=[1 -5 8 -4];
>> a=roots(u) % Finding the roots of the polynomial
a=
2.0000
2.0000
1.0000
>> g = [1 2 3]
>> t = poly(g) % polynomial construction from its roots
t=
1 -6 11 -6
Another common operation is to find the partial fraction expansion of a rational polynomial
[r,p,k]=residue(num,den)
num r( 1 ) r( 2 ) r( n )
= + +L+ +k
den s − p( 1 ) s − p( 2 ) s − p( n )
example: find partial fraction expansion of the following rational polynomial
2s 2 + 13s + 1
s 3 + 7 s 2 + 6s
MATLAB EXAMPLE partial fraction expansion
>> num = [2 13 1];
>> den = [1 7 6 0];
Slobodan Pajic (WPI) MATLAB notes 10
>> [r,p,k]=residue(num,den) % Finding partial fraction expansion
r=
-0.1667
2.0000
0.1667
p=
-6
-1
0
k=
[]
Therefore
2s 2 + 13s + 1 − 0.1667 2 0.1667
= + +
s + 7 s + 6s
3 2
s+6 s +1 s
Save session to file
>> diary(‘filename’)writes a copy of all subsequent keyboard input and
the resulting output to the named file in the current
MATLAB directory
>> diary off suspends the diary
>> diary on resumes diary mode using the current filename
Saving variables
• Save all or some of the variables you have defined during a MATLAB session, and then
load them in a later MATLAB session (MAT-files).
MATLAB EXAMPLE: saving/loading variables
>> save my_filename % saves all variables from workspace into my_filename.MAT
>> save some_of_my_variables var_a var_b % saves var_a and var_b
>> load my_filename
Slobodan Pajic (WPI) MATLAB notes 11
>> load some_of_my_variables
Symbolic Computation in MATLAB
Bilinear transformation (mapping from the s-plane to the z-plane)
2 1 − z −1
s=
T 1 + z −1
s
H( s ) = Find H(z) ?
s +ω
MATLAB EXAMPLE
>> clear
>> syms z w;
>> s = 2*(1-z^(-1))/(1+z^(-1));
>> H = s/(s+w);
>> pretty(collect(simplify(H),z))
Result
z −1
H ( z) = 2
(2 + ω ) z − 2 + ω
Programming in MATLAB
M-Files
• Sets of MATLAB commands can be executed via scripts
• Scripts are written into files with extensions *.m
Ex. filename.m
• These scripts are executed in MATLAB by entering the name of .m file
>> filename
Functions
• Commonly performed operations can be written into functions
• In a file named functionname.m
Slobodan Pajic (WPI) MATLAB notes 12
Function [output]=functionname(input)
Command 1
Command 2
File → New → M-file
MATLAB EXAMPLE: M file
clear
t = -4*pi:pi/10:4*pi;
y1 = sin(t);
y2 = cos(t);
figure(1)
plot(t,y1,t,y2)
title('Plot of sin(\theta) and cos(\theta)')
xlabel('-4\pi \leq \theta \leq 4\pi'), ylabel('function')
legend('y_{1}=sin(\theta)','y_{2}=cos(\theta)')
text(-3.5,0.6,' \leftarrow '), text(-2,0.6,'y_{1}'),
text(-8.5,0.6,' \rightarrow '), text(-9,0.6,'y_{2}')
figure(2)
subplot(2,1,1), plot(t,y1,'-.'), title('y_{1}=sin(\theta)')
subplot(2,1,2), plot(t,y2,'m--'), title('y_{2}=cos(\theta)')
figure(3)
q=sin(t)./t;
plot(t,q), grid on
title('Plot of sin(\theta)/\theta')
xlabel('-4\pi \leq \theta \leq 4\pi'), ylabel('function')
Figure No. 1
Slobodan Pajic (WPI) MATLAB notes 13
Plot of sin(θ) and cos(θ)
1
y 1=sin(θ)
y 2=cos(θ)
0.8
0.6 y 2→ ← y1
0.4
0.2
function
-0.2
-0.4
-0.6
-0.8
-1
-15 -10 -5 0 5 10 15
-4π ≤ θ ≤ 4π
Figure No. 2
y 1=sin(θ)
1
0.5
-0.5
-1
-15 -10 -5 0 5 10 15
y2=cos(θ)
1
0.5
-0.5
-1
-15 -10 -5 0 5 10 15
Figure No. 3
Slobodan Pajic (WPI) MATLAB notes 14
Plot of sin(θ)/θ
1
0.8
0.6
0.4
function
0.2
-0.2
-0.4
-15 -10 -5 0 5 10 15
-4π ≤ θ ≤ 4π
References:
MathWorks: http://www.mathworks.com
Tutorial: http://www.mathworks.com/academia/student_center/tutorials/launchpad.html
“Mastering MATLAB 6” Duane Hanselman & Bruce Littlefield, Prentice Hall, 2001