LCS Lab1 Introduction of Matlab
LCS Lab1 Introduction of Matlab
PRACTICAL HANDOUTS
TELE – 531 CONTROL SYSTEMS LAB
and polynomials.
Signature : ____________________________________________
MATLAB:
MATLAB is an interactive program for numerical computation and data visualization; it is
extensively used by control system engineers for analysis and design. There are many different
toolboxes available which extend the basic functions of MATLAB into different application areas.
MATLAB (matrix laboratory) is a fourth-generation programming language. Developed by Math
Works, MATLAB allows Matrix manipulations, plotting of functions and data, implementation of
algorithms, creation of user interfaces, and interfacing with programs written in other languages.
VECTORS:
Let's start off by creating something simple, like a vector. Various commands are used in MATLAB
for vector analysis including:
Entering a Vector: Enter each element of the vector (separated by a space) between
brackets, and set it equal to a variable. For example, to create the vector “A”, enter into
the MATLAB command window:
>> A = [1 2 3 4 5 6 9 8 7]
1 2 3 4 5 6 9 8 7
Let's say you want to create a vector with elements between 0 and 20 evenly spaced in
increments of 2 (this is frequently used to create a time vector, start: increment: end):
T=
0 2 4 6 8 10 12 14 16 18 20
>> B = A+2
B=
3 4 5 6 7 8 11 10 9
Now suppose, you would like to add two vectors together. If the two vectors are
the same length, it is easy. Simply add the two as shown below:
>> C = A+B
C=
4 6 8 10 12 14 20 18 16
Subtraction of vectors of the same length works exactly the same way.
C = A-B
Multiplication of vectors of the same length works exactly the same way.
D = A*B
MATRICES:
Entering matrices into MATLAB is the same as entering a vector, except each row of
elements is separated by a semicolon (;):
>> B = [1 2 3 4;5 6 7 8;9 10 11 12]
B=
1 2 3 4
5 6 7 8
9 10 11 12
Use of Semicolon and Parenthesis: Semicolon is used to terminate the corresponding line
operation and shift the cursor to the next line without executing it while ( ) are used to
execute ay function you specified.
Clear and Clc command: Clear command clear all the variables which are used from the
memory location preventing any unpredictable result. Clc command just clear the screen
or the command window.
WHO command: who command is used to display all the variables which are declared in
the command window before using CLEAR command. who lists the variables in the
current workspace
>> who
Your variables are:
A B C D E F G P T X ans t x
Linspace: Linearly spaced vector. It generates a row vector of 100 linearly equally
spaced points between X1 and X2.
logspace(X1, X2)
Identity Matrix: eye Identity matrix. eye(N) is the N-by-N identity matrix.
>> t=0:0.25:7;
>> y=sin(t);
>> plot(t,y)
The plot contains approximately one period of a sine wave. Basic plotting is very easy in
MATLAB, and the plot command has extensive add-on capabilities.
Continuous Time Plots:
Plot command: PLOT command is used to plot continuous time signal defined in time
domain as shown in figure.
plot(X,Y): plots vector Y versus vector X. If X or Y is a matrix, then the vector is
plotted versus the rows or columns of the matrix, whichever line up. If X is a
scalar and Y is a vector, disconnected line objects are created and plotted as
discrete points vertically at X.
plot(Y) plots the columns of Y versus their index. If Y is complex, plot(Y) is
equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary part is
ignored.
Various line types, plot symbols and colors may be obtained with plot(X,Y,S)
where S is a character string made from one element from any or all the
following 3 columns:
Subplot: It is used to display 2 or more graphical function on the same graph with two
different plots as shown in the figure.
subplot(2,1,1)
2 shows number of graphs, 1 shows one is plotted and last one shows out of 2 functions
1st function is plotted first. (Note: Use plot command first)
subplot(2,1,2)
Multiple functions on same plot: to plot 2 or more function simultaneously on one graph use the
following code
Plot (axis1,function1,axis2,function2)
Plot(t,y1,t,y2)
Plot(t,y1,’o’,t,y2,’*’)
Bar charts:
Bar(x)
Stem Command: STEM command to plot discrete time signals. Define n vector rather t.
Modifying Your Graph:
Axis Command: Define x and y axis and give range for x and y axis
Axis ([x1 x2 y1 y2])
X-Label Command: It gives x-axis appropriate label
Xlabel(‘time in seconds’)
Y-Label Command: It gives x-axis appropriate label
Ylabel(‘particular function’)
Title: It gives suitable title to your graph.
Title(‘Electronics’)
Text Command: It adds text on the desired position (row and column) on the graph.
Text (a, b,’Electronics Engineering’)
Text (1.3,2.1,’Electronics Engineering’)
Where a and b are row and column positions
POLYNOMIALS
In MATLAB, a polynomial is represented by a vector. To create a polynomial in MATLAB, simply
enter each coefficient of the polynomial into the vector in descending order. For instance, let's
say you have the following polynomial:
𝒔𝟒 + 𝟏
Would be represented in MATLAB as:
>> y = [1 0 0 0 1]
y=
1 0 0 0 1
You can find the value of a polynomial using the polyval function. For example, to find the value
of the above polynomial at s=2,
>> z = polyval([1 0 0 0 1],2)
z=
17
You can also extract the roots of a polynomial. This is useful when you have a high-order
polynomial such as
𝑺𝟒 + 𝟑𝑺𝟑 − 𝟏𝟓𝑺𝟐 − 𝟐𝑺 + 𝟗
Finding the roots would be as easy as entering the following command;
>> roots([1 3 -15 -2 9])
ans =
-5.5745
2.5836
-0.7951
0.7860
Linspace(1,10)
2. Write the following code in M file or command window to plot sine of 3. Xlabel,ylabel and text
command is also used.
t=0:0.005:10;
q=sin(3);
plot(t,q);
xlabel('sine function');
ylabel('graph');
text(1.2,0.6,'sine function');
3. Write the following code to observe the use of subplot.
t=0:0.01:7;
p=cos(t);
Explore:
Help, Matlab Environment, Square root function (sqrt), hold on and hold off command, exp function and
M-file. (attach their answers in the handouts)
1. Write any program of your choice and use at least 3 commands mentioned above.
2. Find Square root of 3, 4, and 9.
3. Write a matlab code to plot following functions (attach graphs) Using t=1:0.01:10
(i) cos(𝜋𝑡) (ii) sin(𝑡) (iii) cos(𝑡) (iv) cosec(2𝜋𝑡)
4. List few features of MATLAB. Why it is called Integrated development environment?
5. When the graph is plotted, go to its menu bar and explore the following.
(file, edit, view, insert, tools)