Professional Documents
Culture Documents
COMSATS University Islamabad (Sahiwal Campus) : Lab Manual
COMSATS University Islamabad (Sahiwal Campus) : Lab Manual
Control Systems
Lab Manual
CONTROL SYSTEMS
Submitted to:
Submitted by:
Registration # FA16-BEE-036
Checked by:
_______________
1.2.8.1 Question 1
Create a vector a=[0,0.1,0.2,….10] and vector b=[cos0,cos0.2,…,20].
Compute the following:
a. c where c=a/b
b. d where d=a^4
c. The element-wise product of a and b.
d. The dot product of a and b.
Matlab code:-
clc;
clear all;
close all;
%zainab
a=[0:0.1:10]; %defining vetor a
b1=[0:0.2:20];
b=cos(b1); %defining vetor b
c=a./b; %performing ai/bi
d=a.^4; %performing ai^4
p=a.*b; %element wise product of a and b
D_P=dot(a,b); %dot product of a and b
Analysis:-
In this program, the two matrices of specific order are defined which performs
different mathematical operations by using matlab built in functions.
1.2.8.2 Question 2
Create a matrix with all its elements randomly distributed in [0,1] . Compute:
a. The inverse matrix.
b. The transpose of the matrix.
c. The determinant of the matrix.
d. The size of the matrix
Matlab code:-
clc;
clear all;
close all;
%zanib
m=rand(3)%create order 3 matrix between 0 and 1
a=inv(m); %taking inverse of m
b=transpose(m); %taking transpose of m
c=det(m); %taking determinent of m
d=size(m); %taking size of m
Analysis:-
1.2.8.3 Question 3
Use the built-in function eig to find the eigenvalues and eigenvectors of the matrix ,
where x=[1/4 1/4 5/4, -1 2 1, -5/4 -1/4 15/4]
Matlab code:-
clc;
clear all;
close all;
%zanib
x=[1/4 1/4 5/4;-1 2 1;-5/4 -1/4 15/4];
y=eig(x) %gives eig values and vectors
y1=eig(x,'nobalance'); %performs computation with balancing disabled
y2=eig(x,'balance'); %same as eig(A)
y3=eig(x,'vector');%gives eig values in column vector
y4=eig(x,'matrix');%gives eigenvalues in diagnol
output:-
Analysis:-
In this program,given matrices is defined and find the eigenvalues and eigenvectors of
that matrix by using matlab built in functions.
1.2.8.4 Question 4
a. Create a function that plots the function sincx.
b. Plot, using your function, the graph of in the interval [-2pi,pi].
Matlab code:-
function sinc_fun
%zanib
x=-2.*pi:0.02:2.*pi
sincx=(sin(pi.*x)/pi.*x)%defining sinc fun
plot(x,sincx)
end
output:-
Lab Manual
Control Systems
Analysis:-
This program gives the output and graph of sinc function within a limit by using function in matlab
1.2.8.5 Question 5
a. Find out, how functions are written in MATLAB? Write a function that performs
the convolution of two input signals without using any built-in functions, other
than those we have covered in this lab manual.
b. Compare the output of your function with the built-in conv function for multiple
inputs to see that it produces the same output.
Matlab code:-
clc;
clear all;
close all;
% zanib
% without convolution
x=input('Enter x:')
h=input('Enter h:')
m=length(x)
n=length(h)
X=[x,zeros(1,n)]
H=[h,zeros(1,m)]
for i=1:n+m-1 %using for loop to conv x and h
Y(i)=1
for j=1:i
Y(i)=Y(i)+X(j)*H(i-j+1)
end
end
subplot(2,1,1)
stem(Y)
ylabel('Y[n]')
xlabel('n')
title('Convolution of Two Signals without conv function')
%with convolution
Lab Manual
Control Systems
z=conv(x,h)
subplot(2,1,2)
stem(z)
ylabel('Y[n]')
xlabel('n')
title('Convolution of Two Signals with conv function')
c=Y==z%comparing output
output:-
Analysis:-
In this code, the two signals convolve by using two methods i.e with built in matlab command and formula
of convolution by using for loop. At the end by compare both outputs which are equal,. So the conclusion
shows whatever the function is used for convolution the output will remain same.
Simulink model:-
Output:-
Lab Manual
Control Systems
Analysis:-
Simulink model:-
Output:-
Analysis:-
Lab Manual
Control Systems
The given transfer function is the over damped function which gives the impulse response at
1.
Task:-
The inputs given below are used for what characterization of the system. Explain briefly.
1) Impulse
2) step
3) ramp
4) sinusoidal
1)Impulse
2)STEP:-
The step response of a system in a given initial state consists of the time evolution of its
outputs when its control inputs are Heaviside step functions.
In control systems, step response is the time behaviour of the outputs of a
general system when its inputs change from zero to one in a very short time. a state response
to the step input is called the step response. In Simulink, it will generates a step function
Description
The Step block provides a step between two definable levels at a specified time. If the
simulation time is less than the Step time parameter value, the block's output is the Initial
Lab Manual
Control Systems
value parameter value. For simulation time greater than or equal to the Step time, the output
is the Final value parameter value.
3)ramp
The ramp is a signal, which starts at a value of zero and increases linearly with time.In
Simulink it generates constantly increasing or decreasing signal
Description
The Ramp block generates a signal that starts at a specified time and value and changes by a
specified rate. The block's Slope, Start time, Duty Cycle, and Initial output parameters
determine the characteristics of the output signal.
4)sinusoidal:-
Description
The Sine Wave block provides a sinusoid. The block can operate in either time-based or
sample-based mode.
Time-Based Mode
Using the Sine Wave Block in Continuous Mode
Using the Sine Wave Block in Discrete Mode
Sample-Based Mode
Lab Manual
Control Systems
1)Matlab code:-
clc;
close all;
clear all;
% zanib
syms t
f=5*t*exp(-5*t);
l=laplace(f);
pretty(l)
output:-
2) Matlab code:-
clc;
close all;
clear all;
% zanib
syms t
g=(t*sin(2*t)+exp(-2*t));
l=laplace(g);
pretty(l)
output:-
3) Matlab code:-
clc;
close all;
Lab Manual
Control Systems
clear all;
% zanib
syms t
g=(sin(2*t)*cos(2*t));
l=laplace(g);
pretty(l)
output:-
4) Matlab code:-
clc;
close all;
clear all;
% zanib
syms t
g=5*exp(-2*t);
l=laplace(g);
pretty(l)
output:-
1)Matlab code:-
clc;
close all;
clear all;
% zanib
syms s
tf=1/(s*(s+2)*(s+3));
l=ilaplace(tf);
pretty(l)
output:-
Lab Manual
Control Systems
2)Matlab code:-
clc;
close all;
clear all;
% zanib
syms s
tf=10/(((s+1)^2)*(s+3));
l=ilaplace(tf);
pretty(l)
output:-
3)Matlab code:-
clc;
close all;
clear all;
% zanib
syms s
tf=(2*(s+1))/(s*(s^2+s+2));
l=ilaplace(tf);
pretty(l)
output:-
4)Matlab code:-
output:-
clc;
close all;
Lab Manual
Control Systems
clear all;
% zanib
syms s
tf=(10*(s+2))/(s*(s^2+2*s+5));
l=ilaplace(tf);
pretty(l)
Task 1:-
MATLAB find the inverse Laplace transform of the following function:
2 ( s+3 ) ( s +5 ) ( s +7 )
F ( s) =
s ( s+8 ) ( s2 +10 s +100 )
Matlab code:-
clc;
close all;
clear all;
%zanib
syms s
%inverse laplace
tf=(2.*((s + 3)*(s + 5)*(s + 7)))/(s*((s + 8)*(s^2 + 10*s + 100)));
f=ilaplace(tf);
pretty(f)
output:-
Matlab code:-
clc;
close all;
clear all;
%zanib
syms t
f=0.0075-0.00034*exp(-2.5*t)*(cos(22*t))+0.087*exp(-2.5*t)*(sin(22*t))-0.007*exp(-8*t);
tf=laplace(f);
pretty(tf)
output:-
Lab Manual
Control Systems
Lab 03:- LTI viewer and modeling of 1st and higher order
systems in Simulink
2.3.1 Lab Tasks
Question No 1: Determine the transfer function when,
1. There is no zero, P1 = -1+2j, P2 = -1-2j, k = 10.
2. Zero is at zero, P1 = -1+2j, P2 = -1-2j, k = 10.
3. z = -1, p1 = -2, p2 = -4, p3 = -8, k=12.
Lab Manual
Control Systems
Matlab code :-
clc;
close all;
close all;
%zanib
%1%)
tf=zpk(1,[-1 2.*j,-1 -2.*j],10)
%2%)
tf=zpk(0,[-1 2.*j,-1 -2.*j],10)
%3%)
tf=zpk(-1,[-2,-4,-8],12)
output:-
1)Simulink model:-
Lab Manual
Control Systems
Output:-
Scope0
Scope1:-
Lab Manual
Control Systems
2)Simulink model:-
Output:-
MATLAB Simulink:
Lab Manual
Control Systems
Output:
(Assume initial conditions to be zero). Analyze the system using the following inputs: Impulse, step,
ramp and sinusoid. Transform the differential equation into a transfer function. Plot its response using
LTI view for impulse and step inputs. Compare the responses obtained using LTI view and MATLAB
build in functions. Comment if any difference/s is/are found (Note: Analysis should be detailed and must
include comments on transient, steady state response).
Simulink:
Output:
Lab Manual
Control Systems
MATLAB® Code:
clc ;
clear all
close all
%zanib
T=tf([5 0],conv([1 7],[1 0 4]))
Ltiview
Output:
Step response:
Impulse response:
Lab Manual
Control Systems
Analysis:
Both of the system are not stable as the transient response of the impulse and step response is high due high level of
distortion and the steady state exist in the step response when the time approaches Infinity and there the peak
response exist at the 0.344 at time 0.912 sec in step response and the peak response of impulse response exist at
-0.677 at time 1.71 sec.
Lab Manual
Control Systems
and denominator(s) specified by num and den. The output sys is a tf model
object, when num and den are numeric arrays, continuous-time transfer function
2. By using pole zero: sys = zpk(z,p,k) zpk Constructs zero-pole-gain model or converts to zero-pole-gain
format SYS = zpk(Z,P,K) creates a continuous-time zero-pole-gain (zpk) model SYS with zeros Z, poles P,
and gains K.
3. By using a rational expression to create a SISO tf model by using syms s.
4. ‘tf’ commands also converts zpk form of system to tf form same holds for zpk
command.
Write above commands (tf, zpk) in input space of block to define a linear system.
-pzmap(sys) plots the pole-zero map of the continuous- or discrete-time LTI model sys. For SISO systems,
pzmap plots the transfer function poles and zeros.
It returns the system poles and zeros in p and z. No plot is drawn on the screen. You
can use the functions sgrid or zgrid to plot lines of constant damping ratio and natural
pzmap(sys)
MATLAB Code:
clc;
clear all;
close all;
%zanib
x=tf([1 2],[1 2 3 4 5 6])
output:-
MATLAB Code:
clc;
clear all;
close all;
%zanib
x=tf([1 2],[1 6 7 9 1 11])
Output:-
Matlab code:-
Clc;
clear all;
close all;
%zanib
G=zpk([],[-1,-1],[1]);
C=pid(2,1.3,0.3,0.5);
S=tf(5,[1 4]);
F=tf(1,[1 1]);
H = G*C
H = series(C,G)
T = H/(1+H*S)
T_ry = T*F
Output:
Analysis:
In this task we are using the series command and the positive feedback to obtain the final transfer function
Example 2
MATLAB code:
Clc;
Clear all;
close all;
%zanib
s=tf('s');
A=exp(-93.9*s)*5.6/(40.2*s+1);
A.inputName='u'
A.outputName='y'
Gp=5.6/(40.2*s+1)
Gp.inputName='u'
Gp.outputName='yp'
Dp=exp(-93.9*s)
Lab Manual
Control Systems
Dp.inputName='yp'
Dp.outputName='y1'
B=1/(20*s+1)
B.inputName='dy'
B.outputName='dp'
C=pidstd(0.574,40.1)
C.inputName='e'
C.outputName='u'
sum1=sumblk('e=ysp-ym')
sum2=sumblk('ym=yp+dp')
sum3=sumblk('dy=y-y1')
T=connect(A,Gp,Dp,C,B,sum1,sum2,sum3,'ysp','y')
N=pade(T)
LTIVIEW
Output:-
LTIVIEW
Lab Manual
Control Systems
Analysis:
In this task we are obtaining the Simulink result via using the connect command. And then padding the final result.
Q2 part c
MATLAB Code:-
Clc;
clear all;
close all;
%zanib
J = 3.2284*exp(-6);
b = 3.5077*exp(-6);
K = 0.0274;
R = 4;
L = 2.75*exp(-6);
s=tf('s');
P_motor = K/(s*((J*s+b)*(L*s+R)+K^2));
Ltiview
Analysis:
In this program we are obtaining the transfer function of the motor by using the tf command.
Q3. Impulse Response, Step Response: View impulse response, step response, impulse response characteristics and
step response characteristics of systems given in task2. Repeat task 3 using LTIVIEW TOOL.
Impulse Response
Lab Manual
Control Systems
Step Response
Analysis:
So, in this program we are analysing the impulse and step responses of the system by which we are calculating the
different values like the settling time, rise time and the final values.
4.Impulse Response, Step Response: Obtain the unit impulse response for the following system
MATLAB code:;-
Clc;
clear all;
close all;
%zanib
s=tf('s');
A=1/(s^2+0.2*s+1)
y1=tf(A)
B=s/(s^2+0.2*s+1)
y2=tf(B)
ltiview
Output
Impulse response
Analysis
Both of them are equal because the system B has a s in the numerator which will cause the systems to generate the
same outputs.
3.Stability Analysis: Do stability analysis of systems given in task two.
MATLAB code:;-
clc;
clear all;
close all;
%zanib
a=tf([1 0 0 0 0 3],[1 0 5 0 6 9]) %using Method (1)
B=(s^5+3)/(s^5+5*s^3+6*s+9)
Output
Analysis:
Both of the above systems are unstable and are the special cases with first element zero.
DC Motor Model
J = 3.2284E-6; b = 3.5077E-6;
K = 0.0274;
R = 4;
L = 2.75E-6;
s = tf('s');
P_motor = K/(s*((J*s+b)*(L*s+R)+K^2))
MATLAB Code:;-
Lab Manual
Control Systems
clc
clear all;
close all;
%zanib
J = 3.2284*exp(-6);
b = 3.5077*exp(-6);
K = 0.0274;
R = 4;
L = 2.75*exp(-6);
s=tf('s');
P_motor = K/(s*((J*s+b)*(L*s+R)+K^2));
Ltiview
Direct form I
Simulink model:-
Output
Lab Manual
Control Systems
Simulink model:-
Direct form II
Direct form II
Scope
Lab Manual
Control Systems