Matlab Programming for Engineers
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
Use MATLAB in Electrical
Circuits :
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
Solution usng MATLAB :
Z = [40 -10 -30;
-10 30 -5;
-30 -5 65];
V = [10 0 0]';
% solve for the loop currents
I = inv(Z)*V;
% current through RB is calculated
IRB = I(3) - I(2);
fprintf('the current through R is %f Amps \n',IRB)
% the power supplied by source is calculated
PS = I(1)*10;
fprintf('the power supplied by 10V source is %f watts
\n',PS)
7/31/15 09:48:50 AM
MATLAB PRESENTATION
MATLAB answers are :
the current through R is 0.037037 Amps
the power supplied by 10V source is
4.753086 watts
7/31/15 09:48:50 AM
MATLAB PRESENTATION
Vectors and Matrices
Arithmetic operations Matrices
Example:
-j5
290o
10
j10
1.50
o
Solve for V1 and V2
7/31/15 09:48:50 AM
MATLAB PRESENTATION
Vectors and Matrices
Arithmetic operations Matrices
Example (cont)
(0.1 + j0.2)V1 j0.2V2 = -j2
- j0.2V1
+ j0.1V2 = 1.5
0.1 j0.2 j0.2
j0.2
j0.1
V1
V
2
j2
=
1
.
5
Vectors and Matrices
Arithmetic operations Matrices
Example (cont)
>>>A=[(0.1+0.2j)0.2j;0.2j0.1j]
A=
0.1000+0.2000i00.2000i
00.2000i0+0.1000i
>>>y=[2j;1.5]
y=
02.0000i
1.5000
*
>>>x=A\y
A\B is the matrix division of A in
x=
to B, which is roughly the same as
14.0000+8.0000i
INV(A)*B
28.0000+1.0000i
*
>>>
Vectors and Matrices
Arithmetic operations Matrices
Example (cont)
>>>V1=abs(x(1,:))
V1=
16.1245
>>>V1ang=angle(x(1,:))
V1ang=
0.5191
V1 = 16.1229.7o V
7/31/15 09:48:51 AM
MATLAB PRESENTATION
12
HOW TO IMPORT LOAD DATA
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
If R = 10 Ohms and the current is increased from 0 to 10
A with increments of 2A, write a MATLAB program to
generate a table of current, voltage and power
dissipation.
7/31/15 09:48:51 AM
MATLAB PRESENTATION
16
M-files : script and function files (script)
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
M-files : script and function files (script)
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
M-files : script and function files (script)
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
M-files : script and function files (script)
Example RLC
circuit
R = 10
+
V
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
7/31/15 09:48:51 AM
MATLAB PRESENTATION
20
M-files : script and function files (script)
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
Model simplified representation of a system e.g. using
mathematical equation
We simulate a model to study the behavior of a system
need to verify that our model is correct expect results
Knowing how to use Simulink or MATLAB does not
mean that you know how to model a system
7/31/15 09:48:51 AM
MATLAB PRESENTATION
22
Simulink
Problem: We need to simulate the resonant circuit
and display the current waveform as we change the
frequency dynamically.
10
i
Varies from 0
to 2000 rad/s
100 uF
+
v(t) = 5 sin t
0.01 H
Observe the current. What do we expect ?
The amplitude of the current waveform will become
maximum at resonant frequency, i.e. at = 1000 rad/s
7/31/15 09:48:52 AM
MATLAB PRESENTATION
23
Simulink
How to model our resonant circuit ?
i
10
100 uF
+
v(t) = 5 sin t
0.01 H
Writing KVL around the loop,
7/31/15 09:48:52 AM
di 1
v iR L
idt
dt C
MATLAB PRESENTATION
24
Simulink
Differentiate wrt time and re-arrange:
2
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
Thus the current can be obtained from the voltage:
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
Start Simulink by typing simulink at Matlab prompt
Simulink library and untitled windows appear
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
Constructing the model using Simulink:
Drag and drop block from the Simulink library
window to the untitled window
1
Sine Wave
7/31/15 09:48:52 AM
s+1
Transfer Fcn
MATLAB PRESENTATION
simout
To Workspace
28
Simulink
Constructing the model using 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
We need to vary the frequency and observe the current
5
Amplitude
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
From initial problem definition, the input is 5sin(t).
You should be able to decipher why the input works, but you
do not need to create your own input subsystems of this
form.
7/31/15 09:48:52 AM
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
The waveform can be displayed using scope similar to
the scope in the lab
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