MATLAB_notes
MATLAB_notes
What is MATLAB?
MATLAB (MATrix LABoratory) is an interactive program for scientific and engineering numeric calculation
and programming language
>> 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
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θ
z=
1.0000 - 2.0000i
z = a + jb = z e jθ
z = Re{z} + Im{z} = a 2 + b 2
2 2
⎛ Im{z} ⎞ ⎛b⎞
θ = tan −1 ⎜⎜ ⎟⎟ = tan −1 ⎜ ⎟
⎝ Re{z} ⎠ ⎝a⎠
⎛2⎞
In our case: θ = tan −1 ⎜ ⎟ = 1.10715
⎝1⎠
mag_z =
2.2361
theta =
-1.1071
theta_deg =
-63.4349
Complex conjugate
z_conj =
1.0000 + 2.0000i
z=
Slobodan Pajic (WPI) MATLAB notes 4
0 -13.0000i
>> q=x/y
q=
0.9231 - 0.3846i
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
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:
For complete list and help on elementary matrices and matrix functions type help elmat and for
complete list on special matrices, type help specmat
>> 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
H=
1 2
3 4
5 6
>>size(H)
ans =
3 2
m=
3
n=
2
Matrix multiplication
Recall: Multiplication of two matrices can be implemented if A∈ℜ(m×p) B∈ℜ(p×n) and
C=AB∈ℜ(m×n)
A=
1 2 3
4 5 6
B=
1 2
3 4
5 6
>> C=A*B
C=
22 28
49 64
− 4 x1 + 3x 2 − 6 x3 = 1
2 x1 − 8 x 2 − 3x3 = 2
− 2 x1 − x 2 − 5 x3 = 3
⎛ − 4 3 − 6⎞ ⎛1⎞
⎜ ⎟ ⎜ ⎟
A = ⎜ 2 − 8 − 3⎟ b = ⎜ 2⎟
⎜ − 2 −1 − 5⎟ ⎜ 3⎟
⎝ ⎠ ⎝ ⎠
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
MATLAB: q = quad(fun,a,b)
Example:
0.01
q= ∫ 60 ⋅ cos(100t ) * sin (50t )dt
0
Slobodan Pajic (WPI) MATLAB notes 8
q=
0.1124
Polynomials
Let assume following polynomials
u = s 3 − 5s 2 + 8 s − 4
v = s 2 − 3s + 2
Polynomial multiplication
u=[1 -5 8 -4]
v=[1 -3 2]
x=
1 -8 25 -38 28 -8
q=
1 -2
r=
0 0 0 0
Slobodan Pajic (WPI) MATLAB notes 9
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
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 )
2s 2 + 13s + 1
s 3 + 7 s 2 + 6s
r=
-0.1667
2.0000
0.1667
p=
-6
-1
0
k=
[]
Therefore
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).
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
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
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