Controlmanual Complte UpdatedwithCLOS (1)
Controlmanual Complte UpdatedwithCLOS (1)
Controlmanual Complte UpdatedwithCLOS (1)
Assingment
Submitted to:
Engr. Ammad Arshad
Submitted by:
Name: Mohsin Shahzad
(Meen211101082)
(Meen 6B)
Matlab is a tool for doing numerical computations with matrices and vectors. It can
also display information graphically. The best way to learn what Matlab can do is to work
through some examples at the computer.
Matlab program and script files always have filenames ending with ".m"; the
programming language is exceptionally straightforward since almost every data object is
assumed to be an array. Graphical output is available to supplement numerical results.
1.1 Matrices
Once you know how to enter and display matrices, it is easy to compute with them. First we
will square the matrix a :
>> a * a
Wasn't that easy? Now we'll try something a little harder. First we define a matrix b:
>> b = [ 1 2; 0 1 ]
Then we compute the product ab:
>> a*b
>> b*a
Now let's store the result of this addition so that we can use it later:
>> s = a + b
Matrices can sometimes be inverted:
>> inv(s)
To check that this is correct, we compute the product of s and its inverse:
>> s * inv(s)
The result is the unit, or identity matrix. We can also write the computation as
>> s/s
>> s\s
which is the same as
>> inv(s) * s
To see that these operations, left and right division, are really different, we do the following:
>> a/b
>> a\b
Not all matrices can be inverted, or used as the denominator in matrix division:
>> c = [ 1 1; 1 1 ]
>> inv(c);
>> det(a)
>> det(c)
A transpose of a matrix
>> a’
rand(7)
You can generate random matrices of other sizes and get help on the rand command within
matlab:
rand(2,5)
help rand
Another special matrix, called a Hilbert matrix, is a standard example in numerical linear
algebra.
hilb(5)
help hilb
magic(5)
help magic
A magic square is a square matrix which has equal sums along all its rows and columns.
We'll use matrix multiplication to check this property a bit later.
Some of the standard matrices from linear algebra are easily produced:
eye(6)
zeros(4,7)
ones(5)
You can also build matrices of your own with any entries that you may want
who
Whos
>> whos
Name Size Bytes Class
Clc
Clear
Clear (variables)
Colon Notation:
Matlab offers some powerful methods for creating arrays and for taking them apart.
x=-2:1
length(x)
-2:.5:1
-2:.2:1
a=magic(5)
a(2,3)
a(:,3)
a(2:4,:)
a(:,3:5)
a(2:4,3:5)
a(1:2:5,:)
a(:,[1 2 5])
And
casesen
format long
format short
will return to the shorter display. It is also possible to toggle back and forth in the scientific
notation display with the commands
format short e
and
format long e
It is not always necessary for MATLAB to display the results of a command to the screen. If
you do not want the matrix A displayed, put a semicolon after it, A;. When MATLAB is ready
to proceed, the prompt >> will appear. Try this on a matrix right now.
Sometimes you will have spent much time creating matrices in the course of your MATLAB
session and you would like to use these same matrices in your next session. You can save these
values in a file by typing
save filename
filename.mat
[a][x] = [b]
where
a11 a12 a13 ..... a1n x1 b1
Note that only when the equations are linear in the unknown
xi's is the matrix formulation possible.
a-1 a = I
where a-1 denotes the 'inverse of matrix a' and I denotes the
identity matrix of the same order as matrix 'a'. If 'a' is a
known coefficient matrix and if 'b' is a column vector of known
terms, the problem is one of finding the n vales of x1, x2, ....
[a-1][a][x] = [a-1][b]
or
[x] = [a-1][b]
C = [1 -4 3 2; 3 1 -2 1; 2 1 1 -1; 2 -1 3 1]
C_inverse = inv(C)
x1 - 4x2 + 3x3 = -7
3x1 + x2 - 2x3 = 14
2x1 + x2 + x3 = 5
Using 'matlab', define the two matrices for [a] and [b].
a = [ 1 -4 3; 3 1 -2; 2 1 1];
b = [ -7; 14; 5];
x = [ 3
1
-2 ]
x1 - 4x2 + 3x3 = -7
13x2 - 11x3 = 35
(34/13)x3 = (68/13)
In this format, we see from the third equation the solution for
x3= 2 and knowing this, the second equation yields x2 = 1, and
knowing both x3 and x2, the first equation gives x1 = 1. This
is an application of back substitution, whereby each unknown is
obtained by simple sequential solution to a single equation.
Methods of back substitution are employed by 'matlab' when we
invoke the 'right division' and 'left division' commands.
\ left division
/ right division
[x] = [a]\[b]
a = [ 1 -4 3; 3 1 -2; 2 1 1];
b = [ -7; 14; 5];
x1 = [ 3
1
-2 ]
[x][A] = [B]
Note that in this form [x] and [B] are row vectors rather than
column vectors as above. This form represent a 1xn matrix (x)
pre- multiplying a nxn matrix (A), resulting an a 1xn matrix
(B). Again, recalling the set of equations,
x1 - 4x2 + 3x3 = -7
2x1 + x2 + x3 = 5
[x][A] =[B]
if
x = [x1 x2 x3] B = [ -7 14 5]
and
1 3 2
[A] = -4 1 1
3 -2 1
A = a'
Having so defined A and B, the solution for x can be obtained
by
right division.
x = B/A
results in
x = [ 3
1
-2 ]
1.8 Exercise:
r + s + t + w = 4
2r - s + w = 2
3r + s - t - w = 2
r - 2s - 3t + w = -3
Rubric 1
Marks CLO1 – Level C2 mapped to PLO 5 (Software Usage)
02 Does not know how to use MATLAB to program and analyse data.
Major help is required in handling MATLAB.
06 Uses MATLAB to program and analyse data with minor error help.
2.0 Plotting:
The simplest graphs to create are plots of points in the cartesian plane. For example:
>> x = [1;2;3;4;5];
>> y = [0;.25;3;1.5;2];
>> plot(x,y)
The resulting graph is displayed in Figure
Notice that, by default, Matlab connects the points with straight line segments. An alternative
is the following ():
>> plot(x,y,'o')
>> x = -10:.1:10;
>> plot(sin(x))
Note that the horizontal axis is marked according to the index, not
the value of x. Fix this as follows:
We can plot the "inverse relationship" (for example, the squaring function and +/- square
root) easily:
>> t = (0:.1:2*pi)';
>> subplot(2,2,1)
>> plot(t,sin(t))
>> subplot(2,2,2)
>> plot(t,cos(t))
>> subplot(2,2,3)
>> plot(t,exp(t))
>> subplot(2,2,4)
>> plot(t,1./(1+t.^2))
If you have hard time seeing some of the plots that you do in
matlab on the color workstations, you should probably change
the colors.
To find out how to do that, you can type at the matlab prompt:
help plot
plot(x,y,'w')
to plot a white line. You can also use other colors -- they
are all listed in the plot help information.
Functions of two variables may be plotted, as well, but some "setup" is required!
Here are two options for plotting the surface. Look at the help page for details.
2.4 3D Plots:
In order to create a graph of a surface in 3-space (or a contour plot of a surface), it is necessary
to evaluate the function on a regular rectangular grid. This can be done using the meshgrid
command. First, create 1D vectors describing the grids in the x- and y-directions:
>> x = (0:2*pi/20:2*pi)';
>> y = (0:4*pi/40:4*pi)';
Having created the matrix containing the samples of the function, the surface can be graphed
using either the mesh or the surf commands :
>> mesh(x,y,z)
>> surf(x,y,z)
(The difference is that surf shades the surface, while mesh does not.) In addition, a contour
plot can be created :
>> contour(x,y,z)
Polar plots:
angle = 0:.1*pi:3*pi;
radius = exp(angle/20);
polar(angle,radius),...
title('An Example Polar Plot'),...
grid
Bar graph:
Multiple plots:
x1=0:.05*pi:pi;
y1=sin(x1);
plot(x1,y1)
hold
y2=cos(x1);
plot(x1,y2)
x1 y1
x2 y2
x3 y3
.. ..
xn yn
load test.dat
x = 2,4 10,25,16,13,14,18
y = -1,2,4,7,6,12,13,-16
[y] = [X][a]
y1 1 x1 x12.....x1n+1 a1
y2 1 x2 x22.....x2n+1 a2
[y] = [X] = [a] =
y3 1 x3 x32.....x3n+1 a3
. . . . . .
. . . . . .
. . . . . .
yn+1 1 xn+1 xn+12... xn+1n+1 an+1
Note that the right side of the matrix equation must be the
product of a 'n+1' x 'n+1' square matrix times a 'n+1' x 1
column matrix! The solution using Gauss elimination calls for
left division in 'matlab' or
[a] = [X]\[y]
x y
2 4
3 3
4 5
5 4
6 7
7 5
8 7
9 10
10 9
x9 = [2:1:10];
plot(x9,y9,'o')
y = y9'
X =
[ones(1,9);x9;x9.^2;x9.^3;x9.^4;x9.^5;x9.^6;x9.^7;x9.^8]'
[X][a] = [y]
a = X\y
which results in
a = [ 1.0e+003*
3.8140
-6.6204
4.7831
-1.8859
.4457
- .0649
.0057
- .0003
.0000 ]
format long a
x = [ 2:.1:10 ];
plot(x,y,x9,y9,'o')
02 Does not know how to use MATLAB to program and analyse data.
Major help is required in handling MATLAB.
06 Uses MATLAB to program and analyse data with minor error help.
>> t = rand(1);
>> if t > 0.75
s = 0;
elseif t < 0.25
s = 1;
else
s = 1-2*(t-0.25);
end
>> s
s =
0
>> t
t =
0.7622
The logical operators in Matlab are <, >, <=, >=, == (logical equals), and ~= (not equal).
These are binary operators which return the values 0 and 1 (for scalar arguments):
>> 5>3
ans =
1
>> 5<3
ans =
0
>> 5==3
ans =
0
if expr1
statements
elseif expr2
statements
.
.
.
else
statements
end
Matlab provides two types of loops, a for-loop (comparable to a Fortran do-loop or a C for-
loop) and a while-loop. A for-loop repeats the statements in the loop as the loop index takes
on the values in a given row vector:
>> x=1;
>> while 1+x > 1
x = x/2;
end
>> x
x =
1.1102e-16
.. . 2
X + AX + K X = 0 <= Damped harmonic oscillator
Now, you need to write a matlab function that takes Y1, Y2,
and time as arguments and returns Y1 and Y2. To do this,
create a file using matlab editor or your own favorite editor
that contains the following:
Ydot(1) = Y(2);
Ydot(2) = -A*Y(2)-K^2*Y(1);
Now, the moment we have been waiting for: let's solve this
ODE. At the matlab prompt, type:
Note:
X(t=0) = 1
.
X(t=0) = 0
>> plot(T,Y)
or,
>> plot(T,Y(:,1),'-',T,Y(:,2),'--');
.. . 2
X + AX + K X = B cos(wt)
Ydot(1) = Y(2);
Ydot(2) = -A*Y(2)-K^2*Y(1)+B*cos(w*t);
----------------------------------------------------------------
Make the program and solve the following equation. Submit the program as well as the result
print out.
.. .
y(t ) 3 y(t ) 2 y(t ) f (t )
.
y(0) 1 y(0) 0
f (t ) 1 t 0
B=roots(a)
(x2+2x-1)(x+1)
It can be multiplied
[B,A] = RESIDUE(R,P,K), with 3 input arguments and 2 output arguments, converts the
partial fraction expansion back to the polynomials with coefficients in B and A.
Rubric 1
Marks CLO1 – Level C2 mapped to PLO 5 (Software Usage)
02 Does not know how to use MATLAB to program and analyse data.
Major help is required in handling MATLAB.
06 Uses MATLAB to program and analyse data with minor error help.
Communications Toolbox
Signal processing Toolbox
Filter design Toolbox
Control systems Toolbox
Optimization Toolbox
Image Processing Toolbox
Symbolic Toolbox
Financial Toolbox
Mathematics etc.
Command Prompt
Entering Variables
Controlling Input and output
Case sensitivity
Suppressing output
4.4 Finding roots of a transfer function and plotting its poles and zeros
Writing polynomial :
den = conv(den1,den2);
printsys(num,den)
den_r = roots(den)
den = poly(den_r);
val_den = polyval(den,s);
pzmap(num,den)
series()
parallel()
G1(s)
+
Y(s)
X(s) G1(s)+-
X(s) Y(s)
G2(s)
G2(s)
Control Engineering Lab 40/10
1
4.5.3 Closed loop transfer function calculation:
cloop()
feedback()
H(s)
%
% ME 224 Control Systems
% Author : Liaquat Ali Khan
% Date : xx-xx-xxx
% Lab No. 1
% Class : BEMTS-VI A&B
% File name : blk_reduction_ex.m
% Description : The function implements a transfer function and demo for
% following functions:
% reducing blocks in series: series()
% reducing blocks in parallel: parallel()
% calculating closed loop gain: cloop()
% calculating feedback: feedback()
%
% series()
sys = series(Gs1,Gs2)
pause
% parallel()
sys = parallel(Gs1,Gs2)
pause
sys = parallel(Gs1,-Gs2)
pause
% cloop()
% obsolete now, use feedback with H(S) = 1
sys = feedback(Gs1,1,+1)
pause
sys = feedback(Gs1,1,-1)
pause
% feedback
sys = feedback(Gs1,Gs2,+1)
pause
sys = feedback(Gs1,Gs2,-1)
pause
%
% End
%
%
% ME 224 Control Systems
% Author : Liaquat Ali Khan
% Date : xx-xx-xxx
% Lab No. 1
% Class : BEMTS-VI A&B
% File name : min_real_ex.m
% Description : The function implements a transfer function and demo for
% following functions:
% minimizing a transfer function using minreal()
Control Engineering Lab 42/10
1
%
% Defining a T(s)
num = [1 -1 0]
den = [1 0 0 -1]
printsys(num,den)
pause
sys=tf(num,den);
min_sys = minreal(sys)
pause
%
% End
%
4.8 Exercises
You have to submit the solution of every exercise in group containing the following.
Exercise-1:
Exercise-3:
Steady state value of the system is that value which is the desired response of
the system, This is also called system d.c. gain. Manually it can be found by
just putting s=0 in the transfer function or t=∞.
Td(s)
Amplifier -
R(s) + Ea(s) + Km/ Tm(s) TL(s) 1/
w(s)
Ra + Js+b
- -
Kb
Tachometer
Vt(s)
5.2.1 Requirement:
Td(s)
-
+ Km/ Tm(s) TL(s) 1/
Va(s) w(s)
Ra + Js+b
-
Ra Km J b Kb Ka Kt
1 10 2 0.5 0.1 54 1
Td(s)
Amplifier
Ea(s) -
+ Km/ Tm(s) TL(s) 1/
w(s)
Ra + Js+b
-
Tachometer
Vt(s)
1
T (s)
2s 541.5
5.2.2 Result: Adding negative feedback, increases the Disturbance Rejection property of
the system, since steady state achieved Quickly with very little change in output
w(s).
5.2.3 MATLAB Code for the Open-loop without tachometer feedback of the above
example.
%
% ME 224 Control Systems
% Author : Liaquat Ali Khan
% Date : xxxxxxdx
% Lab No. 5
% Class : BEMTS VI (A & B)
% File name : opentach.m
% Description : The function implements the speed tachometer example.
%
%
% Define Tachometer control system parameters
Ra = 1;
Km = 10;
J = 2;
b = 0.5;
Kb = 0.1;
Ka = 54;
5.2.4 MATLAB Code for the Open-loop/Close-loop with/ without tachometer feedback of
the above example.
%
% ME 224 Control Systems
% Author : Liaquat Ali Khan
% Date xxxxxx
% Lab No. 5
% Class : BEMTS VI (A & B)
% File name : open_close_tach.m
% Description : The function implements the speed tachometer example.
%
%
% Define Tachometer control system parameters
Ra = 1;
Km = 10;
J = 2;
b = 0.5;
Kb = 0.1;
Ka = 54;
Kt = 1;
Control Engineering Lab 50/10
1
% Define G(s)= 1 / Js+b
num1 = [1];
den1 = [J b];
% Define H(s) = Km*Kb/Ra
numo = [Km*Kb/Ra];
deno = [1];
% Find the T(S)= w(s) / Td(s)
[numop,denop] = feedback(num1,den1,numo,deno);
% Change the sign of T(s) since Td(s) is negative
numop = -numop
% print the final T(S)
printsys(numop,denop);
% Compute response to step disturbance
[step_resp_op,x_op,t_op] = step(numop,denop);
%
% Define G1(s) = Ka*Kt
num2 = [Ka*Kt];
den2 = [1];
% Define G2(s) = Kb
num3 = [Kb];
den3 = [1];
% Define G3(s) = Km/Ra
num4 = [Km / Ra];
den4 = [1];
% Combine G1(s) and G2(s) in parallel
[numa,dena] = parallel(num2,den2,num3,den3);
% Combine the above and G3(s) in series
[numb,denb] = series(numa,dena,num4,den4);
% Combine the above and G(S) in feedback
[numcl,dencl] = feedback(num1,den1,numb,denb);
% Change the sign of T(s) since Td(s) is negative
numcl = -numcl
% print the final T(S)
printsys(numcl,dencl);
% Compute response to step disturbance
[step_resp_cl,x_cl,t_cl] = step(numcl,dencl);
%
figure
subplot(211)
plot(t_op,step_resp_op);
title('Open-loop Disturbance Step Response');
ylabel('speed');
grid;
subplot(212)
plot(t_cl,step_resp_cl);
title('Close-loop Disturbance Step Response');
xlabel('time[sec]');
Control Engineering Lab 51/10
1
ylabel('speed');
grid;
D(s)
E(s) +
+
R(s) K+11s 1/s(s+1) w(s)
+
-
1
Y(s) K 11s R(s) D(s)
s2 12s K s2 12s K
5.3.1 Case-I Without Disturbance the closed loop system
E(s)
+
R(s) K+11s 1/s(s+1) w(s)
-
5.3.1.1 Result:
Under no disturbance System with K=20 has more settling time but low overshoot
D(s)
+
K+11s 1/s(s+1) w(s)
+
5.3.2.1 Result:
Under disturbance System with K=100 shows better performance If our
requirement is disturbance rejection then K =100 Is better choice
5.3.3 MATLAB Code for the close-loop with K=20, and K=100
%
% ME 224 Control Systems
% Author : Liaquat Ali Khan
% Date xxxxxxxxx
% Lab No. 5
% Class : BEMTS (VI and A & B)
% File name : english1.m
% Description : The function implements the English channel boring
%
%
% response to a uit step input R(s)=1/s for k = 20 and k = 100
%
numg = [1];
deng = [1 1 0];
K1 = 100;
K2 = 20;
num1 = [11 K1];
num2 = [11 K2];
den = [0 1];
%
[na,da] = series(num1,den,numg,deng);
[nb,db] = series(num2,den,numg,deng);
%
[numa,dena] = cloop(na,da);
Control Engineering Lab 53/10
1
[numb,denb] = cloop(nb,db);
%
t=[0:0.01:2.0];
[y1,x,t] = step(numa,dena,t);
[y2,x,t] = step(numb,denb,t);
%
figure
subplot(211)
plot(t,y1)
title('Step response for K=100')
ylabel('y(t)')
grid
subplot(212)
plot(t,y2)
title('Step response for K=20')
xlabel('time[sec]')
ylabel('y(t)')
grid
%
% End Function
%
Exercise-1
Consider the torsional mechanical system in the Fig-5.10. The torque due to the twisting of
the shaft is kθ; the damping torque due to the braking device is bθ ; the disturbance
torque is d(t); the input torque is r(t); an the moment of inertia of the mechanical system is J.
The transfer function of the torsional mechanical system is shown below.
G(s) 1
s 2 (b / J )s k / J
Suppose the desired angle θd = 0°, k = 5 , and J = 1.
Do the following:
(a) Draw the control canonical form of the Block diagram and the transfer function, and
also confirm the transfer function by taking transfer function using Mason’s Gain
Formula.
(b) Determine the open-loop response θ(t) of the system for a unit step disturbance d(t)
using MATLAB (set r(t)=0) . Also confirm the result by finding the analytical result.
(c) With the controller gain Ko = 50, determine the closed-loop response, θ(t), to a unit
step disturbance, d(t), using MATLAB.
(d) Co-plot the open-loop versus the closed-loop response to the disturbance input.
(e) Discuss your results and make an argument for using closed-loop feedback control to
improve the disturbance rejection properties.
(f) Write the state-space equation, from the block diagram, and confirm the transfer
function, manually and then by using MATLAB.
Tffss= ss (A, B, C, D)
Where, A, B, C and D are the metrics from the state space equation.
Exercise-2:
The control of the roll angel of an airplane is achieved by sing the toque developem by
the ailerons, as shown in Fig 5.11(a) and (b). A linear model of the roll control system
for a small experimental aircraft is shown in Fig 5.11(c), where q(t) is the flow of fluid
into a hydraulic cylinder and
1
G(s)
s2 4s 9
The goal is to maintain a small roll angle θ due to disturbances. Select an appropriate
gain KK1, that will reduce the effect of the disturbance while attaining a desirable
transient response to a step disturbance, with θd(t) = 0. To require a desirable transient
response, let KK1<35. Analyze completely in MATLAB by making an “m-file”.
θ(s)
Note: You have to submit all the calculations, m-files, and their responses output in terms of
graphs.
02 Is not able to write the code in MATLAB using control functions and
interpret it. Major help is required in writing the program.
06 Can write and interpret MATLAB codes using control functions with
minor error help.
10 Can write and interpret MATLAB codes using control functions
effectively and confidently.
ys = dcgain(num,den) : To get the d.c. gain of the system (steady state value)
For the following systems find first analytically and then by using the MATLAB the
response of the systems due to Step input and Impulse input. Also calculate the
system
(i) Time constant
(ii) Settling time
(iii) System d.c. gain.
(iv) Percent over shoot (part b only)
(v) Maximum amplitude.
(vi) Peat time
(a)
R(s) C(s)
(b)
R(s) C(s)
(c) Make (a) and (b) as the unity feedback system and find the closed loop transfer
function and step response of the system. Do manually and then by doing
everything using MATLAB m.file.
π 4
TP Ts M PT 1 eζπ / β
ϖn ςϖ
n
G(s) wn2
s2 2ζ wn wn2
The step response of this 2nd order system is as follows.
1
c(t) 1 eζϖ n sin( θ )
t
nt
βϖ β
β 1 ζ 2
Plot the different responses with varying ωnt (0-14) take ωn=1 rad/s. Vary the value
of ζ from 0 to 2 with an increment of 0.1. Do everything making an m.file in
MATLAB.
6.3 Impulse response of a second order system with varying value of damping
coefficient (ζ)
Plot the impulse response of the 2nd order system given in section 5.1. The impulse
response is given below. It is actually the derivation of the step response.
The engine, body, and tires of a racing vehicle affect the acceleration and speed
attainable. The speed control of the car is represented by the model shown in the
following figure (6.3)
(a) Calculate the steady-state error of the car to a step command in speed.
(b) Calculate overshoot of the speed to step input command, and other useful
commands mention at the beginning of the lab.
For year, Amtrak ahs struggled to attract passengers on its routs in the Midwest, using
technology developed decades ago. During the same time, foreign railroads were developing
new passenger rai systems that could profitably compete with air travel. Two of these systems,
the French TGV and the Japanese Shinkansen, each speeds of 160 mph. The tranrapid-06, a
U.S. experimental magnetic levitation train is shown in the following figure.
The use of magnetic levitation and electro-magnetic propulsion to provide contact less
vehicle movement makes the Transrapid-06 technology radically different from the existing
(a) Find the step response for K=40, and also calculate the percent over shoot.
(b) Select or analyze the value of K such that the over shoot is not more than 5%
for the step input.
2(s 8)
G(s)
s(s 4)
(a) Determine the closed-loop transfer function T(s)=Y(s)/R(s) using manually, and
using the MATLAB.
Effective control of insulin injections can result in better lives for diabetic persons.
Automatically controlled insulin injection by means of a pump and a sensor that
measures blood sugar can be very effective. A pump and injection system has a
feedback control as shown in Fig(6.5). Calculate the suitable gain K so that the
overshoot of the step response due to the drug injection is approximately 7%. R(s) is
the desired blood-sugar level and Y(s) is the actual blood-sugar level.
The roll control autopilot of a jet fighter is shown in the Fig(6.6). The goal is to
select a suitable K so that the response to a unit step command φd(t)=1, t ≥ 0, will
provide a reponse φ(t) that is a fast response andhas an overshoot of less than 20%.
02 Is not able to write the code in MATLAB using control functions and
interpret it. Major help is required in writing the program.
06 Can write and interpret MATLAB codes using control functions with
minor error help.
10 Can write and interpret MATLAB codes using control functions
effectively and confidently.
Simulink is a time based software package that is included in Matlab and its main task is to
solve Ordinary Differential Equations (ODE) numerically. The need for the numerical
solution comes from the fact that there is not an analytical solution for all DE, especially for
those that are nonlinear.
The whole idea is to break the ODE into small time segments and to calculate the solution
numerically for only a small segment. The length of each segment is called “step size”. Since
the method is numerical and not analytical there will be an error in the solution. The error
depends on the specific method and on the step size (usually denoted by h).
There are various formulas that can solve these equations numerically. Simulink uses
Dormand-Prince (ODE5), fourth-order Runge-Kutta (ODE4), Bogacki-Shampine (ODE3),
improved Euler (ODE2) and Euler (ODE1). A rule of thumb states that the error in ODE5 is
proportional to h5, in ODE4 to h4 and so on. Hence the higher the method the smaller the error.
Unfortunately the high order methods (like ODE5) are very slow. To overcome this problem
variable step size solvers are used. When the system’s states change very slowly then the step
size can increase and hence the simulation is faster. On the other hand if the states change
rapidly then the step size must be sufficiently small.
To summarize the best method is ODE5 (or ODE45), unless you have a stiff problem, and a
smaller the step size is better, within reason.
Since the key idea of Simulink is to solve ODE let us see an example of how to accomplish
that. Through that example many important features of Simulink will be revealed.
To start Simulink click on the appropriate push button from the command window:
There are two major classes of items in Simulink: blocks and lines. Blocks are used
to generate, modify, combine, output, and display signals. Lines are used to transfer
signals from one block to another.
(i) Blocks
(ii) Lines
7.3.1 Blocks:
Blocks have zero to several input terminals and zero to several output terminals. Unused
input terminals are indicated by a small open triangle. Unused output terminals are indicated
by a small triangular point. The block shown below has an unused input terminal on the left
and an unused output terminal on the right.
7.3.2 Lines:
Lines transmit signals in the direction indicated by the arrow. Lines must always transmit
signals from the output terminal of one block to the input terminal of another block. On
exception to this is a line can tap off of another line, splitting the signal to each of two
destination blocks, as shown below.
A signal can be either a scalar signal or a vector signal. For Single-Input, Single-Output
systems, scalar signals are generally used. For Multi-Input, Multi-Output systems, vector
signals are often used, consisting of two or more scalar signals. The lines used to transmit
scalar and vector signals are identical. The type of signal carried by a line is determined by
the blocks on either end of the line.
7.3.4 Example:
The simple model consists of three blocks: Step, Transfer Fcn, and Scope. The Step is a
source block from which a step input signal originates. This signal is transfered through the
line in the direction indicated by the arrow to the Transfer Function linear block. The
Transfer Function modifies its input signal and outputs a new signal on a line to the Scope.
The Scope is a sink block used to display a signal much like an oscilloscope.
There are many more types of blocks available in Simulink, some of which will be discussed
later. Right now, we will examine just the three we have used in the simple model.
A block can be modified by double-clicking on it. For example, if you double-click on the
"Transfer Fcn" block in the simple model, you will see the following dialog box.
This dialog box contains fields for the numerator and the denominator of the block's transfer
function. By entering a vector containing the coefficients of the desired numerator or
denominator polynomial, the desired transfer function can be entered. For example, to
change the denominator to s^2+2s+1, enter the following into the denominator field:
[1 2 1]
and hit the close button, the model window will change to the following,
The "step" block can also be double-clicked, bringing up the following dialog box.
The most complicated of these three blocks is the "Scope" block. Double clicking on this
brings
In this section, you will learn how to build systems in Simulink using the building blocks in
Simulink's Block Libraries. You will build the following system.
First you will gather all the necessary blocks from the block libraries. Then you will modify
the blocks so they correspond to the blocks in the desired model. Finally, you will connect the
blocks with lines to form the complete system. After this, you will simulate the complete
system to verify that it works.
Create a new model (New from the File menu or Ctrl-N). You will get a blank model
window.
This opens the Sources window which contains the Sources Block Library. Sources are
used to generate signals.
Double-click on the Sinks icon in the main Simulink window to open the Sinks
window.
Drag the Scope block into the right side of your model window.
7.4.2 Modify Blocks:
Double-click your Sum block. Since you will want the second input to be subtracted,
enter +- into the list of signs field. Close the dialog box.
Change the name of the first Transfer Function block by clicking on the words
"Transfer Fcn". A box and an editing cursor will appear on the block's name as shown
below. Use the keyboard (the mouse is also useful) to delete the existing name and
type in the new name, "PI Controller". Click anywhere outside the name box to finish
editing.
Similarly, change the name of the second Transfer Function block from "Transfer
Fcn1" to "Plant". Now, all the blocks are entered properly. Your model should appear
as:
Drag the mouse from the output terminal of the Step block to the upper (positive) input
of the Sum block. Let go of the mouse button only when the mouse is right on the input
terminal. Do not worry about the path you follow while dragging, the line will route
itself. You should see the following.
The resulting line should have a filled arrowhead. If the arrowhead is open, as shown
below, it means it is not connected to anything.
Draw a line connecting the Sum block output to the Gain input. Also draw a line from
the Gain to the PI Controller, a line from the PI Controller to the Plant, and a line
from the Plant to the Scope. You should now have the following.
The line remaining to be drawn is the feedback signal connecting the output of the
Plant to the negative input of the Sum block. This line is different in two ways. First,
since this line loops around and does not simply follow the shortest (right-angled)
route so it needs to be drawn in several stages. Second, there is no output terminal to
start from, so the line has to tap off of an existing line.
To tap off the output line, hold the Ctrl key while dragging the mouse from the
point on the existing line where you want to tap off. In this case, start just to the
Now, the open arrowhead of this partial line can be treated as an output terminal.
Draw a line from it to the negative terminal of the Sum block in the usual manner.
Now, you will align the blocks with each other for a neater appearance. Once
connected, the actual positions of the blocks does not matter, but it is easier to read if
they are aligned. To move each block, drag it with the mouse. The lines will stay
connected and re-route themselves. The middles and corners of lines can also be
dragged to different locations. Starting at the left, drag each block so that the lines
connecting them are purely horizontal. Also, adjust the spacing between blocks to leave
room for signal labels. You should have something like:
Type an r in this box, labeling the reference signal and click outside it to end editing.
Label the error (e) signal, the control (u) signal, and the output (y) signal in the same
manner. Your final model should appear as:
7.5 Simulation
Now that the model is complete, you can simulate the model. Select Start from the
Simulation menu to run the simulation. Double-click on the Scope block to view its output.
Hit the autoscale button (binoculars) and you should see the following.
Now, you can re-run the simulation and view the output on the Scope. The result should be
the same as before.
Besides variable, signals, and even entire systems can be exchanged between MATLAB and
Simulink.
1- Students should read this lab handout carefully, and understand the basics of
SIMULINK.
2- They should perform the complete exercise, simulate it and save the model also.
3- They should attach the SIMULINK model and as well as the simulation response
of their practice. Results will be similar to those given in the handout.
02 Is not able to write the code in MATLAB using control functions and
interpret it. Major help is required in writing the program.
06 Can write and interpret MATLAB codes using control functions with
minor error help.
10 Can write and interpret MATLAB codes using control functions
effectively and confidently.
di(t) 1
Step 1: First of all we must isolate the highest derivative: (u(t) i(t)R)
dt L
Step 2: We will use as many integrators as the order of the DE that we want to solve:
The integrator block is in:
To set the value of the gain block double click on it and then change its value:
Step 6: To run the simulation we must give values to L, R. In the workspace we type:
R=0.01; L=0.01.
Step 7: To see the solution we must run the simulation and then double click on the Scope:
_
By applying Newton’s second law: F ma or F (t) Kx(t) Bu(t) ma(t) where F is
the external force applied on the mass (m), K is the spring constant, a is the acceleration of
the mass, u is the speed of the mass, x is the distance that is covered and B is the friction
dx(t) d 2x(t)
factor. F (t) K Bm . The question here is what is going to be the
dt dt 2
behaviour of the mass due to a sudden force change, assuming again zero initial conditions.
To solve we will follow the previous steps:
d 2x(t) 1 dx(t)
First isolate the highest derivative: 2
(F (t) k B)
dt m dt
7.4 Exercise:
4- Students should read this lab handout carefully, and perform everything using
SIMULINK.
5- They should perform the complete exercise, simulate it and save the model also.
6- They should attach the SIMULINK model and as well as the simulation response
of their practice. Results will be similar to those given in the handout.
02 Is not able to analyse the system. Major help is required in writing the
program and analysing the results.
06 Can analyse and interpret the response of system with minor error help.
1. Start the Matlab engine and type simulink3 at the Matlab command prompt. This
will start the Simulink3 library.
7- Students should read this lab handout carefully, and perform everything using
SIMULINK.
8- They should perform the complete exercise, simulate it and save the model also.
9- They should attach the SIMULINK model and as well as the simulation response
of their practice. Results will be similar to those given in the handout.
a. Derive the steady-state error ess for a ramp reference r(t) = At, t > 0.
b. Determine under what conditions (i.e., values of a and K) the steady-state error is less
than or equal to 22% of the ramp slope A.
02 Is not able to analyse the system. Major help is required in writing the
program and analysing the results.
06 Can analyse and interpret the response of system with minor error help.