KATHMANDU
School of Engineering
UNIVERSITY
Department of Electrical & Electronics Engineering
NETWORK ANALYSIS LABORATORY EXERCISE-I
1. Introduction to MATLAB
1.1 Basic arithmetic operations
Example-1.1.1
>> 5+4/2
ans =
7
>> (5+5)/3
ans =
3.3333
>> 9^(1/3)+16^0.3
ans =
4.3775
Example-1.1.2
>> x = 2+j*3
x=
2.0000 + 3.0000i
>> q = imag(x)
q=
3
>> p = real(x)
p=
2
>> r=conj(x)
r=
2.0000 - 3.0000i
>> m=abs(x)
m=
3.6056
>> theta=angle(x)
theta =
0.9828
Example-1.1.3
>> p=2
p=
2
>> q=3
q=
3
>> [theta,m]=cart2pol(p,q)
theta =
0.9828
m=
3.6056
>> [p,q]=pol2cart(theta,m)
p=
2
q=
3.0000
1.2 Matrix operation
Example-1.2.1
>> A=[1:5:26]
A=
1 6 11 16 21 26
>> B=[1:10]
B=
1 2 3
9 10
1
>> A=[1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
X=
5.0000 6.0000 0.5000
4.0000 10.0000 3.0000
>> p =5; q=2; r=3;
>> X=[p q*r sin(pi/6); q^2 10 p-q]
>> size(X)
ans =
2 3
Example-1.2.2
>> A=[ 1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> B=[0 2 4; 1 3 5; 4 6 2]
B=
0 2 4
1 3 5
4 6 2
>> C=A*B
C=
14 26 20
29 59 53
44 92 86
>> D=A'
D=
1 4 7
2 5 8
3 6 9
>> D=inv(A)
D=
1.0e+16 *
>> C = A+B
C=
1 4 7
5 8 11
11 14 11
-0.4504 0.9007 -0.4504
0.9007 -1.8014 0.9007
-0.4504 0.9007 -0.4504
>> C=A-B
C=
1 0 -1
3 2 1
3 2 7
>> det(A)
ans =
6.6613e-16
>> C=A/B
C=
-0.5000 1.0000
-3.5000 4.0000
-6.5000 7.0000
0
0
0
1.3 Basic statistics
Example-1.3.1
>> v=[ 2 5 0 8 4 6]
v= 2 5 0 8
6
2
>> mean(v)
ans =
4.1667
>> min(v)
ans =
0
>> median(v)
ans =
4.5000
>> [a,n]=max(v)
a=
8
n=
4
>> std(v)
ans =
2.8577
>> max(v)
ans =
8
>> sum(v)
ans =
25
>> sort(v)
ans =
0 2 4
1.4 Logical operations
Example-1.4.1
The marks obtained by ten students in a subject are 50 65 72 70 80 85 88 70 75 90. Determine (i)
the number of students who have scored 75 and above, (ii) the number of students who scored
between 65 and 85 and (iii) the order of students who scored between 50 and 70.
>> A = [50 65 72 70 80 85 88 70 75 90]
A=
50 65 72 70 80 85 88 70 75 90
>> A_above = A>=75;
>> no_of_students_above_75 = sum(A_above)
no_of_students_above_75 =
5
>> A_between_65_and_85 = (A>=65)&(A<=85);
>> no_of_students_between_65_and_85 = sum(A_between_65_and_85)
no_of_students_between_65_and_85 =
7
>> A_between_50_and_70 =find((A>=50)&(A<=70))
A_between_50_and_70 =
1 2 4 8
1.5 X-Y Plots
Example-1.5.1
>> X=[2004 2005 2006 2007 2008 2009]
X=
2004
2005
2006
2007
2008
2009
>> Y=[85 100 90 95 85 90]
Y=
85 100 90 95 85 90
>> plot(Y)
>> plot(X,Y)
>> grid
100
100
95
95
90
90
85
85
2004 2004.5 2005 2005.5 2006 2006.5 2007 2007.5 2008 2008.5 2009
1.5
2.5
3.5
4.5
5.5
Example-1.5.2
>> f=50;
>> w=2*pi*f;
>> t=0:1e-3:0.04;
>> x=w*t;
>> y=sin(x);
>> plot(t,y);
>> grid
>> title('voltage plot');
>> xlabel('time');
>> ylabel('voltage');
1
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
voltage
-0.2
-0.2
-0.4
-0.4
-0.6
-0.6
-0.8
-0.8
-1
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
voltage plot
-1
0.005
0.01
0.015
0.02
time
0.025
0.03
0.035
0.04
Example-1.5.3
>> f=50;
>> w=2*pi*f;
>> t=0:1e-3:0.04;
>> x=w*t;
>> y=sin(x);
>> plot(x,y,'r - d');
>> grid
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
Example-1.5.5
>> t=0:1e-3:0.04;
>> f=50;
>> w=2*pi*f;
>> phi=pi/3;
>> v=5*sin(w*t);
>> i=2*sin((w*t)-phi);
>> plot(t,v);
>> grid;
>> title('multiple plots');
>> xlabel('time (sec)');
>> ylabel('voltage & current');
>> hold on;
>> plot(t,i,'r o');
-1
0
10
12
14
multiple plots
5
4
3
voltage & current
2
1
0
-1
-2
-3
-4
-5
0
0.005
0.01
0.015
0.02
0.025
time (sec)
0.03
0.035
0.04
multiple plots
5
4
3
2
voltage & current
Example-1.5.4
>> t=0:1e-3:0.04;
>> f=50;
>> w=2*pi*f;
>> phi=pi/3;
>> v=5*sin(w*t);
>> i=2*sin((w*t)-phi);
>> plot(t,v,t,i);
>> title('multiple plots');
>> xlabel('time (sec)');
>> ylabel('voltage & current');
>> grid;
1
0
-1
-2
-3
-4
-5
0
0.005
0.01
0.015
0.02
0.025
time (sec)
0.03
0.035
0.04
Example-1.5.6
>> t=0:1e-3:0.04;
>> f=50;
>> phi=pi/3;
>> v=5*sin(w*t);
>> i=2*sin((w*t)-phi);
>> subplot(2,1,1);
5
-5
0
0.005
0.01
0.015
0.02
time
0.025
0.03
0.035
0.04
0.005
0.01
0.015
0.02
time
0.025
0.03
0.035
0.04
2
1
current
>> subplot(2,1,1);
>> plot(t,v);
>> grid
>> xlabel('time');
>> ylabel('voltage');
>> subplot(2,1,2);
>> subplot(2,1,2);
>> plot(t,i);
>> grid
>> xlabel('time')
>> ylabel('current')
voltage
0
-1
-2
0
1.6 Three-Dimensional Plots
0.4
Example-1.6.1
(
) .
Plot of =
.
using mesh
>> t=-4:0.3:4;
>> [x,y]=meshgrid(t,t);
>> z=sin(x).*cos(y).*exp(-(x.^2+y.^2).^0.5);
>> mesh(x,y,z)
0.2
0
-0.2
-0.4
4
2
0
-2
-4
Example-1.6.2
Cylindrical surface created by p = 3 + sint
using cylinder function
-2
-4
1
0.8
>> t=0:pi/5:6*pi;
>> p=3+sin(t);
>> cylinder(p)
0.6
0.4
0.2
0
4
2
0
-2
-4
1.7 MATLAB for circuit analysis
-4
-2
time response of a RC circuit
1
0.9
Example 1.7.1: Time response of a RC circuit
0.8
0.7
current (amp)
>> R=100; C=1000*1e-6; V=100;
>> t=0:1e-3:1;
>> i=(V/R)*exp(-t/(R*C));
>> plot(t,i);
>> grid;
>> xlabel('time(sec)');
>> ylabel('current (amp)')
>> title('time response of a RC circuit')
0.6
0.5
0.4
0.3
0.2
0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
time(sec)
0.7
0.8
0.9
Example 1.7.2: Time response of a RL circuit
time response of a RL circuit
1.5
>> R=10; L=500*1e-3; V=15;
>> t=0:1e-3:1;
>> i=(V/R)*(1-exp(-R*t/L));
>> plot(t,i);
>> xlabel('time(sec)');
>> ylabel('current (amp)')
>> title('time response of a RL circuit')
>> grid;
current (amp)
0.5
0.1
0.2
0.3
0.4
0.5
0.6
time(sec)
0.7
0.8
0.9
Example 1.7.3: Time response of a RLC circuit
>> L=1;
>> R=10;
>> C=0.002;
>> K = 1/C;
>> num=[0 0 1];
>> den = [L R K];
>> step(num,den)
>> hold on
>> R=44.7;
>> den = [L R K];
>> step(num,den)
>> hold on
>> R=100;
>> den = [L R K];
>> step(num,den)
-3
Step Response
x 10
2.5
Amplitude
1.5
0.5
0
0
0.2
0.4
0.6
0.8
1.2
Time (seconds)
Also determine the value of Mp, tp, ts and tr from the plot. Right click on the plot and click to
characteristics and find the values.
-3
Step Response
x 10
2.5
System: sys
Peak amplitude: 0.00297
Overshoot (%): 48.5
At time (seconds): 0.147
System: sys
Settling time (seconds): 0.756
Amplitude
1.5
System: sys
Rise time (seconds): 0.0551
0.5
0
0
0.2
0.4
0.6
0.8
1.2
Time (seconds)
1.8 Transfer Function
Example 1.8.1
>> num=[1 4];
>> den=[1 2 10];
>> sys=tf(num,den)
sys =
s+4
-------------s^2 + 2 s + 10
continuous-time transfer function
Example 1.8.2
>> num=[1 11 30 0];
>> den=[1 9 45 87 50];
>> [z,p,k]=tf2zp(num,den)
z=
0
-6.0000
-5.0000
p=
-3.0000 + 4.0000i
-3.0000 - 4.0000i
-2.0000
-1.0000
k=
1
Example 1.8.3
>> z=[-6;-5;0];
>> k=1;
>> i=sqrt(-1);
>> p=[-3+4*i;-3-4*i;-2;-1];
>> [num,den]=zp2tf(z,p,k)
num =
0 1 11 30
den =
1 9 45 87 50
1.9 Root Locus
Example 1.9.1
Plot the root locus for the system whose open loop transfer function is given as:
( + 2)
( ) ( )=
(3 + 5 + 7)
Matlab codes to plot the root locus are:
>> num = [1 2];
>> den = [3 5 7];
>> rlocus(num,den)
1.10 Bode Plot
Example 1.10.1
Obtain the Bode plot of the system whose open loop transfer function is given as below:
200( + 3)
( ) ( )=
( + 2) ( + 4 + 100)
Matlab codes for the Bode plot are:
>> n=[200 600];
>> d=conv([1 2 0], [1 4 100]);
>> bode(n,d);
>> grid
Matlab Codes to find Gain margin and phase margin
>> [Gm, Pm, pcf, gcf]=margin(n,d)
Gm = 1.8870 , Pm = 72.1652,
pcf = 9.8099, gcf = 2.5816
Assignments:
1. For the matrix equation below, AX = B, determine the vector X.
4 2 10
10
= 32
2 10 12
4 6 16
16
2. Determine the inverse of matrix A in question 1, and then determine X.
3. Create a linear X-Y plot for the following variables
X
0
0.5
1.0
1.5
2.0
2.5
3.0
Y
10
10
16
24
30
38
52
3.5
68
4.0
82
4.5
96
5.0
123
4. Plot the curve v = 10sin (t), where t varies from 0 to 4. Label the axis and give a
suitable title.
5. Determine the time response of the second order system defined by the transfer function:
( )=
+2
+
Consider wn = 1 and z(zeta) = 0.1, and plot the step response of the system. Also plot the
response by varying the value of zeta as z =1, 0, 2, and 0.5 on the previous plot (use hold on
code). Comment on the nature of curves for different values of zeta.
6. Obtain the Bode plot of the system whose open loop transfer function is given as below,
also find Gain margin and phase margin. Comment on the stability of the system
(a) ( ) ( ) =
(b) ( ) ( ) =
)(
(
. )
)
10