mat lab course
TRANSCRIPT
-
7/30/2019 Mat Lab Course
1/60
1
InstructorEng\ Hadeer Sobhy
-
7/30/2019 Mat Lab Course
2/60
2
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
3/60
3
Vectors or Arrays:
How to write vector?
x=[1 5 9 3 5] random elementsy=0:0.1:5 (start :step :end)z=linspace(0,10,11) (start, end, No. of points)
linear spaces relationz=logspace(0,10,11) (start, end, No. of points) 10x
Operation on vector:
y(7) (7th element of the vector y)y(5:9) (5th to the 9th element)z(5:end) (from the 5th element to the end)z(5:length(z)) (from the 5th element to the end)
x(1:2:21) (show elements of x that start with1& count up by 2 to element 21
y(20:1:1) (as above but count down by 1)
y([1 20 3 9]) (Show elements that their places arein vector)
z (z transpose with conjugate if zcomplex)
z.' (z transpose without conjugate)[r,c] = size(x) (r= no. of rows & c= no. of columns)sum(x) (adding all the elements of x)
mean(y) (average of the vector elements)prod(y) (product of vector elements)
length(x) (no. of elements of x)x > 4 produce ans variable has the same
length of x and for each element of x>4 ans=1 otherwise ans=0.
-
7/30/2019 Mat Lab Course
4/60
4
x1 = x>4 x1 is as ans variable of last step wecan use ( > , >= , < , 4) (search for the elements of x greater treturn their positions in variable pos
g = x(position) (return in g values of x that greater
Than 4)find(x>4 & x=1)y = x.^2 (squaring for each element in x)y = x^2 (error where x is vector)
Plotting two vectors (steps to plot any function)
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
5/60
5
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
6/60
6
Matrices
How to write matrix?
a=[2 4 6;0 3 8;-1 -6 11] (defines a as 3x3 matrix)
z=zeros(3,4) (z is 3x4 zero matrix)
h=ones(2,5) (h is 2x5 ones matrix)
e=eye(4) (e is 4x4 identity matrix)
r=rand(4) r=rand(4)
m=magic(4) m=magic(4)
Operation on matrices
a(2,3) (display element of a matrix whoseposition is row 2 & column 3)
a(2,3)=7 (set the element of the 2nd row and3rd column to be equal to 7)
s = a(2,1:3) show 2nd row from element no. 1 toelement no. 3 by step 1
a(2,7) = 2 note that a matrix is 3*3 but thisstatement rebuild a matrix withdimension 3*7 and put a(2,7)=2
x = [10 20 30] start with vectors to next operationsy = [5 6 7]
d = [x;y]
a. ( a transpose)
a ( a conjugate transpose)
inv(a) (inverse of a)
det(a) (determinate of a)
[r,c]=size(a) (r no. of rows & c no. Of columns)
sum(a) (sum of each column)
prod(a) (product of each column)
b=a^2 (a square)
c=a.^2 (square of each element)
a*b (square of each element)
b*a
-
7/30/2019 Mat Lab Course
7/60
7
a*b b*a (are they equal)
a.*b b.*a (are they equal)
a/b (a*inv(b))
a\b (inv(a)*b)
sort(a) If a is vector then sorting from minto max. But if a is matrix thensorting each column
[q w]=sort(a) q is new sorted vectorw is position of elements
[q w]=max(a) q is row contain max elements ineach column w is position
d=diag(a) Diagonal of a
fliplr(a) Flip left right
flipud(a) Flip up down Flip up down
Important Applications
Solving linear equations Ax=b:1.To solve (square system):
2x+yz = 6 , xyz =3 , x+2y3z =9
Program:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
8/60
8
2.To solve Overdeterminant system Ax=b:
(Normally used for curve fitting)
Example 1
Fit the following data to a straight line y=mx+c,Show answer graphically
x = [1 2 3 4 5 6] , y= [.89 2 2.98 3.75 5.1 5.8]
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
9/60
9
Example 3(task)
A spring is a mechanical element which, for the simplest model, is
characterized by a linear force deformation relationship
F = kx
F is being the force loading the spring, k the spring constant and x the spring
deformation. In reality the linear forcedeformation relationship is only an
approximation, valid for small forces and deformations. A more accurate
relationship, valid for larger deformations, is obtained if nonlinear terms
are taken into account. Suppose a spring model with a quadratic relationship
F = k1*x + k2*x^2is to be used and that the model parameters, k1 and k2, are to be determined
from experimental data. Five independent measurements of the force and the
corresponding spring deformations are measured and these are presented in
next table:
F (N) = [5 50 500 1000 2000]X (cm) = [.001 .011 .013 .3 .75]
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
10/60
10
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
11/60
11
Flow controlFOR LOOPS:1-for n=1:.1:10;
end
2-for n=[1 9 2 -2]
% take element by element
..
..
end
PROGRAMS
1-
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
12/60
12
----------------------------------------------------------
2-
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
13/60
13
WHILE LOOPS:while (condition)
% condition: x=2, x==2, x~=2
..
..
end
IF statement:if (x>2)
elseif x
-
7/30/2019 Mat Lab Course
14/60
14
PROGRAMS
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
15/60
15
TRY CATCH STATEMENTStry
catch
end
Explain:
If there is error in try statements go to catch statements
PROGRAMS
SOME IMPORTANT COMMANDScontinue :(break loop & go to enter it with next value)
break :(break loop & go out &continue flow of program after loop
Statements
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
16/60
-
7/30/2019 Mat Lab Course
17/60
17
GETCO FUNCTION
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
18/60
18
PROGRAMSWrite program that give figure to user & give the program 3 points
then program plot curve that pass with them from equation
y=a*x2+b*x+c
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
19/60
19
Continue on the example ask user about enter 3 point on figure or
algebraic
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
20/60
20
THE GUIDE TOOL.Matlab has a program called guide which allows users to setup their GUI. The
guide tool is very intuitive to use and can be accessed from the command line by
simply typing in the following.
>> guide
STEP 1: In this step we will setup our GUI.Start up Matlab6 and type in the following.
>> guide
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
21/60
21
STEP 2: Set up the GUI in the following manner.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
22/60
22
STEP 3: Right Click on the Push Button and a pop up menu should appear.From that menu pick the Inspect Properties option.
String -------- Change from Push Button to Plot Function.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
23/60
23
STEP 4: Right click on the lowest Static Text object and select Inspectproperties.
String ------- Change property from Static Text to Sin(x) .
Tag ---------- Change property from text3 to sin_func .
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
24/60
24
STEP 5: Right click on the other Static Text object and select Inspect properties.
String -------- Change from Static Text to cos(x)
Tag ----------- Change from text1 to COS_FUN
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
25/60
25
STEP 6: Right click on the third Static Text object and select Inspectproperties.
Font Size --------- Change from 8.0 to 15.
String ------------- Change from Edit Text to 1.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
26/60
26
STEP 7: At this point your GUI should look like this.
If you have made a mistake in the GUI you can easily correct it at this point without
moving on. For instance, if you look closely in this GUI there is really no way to
select which of the two (sin(x) or cos(x) are to be plotted. We should have used a
different object in the first place. perhaps a check box. We can easily delete the
wrong object and replace it with the one we want. Since this is our first GUI we will
keep it simple and get rid on one of the static text boxes.
Click on the Cos(x) box and hit thedelete key and the cos(x) should disappear from
the GUI. Once that is done, save as mygui.
As soon as you save it Matlab should generate the skeleton source code for you and
the source code should automatically open in an editor.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
27/60
27
Step 8: Activating the buttons.Add the following code to the skeletal code.
STEP 9: Running the program.To run the program, simply go to the Matlab main window and call your program.
>> mygui
EngEngEngEng \\\\ HadeerHadeerHadeerHadeer SobhySobhySobhySobhy
-
7/30/2019 Mat Lab Course
28/60
28
GUI PROGRAME
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
29/60
29
F(t) F(s)
Delta(t) 1
U(t) 1/s
Exp(-at) 1/(s+a)
T 1/s^2
T^n n!/(s^(n+1))
T*exp(-at) 1/(s+a)^2
(t^n)*(exp-at)/(n!) 1/(s+a)^(n+1)
Sin bt b/(S^2+b^2)
Cos bt S/(S^2+b^2)
Exp(-at)*Sinbt b/(S^2+a^2)+b^2
Exp(-at)*Cosbt (S+a)/(S^2+a^2)+b^2
AF(t) AF(s)
F1(t)+F2(t) F1(s)+F2(s)
F(t-to)*u(t-to) (exp(-t*s))*F(s)
t*f(t) -dF(s)/dS
Integrate(f(t)*dt) F(S)/S
df/dt S*f(s)
Second_Dif(df/dt) S^2*F(S)
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
30/60
30
Laplace transform on MATLAB
Syntax:
Examples:
Inverse laplace transform on MATLABSyntax
Examples:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
31/60
31
Ex: the following equation
The Laplace Transform:
System Identification
1- Transfer function
2- System zeros, poles, and gain
3- Feedback systems
4-Block Diagrams5-Transformations
System Response
1- The step response2-The impulse response3-Arbitrary input response
System characteristics
1- Damping
2- Root locus
Frequency analysis
1- Bode plots
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
32/60
32
o o
i i
V Z
V Z=
System Identification1-Transfer function
The transfer function is the direct relationship between system output and its input
regardless of the internal components of the system. Every system that has an input
and output may be considered a system that has a transfer function. For example,
the electronic buffer is a system that has an input voltage Vi and output voltage Vo
such that the transfer function of the system is: 1o
i
V
V=
The operational amplifier in the shown configuration represents a system with a
transfer function:
Note that Zo orZi may be a capacitor, coil, resistor, or combination of them, i.e. it
will be a function of S ( S-domain).
So, electric and electronic components and basic circuits may be considered as
systems and can have a transfer function.
In our course, we concern with control systems that have their transfer function in
the S-domain using Laplace transform. In order to deal with MATLAB to analyze
any system and control it we should first define this system to MATLAB by any
method of system identification methods (will be mentioned one by one), in this partof the lab. We will focus on defining the system using its transfer function (it will be
your task to get the transfer function in the S-domain using lectures and tutorials.)
then use the given transfer function to identify the system to MATLAB.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
33/60
33
So, lets see an example:
Assume that the transfer function of a system is given as follows and you are
required to define this system to MATLAB:
To define any system, we should give it a name and then specify its T.F. numerator
and denominator coefficients.
First, let the system name besys, then lets identify the denominator coefficients:
Take a look at the polynomial in the denominator of the T.F. and define it as we
learned in theIntroduction to MATLAB chapter.
Lets name the denominator polynomialdenom and the numerator polynomialnum
then define these polynomials to MATLAB as follows:
>> num=[1];
>>denom=[1 5 6];
Then define the systemsys through its componentsnum &denom as follows:
>>sys=tf(num,denom)
Which means that the instruction tf is the one that is used for defining systems
through their transfer function (note that its an abbreviation of Transfer Function).
The format of this instruction is as follows:
Sys_name = tf (T.F._numerator_polynomial , T.F._denominator_polynomial)
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
34/60
34
Another example:
Define the following system to MATLAB using its shown transfer function:
The used code will be:
>> a=[2 3];
>>b=[1 3 2 5];
>>system=tf(a,b)
You can get the same results using the following code in which we defines as a
symbol in the transfer function of system:
>> s=tf(s);
>> system= (2*s+3)/(s^3+3*s^2+2*s+5)
2-System zeros, poles, and gain
Another method of defining systems is through knowing their zeros (roots of the
numerator of the transfer function of this system), poles (roots of the denominator
of the transfer function of this system), and gain (over all constant gain of the
transfer function).
So, lets have an example, to learn how to use this method.
Example:
A system is defined by the shown transfer function:
And you are required to define this system to MATLAB.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
35/60
35
Note that the system zeros are got by: (S+2)=0 S=-2 , also system poles can be got
using the same manner to be: S = 0, -1, -3. The system gain is of course 3.
Thus we can define this system as follows:
>>zeros=[-2];
>>poles=[0 -1 -3];
>>gain=3;
>>system=zpk(zeros,poles,gain)
Note that you can define this system in only one step as follows:
>>system=zpk([-2],[0 -1 -3],3)
Also, we can define this system directly using the transfer function method as
follows:
>>system=tf([3 6],[1 4 3 0])
This means that the format of the zpk function is as follows:
Sys_name = zpk (System_zeros , System_poles, overall_gain)
Note that the zeros, poles, and gain of the system may be given without the transfer
function, then you are required to define the system. Of course we are desired only
in getting the zeros and the poles of the system without any care for how we got
them.
Another example:
Define the system given by the shown T.F. to MATLAB:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
36/60
36
The code will be:
>>a=[ ];
>>b=[-1 -4];
>>k=1;
>>sys=zpk(a,b,k)
Or directly in one step:
>>sys=zpk([ ],[-1 -4],1)
Note that this system doesnt have any zeros as the polynomial of the numerator of
the transfer function cant equal zero any way. So, we define the system zeros to be
an empty matrix as described.
3-Feedback systems
In this part, we will only care for using the previously discussed methods in
interconnecting system components such as feedback systems to define the overall
system to MATLAB.
So, lets consider the simple feedback system shown:
The feed forward T.F. G(s) and the feed back transfer function H(s) are simply
systems that can be defined to MATLAB either by transfer function method or
zeros-poles-gain method. Then we can from a feedback from these systems to get the
overall system definition.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
37/60
37
Example:
A closed loop system has a feed forward transfer function and
a feedback transfer function, you are required to get the overall transfer function of
the feedback system.
Solution:
G(s) and H(s) should be defined separately as if isolated systems, then they should
be combined to get the required feedback system using the following code:
>> G=zpk([ ],[-1],[2]);
>> H=zpk([ ],[0],[3]);
>> system=feedback(G,H)
Using the tf instruction we can build a similar code as follows:
>> G=tf([2],[1 1]);
>> H=tf([3],[1 0]);
>> system=feedback(G,H)
What about positive feedback?
The feedback instruction assumes negative feedback connection but if its required
to build a positive feedback system, an additional input argument is added as will be
illustrated in the following example:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
38/60
38
Example:
Get the transfer function of the system SYS whose feed forward transfer function is
(2/S) and feed back transfer function is 0.25 knowing that SYS is a positive feedback
system.
Solution:
>> G=tf([2],[1 0]);
>> H=tf([1],[4]);
>> SYS=feedback(G,H,+1)
Now, in general the feedback instruction has the following syntax:
Sys_name=feedback(Feedforward_transfer_function,Feedback_transfer_function,1)
Note that the +1 is applied only for positive feedback systems while -1 is applied for
negative feedback systems (the -1 may be canceled as it is the default value for
MATLAB).
4-Block diagrams
Sometimes systems may be defined using their block diagram. So, how to define a
system in block diagram form for MATLAB? And how to get its transfer function
without involving in block-diagrams reduction rules?
Lets analyze this method using an example:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
39/60
39
Example:
Define the following system to MATLAB and hence show how to get its transfer
function.
You can use the rules of reduction of block diagrams to simplify this system and get
the transfer function of this system Y(s)/U(s).
Step (1)
Define each block as a separate sub-system and assume that the sub-systems are:
Now, define each sub-system to MATLAB:
>>sys1=tf([3],[1 2]);
>>sys2=tf([1],[1 3]);
>>sys3=tf([2],[1 1]);
>>sys4=tf([3],[1]);
>>sys5=tf([1],[1 0]);
>>sys6=tf([2],[1]);
5-Transformations
In this section, the transformation from any representation method to any other one
and getting the data of any representation will be covered. First, getting the
information of any representation should be covered as follows:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
40/60
40
Assume that a certain system SYS is defined for MATLAB in transfer function form
and you are required to get the parameters of this representation which are the
polynomial of the numerator and the polynomial of the denominator of the transfer
function corresponding to this system. In this case, you may use the tfdata
instruction.
Example:
The transfer function of a certain system is given by:
And its defined for MATLAB using the TF form with the system name SYS, so its
required to get the parameters of the TF representation using MATLAB.
Solution:
>> [num,denom]=tfdata(SYS)
Now,num contains the polynomial of the numerator of the transfer function of SYS,
which is [2 3] while the polynomial of the denominator is indenom [1 5 6].
If the same system is defined for MATLAB using the ZPK method and you are
required to get the poles, zeros, and gain of the system; it will be suitable to use the
zpk data instruction as follows:
>> [z,p,k]=zpkdata(SYS)
Now, Z contains the zeros of the system, P contains the poles of the system, and K
contains the overall gain of the system. Z=[-1.5] , P=[-2 -3], K=[2].
[numerator,denomenator]=tfdata(System_name_defined_in_tf_format)
[zeros, poles, gain]=zpkdata(System_name_defined_in_zpk_format)
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
41/60
41
Transformation:
Suppose that a system is defined using append/connect instruction or zpk
instruction and you would like to get this system in transfer function form. In this
case, you may use the tf instruction in the shown syntax.
System_representation_in_TF_form = tf (System_representation_ in_any_form )))
Example:
A system is defined for MATLAB in the zpk form with name SYSTEM and its
required to get the system representation in transfer function form.
Solution:
>> SYSTEM=tf(SYSTEM)
Now, assume that the system is defined in the tf from and its required to get the
system representation in the zpk form and hence get the zeros, poles, and gain of the
system.
System_representation_in_ZPK_form = zpk (System_representation_ in_any_form)
>> SYSTEM=zpk(SYSTEM);
>>[Z,P,K]=zpkdata(SYSTEM)
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
42/60
42
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
43/60
43
System Response
1-The step response
The response of a system is the shape and characteristics of the output of thissystemfor a certain input.In the analysis of systems, some basic test input signals are used
such as the unit step, theimpulse, and the ramp function.
In this section, the response of the system for a step input will be taken into
consideration while the response for different inputs will be discussed later.
To get the step response of a certain system using MATLAB, the system should first
be defined using either block diagrams, transfer functions, or zeros-poles-gain, then
use the following function syntax.
step (System_name , time_duration_of_analysis )
In this case, the response of the system will be displayed in the time interval
specified by the second input argument of the step function.
Example:
A system has the following transfer function:
and you are required to get the step response of this system and hence get the value
of therise time,peak time,settling time,system overshoot, and thesteady state error.
Solution:
A suitable period of analysis is about 15 seconds as an estimation (or by trial), so the
time period is set to be 15 seconds with step size of 0.01 second. The code will be as
follows.
>> t=0:0.01:15;
>> sys=tf([1 3],[1 1 4]);
>> step(sys,t);
-
7/30/2019 Mat Lab Course
44/60
44
The output will be displayed as in the following window:
On the previously shown window, right click your mouse and then activate the
characteristics as shown:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
45/60
45
Its now clear that the transient and steady state characteristics can be obtained
from the figure or the window in which the step response is displayed using the right
mouse key and then choosing characteristics to select between any one of the
characteristics.
2-The impulse response
In the same manner, the response of the system to a unit impulse is called the
impulse response which can be got using the following function syntax.
impulse (System_name , time_duration_of_analysis )
The output will also be displayed in a window that allows right clicking to get the
characteristics of the response.
Example:
For the previously discussed example, its required to get the impulse response of
this system and hence find the peak response and the settling time.
Solution:
>> t=0:0.01:15;
>> sys=tf([1 3],[1 1 4]);
>> impulse(sys,t);
The output will be displayed as shown:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
46/60
46
3-Arbitrary input response
Sometimes, there may be a need to check the system response if the input is a
sinusoidal input or in general an arbitrary input signal. In this case, you may use the
following function syntax.
lllsssiiimmm(((SSSyyysssttteeemmm___nnnaaammmeee,,,IIInnnpppuuuttt___fffuuunnnccctttiiiooonnn,,,tttiiimmmeee___ddduuurrraaatttiiiooonnn___ooofff___aaannnaaalllyyysssiiisss)))
where the input function is a function of the time of analysis which means that it is a
vector of the same length as the time vector.
Example:
A system is defined by the following transfer function:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
47/60
47
And its required to get the response of the system for an input signal u(t), where:
a) U(t) is a unit ramp function, where U(t)=t.
b) U(t) is a cosine function, where U(t)=cos(t).
Solution:
In this case, the time period of analysis should be specified first then the input
function is defined and finally the LINEAR SIMULATION function should be
applied on the defined system as follows.
>> sys=tf([2],[1 2 6]);
>> t=0:0.01:10;
>> u1=t;
>> u2=cos(t);
>> figure(1);
>> lsim(sys,u1,t);
>> grid;
>> figure(2);
>> lsim(sys,u2,t);
>> grid;
In this code, u1 is a ramp function as it increases with time in the same rate as the
time vector t, while u2 is a cosine function. Both signals are applied to the same
system in a time interval specified by t and the output of each input signal is
displayed in a separate figure using the FIGURE instruction. The figures will look
like the following ones.
Note that the linear simulation function can also display the peak response of the
system output using the right mouse key and then the characteristics option. The
plots displayed in each figure represent the input signal (in gray) and the output (in
blue).
Now, its clear that MATLAB is helpful for getting the response of the system for
any input function form.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
48/60
48
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
49/60
49
System characteristics
1-Damping
MATLAB can help you in analyzing systems by getting their damping ratio _ and
natural frequency n for further analysis and calculations of the transient response
parameters.
To do so, you can use the following syntax.
[w,z]=damp(System_name)
The output argument w is a vertical vector containing the natural frequency of each
pole in the system, while z is also a vertical vector containing the damping ratio of
each pole in the system.
Example:
A system is defined by the following transfer function:
Its required to get the settling time of this system.
Solution:
The settling time of a system is calculated by the formula:
so its now required to get the damping ratio zeta and the natural frequency n of
the system. The code will be:
>> sys=tf([1 2],[1 1 4]);
>> [w,z]=damp(sys);
>> ts=4./(w.*z)
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
50/60
50
Note that the code uses the dot operator because w and z are both vectors and its
required to deal with their adjacent components. Why?
You can answer this by typing the following instruction and noticing the output.
>> damp(sys)
The output is as follows:
Which means that the first element in the damping vector is related to the first one
in the frequency vector and so on.
2-Root locus
Its a method of analyzing systems and compensation design using the possible root
locus. It simplifies the method of gain adjustment to get a desired pole location. To
plot the root locus of a system, the systems feed forward transfer function should be
first defined then use the following syntax.
rlocus(System_feed_forward_transfer_function)
Example:
Given the feed forward transfer function of a certain system:
You are required to plot the root locus of this system, hence find the value of the
system gain such that the poles of the system lie at: 0, -0.244 0.831i.
solution:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
51/60
51
On the MATLAB command window,
type:
>> G=tf([1],[1 1 1 0]);
>> rlocus(G)
The shown window will appear.
The plot specifies the locations of
the poles and their possible loci.
For better identification for the system
the values of zeta andn should be
displayed. To do so, you need to
right click on the shown window and
select Grid from the pop-up menu.
The result is shown in the figure at
the beginning of the next page.
Now, constant _ lines appear,
also constant n lines appear.
Thus full identification of the
system is possible.
Currently, the poles of the
system lie at: 0, -0.5 0.866i.
In order to locate the poles of
the system in another location,
simple gain adjustment should
be made.
To do so using MATLAB, just click any point on the plots of the locus then drag it
to check different gains and pole locations as shown in the next figure. As shown, its
now possible to change the system gain to get another location for the poles.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
52/60
52
From this figure, the suitable
value of the gain is: 0.384.
Note that the question may be
formed in many other forms
such as asking about the value
of the gain that ensure certain
damping ration, percent
overshoot, or frequency.
All you have to do, is to drag
the cursor on the plots and get
that gain to ensure a certain
property for the system.
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
53/60
53
PROGRAMS
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
54/60
54
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
55/60
55
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
56/60
56
Frequency analysis
Bode plots
MATLAB can help getting the frequency response of any system using bode plots.First, you should define the feed forward transfer function of the system. Then, use
the following syntax.
bode(System_feed_forward_transfer_function)
Example:
Given the feed forward transfer function of a certain system:
You are required to plot the bode plots of this system.
Solution:
On the MATLAB command window, type:
>> G=tf([1],[1 1 1 0]);
>> bode(G)
You will get the shown figure after right clicking the figure window and choosing
Grid.
-
7/30/2019 Mat Lab Course
57/60
57
Bode plot Appendix
Bode Plot:- Show the frequency response of a system (Laplace Transform).
- s = j
Poles:General Form:
Zeros:General Form:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
58/60
58
Special Case:
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
59/60
59
Example 1: Sketch the gain and phase vs frequency of the following transfer
function
There are two poles and two zeros:
Poles: 10, 2000
Zeros: 0, 500000
EngEngEngEng \\\\ Hadeer SobhyHadeer SobhyHadeer SobhyHadeer Sobhy
-
7/30/2019 Mat Lab Course
60/60