Linear Control Systems Lab: E X Per I M E NT NO: 1
Linear Control Systems Lab: E X Per I M E NT NO: 1
EXPERIMENT NO: 1
Batch:
Date of Experiment:
Remarks:
Instructor’s Signature
Experiment No 1:
Introduction to Matlab, LabVIEW and Control System Toolbox
Objectives:
The objective of this session is to learn how to represent polynomials in MATLAB as well as in
LABVIEW, find their roots, create polynomials when roots are known and obtain partial fraction.
Students will also learn in detail about Control System Toolbox.
Polynomial Overview:
A polynomial is an expression containing some variables with some degrees. MATLAB provides
functions for standard polynomial operations, such as polynomial roots, evaluation, and differentiation.
Program:
m=magic(6) % generating a matrix "m" of size 6*6 %
m(4,:) % extracting R4 from the matrix "m" %
Show the results of ‘x’ multiply by ‘y’ and ‘y’ divides by ‘x’.
Program:
x = [0:0.1:1.1]; % declaring vector x %
y = [10:21]; % declaring vector y %
a=x.*y % performing element wise multiplication of vector x and y %
b=y./x % performing element wise division of y by x %
Generate random matrix ‘r’ of size 4 by 5 with number varying between -8 and 9
Program:
% r=randi([Initial limit Final limit],m ,n) %
r=randi([-8 9],4,5) % creating a random matrix of range [-8 9], of the order 4*5 %
Operations on polynomials:
1
Now we will perform multiplication and other operations on polynomial to find roots and will evaluate
the roots to get the polynomial back.
Program:
p=[1 2 1];
q=[1 2];
conv(p,q) % multiplying two polynomials p and q %
Program:
p=[1 2 1];
q=[1 2];
a=roots(p)
b=roots(q)
Program:
clc;
clear all;
close all;
p=[1 2 1]
q=[1 1]
p(-1)
q(6)
Result:
For p(-1) and q(6) , Index exceeds matrix dimensions.
Program:
b=[2 1 0 0];
a=[1 0 1 1];
[r,p,k] = residue(b,a) % calculating partial fraction %
b ( s ) x 3+ 3 x 2 +6 x +7
F ( s) = =
a( s ) 2
x +3 x+ 2
4 x +7
After Long Division we get, x +
(x+ 2)( x +1)
Program:
b=[4 7];
a=[1 3 2];
[r,p,k] = residue(b,a) % calculating partial fraction %
Representing Polynomials
3
MATLAB represents polynomials as row vectors containing coefficients ordered by
descending powers. For example, consider the equation
>>p = [1 0 -2 -5];
Polynomial Roots
The roots function calculates the roots of a polynomial:
>>r = roots(p)
By convention, MATLAB stores roots in column vectors. The function poly returns to t h e
polynomial coefficients:
>>p2 = poly(r)
p2 =
1 8.8818e-16 -2 -5
Polynomial Evaluation
The polyval function evaluates a polynomial at a specified value. To evaluate p at s = 5, use
>>polyval(p,5)
ans =
110
4
Use deconvolution to divide back out of the product:
>>[q,r] = deconv(c,a)
q=
4 5 6
r =
0 0 0 0 0
Polynomial Derivatives
The polyder function computes the derivative of any polynomial. 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.
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
Calculate the derivative of the quotient a/b by calling polyder with two output arguments:
>>[q,d] = polyder(a,b)
q=
-2 -8 -2
d=
4 16 40 48 36
5
Exercise:
x=pi/2:pi/10:2*pi; y=sin(x);
z=cos(x);
This section covers those MATLAB functions that provide conditional program control. if, else, and
elseif. The if statement evaluates a logical expression and executes a group of statements when the
expression is true. The optional elseif and else keywords provide for the execution of alternate groups of
statements. An end keyword, which matches the if, terminates the last group of statements.
The groups of statements are delineated by the four keywords—no braces or brackets are involved as
given below.
It is important to understand how relational operators and if statements work with matrices. When you
6
want to check for equality between two variables, you might use
if A == B, ...
This is valid MATLAB code, and does what you expect when A and B are scalars. But when A and B
are matrices, A == B does not test if they are equal, it tests where they are equal; the result is another
matrix of 0's and 1's showing element-by-element equality. (In fact, if A and B are not the same size,
then A == B is an error.)
The proper way to check for equality between two variables is to use the isequal function:
If isequal (A, B), ...
isequal returns a scalar logical value of 1 (representing true) or 0 (false), instead of a matrix, as the
expression to be evaluated by the if function.
>>isequal(A, B)
ans
=
0
Here is another example to emphasize this point. If A and B are scalars, the following program will
never reach the "unexpected situation". But for most pairs of matrices, including
if A > B
'greater'
elseif A < B
'less'
elseif A ==
B 'equal'
else
error('Unexpected situation')
end
our magic squares with interchanged columns, none of the matrix conditions A > B, A < B, or A == B is
true for all elements and so the else clause is executed:
7
Several functions are helpful for reducing the results of matrix comparisons to scalar conditions for use
with if, including ‘isequal’, ‘isempty’, ‘all’, ‘any’.
otherwise
<statements>;
end
There must always be an end to match the switch. An example is shown below.
n=5
switch rem(n,2) % to find remainder of any number ‘n’
case 0
disp(‘Even Number’) % if remainder is zero
case 1
disp(‘Odd Number’) % if remainder is one
end
Unlike the C language switch statement, MATLAB switch does not fall through. If the first case
statement is true, the other case statements do not execute. So, break statements are not required.
8
for n = 1:4
r(n) = n*n; % square of a number
end
r
The semicolon terminating the inner statement suppresses repeated printing, and the r after the loop
displays the final result.
It is a good idea to indent the loops for readability, especially when they are nested:
for i = 1:m
for j = 1:n
H(i,j) = 1/(i+j);
end
end
Once the Control Design Toolkit is installed, the Control Design palette is available from the Functions
palette. The Control Design palette is shown in the figure below.
9
Below is a list of some of the most useful functions on the Control Design palette.
10
o Draw Transfer Function Equation (for displaying the transfer function nicely on the screen, as
writing it on paper)
o Read Model from File
o Write Model from File
Model Information palette
Model Conversion palette:
o Convert to State-Space Model
o Convert to Transfer Function Model
o Convert Delay with Pade Approximation
o Convert Continuous to Discrete (with various methods, e.g. Euler, Tustin, zero order hold)
o Convert Discrete to Continuous
o Convert Control Design to Simulation (converting models used in Control Design Toolkit for use
in Simulation Module)
o Convert Simulation to Control Design (converting models used in Simulation Module for use in
Control Design Toolkit)
Model Interconnection palette:
o Serial
o Parallel
o Feedback
Time Response palette:
o Step Response (step input)
o Initial Response (response from initial state, with zero input)
Frequency Response palette:
o Bode (calculating frequency response data and plotting the data in a Bode diagram)
o Nyquist
o Nichols
o Gain and Phase Margin
o Bandwidth
Dynamic Characteristics palette:
o Pole-Zero Map
o Damping Ratio and Natural Frequency
Model Reduction palette
Implementation palette (functions here converts the model into a function to be used in a block
diagram, e.g. converting a discrete-time transfer function model into a function that actually
implements the input-output relation that the model represents)
Analytical PID Design (for calculating PID settings so that stability is guaranteed for a process with
a given variation of parameters)
11
The Model Construction palette contains several functions for creating models. The resulting model is
represented as a cluster. This cluster can be used as input argument to other functions, e.g., for
simulation, frequency response analysis, etc.
On the Model Construction palette there are also functions for displaying the transfer function nicely
on the front panel.
Once the model is created, you can simulate it using functions on the Time Response palette, and you
can perform analysis, for example frequency response analysis using functions on the Frequency
Response palette. Note that these simulation and analysis functions can be used on both continuous-time
models and discrete-time models.
The CD Construct Transfer Function Model function (CD means Control Design) (on
the Model Construction palette) defines a transfer function.
The CD Draw Transfer Function (on the Model Construction palette) displays the transfer
function nicely in on the front panel (using a picture indicator which can be created by right-
clicking on the Equation output of the function).
The CD Step Response function (on the Time Response palette) simulated the step response,
assuming the input step has amplitude 1.
The CD Bode function (on the Frequency Response palette) plots the frequency response in a
Bode plot.
12
Note: If the time delay is zero, the Delay input argument of the CD Construct Transfer Function
Model function can be unwired since the default value of the time delay is zero.
Also note: The CD Construct Transfer Function Model function has an input parameter called
Sampling Time. When creating continuous-time models this input must be either unwired (as in above
Example) or wired with value zero. If a non-zero sampling time is connected, a discrete-time transfer
13
function will be created (with the numerator and denominator coefficients as defined in the Numerator
and Denominator arrays).
Figure:
14
4. The function “Polynomial Evaluation” is used to evaluate the value of a polynomial at the given
value of the variable. The polynomial N(s) has the value N (−5) = −66, as shown in following
figure.
Figure:
Comments:
15
Lab Report should include the following:
Create a simple Matlab program using if else condition for any problem.
Create a simple Matlab program using for loop for any problem.
Design a LabVIEW VI to create transfer function of any polynomial. Find roots and examine the
step response and frequency response for that model.
16