Activity No. 1
Activity No. 1
Laboratory Activity #1
MATLAB Basic Functions and Commands for Feedback & Control Systems
I. Learning Objectives:
1. Review MATLAB basic commands and functions
2. Use MATLAB in solving for the Laplace transform of a time – domain function and
the inverse Laplace transform of s – domain function
3. Implement partial – fraction expansion with MATLAB
II. Primer:
MATLAB is a high level technical computing environment for high-performance numeric
computation and analysis. It integrates computation, visualization, and programming in an
easy-to-use environment where problems and solutions are expressed in familiar mathematical
notation. Applications such as modeling, data analysis, simulation, exploration, and visualization
can be done with this tool. For these reasons, MATLAB has been commonly employed in
Feedback Control Systems design. The name MATLAB stands for matrix laboratory. It uses the
functionality and versatility of matrices in order to perform complex computations.
MATLAB Familiarization
To start MATLAB, the MATLAB desktop appears, containing tools (graphical user
interfaces) for managing files, variables, and applications associated with MATLAB. The first
time MATLAB starts, the desktop appears as shown in Figure 1, although the Launch Pad may
contain different entries. The following important entities in the MATLAB user interface are
explained in detail.
• Command Window: This is where to input commands like entering values in variables, or
running scripts (m-files). M-files are scripts that simply execute a series of MATLAB statements,
or they can be functions that also accept arguments and produce output. The prompt >> is an
indicator where to input values, basic expressions and scripts.
• Command History: Lines entered in the Command Window are logged in the Command
History window. In the Command History, previously used functions can be viewed, and copied
and selected lines can be executed. The green text %-- 11:18 AM 4/04/04 --% indicates the
Timestamp in which the command was executed, while the succeeding texts indicate the
commands performed on that particular time.
The second example shows an expression with values stored in variables “a” and “b”.
>> a= 234.56778
a=
2.3457e+002
>> b=3.1416*(cos(3.1416/6))+2
b=
4.7207e+000
In displaying answers and expressions, the % indicates that any typed expression is
converted into a comment while typing a semicolon, ;, after a mathematical expression, omits
the MATLAB response or does not display the result. An example is shown in basic display
manipulation. The first expression does not display the value of a1 but it is still in the workspace
while the second expression is converted into a comment.
>> a1=3.5445/64;
>> % a1=3.5445/64
Using the command fprintf you can directly manipulate the format of the output of an
expression or function. This command displays the result with a desired format on the screen or
to specified filename. The %8.4f, indicates that the output value is a float number that has 4
decimal values and has 8 characters in length, if length of characters is less than 8 (7 in the
example) the 8th would be a space. All expressions inside the single quote sign are the ones to
be displayed. The \n indicates that the next output to be displayed (if there’s any) would be on
the next line. The expression to be evaluated is typed after the comma , sign.
MATLAB has several predefined variables and are listed below. Try the following
variables in the Command Window to check their functionality.
Special Variables and Commands
ans : most recent answer
eps : floating point relative accuracy
i, j : imaginary unit
inf : infinity
NaN : not – a – number
pi :π
There are several commands that perform specific functions. These commands are
listed below. It is recommended to try them to see how it works.
Special Commands
clc : clears the screen
clear “variable” : clears the content and the variable itself
exit : exits MATLAB
help “command” : asks help on a particular command
who : displays variables in workspace
who “variable” : displays the number of elements in a variable
Character String
A sequence of characters in single quotes is called a character string or text variable.
Characters can be augmented or combined by using a vector “[‘first character’,’second
character’]” An example is shown below.
>> c='Manila'
c=
Manila
>> cs=[c,',Philippines']
cs =
Manila,Philippines
>> B = [3; 4; 5]
B=
3
4
5
Combining the single column and single row matrix instructions can create an m x n
matrix. To create a matrix, it is entered in MATLAB by using spaces or commas with semicolons
or carriage returns, as shown below.
>> C = [1 2;3 4]
C=
1 2
3 4
>>
>> E=D'
E=
5 7
6 8
Vectors can be generated by just specifying the first, last and the increment desired for
each element. For example, to create a row vector with a first element of ‘1’ and a last element
of 9’ and an increment of ‘1’, use the syntax in the example below. The default increment is ‘1’
so even without the second parameter ‘1’, >>F = (1:9), still the result is the same. Try to
experiment and change the increment, and see what happens.
Elementary Matrices
eye Identity Matrix
meshgrid X and Y arrays for 3-D plots
ones Ones matrix
zeros Zeros matrix
rand niformly distributed random numbers
randn Normally distributed random numbers
Elementary Math functions and operators in MATLAB are very easy to use since they are
basic and straightforward in nature. Basic operations like addition, subtraction, multiplication,
division can be represented by “ + , - , * , /” respectively. In order to raise a number to a certain
exponent, just insert ^ after the number and before the exponent. For example, to evaluate 26,
just type >>2^6. For matrices or arrays, operations are different (for exponents, multiplication
and division) as stated earlier.
Some MATLAB basic math functions automatically operate element by element on an
array. Functions that operate element by element are given below.
Polynomial Functions
Plots
MATLAB can create high-resolution, publication-quality 2-D, 3-D, linear, log, semilog,
polar, bar chart and contour plots on plotters, dot-matrix printers, and laser printers. Some of
the 2-D graph types are plot, loglog, semilogx, semilogy, polar, and bar. The command grid adds
a grid to the graph, and the command title(‘text’), xlabel(‘text’), ylabel(‘text’), and text(‘text’)
can be used for labeling and placing text on the graph. MATLAB provides automatic scaling. The
function axis([xmin, xmax, ymin, ymax])enforces manual scaling.
As an example, to plot a sinusoidal function, let y = 2sinx, where x is the abscissa (an
angle) and y is the ordinate. Take note that angles are in radians. The listing is shown below.
>> x = (0:1/1000:2*pi);
>> y = 2*sin(x);
>> plot(x,y), title('Sinusoidal waveform'),
Sinusoidal Waveform
2.5
1.5
0.5
-0.5
-1
-1.5
-2
-2.5
0 1 2 3 4 5 6
The functions to be presented are basic and are commonly used in Control Systems
applications. These functions are not used directly for solving Control System problems but
rather application of these commands will greatly help in solving them.
Complex numbers
In MATLAB, imaginary numbers are represented using i or j. Mathematical operations
are straightforward, similar to operations in real numbers.
>> %Addition
>> (25+65j) + (30+80j)
ans =
5.5000e+001 +1.4500e+002i
>> %Division
>> (25+65j)/(30+80j)
ans =
0.8151 - 0.0068i
Some parameters of the complex numbers can be also extracted like the magnitude and
phase angle, real part and imaginary part. , we need to get the magnitude and phase angle of
the complex number (25+j65). We have to convert the angle in degrees so we multiplied 180/
to the answer of angle (always in radians).
>> abs(25+65j)
ans =
69.6419
>> angle(25+65j)*180/pi
ans =
68.9625
>> real(25+65j)
ans =
25
>> imag(25+65j)
ans =
65
The MATLAB is capable of converting polynomial ratio into partial fraction expansion.
Consider:
n n−1
B ( s ) Num b0 s +b 1 s +…+b n
= = n
A ( s ) Den s +a 1 sn−1 +… a n
Num = [b 0, b 1, ….b n] – coefficient of the numerator
Den = [1,a 1,a 2, …… a n] – coefficient of the denominator
Polynomials (Num & Den) Creation from the Partial – Fraction Expansion
The syntax [ Num , Den ] =residue ( r , p , k ) converts a partial fraction expansion back to
polynomial ratio.
>> %Converts the Partial - fraction Expansion Back to the Polynomial Ratio B(s)/A(s)
>> r = [-6 -4 3];
>> p = [-3 -2 -1];
>> k = 2;
>> [Num,Den] = residue(r,p,k);
>> printsys(Num,Den,'s')
num/den =
2 s^3 + 5 s^2 + 3 s + 6
-----------------------
s^3 + 6 s^2 + 11 s + 6
B ( s ) 2 s 3 +5 s 2+ 3 s+ 6
The ploynomial ratio is =
A ( s ) s3 +6 s2 +11 s+6
4. Use MATLAB and the Symbolic Math Toolbox to find the Laplace transform of the following
time functions:
a) f ( t )=2 t 3 e 2 t
b) f ( t )=10 e−3 t cosh ( 5t )
c) f ( t )=9u ( t ) +5 e−3t
d) f ( t )=8 t 2 cos ( et+ 45 ° )
e) f ( t )=3 t e−2t sin ( 4 t+ 60° )
5. Obtain the partial fraction expansion and then use the result to find the inverse Laplace
transform for the following s – domain functions using MATLAB and the Symbolic Math
Toolbox.
10
a ¿ . F ( s )= 2
s ( s +2 )( s+3 )
3 s+1
b ¿ . F ( s )= 2
s +2 s+ 9
10 ( s+7 )
c ¿ . F ( s )=
( s+ 3 )( s+ 6 )
8 ( s+ 1 )( s+3 )
d ¿ . F ( s )=
( s+ 2 )( s+ 4 ) ( s+6 )2
5 ( s+2 )
e ¿. F ( s ) = 2
s ( s +8 s+15 )
7. Use MATLAB to generate the partial fraction expansion of the following function:
104 ( s +5 ) ( s+ 70 )
G ( s )= 2 2
s ( s+ 45 ) ( s +55 ) ( s + 7 s+ 110 ) ( s +6 s+95 )
References:
1. Emmanuel A. Gonzalez, Martin Christian G. Leonor, Feedback Control Systems Engineering
Using MATLAB, gonzaleze@dlsu.edu.ph (2006)
2. Katsuhiko Ogata, MATLAB for Control Engineers, New Jersey, Pearson Education (2008)
3. Norman Nise, Control Systems Engineering, 5th ed., New Jersey, John Wiley & Sons (2008)
4. Richard C. Dorf, Robert Bishop, Modern Control Systems, 8th ed., California, Addison –
Wesley (1998)
Program Codes:
Insert Screenshots of the code used to execute laboratory activity. Make it readable.
Program Response:
Insert Screenshots of the response of each command executed in the laboratory activity.
Final Answer:
Please TYPE the final answer printed by the system. Use appropriate subscripts and superscripts
if needed. If possible, utilize the equation in the insert tab of MS Word.