Professional Documents
Culture Documents
LAB MANUAL Control System Matlab Aamir S
LAB MANUAL Control System Matlab Aamir S
Submitted By:
Aamir Sohail Nagra
13-ECT-10
5th Semester
BSc. Electronics Engineering.
Lab Manual:
USING MATLAB
Contents:
1- a) Introduction to Matlab in control System………………………………………..2
b) Matrices operation………………………………………………………………..2
2- Equations in MATLAB……………………………………………………………..6
3- Pole-Zero map of a System………………………………………………………….9
4- Block Diagram Reduction………………………………………………………….12
5- Block diagram using Simulink……………………………………………………..15
6- State Space modelling……………………………………………………………...17
7- Loops in Matlab……………………………………………………………………21
8- Routh-Hurwitz in MATLAB……………………………………………………....23
9- Root-locus in Matlab……………………………………………………………....25
10- Designing of PID controller………………………………………………………..27
11- Bode plot…………………………………………………………………………...29
Page | 1
Prepared by: AAMIR SOHAIL NAGRA
Lab # 01:
i) Introduction to Matlab in Control System:
What is Matlab??
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming in an easy-to-use environment where problems and solutions
are expressed in familiar mathematical notation. Typical uses include:
a) Math and computation
b) Algorithm development
c) Modelling, simulation, and prototyping
d) Data analysis, exploration, and visualization
e) Scientific and engineering graphics
f) Application development, including graphical user interface building.
M = [1 0 0; 0 j 1; j j+1 -3]
k = [2.75]
M(:,3)=[]
Page | 2
Prepared by: AAMIR SOHAIL NAGRA
Multiplication of matrix (element by element)
Division of matrix
Inverse of a matrix
Let’s find inverse of a matrix ‘X’:
Problem 1:
Source Code:
%lab no 1
% what is the size of C??
%What is the value of C(3,2)
% list the subscript of all elements containing 0.6
c=[1.1 -3.2 3.4 0.6 ;0.6 1.1 -0.6 3.1; 1.3 0.6 5.5 0 ]
% size of C??
k=size(c)
% value of C(3,2)
c(3,2)
% list the subscript of all elements containing 0.6
c(3,2)
c(1,4)
Page | 3
Prepared by: AAMIR SOHAIL NAGRA
c(2,1)
c(2,3)
Problem 2:
If a, b and c are respectively:
2 1 -1 3 2
0 2 -1 4 1
a) add=a+b???
b) multiplication=a*d???
c) dot=a.*d
d) division=a./b
e) power=a.^b
Page | 4
Prepared by: AAMIR SOHAIL NAGRA
Source Code:
%lab1_b
% matrix a
a=[2 1;-1 4]
%matrix b
b=[-1 3;0 2]
%matrix c
c=[2;1]
% eye(2) gives identity matrix of 2*2
d=eye(2)
% to simply add matrix a and b
add=a+b
%to simplify matrix multiplication
multiplication=a*d
% returns the scaler product of two matrice
dot=a.*d
%dot2=a.*c;error
division=a./b
% array power
power=a.^b
Output:
Page | 5
Prepared by: AAMIR SOHAIL NAGRA
Lab # 02:
Equations in Matlab:
You can solve a variety of equations and systems under conditions you specify using solver
functions. For a general-purpose analytical solver, use solve. To solve equations numerically,
usevpasolve. For solving linear equations, use linsolve.
Problem # 1:
Write a code of simple calculator which counts the differences (distance) between any two
points in co-ordinate system entered by user.
Source Code:
%lab2
% design a calculator which calculates the distance between two points of
% co-ordinate system.
% code
% taking the X-cordinate input of 1'st point
x1=input('put the x part of initial point =')
% taking the Y-cordinate input of 1'st point
y1=input('put the y part of the initial point =')
% taking the X-cordinate input of Final point
x2=input('put the x part of final point =')
% taking the Y-cordinate input of 2'nd point
y2=input('put the y part of final point =')
% Calculating X-coordinate difference.
x=x2-x1;
% Calculating Y-coordinate difference.
y=y2-y1;
% Calculating X-coordinate and Y-Coordinate square.
r=x^2+y^2
% Calculating the square root of r
d=sqrt(r)
Page | 6
Prepared by: AAMIR SOHAIL NAGRA
Output:
Problem # 2:
Write a code which calculates the Value of Energy by taking the value of force and Spring
Constant Value from User.
Source Code:
%lab2b
% Write a simple code which calclate the Energy using Farmula
% Energy=1/2kx^2
%code
% taking the values of force 'f' from usesr
f=input('put the f =')
% taking the value of spring Constant 'k'
k=input('put the k =')
%Calculating the Value of distance 'x' using f=kx
x=f/k
%Calculating the Energy
E=0.5*k*x^2
Page | 7
Prepared by: AAMIR SOHAIL NAGRA
OutPut:
Page | 8
Prepared by: AAMIR SOHAIL NAGRA
Lab #03:
Pole_ Zero Map of a System:
General Description:
Poles and Zeros of a system gives you the information of system behaviour at any point like
system settling time, peak time and moreover provides a very useful information about the
stability of a System.
If a transfer function of a given system is
T.F=G(s)/H(s)
Where G(s), H(s) are two polynomials. Putt H(s)=0, then roots of this polynomial will gives
you the system Pole and putt G(s)=0 , the roots of this polynomial will gives you the system
Zeros.
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. For MIMO systems, it plots
the system poles and transmission zeros. The poles are plotted as x's and the zeros are plotted
as o's.
pzmap(sys1,sys2,...,sysN) plots the pole-zero map of several LTI models on a single figure.
The LTI models can have different numbers of inputs and outputs and can be a mix of
continuous and discrete systems.
[p,z] = pzmap(sys)
returns the system poles and (transmission) zeros in the column vectors 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
frequency in the - or -plane.
Page | 9
Prepared by: AAMIR SOHAIL NAGRA
Problem:
Find the pole and Zeros of System and draw in S-plan using Matlab. If:
a) G(s)=6s^2+1/(s^3+3s^2+3s+1)
b) H(s)=(s+1)(s+2/((s+2i)(s-2i)(s+3))
c) E=G/H
d) Find Poles and Zeros of E.
Source Code:
% lab3
% Find Pole_Zero Map Of a system.
% G(s)=6s^2+1/(s^3+3s^2+3s+1)
% H(s)=(s+1)(s+2/((s+2i)(s-2i)(s+3))
%find G(s)/H(s)
%find Zero_Map of System.
% Code
%Declaring the Neumenator of G(s)
num1=[6 0 1];
%Declaring the Denominator of G(s)
den1=[1 3 3 1];
% Declaring The denomitor H(s) 1'st product
d1=[1 2*i];
% Declaring The denomitor H(s) 2'nd product
d2=[1 -2*i];
% Declaring The denomitor H(s) 2'rd product
d3=[1 3];
% Convollving all product to Obtain the denominator of H(s)
d4=conv([1 2*i],[1 -2*i]);
den2=conv([1 0 4],[1 3]);
%Declaring the Neumenator of H(s)
num2=[1 3 2];
% Finding The Transfer_ Function of G(s)
G=tf(num1,den1)
% Finding The Transfer_ Function of H(s)
H=tf(num2,den2)
% Finding E=G/h
E=G/H;
% Finding Pole_Zero of System
pzmap(E)
Page | 10
Prepared by: AAMIR SOHAIL NAGRA
Output:
PZ-Graph:
Page | 11
Prepared by: AAMIR SOHAIL NAGRA
Lab# 04:
i) conv
Syntax
w = conv(u,v)
C = conv(...,'shape')
Description:
ii) Tf
tf
sys = tf(num,den)
sys = tf(num,den,Ts)
sys = tf(M)
sys = tf(num,den,ltisys)
tfsys = tf(sys)
Page | 12
Prepared by: AAMIR SOHAIL NAGRA
Description:
tf is used to create real- or complex-valued transfer function models (TF objects) or
to convert state-space or zero-pole-gain models to transfer function form.
iii) Feedback
Syntax:
sys = feedback(sys1,sys2)
Description:
The closed-loop model sys has as input vector and as output vector. The LTI models
sys1 and sys2 must be both continuous or both discrete with identical sample times.
Precedence rules are used to determine the resulting model type.
Problem1:
Find the Close-loop gain of a given system in Matlab using feedback.
Source Code:
%lab4_a
%Block diagram reduction
%convolving the cascaded System neuminator
num=conv([1 1],[1]);
%convolving the denominator system denominator
den=conv([1 2],[500 0 0]);
%calculating the tf of a system
G=tf(num,den)
%using feed-back command to find cloase loop tf.
T=feedback(G,1)
Output:
Page | 13
Prepared by: AAMIR SOHAIL NAGRA
Problem# 02:
Find the Close loop Transfer function of a given system using Matlab.
%lab7b
%find the close loop transfer function of a system.
%code
%declaring the neumenator of feed-forward path.
num1=[1];
%declaring the denominator of feed-forward path.
den1=[500 0 0];
%declaring the neumenator of feed-back path.
num2=[1 1];
%declaring the denomiator of feed-back path.
num2=[1 2];
%finding tf of feed-forward path
G=tf(num1,den1)
%finding tf of feed-back path
H=tf(num2,den2)
%finding tf of close-loop system
T=feedback(G,H,1)
Output:
Page | 14
Prepared by: AAMIR SOHAIL NAGRA
Lab # 05:
Block Diagram Modelling Using Simulink:
Introduction to Simulink:
Simulink is a software package that enables you to model, simulate, and analyze systems whose
outputs change over time. Such systems are often referred to as dynamic systems. The Simulink
software can be used to explore the behaviour of a wizde range of real-world dynamic systems,
including electrical circuits, shock absorbers, braking systems, and many other electrical,
mechanical, and thermodynamic systems. This section explains how Simulink works.
Simulating a dynamic system is a two-step process. First, a user creates a block diagram, using
the Simulink model editor that graphically depicts time-dependent mathematical relationships
among the system's inputs, states, and outputs. The user then commands the Simulink software
to simulate the system represented by the model from a specified start time to a specified stop
time.
To start the Simulink software, you must first start the MATLAB® technical computing
environment. Consult your MATLAB documentation for more information. You can then start
the Simulink software in two ways:
Simulating the model allows you to observe how the system responses with the change in
input.
1. In the demo model window, double-click the Scope block named PlotResults.
2. Select Simulation > Start in the model window.
Problem:
Find the Step Response of a system using Simulink.
Page | 15
Prepared by: AAMIR SOHAIL NAGRA
Step-Response O/P:
Page | 16
Prepared by: AAMIR SOHAIL NAGRA
Lab # 06:
State-Space Modelling:
i) General Description:
In control engineering, a state-space representation is a mathematical model of a physical
system as a set of input, output and state variables related by first-order differential equations.
"State space" refers to the space whose axes are the state variables. The state of the system can
be represented as a vector within that space.
ii) State-space in Matlab
The state-space model structure is a good choice for quick estimation because it requires
you to specify only one input, the model order, n. The model order is an integer equal
to the dimension ofx(t) and relates to, but is not necessarily equal to, the number of
delayed inputs and outputs used in the corresponding linear difference equation.
Useful Commands: ss
ss
sys = ss(a,b,c,d)
sys = ss(a,b,c,d,Ts)
sys = ss(d)
sys = ss(a,b,c,d,ltisys)
sys_ss = ss(sys)
Description
Page | 17
Prepared by: AAMIR SOHAIL NAGRA
with sample time Ts (in seconds). Set Ts = -1 or Ts = [] to leave the sample time unspecified.
sys = ss(a,b,c,d,ltisys) creates a state-space model with generic LTI properties inherited from
the LTI model ltisys (including the sample time).
Problem # 01:
0 1 0
0
0 0 1
, 0 , c=[1 1 0 ] ,d= [ 0]
-4 -5 -8
1
Source Code:
%lab6
%State-spacae modelling
%Declaring the first matrix A
a1=[0 1 0;0 0 1;-4 -5 -8];
%Declaring the first matrix B
b1=[0;0;4]
%Declaring the first matrix C
c1=[1 1 0];
%Declaring the first matrix A
d1=[0];
%Finding SS of the System
sys=ss(a1,b1,c1,d1)
%Finding transfer function of a function
systf=tf(sys)
Page | 18
Prepared by: AAMIR SOHAIL NAGRA
Output:
Page | 19
Prepared by: AAMIR SOHAIL NAGRA
Problem # 02
Source Code:
% determine state space representation of controller
numC=[1]
denmC=[1 2]
sys=tf(numC,denmC)
systf=ss(sys)
% plant state-space moodel
nump=[1]
denmp=[1 2 4]
sysa=tf(nump,denmp)
systfa=ss(sysa)
% use series and feed back to find impulse respose
[numA,denmA]=series(numC,denmC,nump,denmp)
numh=[1]
denmh=[1]
[numA1,denmA1]=feedback(numA,denmA,numh,denmh,-1)
sysA=tf(numA1,denmA1)
impulse(sysA)
Impulse response:
Page | 20
Prepared by: AAMIR SOHAIL NAGRA
Lab # 07:
Loops in Matlab:
A loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times.
Commonly two types of loops are used.
i) For loop
ii) While Loop
For Loop
for statements loop a specific number of times, and keep track of each iteration with
an incrementing index variable.
Syntax:
While Loop:
while statements loop as long as a condition remains true. Each loop requires
the end keyword.
Syntax:
Problem # 1:
Page | 21
Prepared by: AAMIR SOHAIL NAGRA
% using short-cut expression to declare different time interval
t=[0:0.1:20]
%applying for loop which executes 3-times
for(i=1:n)
%declaring the neumenator of feed forward-path
numA=[20.*k(i)]
%declaring the denominator of feed forward-path
denmA=[1 4.5 64]
%declaring the neumenator of feedback-path
numH=[1]
%declaring the neumenator of forward-path
denmH=[1 1]
%Appling the feed-back path
[numF,denmF]=feedback(numA,denmA,numH,denmH)
%Applying the transfer functio of CL system.
sys=tf(numF,denmF)
r(:,i)=step(sys,t);
%ploting graph for 3 different values of K
plot(t,r(:,1),t,r(:,2),'bo',t,r(:,3),'g')
end %End of for loop
%Naming the X-axis as time
xlabel('time')
%%Naming the Y-axis as step-response
ylabel('step_response')
%Using legened command to ditinguished between three graphs
legend('k=10','k=12','k=15')
Step Response:
Page | 22
Prepared by: AAMIR SOHAIL NAGRA
Lab # 08:
Routh-Hurwitz Method in Matlab:
General Description:
This is a very useful method which gives us the information about the stability of a system
without the need to solve the closed loop system poles. Using this method we can tell how
many closed loop system-poles in right half plane ,in the left half plane and on the jw axis.
In simply stated the routh Hurwitz criterion declare that the number of roots of polynomial
that are in right half plane is equal to the number changes in first column.
Problem:
If Transfer function of a system is given as :
1
----------------------------
s^3 + 10 s^2 + 10 s + k
i) K=[1:1:20]
ii) For what value of K system will be stable.
Source Code:
Page | 23
Prepared by: AAMIR SOHAIL NAGRA
Graphical output:
Conclusion:
Hence system is stable for Value of 6<k<20.
Page | 24
Prepared by: AAMIR SOHAIL NAGRA
Lab # 09:
Root Locus in Matlab:
i) General Description:
In control theory and stability theory, root locus analysis is a graphical method for examining
how the roots of a system change with variation of a certain system parameter, commonly a
gain within a feedback system.
This technique is very useful to find the stability information and also provides very useful
information about system parameters. As one says if you can measure it , you can control it so
this technique is very useful in this regard.
ii) Root Locus in Matlab:
Syntax
rlocus
rlocus(sys)
rlocus(sys1,sys2,...)
Description
rlocus computes the Evans root locus of a SISO open-loop model. The root locus gives the
closed-loop pole trajectories as a function of the feedback gain (assuming negative feedback).
Root loci are used to study the effects of varying feedback gains on closed-loop pole locations.
In turn, these locations provide indirect information on the time and frequency responses.
rlocus(sys) calculates and plots the root locus of the open-loop SISO model sys.
Problem:
S^3+8S^2+10S+1
C(s)
Page | 25
Prepared by: AAMIR SOHAIL NAGRA
Source Code:
%lab 9
%find cltf,
% The value of K where damping ratio is 0.7
k=[1:10:1000]
% declaring the feed-forward neumenator.
numA=[1]
% declaring the feed-forward denuminator.
denmA=[1 8 10 1]
% declaring the feed-back neumenator.
numh=[1]
% declaring the feed-back denuminator.
denmh=[1]
%Solving for unity feed back gain
[numA1,denmA1]=feedback(numA,denmA,numh,denmh,-1)
sys=tf(numA1,denmA1)
%finding the loci
rlocus(sys,k)
Conclusion:
The Value of gain(k=3.04 ) where damping ratio is 0.707.
Locus fig:
Page | 26
Prepared by: AAMIR SOHAIL NAGRA
Lab # 10:
PID Controller:
General Description:
A proportional–integral–derivative controller (PID controller) is a control loop feedback
mechanism (controller) commonly used in industrial control systems. A PID controller
continuously calculates an error value as the difference between a measured process
variable and a desired setpoint. The controller attempts to minimize the error over time by
adjustment of a control variable, such as the position of a control valve, a damper, or the power
supplied to a heating element, to a new value determined by a weighted sum:
P accounts for present values of the error (e.g. if the error is large and positive, the
control variable will be large and negative),
I accounts for past values of the error (e.g. if the output is not sufficient to reduce the
size of the error, the control variable will accumulate over time, causing the controller
to apply a stronger action), and
D accounts for possible future values of the error, based on its current rate of change.[
Problem:
Source Code:
%Lab10
%proportionl integrator and derivative (PID)
Page | 27
Prepared by: AAMIR SOHAIL NAGRA
% declaring the Neumenator of plant
numkp=[1 ]
% declaring the denuumenator of plant
denmkp=[1 3 1]
% declaring the Neumenator of PID controller (8s^2+24s+1)/s
numkc=[8 24 1]
% declaring the denuumenator of plant
denmkc=[1 0]
%Calculating tf of plant
gc=tf(numkp,denmkp)
%Calculating tf of PID
Gp=tf(numkc,denmkc)
% unity feed-back
h=[1]
%solving the close loop system
sys=feedback(gc*Gp,h)
% time interval using short cut expression
t=[0:0.01:2]
%step response of a system
step(sys,t)
Step-Response:
Page | 28
Prepared by: AAMIR SOHAIL NAGRA
Lab # 10:
Bode Plot:
i) General Description:
Bode plots are a very useful way to represent the gain and phase of a system as a function of
frequency. This is referred to as the frequency domain behaviour of a system.
ii) Matlab Command
bode
Syntax
bode
bode(sys)
bode(sys,w)
bode(sys1,sys2,...,sysN)
bode(sys1,sys2,...,sysN,w)
bode(sys1,'PlotStyle1',...,sysN,'PlotStyleN')
Description
bode computes the magnitude and phase of the frequency response of LTI models. When you
invoke this function without left-side arguments, bode produces a Bode plot on the screen. The
magnitude is plotted in decibels (dB), and the phase in degrees. The decibel calculation for mag
is computed as 20log10 magnitude of H(jw), where H(jw) is the system's frequency response.
You can use bode plots to analyze system properties such as the gain margin, phase margin,
DC gain, bandwidth, disturbance rejection, and stability.
bode(sys) plots the Bode response of an arbitrary LTI model sys. This model can be continuous
or discrete, and SISO or MIMO. In the MIMO case, bode produces an array of Bode plots,
each plot showing the Bode response of one particular I/O channel. The frequency range is
determined automatically based on the system poles and zeros.
bode(sys,w) explicitly specifies the frequency range or frequency points for the plot. To focus
on a particular frequency interval [wmin,wmax], set w = {wmin,wmax}. To use particular
frequency points, set w to the vector of desired frequencies. Use logspace to generate
logarithmically spaced frequency vectors. Specify all frequencies in radians per second (rad/s).
bode(sys1,'r--',sys2,'gx')
Page | 29
Prepared by: AAMIR SOHAIL NAGRA
produces a red dashed lines for the first system sys1 and green 'x' markers for the second
system sys2.
When you invoke this function with left-side arguments, the commands
[mag,phase,w] = bode(sys)
[mag,phase] = bode(sys,w)
return the magnitude and phase (in degrees) of the frequency response at the frequencies w
(in rad/s). The outputs mag and phase are 3-D arrays with the frequency as the last dimension
(see "Arguments" for details). To convert the magnitude to decibels, type
magdb = 20*log10(mag)
Problem # 1:
Find the bode-plot of close loop System:
Source Code:
%lab11
%find the close loop transfer function of a system.
%code
%declaring the neumenator of feed-forward path.
num1=[1];
%declaring the denominator of feed-forward path.
den1=[1 4 3];
%declaring the neumenator of feed-back path.
num2=[1 ];
%declaring the denomiator of feed-back path.
den2=[1 4];
%finding tf of feed-forward path
G=tf(num1,den1)
%finding tf of feed-back path
H=tf(num2,den2)
%finding tf of close-loop system
T=feedback(G,H,-1)
%finding close-loop tranesfer function.
Sys=tf(T)
% finding bode-plot of system
bode(Sys)
Page | 30
Prepared by: AAMIR SOHAIL NAGRA
Output:
Page | 31
Prepared by: AAMIR SOHAIL NAGRA