Introduction to MATLAB (1)
Introduction to MATLAB (1)
Command window
• Main window – where commands are entered
Example of MATLAB Release 13 desktop
Variables
– Vectors and Matrices –
ALL variables are matrices
e.g. 1 x 1Variables
4x1 1x4 2x4
3 3 2 i.e1x 7X
4 •They arecase–sensitive 2 1 5 6
2 9 3 2 4
•Their names
can contain up to 31
characters
9with
•Must start a letter
3
EDU» A(2,:)
EDU» A(1,:)
ans =
ans =
4 5 6
1 2 3
Vectors and Matrices
EDU» 1/0
Warning: Divide by zero.
beep
ans =
pi () Inf
inf (e.g. 1/0) EDU» pi
ans =
i, j ( 1 )
3.1416
EDU» i
ans =
0+ 1.0000i
Vectors and Matrices
EDU» A-2
ans =
-1 0 1
2 3 4
5 6 7
Vectors and Matrices
EDU» A/3
ans =
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
2.3333 2.6667 3.0000
Vectors and Matrices
1 2 3 1 1 1 14 14 14
4 5 6 2 2 2 32 32 32
=
A*B 7 8 9 3 3 3 50 50 50
1x1 2 x1 3x1 1 2 3
4 x 2 5x 2 6 x 2 8 10 12
A.*B =
7 x 3 8x 3 9 x 3 21 24 27
Vectors and Matrices
A/B ?
11 21 31 1 2 3
A.^B 2
4 52
62
=
16 25 36
73 83 93 343 512 729
Vectors and Matrices
Example:
-j5
j10
2-90o 10 1.50o
A x = y
Vectors and Matrices
V1 = 16.1229.7o V
Built in functions
(commands)
Scalar functions – used for scalars and operate
element-wise when applied to a matrix or vector
EDU» b=sin(a)
b =
Columns 1 through 7
0 0.6428 0.9848 0.8660 0.3420 -0.3420
-0.8660
Columns 8 through 10
-0.9848 -0.6428 0.0000
EDU»
Built in functions (commands)
x=
0.9501 0.8913 0.8214 0.9218 ans =
A x = y
EDU» x=inv(A)*y
x =
14.0000+ 8.0000i
28.0000+ 1.0000i
Built in functions (commands)
0.6
0.4
0.2
y1 and y2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
angular frequency (rad/s)
Built in functions (commands) eg2_srf.m
Script Function
Collections of commands User defined commands
Executed in sequence when called Normally has input &
Saved with extension “.m” output
Saved with extension “.m”
M-files : script and function files (script) eg1_plt.m
At Matlab prompt type in edit to invoke M-file editor
R = 10 C
+
V L
–
Exercise 1:
Write an m–file to plot Z, Xc and XLversus
frequency for R =10, C = 100 uF, L = 0.01 H.
M-files : script and function files (script)
When XC XL
1
o
LC
M-files : script and function files (script) eg4.m
eg5_exercise1.m
Example – RLC circuit
120
Z
Xc
100 Xl
80
60
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
M-files : script and function files (script) eg6.m
For a given values of C and L, plot the following versus the frequency
a) the total impedance ,
b) Xc and XL
c) phase angle of the total impedance
for 100 < < 2000
M-files : script and function files (script) eg6.m
Example – RLC circuit
Magnitude
100
Mag imp
80
Xc
60 Xl
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
Phase
100
50
-50
-100
0 200 400 600 800 1000 1200 1400 1600 1800 2000
M-files : script and function files (function)
function output=function_name(inputs)
Output variable
Must match the input variable
file name
M-files : script and function files (function)
Function – a simple example
function y=react_C(c,f)
%react_C calculates the reactance of a capacitor.
%The inputs are: capacitor value and frequency in hz
%The output is 1/(wC) and angular frequency in rad/s
y(1)=2*pi*f;
w=y(1);
y(2)=1/(w*c);
File must be saved to a known path with filename the same as the
function name and with an extension ‘.m’
if nargin <4
error('not enough input arguments')
end;
x(1) = 1/(w*c);
x(2) = w*l;
Zt = r + (x(2) - x(1))*i;
x(3) = abs(Zt);
x(4)= angle(Zt);
M-files : script and function files (function) eg7_fun.m
R=input('Enter R: ');
C=input('Enter C: ');
L=input('Enter L: ');
w=input('Enter w: ');
y=impedance(R,C,L,w);
fprintf('\n The magnitude of the impedance at %.1f
rad/s is %.3f ohm\n', w,y(3));
fprintf('\n The angle of the impedance at %.1f rad/s is
%.3f degrees\n\n', w,y(4));
Simulink
Used to model, analyze and simulate dynamic
systems using block diagrams.
Provides a graphical user interface for constructing
block diagram of a system – therefore is easy to use.
However modeling a system is not necessarily easy !
Simulink
i 10 100 uF
+
v(t) = 5 sin t 0.01 H
–
sV R 2 I
sI s I
L L LC
sV 2 R 1
I s s
L L LC
Simulink
s(1/ L)
V R 1 I
2
s s
L LC
Simulink
1
simout
s+1
Sine Wave Transfer Fcn To Workspace
Simulink
s(1/ L)
s(100)
2 R 1
s s 2
s 1000s 110 6
L LC
100s
i
s2+1000s+1e6
Sine Wave To Workspace
Transfer Fcn
To Workspace1
Simulink eg8_sim.mdl
Ramp Amplitude v
w To Workspace3
To Workspace2 100s
1 i
sin s2+1000s+1e6
1000 s To Workspace
Dot Product2 Transfer Fcn1
Constant Dot Product3 Integrator Elementary
Math
Simulink
0.5
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Simulink eg9_sim.mdl
5
Constant1
100s
1
2000 0.802 sin s2+1000s+1e6
s Scope
Constant Dot Product2
Slider Integrator Elementary Transfer Fcn
Gain Math
Simulink
Masking
Masking
Simulink
i 10 100 uF
+ vc –
Vs
0.01 H
Simulink
st 2R 1 2 R 1
Ae s s 0 s s 0
L LC L LC
Characteristic equation
Simulink
Vc 1
I sC
Vs s(1/ L) I 1 Vc
R 1 sC
s2 s
L LC
Simulink
Method 1
By displaying the response (Vc) using scope as we change R
Method 2
By displaying the response (Vc) using 3–D plot with t and R as the
variables
Simulink
Method 1
V s(1/ L) I 1
Vc
R 1 sC
s2 s
L LC
Simulink eg11_sim_square.mdl
Simulink
Method 2
for i=1:40,
r=5+i*0.5;
set_param('eg11_sim_step_3D/Subsystem/Transfer Fcn','Denominator','[1 r*100 1e6]');
sim('eg11_sim_step_3D');
for j=1:400,
out(i,j)=vc(j,1);
end;
end;