Examples
Examples
Introduction to Matlab
Matlab Basics
Vectors and Matrices
Loops
Plots
MATLAB examples
Program :
>> r=10;
>> i=0:2:10;
>> v=i.*r;
>> p=(i.^2)*r;
>> sol=[p v i]
Output :
sol =
Columns 1 through 6
0
40
160
360
640
60
80
1000
Columns 7 through 12
0
20
40
100
Columns 13 through 18
0
7/31/15 09:48:50 AM
10
MATLAB PRESENTATION
7/31/15 09:48:50 AM
MATLAB PRESENTATION
7/31/15 09:48:50 AM
MATLAB PRESENTATION
7/31/15 09:48:50 AM
MATLAB PRESENTATION
MATLAB PRESENTATION
7/31/15 09:48:50 AM
MATLAB PRESENTATION
290o
10
j10
1.50
o
MATLAB PRESENTATION
+ j0.1V2 = 1.5
j0.1
V1
V
2
j2
=
1
.
5
7/31/15 09:48:51 AM
MATLAB PRESENTATION
12
SNO
se
re
1
2
3
4
5
1
2
3
4
5
2
3
4
5
6
7/31/15 09:48:51 AM
Resistance
0.0005
0.0005
0.0015
0.0251
0.3660
MATLAB PRESENTATION
Reactance
0.0012
0.0012
0.0036
0.0294
0.1864
13
ldata=[
1 1 2 0.0005 0.0012
2 2 3 0.0005 0.0012
3 3 4 0.0015 0.0036
4 4 5 0.0251 0.0294
5 5 6 0.3660 0.1864
];
7/31/15 09:48:51 AM
MATLAB PRESENTATION
14
clear all
ldata=[
1 1 2 0.0005 0.0012
2 2 3 0.0005 0.0012
3 3 4 0.0015 0.0036
4 4 5 0.0251 0.0294
5 5 6 0.3660 0.1864
];
nb=6;
nl=nb-1;
se=ldata(:,2);
re=ldata(:,3);
r=ldata(:,4);
x=ldata(:,5)
for i=1:nl
z(i)=r(i)+j*x(i);
end
for i=1:5
disp(z(i))
end
7/31/15 09:48:51 AM
MATLAB PRESENTATION
15
7/31/15 09:48:51 AM
MATLAB PRESENTATION
16
Example RLC
circuit
R = 10
eg4.m
eg5_exercise1.m
+
V
Exercise 1:
Write an mfile to plot Z, Xc and XLversus
frequency for R =10, C = 100 uF, L = 0.01 H.
7/31/15 09:48:51 AM
MATLAB PRESENTATION
17
Example RLC
circuit
Total impedance is given
by:
Z R j L
When
X C XL
Z R
1
o
LC
7/31/15 09:48:51 AM
MATLAB PRESENTATION
18
eg4.m
eg5_exercise1.m
Example RLC
circuit
120
Z
Xc
Xl
100
80
60
40
20
7/31/15 09:48:51 AM
200
400
600
800
1000
1200
1400
MATLAB PRESENTATION
1600
1800
2000
19
eg6.m
Example RLC
circuit
R = 10
+
V
For a given values of C and L, plot the following versus the frequency
a)
b)
Xc and XL
c)
7/31/15 09:48:51 AM
MATLAB PRESENTATION
20
Example RLC
circuit100
eg6.m
Magnitude
Mag imp
Xc
Xl
80
60
40
20
0
200
400
600
800
1000
1200
1400
1600
1800
2000
1200
1400
1600
1800
2000
Phase
100
50
0
-50
-100
7/31/15 09:48:51 AM
200
400
600
800
1000
MATLAB PRESENTATION
21
Simulink
7/31/15 09:48:51 AM
MATLAB PRESENTATION
22
Simulink
i
Varies from 0
to 2000 rad/s
100 uF
+
v(t) = 5 sin t
0.01 H
MATLAB PRESENTATION
23
Simulink
10
100 uF
+
v(t) = 5 sin t
0.01 H
7/31/15 09:48:52 AM
di 1
v iR L
idt
dt C
MATLAB PRESENTATION
24
Simulink
1 dv di R d i
i
2
L dt dt L dt LC
Taking Laplace transform:
sV R
I
2
sI s I
L L
LC
sV
1
2 R
I s s
L
L
LC
7/31/15 09:48:52 AM
MATLAB PRESENTATION
25
Simulink
I V
7/31/15 09:48:52 AM
s(1/ L)
R
1
2
s s
L
LC
s(1/ L )
R
1
2
s s
L
LC
MATLAB PRESENTATION
26
Simulink
It is where we obtain
the blocks to
construct our model
7/31/15 09:48:52 AM
It is here where we
construct our model.
MATLAB PRESENTATION
27
Simulink
7/31/15 09:48:52 AM
s+1
Transfer Fcn
MATLAB PRESENTATION
simout
To Workspace
28
Simulink
s(1/ L)
R
1
2
s s
L
LC
s(100)
2
6
s 1000s 1 10
100s
s2+1000s+1e6
Sine Wave
Transfer Fcn
i
To Workspace
v
To Workspace1
7/31/15 09:48:52 AM
MATLAB PRESENTATION
29
Simulink
eg8_sim.mdl
Ramp
v
To Workspace3
w
1
1000
Constant
To Workspace2
s
Integrator
Dot Product3
100s
s2+1000s+1e6
sin
Elementary
Math
Dot Product2
Transfer Fcn1
i
T o Workspace
MATLAB PRESENTATION
30
Simulink
1
0.5
0
-0.5
-1
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
-5
7/31/15 09:48:52 AM
MATLAB PRESENTATION
31
Simulink
eg9_sim.mdl
5
Constant1
2000
Constant
0.802
Slider
Gain
7/31/15 09:48:52 AM
100s
sin
s
Dot Product2
Integrator Elementary
Math
MATLAB PRESENTATION
s2+1000s+1e6
Transfer Fcn
Scope
32