[go: up one dir, main page]

0% found this document useful (0 votes)
75 views17 pages

Linear Control Systems Lab: E X Per I M E NT NO: 1

This document summarizes an experiment on learning MATLAB, LabVIEW and the Control System Toolbox. The objectives are to represent and operate on polynomials in MATLAB and LabVIEW, find polynomial roots, create polynomials from roots, and perform partial fraction expansion. MATLAB commands for polynomial operations like multiplication, division, differentiation, and representation are demonstrated. Conditional control statements like if-else and switch-case are also introduced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views17 pages

Linear Control Systems Lab: E X Per I M E NT NO: 1

This document summarizes an experiment on learning MATLAB, LabVIEW and the Control System Toolbox. The objectives are to represent and operate on polynomials in MATLAB and LabVIEW, find polynomial roots, create polynomials from roots, and perform partial fraction expansion. MATLAB commands for polynomial operations like multiplication, division, differentiation, and representation are demonstrated. Conditional control statements like if-else and switch-case are also introduced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Linear Control Systems Lab

EXPERIMENT NO: 1

Name and Roll No:

Batch:

Date of Experiment:

Obtained Marks (Rubric Based): P1: P2: P3:

Total Marks: P1: 5 P2: 5 P3: 5 = 15

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.

MATLAB Review and Polynomial Operations:


Always write the following three lines before initiating any code in MATLAB:
clc;
clear all;
close all;

Extract specified row of a matrix generated by “magic()” command.

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’.

Given x = [0:0.1:1.1] and y = [10:21]

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.

Given p(s )=s^2 + 2s + 1 and q(s ) = s+2 .


Using MATLAB compute p(s)*q(s)

Program:
p=[1 2 1];
q=[1 2];
conv(p,q) % multiplying two polynomials p and q %

Compute Roots of p(s) and q(s)

Program:
p=[1 2 1];
q=[1 2];
a=roots(p)
b=roots(q)

p(-1 ) and q(6 )

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.

Partial Fraction Expansion


‘residue’ finds the partial fraction expansion of the ratio of two polynomials. This is particularly useful
2
applications that represent systems in transfer function form.

Use MATLAB command to find the partial fraction of the following:

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 %

Polynomial Function Summary


Function Description
Conv Multiply polynomials
Deconv Divide polynomials
Poly Polynomial with specified roots
Polyder Polynomial derivative
Polyfit Polynomial curve fitting
Polyval Polynomial evaluation
Polyvalm Matrix polynomial evaluation
Residue Partial-fraction expansion (residues)
Roots Find polynomial roots

Representing Polynomials

3
MATLAB represents polynomials as row vectors containing coefficients ordered by
descending powers. For example, consider the equation

To enter this polynomial into MATLAB, we write

>>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

poly and roots are inverse functions

Polynomial Evaluation
The polyval function evaluates a polynomial at a specified value. 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.
The functions conv and deconv implement these operations. Consider the polynomials
and . To compute their product,
>>a = [1 2 3]; b = [4 5 6];
>>c = conv(a,b)
c=
4 13 28 27 18

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

q/d is the result of the operation.

5
Exercise:

Use MATLAB commands to get exactly as the figure shown below

x=pi/2:pi/10:2*pi; y=sin(x);
z=cos(x);

Flow Control in MATLAB:


Conditional Control – if, else, switch

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.

Using the A and B matrices from above, you get

>>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’.

Switch and Case:


The switch statement executes groups of statements based on the value of a variable or expression. The
keywords case and otherwise delineate the groups. Only the first matching case is executed. The syntax
is as follows
switch <condition or expression>
case <condition>
<statements>;

case <condition>
<statements>;

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.

For, while, break and continue:


This section covers those MATLAB functions that provide control over program loops.
for:
The ‘for’ loop, is used to repeat a group of statements for a fixed, predetermined number of times. A
matching ‘end’ delineates the statements. The syntax is as follows:

for <index> = <starting number>:<step or increment>:<ending number>


<statements>;
end

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

Control System Toolbox:


Contents of Control Design Palette:

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.

 Model Construction palette:


o Construct State-Space Model
o Construct Transfer Function Model
o Construct Special Model:
 First order with (or without) time delay
 Second order with (or without) time delay

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)

Creating s-transfer functions. Simulation. Frequency Response

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.

Example 3.1.1: Creating an s-transfer function. Simulation. Frequency response

The VI shown below implements the following:

 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). 

LABVIEW-Finding roots for polynomials and vice versa:


To find the roots from polynomials and vice versa, we will define the following VI’s in the block
diagram of LabView.
1. p is a row vector containing the coefficients of P(s) in ascending order.
“Polynomial Roots (p)” is a row vector containing the roots of the polynomial.
2. Conversely, r is a row vector containing the roots of the polynomial.
“Create Polynomial from Roots(r)” is a row vector with the polynomial coefficients in
ascending order.
We can compute the roots of the polynomial P(s) = x3 – 5x2 + 2x + 8 with the “Polynomial Roots
function”.
In following figure, we show how to reassemble the polynomial with the “Create Polynomial” from
Roots function.

Figure:

LABVIEW-Multiplying two polynomials and polynomial evaluation:


To multiply two polynomials and perform their evaluation, we will define the following VI’s in the
block diagram of Lab view.
1. Multiplication of polynomials is accomplished with the “Multiply Polynomials.vi” function.
2. Suppose we want to expand the polynomial N(s), where N(s) = (3s2 + 2s + 1)(s + 4).
3. This can be done using associated Lab view commands “Multiply Polynomials.vi” function as
shown in figure below. Thus, the expanded polynomial is: N(s) = 3s3 + 14s2 + 9s + 4.

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

You might also like