Documente Academic
Documente Profesional
Documente Cultură
LAB # 1
Prepared By :
M. Hammad Saleem
Assignment statement:
o Variable = number;
o Variable = expression;
Example:
NOTE: when a semi-colon ”;” is placed
>> tutorial = 1234; at the end of each command, the result
>> tutorial = 1234 is not displayed.
tutorial =
1234
Variables (con’t…)
Special variables:
o ans : default variable name for the result
o pi: π = 3.1415926…………
o eps: 𝟄 = 2.2204e-016, smallest amount by which 2 numbers can differ.
o Inf or inf : infinity
o NaN or nan: not-a-number
Vectors
Matrices
Array Operations
ans =
0 0.7854 1.5708
Vectors(con’t…)
Some useful commands
Matrices
A Matrix array is two-dimensional, having both multiple rows and
multiple columns, similar to vector arrays:
o it begins with [, and end with ]
o spaces or commas are used to separate elements in a row.
o semicolon or enter is used to separate rows.
o Example:
>> f = [ 1 2 3; 4 5 6]
f=
1 2 3
4 5 6
>> h = [ 2 4 6
1 3 5]
h=
2 4 6
1 3 5
Matrices (con’t…)
Some useful commands
Matrices (con’t…)
More commands
Array Operations
Scalar-array Mathematics
For addition, subtraction, multiplication, and division of an array by a
scalar simply apply the operations to all elements of the array.
o Example:
>> f = [ 1 2; 3 4]
f=
1 2
3 4
>> g = 2*f – 1
g=
1 3 Each element in the array f is multiplied
5 7 by 2, then subtracted by 1.
Solutions to Systems of Linear Equations
Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3):
3x1 + 2x2 – x3 = 10
- x1 + 3x2 + 2x3 = 5
x1 – x2 – x3 = -1
Let :
Plotting a point:
The function plot () creates a
graphics window, called a Figure
>> plot ( variablename, ‘symbol’) window, and named by default
“Figure No. 1”
o Example : Complex number
>> z = 1 + 0.5j;
>> plot (z, ‘.’)
Plotting (con’t…)
Some useful commands
Plotting (con’t…)
Plotting Curves:
o plot (x,y) – generates a linear plot of the values of x (horizontal axis) and y
(vertical axis).
o semilogx (x,y) – generate a plot of the values of x and y using a logarithmic
scale for x and a linear scale for y
o semilogy (x,y) – generate a plot of the values of x and y using a linear scale
for x and a logarithmic scale for y.
o loglog(x,y) – generate a plot of the values of x and y using logarithmic scales
for both x and y
Plotting (con’t…)
Example: (polynomial function)
plot the polynomial using linear/linear scale, log/linear scale, linear/log
scale, & log/log scale: y = 2x2 + 7x + 9
Plotting (con’t…)
Example: (polynomial function)
plot the polynomial using linear/linear scale, log/linear scale, linear/log
scale, & log/log scale: y = 2x2 + 7x + 9
Plotting (con’t…)
Adding new curves to the existing graph:
Use the hold command to add lines/points to an existing plot.
o hold on – retain existing axes, add new curves to current axes. Axes
are rescaled when necessary.
o hold off – release the current figure window for new plots
LAB # 1
Prepared By :
M. Hammad Saleem
Polynomial Roots
The roots function calculates the roots of a polynomial:
>> r = roots(p)
r=
2.0946
By convention, MATLAB stores roots in column
-1.0473 + 1.1359i
vectors.
-1.0473 - 1.1359i
o The function poly returns to the polynomial coefficients:
>> p2 = poly(r)
p2 =
1 0 -2 -5
Polynomial Evaluation
o The polyval function evaluates a polynomial at a specified value.
o To evaluate p at s = 5, use
>> polyval(p,5)
ans =
110
Convolution and Deconvolution
Polynomial multiplication and division correspond to the operations convolution
and deconvolution.
o The functions conv and deconv implement these operations.
o Consider the polynomials a(s )=s2+2s+3 and b(s )=4s2+5s+6 .
o To compute their product,
>> a = [1 2 3];
>> b = [4 5 6];
>> c = conv(a,b)
c=
4 13 28 27 18
o Use deconvolution to divide back out of the product:
>>[q,r] = deconv(c,a)
q=
4 5 6 b = [4 5 6];
r=
0 0 0 0 0
Polynomial Derivatives
The polyder function computes the derivative of any polynomial.
o To obtain the derivative of the polynomial.
>>p= [1 0 -2 -5]
>>q = polyder(p)
q=
3 0 -2
polyder also computes the derivative of the product or quotient of two polynomials.
o For example, create two polynomials a and b:
>>a = [1 3 5];
>>b = [2 4 6];
Calculate the derivative of the product a*b by calling polyder with a single output argument:
>>c = polyder(a,b)
c=
8 30 56 38
Polynomial Derivatives (con’t…)
Calculate the derivative of the quotient a/b by calling polyder with two output
arguments:
>>[q,d] = polyder(a,b)
q=
-2 -8 -2
q/d is the result of the operation.
d=
4 16 40 48 36
polyder also co mputes the derivative of the product or quotient of two polynomials.
o For example, create two polynomials a and b:
Partial Fraction Expansion
‘residue’ finds the partial fraction expansion of the ratio of two polynomials.
This is particularly useful for applications that represent systems in transfer
function form.
For polynomials b and a,
b2 =
-4 8
a2 =
1 6 8
FEEDBACK CONTROL SYSTEM
LAB # 1
Prepared By :
M. Hammad Saleem
[b1,a1]=residue(r,p,k)
z= p= k=
-3 0 2
-1 -4.5198
-0.7401 + 2.1822i
-0.7401 - 2.1822i
The result specifies that the zeros are at s=-3 and -1, the poles are at
s=0, -4.5198, -0.7401 + 2.1822i and -0.7401 - 2.1822i and the gain is k=2.
o Pole-zero map for this function can be obtained by using the following command.
pzmap(num,den)
1.4) Verify the results obtained for example 1.3 by obtaining the transfer
function from the calculated values of zeros, poles and gain.
close all;
clear all;
clc;
z=[-3; -1];
p=[0; -4.5198; -0.7401 + 2.1822i; -0.7401 - 2.1822i];
k=2;
[n,d]=zp2tf(z,p,k);
printsys(n,d,'s') % prints the transfer function as a ratio of two
polynomials in the transform variable 's'.
clear all;
close all;
clc;
syms t %define the function f(t)
ft=exp(-t)*(1-sin(t));
fs=laplace(ft)
fs =
1/(s + 1) - 1/((s + 1)^2 + 1)
1.6) Find the inverse Laplace transform of the function
F(s) =1/(s + 4).
clear all;
close all;
clc;
syms s t %construct symbolic objects for Laplace
transform variable 's' and time variable 't'.
fs=1/(s+4);
ft=ilaplace(fs)
LAB # 2
Prepared By :
M. Hammad Saleem
o Where;
o According to the laws of physics
-------------------- (1)
------------------------ (3)
Or
------------------------ (4)
function dvdt=cruse_speed(t,v)
M=750;
B=30;
Fa=300;
%dv/dt=(Fa/M)-(B/M)v
dvdt=Fa/M-B/M*v;
2. create a new MATLAB m-file and write
v0=0;% (initial speed)
[t,v]=ode45('cruse_speed',[0 200],v0);
plot(t,v);
grid on;
title('cruise speed time response to a constant fraction force Fa(t)')
Mass-Spring System Example:
o Assume the spring force Fs(x) = k xr(t). The mass-spring
damper is now equivalent to
function dXdt=mass_spring(t,X)
M=705; % (Kg)
B=30; % (Nsec/m)
Fa=300; % (N)
K=15; % (N/m)
dXdt(1,1)=X(2); % dX/dt
dXdt(2,1)=-B/M*X(2)-K/M*X(1)+Fa/M;
2. In MATLAB write
>>[t,X]=ode45('mass_spring', [0 200],X0);
MATLAB RESULTS
clear all
close all
clc
X0=[0;0];% (Initial speed and position)
[t,X]=ode45('mass_spring',[0 200],X0);
figure;
plot(t,X(:,1));
xlabel('Time(t)');
ylabel('Position');
title('Mass spring system');
legend('Position ');
grid;
figure;
plot(t,X(:,2),'r');
xlabel('Time(t)');
ylabel('Speed');
title('Mass spring system');
legend('Speed ');
grid;
CONCLUSIONS:
FEEDBACK CONTROL SYSTEM
LAB # 3
Prepared By :
Muhammad
Hammad Saleem
NED University of Engineering & Technology
The objective of this session is:
Where;
Obtain the state equations for the system, where xb is the system output.
Let
figure;
subplot(2,1,1);
plot(t,X(:,1));
plot(t,X(:,2),'r');
xlabel('Time(t)');
ylabel('Position xa / Speed Va');
title('Mass spring system');
legend(‘xa',‘va');
grid;
subplot(2,1,2)
plot(t,X(:,3));
hold;
plot(t,X(:,4),'r');
xlabel('Time(t)');
legend(‘xb',‘vb');
grid;
FEEDBACK CONTROL SYSTEM
LAB # 4
Prepared By :
M. Hammad Saleem
Where;
o e is applied Potential.
o i is the mesh current.
Exercise 1:
e(t)=VL+VC+VR
𝟏
e(t)=LD i + 𝒊+ iR
𝑪𝑫
Exercise 2:
Obtain the state equations for the given fig.(a)
STATE EQUATION:
The ode45 solver can be now be used:
1. create a MATLAB-function RLC.m
function dXdt=RLC(t,X)
e=60; % (V)
R=10; % (Ohm)
L=1; % (H)
C=10; % (F)
%dX/dt
dXdt(1,1)=(1/C)*X(2);
dXdt(2,1)=(-1/L)*X(1)-(R/L)*X(2)+(1/L)*e;
2. Write an other M. file to call the function:
clear
close
Clc
X0=[0 0];
[t,X]=ode45('RLC',[0 500],X0);
subplot(2,1,1);
plot(t,X(:,1));
legend('Vc');
grid on;
title('Vc');
subplot(2,1,2);
plot(t,X(:,2),'r');
legend('i');
grid on;
title('i');
Graph:
Time constant = RC = 10*10= 100 sec
For first time constant :
Vc=63.2% * e = 0.632*60 =37.92 V
Exercise 2:
Obtain the state equations for the given fig.(a)
STATE EQUATION:
FEEDBACK CONTROL SYSTEM
LAB # 5
Prepared By :
Muhammad
Hammad Saleem
NED University of Engineering & Technology
Lab Experiment 5
o A Simulink block is provided for a Step input, a signal that changes (usually
from zero) to a specified new, constant level at a specified time. These levels
and time can be specified through the dialog box, obtained by double-clicking
on the Step block.
SIGNAL GENERATOR (Source library)
• Start MATLAB.
• Start Simulink.
• Open the libraries that contain the blocks you will need.
These usually will include the Sources, Sinks, Math and
Continuous libraries, and possibly others.
• Open a new Simulink window.
• Drag the needed blocks from their library folders to that
window. The Math library, for example, contains the Gain
and Sum blocks.
• Arrange these blocks in an orderly way corresponding to
the equations to be solved.
• Interconnect the blocks by dragging the cursor from the
output of one block to the input of another block.
Interconnecting branches can be made by right-clicking
on an existing branch.
• Double-click on any block having parameters that must
be established, and set these parameters. For
example, the gain of all Gain blocks must be set. The
number and signs of the inputs to a Sum block must be
established. The parameters of any source blocks
should also be set in this way.
• It is necessary to specify a stop time for the solution.
This is done by clicking on the Simulation >
Parameters entry on the Simulink toolbar.
Mass-Spring System Model
Consider the Mass-Spring system used in the previous exercise as shown
in the figure.
Where;
o Fs(x) is the spring force.
o Ff(t) is the friction coefficient
o x(t) is the displacement
o Fa(t) is the applied force
M-file for parameter values
Fa = 300; %N
M = 750; %kg
K = 15; %N/m
B = 30; % Ns/m
FEEDBACK CONTROL SYSTEM
LAB # 6
Prepared By :
Muhammad
Hammad Saleem
NED University of Engineering & Technology
Lab Experiment 6
LAB # 9
Prepared By :
Muhammad Hammad
Saleem
close all;
clear all;
clc;
n=[0 0 30];
d=[1 5 6];
% the ramp response can be obtained by using step
command for transfer
% function divided by s.The transfer function
G1(s)=G(s)/s.
n1=[0 0 0 30];
d1=[1 5 6 0];
[y,x,t]=step(n1,d1);
% To plot output y vs time t and t vs t i.e ramp signal on
same graph window.
v=[0 10 0 10];
plot(t,y);
axis(v);
grid;
title('Plot of unit ramp response of G(s)=[30]/[s^2+5s+6]');
xlabel('Time');
ylabel('Amplitude');
Example 2:
lsim(n,d,u,t)
Where;
o n and d are numerator and denominator polynomials of system
transfer function.
o u is the arbitrary input signal and t defines time for which response of
the system is required.
Another form of this command, which gives the output y in vector form
without response plot, is;
[y,t]=lsim(n,d,u,t)
The following program can be used:
n=[1 5];
d=[1 2 3 5];
t=0:0.15:9;
r=exp(-0.2*t);
y=lsim(n,d,r,t);
plot(t,r,'-',t,y,'o');
grid;
title('plot of the sytem for arbitrary input r(t)=e-0.2t');
xlabel('Time');
ylabel('Amplitude');
plot of the sysytem for arbitrary input r(t)=e-0.2t
1.4
1.2
0.8
Amplitude
0.6
0.4
0.2
-0.2
-0.4
0 1 2 3 4 5 6 7 8 9
Time
Exercises:
30
s2 + 5s + 30
2) Obtain the response of the system defined in example
2 for an input
r (t) =sint+e-0.2t, for t=0 to 15 sec, in steps of 0.001 sec
and comment on the result.
FEEDBACK CONTROL SYSTEM
LAB # 10
Prepared By :
Muhammad Hammad Saleem
The objective of this exercise will be to learn commands in MATLAB that would
be used to reduce linear systems block diagram using series, parallel and
feedback configuration.
Series Configuration:
o If the two blocks are connected as shown below then
the blocks are said to be in series.
Example 1:
Given the transfer functions of individual blocks generate the system transfer
function of the block combinations.
Parallel Configuration:
o If the two blocks are connected as shown below then
the blocks are said to be in parallel.
Example 2:
For the previous systems defined, modify the MATLAB commands to obtain
the overall transfer function when the two blocks are in parallel
Feedback Configuration:
o If the blocks are connected as shown below then the
“feedback”.
The feedback command is implemented as shown below:
Example 4:
Given a non-unity feedback system as shown in the figure, obtain the overall
transfer function using MATLAB:
Block Diagram Transformation Theorems
Exercises:
For the following multi-loop feedback system, get closed loop transfer
function and the corresponding pole-zero map of the system.
Program:
close all G5=feedback(G3*G4,H1,+1);
clear all G6=feedback(G2*G5,H2/G4,-1);
clc
G7=feedback(G1*G6,H3,-1)
G1 = tf([1],[1 10]);
G2 = tf([1],[1 1]); zpk(G7)
G3 = tf([1 0 1],[1 4 4]); pzmap(G7)
G4 = tf([1 1],[1 6]); figure;
H1 = tf([1 1],[1 2]); step(G7);
H2 = 2; stepinfo(G7)
H3 = 1; figure;
Impulse(G7);
130
LAB # 11
Prepared By :
Muhammad Hammad Saleem
Lab Experiment 11
• Study the three term (PID) controller and its effects on the
feedback loop response.
• Investigate the characteristics of the each of proportional
(P), the integral (I), and the derivative (D) controls, and
• how to use them to obtain a desired response.
132
Introduction:
o Consider the following unity feedback system:
o KP = Proportional gain
o KI = Integral gain
o KD = Derivative gain
134
o This signal (u) will be sent to the plant, and the new
output (Y) will be obtained.
o This new output (Y) will be sent back to the sensor
again to find the new error signal (e).
o The controller takes this new error signal and
computes its derivatives and its internal again.
o The process goes on and on.
137
Example Problem:
Let
M = 1kg
b = 10 N.s/m
k = 20 N/m
F(s) = 1
num=1;
den=[1 10 20];
plant=tf(num,den);
step(plant)
Proportional control:
o The closed-loop transfer function of the above system
with a proportional controller is:
Proportional control:
Kp=300;
contr=Kp;
sys_cl=feedback(contr*plant,1); %by default –ve
feedback
t=0:0.01:2;
step(sys_cl,t)
144
Proportional control:
Proportional-Derivative control:
o The closed-loop transfer function of the given system with
a PD controller is:
Proportional-Derivative control:
Kp=300;
Kd=10;
contr=tf([Kd Kp],1);
sys_cl=feedback(contr*plant,1);
t=0:0.01:2;
step(sys_cl,t)
147
Proportional-Derivative control:
Proportional-Integral control:
o The closed-loop transfer function of the given system with
a PI controller is:
Proportional-Integral control:
Kp=30;
Ki=70;
contr=tf([Kp Ki],[1 0]);
sys_cl=feedback(contr*plant,1);
t=0:0.01:2;
step(sys_cl,t)
150
Proportional-Integral control:
Proportional-Integral-Derivative control:
o Now, let's take a look at a PID controller. The closed-loop
transfer function of the given system with a PID controller
is:
Proportional-Integral-Derivative control:
Kp=350;
Ki=300;
Kd=50;
contr=tf([Kd Kp Ki],[1 0]);
sys_cl=feedback(contr*plant,1);
t=0:0.01:2;
step(sys_cl,t)
153
Proportional-Integral-Derivative control
• no overshoot,
• fast rise time, and
• no steady-state error.
154
Exercise: