0% found this document useful (0 votes)
4 views23 pages

Lab 3

The document is a lab manual for the Control Systems Lab at the Institute of Space Technology, focusing on the analysis of system response using MATLAB. It covers MATLAB basics, including script and function creation, vector and matrix operations, and plotting techniques. The manual provides examples and exercises to help students understand how to implement these concepts in MATLAB.

Uploaded by

NIGHT RANGER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Lab 3

The document is a lab manual for the Control Systems Lab at the Institute of Space Technology, focusing on the analysis of system response using MATLAB. It covers MATLAB basics, including script and function creation, vector and matrix operations, and plotting techniques. The manual provides examples and exercises to help students understand how to implement these concepts in MATLAB.

Uploaded by

NIGHT RANGER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

INSTITUTE OF SPACE TECHNOLOGY

DEPARTMENT OF AERONAUTICS & ASTRONAUTICS


405358 Control Systems Lab
Lab Manual 3: Analysis of system response using MATLAB.

Lab Learning Outcomes

❖ MATLAB introduction
❖ Script
❖ function
❖ Vector/Matrix and Matrix operations.
❖ Plot.
❖ Response of first order system to the impulse input.
❖ Response of first order system to the step input.
MATLAB introduction:
Cleve Moler, the chairman of the computer science department at the University of New
Mexico, started developing MATLAB in the late 1970s. It is now also used in education,
in particular the teaching of linear algebra and numerical analysis and is popular amongst
scientists involved in image processing.
MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-
paradigm programming language and numeric computing environment developed
by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data,
implementation of algorithms, creation of user interfaces, and interfacing with programs
written in other languages.
Figure 1 shows the typical MATLAB interface.

1
Create script

Current Editor
Directory

Variables

Command Window
Workspace

Figure 1 MATLAB interface.

Script:
The simplest type of MATLAB program is called a script. A script is a file that contains
multiple sequential lines of MATLAB commands and function calls. You can run a script
by clicking on a Run button in a MATLAB editor tab. Script can be created by
clicking new button in the editor tab. Any sequential code can be written
in script file and we can call functions in script. Actually, all MATLAB
programming can be done in script files. After running the script file, the declared variables
can be seen in workspace. The output can be seen in the command window and the
numerical value of the declared variables can be seen in variables window. The script file
will be saved with .m extension in current directory. We can edit the script file in editor
window.
Example:
Create a script file using the new button in the editor window tab. Save the file with name
myFirstProgram. The file will be saved in the current work directory with name
myFirstprogram.m. Write the following line of code in the script and run it.
Code:
clc;
clear all;

2
close all;
% declare a variable and assigning a value to it
A=22010101

clc; clears the command window. clear all; delete all the variables stored in workspace.
close all; close all the open MATLAB figures. After these commands we have declared
the variable A with the value. When we run the script, the variable A will be stored in
workspace. The value of the variable will be displayed in the command window. The value
of the variable can also be seen in the variables window as shown in the figure 2.

Current
Editor
Directory

Variables

Workspace Command Window

Figure 2 MATLAB Script

Function:
A function is a group of statements that together perform a task. In MATLAB, functions
are defined in separate files. The name of the file and of the function should be the same.
Functions operate on variables within their own workspace, which is also called the local
workspace, separate from the workspace you access at the MATLAB command prompt
which is called the base workspace.
Functions can accept more than one input arguments and may return more than one
output arguments.
Syntax of a function statement is:

3
function [out1, out2, ..., outN] = myfun (in1, in2, in3, ..., inN)

Function Function Function


Keyword
output name inputs

Function file can be created by the dropdown menu of new button in editors tab as shown
in the figure 3.

Function file
creation

Figure 3 MATLAB function file creation.

Example:
The following function named mymax should be written in a file named mymax.m. It takes
five numbers as argument and returns the maximum of the numbers.
Create a function file, named mymax.m and type the following code in it:
function max = mymax(n1, n2, n3, n4, n5)
% This function calculates the maximum of the
% five numbers given as input
max = n1;
if(n2 > max)
max = n2;
end
if(n3 > max)
max = n3;
end
if(n4 > max)
4
max = n4;
end
if(n5 > max)
max = n5;
end

The first line of a function starts with the keyword function. It gives the name of the
function and order of arguments. In our example, the mymax function has five input
arguments and one output argument.
The comment lines that come right after the function statement provide the help text.
These lines are printed when you type the following in command window:
help mymax
You can call the function in script file or command window as:
mymax(34, 78, 89, 23, 11)
After running the script file MATLAB will execute the above statement and return the
following result :
ans = 89

Vector/Matrix and Matrix operations:


A vector is a one-dimensional array of numbers. MATLAB allows creating two types of
vectors:
• Row vectors
• Column vectors
Row Vectors:
Row vectors are created by enclosing the set of elements in square brackets, using space
or comma to delimit the elements. Create a new script and save it with name
rowvectorscript. The file will save in current work directory having name
rowvectorscript.m. Write the following code and run the file:
clc;
close all;
clear all;
% declaring a vector
r = [7 8 9 10 11]

5
MATLAB will execute the above statement and return the following result in command
window:

r=

7 8 9 10 11

Current
Directory Script for row vector

Row Vector

Workspace output

Figure 4 Script for defining row vector.

Column Vectors:
Column vectors are created by enclosing the set of elements in square brackets, using
semicolon to delimit the elements. Create a new script and save it with name
columnvectorscript. The file will save in current work directory having name
columnvectorscript.m. Write the following code and run the file:

clc;
close all;
clear all;
% declaring a column vector
c = [7; 8; 9; 10; 11]

MATLAB will execute the above statement and return the following result:

c=

6
7

10

11

Script for column vector

Current

Directory

Column Vector

Workspace output

Figure 5 Script for defining column vector.

Referencing the Elements of a Vector:


You can reference one or more of the elements of a vector in several ways. The
ith component of a vector v is referred as v(i). For example
% creating a column vector of 6 elements
v = [ 1; 2; 3; 4; 5; 6];
v(3)

7
MATLAB will execute the above statement and return the following result:

ans = 3

When you reference a vector with a colon, such as v(:), all the components of the vector
are listed.

% creating a column vector of 6 elements


v = [ 1; 2; 3; 4; 5; 6];
v(:)

MATLAB will execute the above statement and return the following result:

ans =
1
2
3
4
5
6

MATLAB allows you to select a range of elements from a vector. For example, let us
create a row vector rv of 9 elements, then we will reference the elements 3 to 7 by
writing rv(3:7) and create a new vector named sub_rv.

rv = [1 2 3 4 5 6 7 8 9];
sub_rv = rv(3:7)

MATLAB will execute the above statement and return the following result:

sub_rv =

3 4 5 6 7

Matrix:

A matrix is a two-dimensional array of numbers. In MATLAB, you create a matrix by


entering elements in each row as comma or space delimited numbers and using
semicolons to mark the end of each row. For example, let us create a 4-by-5 matrix a.
Create a new script and save it with name matrixscript. The file will save in current work
directory having name matrixscript.m. Write the following code and run the file:

8
clc;
close all;
clear all;
% declaring a Matrix
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
MATLAB will execute the above statement and return the following result:
a=
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

Script for Matrix

Current Matrix
Directory

output

Workspace

Figure 6 Matrix script

9
Referencing the Elements of a Matrix:

To reference an element in the mth row and nth column, of a matrix mx, we write:

mx(m, n);

For example, to refer to the element in the 2nd row and 5th column, of the matrix a, as
created in the last section, we type:

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(2,5)

MATLAB will execute the above statement and return the following result:

ans = 6

To reference all the elements in the mth column we type A(:,m). Let us create a column
vector v, from the elements of the 4th row of the matrix a:

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
v = a(:,4)

MATLAB will execute the above statement and return the following result:

v=
4
5
6
7

You can also select the elements in the mth through nth columns, for this we write:

a(:,m:n)

Let us create a smaller matrix taking the elements from the second and third columns:

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)

MATLAB will execute the above statement and return the following result:

10
ans =

2 3
3 4
4 5
5 6

In the same way, you can create a sub-matrix taking a sub-part of a matrix:

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)

MATLAB will execute the above statement and return the following result:

ans =
2 3
3 4
4 5
5 6

In the same way, you can create a sub-matrix taking a sub-part of a matrix. For
example, let us create a sub-matrix sa taking the inner subpart of a:

3 4 5
4 5 6

To do this, write:

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
sa = a(2:3,2:4)

MATLAB will execute the above statement and return the following result:

sa =
3 4 5
4 5 6

11
Matrix Operations:
• Addition and Subtraction of Matrices
a = [ 1 2; 2 3];
b = [ 4 5; 3 6];
add=a+b
sub=a-b

• Division of Matrices
a = [ 1 2; 2 3];
b = [ 4 5; 3 6];
div=a/b

• Scalar Operations of Matrices


a = [ 1 2; 2 3];
b = 3
div=b.*a

• Transpose of a Matrix
a = [ 1 2; 2 3];
atran=a’

• Matrix Multiplication
a = [ 1 2; 2 3];
b = [ 4 5; 3 6];
div=a*b

• Determinant of a Matrix
a = [ 1 2; 2 3];
adet=det(a)

• Inverse of a Matrix
a = [ 1 2; 2 3];
ainv=inv(a)

12
Plots:

To plot the graph of a function, you need to take the following steps:

➔ Define x, by specifying the range of values for the variable x, for which the
function is to be plotted
➔ Define the function, y = f(x)
➔ Call the plot command, as plot(x, y)

The following example would demonstrate the concept. Let us plot the simple
function 𝒚 = 𝒙 for the range of values for x from 0 to 100, with an increment of 5.

Create a script file and type the following code:

clc;
clear all;
close all;
x = [0:5:100];
y = x;
plot(x, y)

When you run the file, MATLAB displays the following plot:

Figure 7 Plot function response.

13
Figure 8 Script for generating a plot.

Adding Title, Labels, Grid Lines, Legends and Scaling on the Graph:

MATLAB allows you to add title, labels along the x-axis and y-axis, grid lines and also to
adjust the axes.

• The xlabel and ylabel commands generate labels along x-axis and y-axis.
• The title command allows you to put a title on the graph.
• Two lines can be labeled using legend command.
• The grid on command allows you to put the grid lines on the graph.
• The axis equal command allows generating the plot with the same scale factors
and the spaces on both axes.
• The axis square command generates a square plot.
Example:

Create a script file and type the following code:

14
clc;
clear all;
close all;
x = [0:0.01:10];
y1 = sin(x);
y2 = cos(x);
plot(x, y1,x,y2)
xlabel('x')
ylabel('amplitude')
title('Graph')
legend('Sin(x)', 'Cos(x)')
grid on
axis equal

MATLAB generates the following graph:

Figure 9 Plot options.

15
Figure 10 Plot options script.

Generating Sub-Plots:

When you create an array of plots in the same figure, each of these plots is called a
subplot. The subplot command is used for creating subplots.

Syntax for the command is:

subplot(m, n, p)

where, m and n are the number of rows and columns of the plot array and p specifies
where to put a particular plot. Each plot created with the subplot command can have its
own characteristics. Following example demonstrates the concept:

Example
Let us generate two plots:

16
y = e−1.5xsin(10x)

y = e−2xsin(10x)

Create a script file and type the following code:

clc;
close all;
clear all;
x = [0:0.01:5];
y = exp(-1.5*x).*sin(10*x);
subplot(1,2,1)
plot(x,y
xlabel('x')
ylabel('exp(–1.5x)*sin(10x)')
axis([0 5 -1 1])
y = exp(-2*x).*sin(10*x);
subplot(1,2,2)
plot(x,y)
xlabel('x')
ylabel('exp(–2x)*sin(10x)')
axis([0 5 -1 1])

When you run the file, MATLAB generates the following graph:

Figure 11 subplot.

17
Figure 12 subplot script.

Response of first order system to the impulse input:


Consider a series capacitor-resistor circuit shown in Figure 13. The differential equation
for the system is given as:
1 1
𝑉̇ = 𝑅𝐶 𝑢 − 𝑅𝐶 𝑉 (1)

𝑹 +
+
𝒖 𝑪 𝑽

Figure 13 Electrical system

18
If we apply the impulse input, then 𝑢 = 𝛿(𝑡). Inserting input in equation (1) we will get:
1 1
𝑉̇ = 𝑅𝐶 𝛿(𝑡) − 𝑅𝐶 𝑉 (2)

Using Laplace transforms:


Considering zero initial condition and taking the Laplace transform of a system we will
get:
1 1
𝑠𝑉(𝑠) − 𝑉(0) + 𝑅𝐶 𝑉(𝑠) = 𝑅𝐶 (3)
1 1
𝑠𝑉(𝑠) + 𝑅𝐶 𝑉(𝑠) = 𝑅𝐶 (4)
1
𝑅𝐶
𝑉(𝑠) = 1 (5)
𝑠+
𝑅𝐶

Let the parameter values are 𝑅 = 1, 𝐶 = 1 then we will get:


1
𝑉(𝑠) = 𝑠+1 (6)

Write the following script:


clc;
close all;
clear all;
% Defining the numerator and denumerator of transfer
function
num=[1];
denum=[1 1];
figure()
impulse(num,denum)
title('Change of voltage across capacitor')
xlabel('t(s)')
ylabel('V(v)')

We will get the following output:

19
𝟎. 𝟑𝟔𝟕𝟖

𝟎. 𝟏𝟑𝟓𝟑
𝟎. 𝟎𝟒𝟗 𝟎. 𝟎𝟏𝟖 𝟎. 𝟎𝟎𝟔

Figure 14 Impulse response of system using “impulse function”.

Response of first order system to the step input:


If we apply the step input of magnitude 2𝑣, then 𝑢 = 2. Inserting input in equation (1) we
will get:
1 1
𝑉̇ = 𝑅𝐶 2 − 𝑅𝐶 𝑉 (10)

Doing the first order derivative approximation we will get:


1 1
𝑉(𝑘 + 1) = 𝑉(𝑘) + 𝑇𝑆 (𝑅𝐶 2 − 𝑅𝐶 𝑉(𝑘)) (11)

Taking zero initial condition and writing the following script file:
clc;
close all;
clear all;
% Sampling time
Ts=0.01;
% system parameters
R=1;
C=1;
% Defining simulation time
t=0:0.01:6;
% Defining data storage array

20
V=zeros(1,length(t));
% Defining initial condition
V(1)=0;
for i=1:length(t)-1
V(1,i+1)=V(1,i)+Ts*((1/R*C)*2-(1/R*C)* V(1,i))
end
figure()
plot(t,V,'linewidth',2)
title('Change of voltage across capacitor')
xlabel('t(s)')
ylabel('V(v)')

We will get the following output:

𝟏. 𝟗𝟖 𝟏. 𝟗𝟗
𝟏. 𝟗𝟔
𝟏. 𝟗
𝟏. 𝟕𝟐𝟗

𝟏. 𝟐𝟔𝟒

Figure 15 Step response by solving differential equation with first order derivative approximation.

Using Laplace transforms:


Considering zero initial condition and taking the Laplace transform of a system we will
get:
1 1
𝑠𝑉(𝑠) − 𝑉(0) + 𝑅𝐶 𝑉(𝑠) = 𝑅𝐶 𝑈(𝑠) (12)
1 1
𝑠𝑉(𝑠) + 𝑅𝐶 𝑉(𝑠) = 𝑅𝐶 𝑈(𝑠) (13)

21
1
𝑉(𝑠) 𝑅𝐶
= 1 (14)
𝑈(𝑠) 𝑠+
𝑅𝐶

Inserting the value of 𝑅 = 1, 𝐶 = 1 we will get:


𝑉(𝑠) 1
= (𝑠+1) (15)
𝑈(𝑠)

Write the following script


clc;
close all;
clear all;
% Defining the numerator and denumerator of transfer
function
num=[1];
denum=[1 1];
step(2*num,denum)
title('Change of voltage across capacitor'),
xlabel('t(s)'),ylabel('V(v)')
We will get the following output:

Figure 16 Step response of system using “Step function”.

22
Lab Tasks:
1) Make a function that takes a matrix as input, replace the zero elements of matrix
with the last two digits of your roll number and return the modified matrix. Test your
function by calling it in script file.
2) Make a function that takes a matrix as input and find whether the matrix is positive
definite or not. If the matrix is positive definite then returns its eigenvalues and
determinant.
3) CLO-1 Consider a series capacitor-resistor circuit shown in Figure 17. Let the
capacitance value is 𝐶 = 1 and the resistance value is the last two digits of your
registration number. Find the voltage across the capacitor when the following
source voltage is applied and plot the response. Analyze the response and
discuss/comments on the results.

𝑹=220101011 +
+
𝒖 𝑪 𝑽

Figure 17 Electrical system.

a) Ramp input.
b) Sinusoidal input.
Find the ramp response and response to sinusoidal input using both first order
derivative approximation and Transfer function method.
4) Write a script that plot the step response, impulse response, ramp response, and
sinusoidal response of Electrical System shown in Figure 17 on a single figure
using subplot command.

23

You might also like