Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

SHUHABUDDIN KHOSO

MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY JAMSHORO


Batch 10ES (3rd year 1st Term)

PRACTICAL HANDOUTS

FEEDBACK CONTROL SYSTEMS

Lab-01
Introduction to MATLAB Environment and Basics

Name: SHUHAB_UD_DIN KHOSO

Roll No: 2K10ES07

Date: ---------------------------------------------------------------

Score: ---------------------------------------------------------------

Signature:-----------------------

Page # 01 10ES07
SHUHABUDDIN KHOSO

Lab Outline:
 To be familiar with the Basics of MATLAB
 Use of MATLAB in Control Systems
 Basic Commands used for vector Analysis
 Basic Plotting techniques
 Examples

Introduction to MATLAB:
MATLAB stands for Matrix Laboratory. It is a powerful
programming language which is used for symbolic and numerical computations effectively. It is
a High level programming language. It is an interactive, matrix-based computer program for
scientific and engineering numeric computation and visualization.

MATLAB will be used extensively in the labs of Signals and


Systems, Digital Signal Processing, Control Engineering and applications. The aim of Matlab is
to enable us to solve complex numerical problems, without writing programs in traditional
languages like C and FORTRAN. Thus, Matlab interprets commands like Basic does, instead of
compiling source code like C and FORTRAN require.
Visit www.mathsworks.com for Assistance.

Use of MATLAB in Control Engineering:


It’s a very powerful tool used in Control System Designing.
User can develop its own applications, can find the desired Transfer Function , Simulate and
analyse the result, Check out various possibilities for multiple inputs, can work in frequency and
time domain, develop your own block diagrams etc. Simulink library gives a huge range of
defined blocks for creating applications without writing codes.

Basic Commands used for vector Analysis and Other functions:


Various command and used in MATLAB for vector analysis
including:
 Entering a Vector: Any vector can be entered in the command window containing
certain values. A=[1234]
B =[ 0 1 3 4 ];
 Operations performed on vectors: Various operations can be performed on the
command window including Addition, multiplication, division etc
B=A+A , C=A-B , D=A*B
 Point Operator: Point operator is used for element by element calculations.
C=A .*A…….. it will multiply corresponding elements of A with A
D=A ./B……….. it will divide corresponding elements of A with A
(Make sure order of matrix should be same)
 Transpose: Transpose can be found using compliment operator or any matrix can be
converted to column matrix by use of semicolon.
B=A’
C= [1;2;3;4]
Both will give same results

Page # 02 10ES07
SHUHABUDDIN KHOSO

 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.
 Trigonometric functions: You have variety of trigonometric functions on MATLAB
incuding sine, cosine, tangent, inverse and hyperbolic of these function, logarithmic etc
x = 20;
cos(x)……Cosine of 20 asin(x)…..inverse of sine
sin(x)…… sine of 20 acos(x)….inverse of cosine
csc(x)..……cosecant of 20 sinh(x)…..hyperbolic of sine
 Zeros and Ones:
Zeros….. will yield 1 by 1 matrix containing all elements zeros
Ones….. will yield 1 by 1 matrix containing all elements ones
ones(2)..will yield 2 by 2 matrix containing all elements one
Zeros….. will yield 2 by 2 matrix containing all elements zeros
 WHO command: who command is used to display all the variables which are declared
in the command window before using CLEAR command.
Who…… press enter
 Inverse of a Matrix: Inverse can be calculated using inv command
A=[1 3;1 2];…….(Matrix should be square Matrix)
Inv(A)
 Linspace:.Linear spaced generates a row vector of 100 linearly
equally spaced points between X1 and X2.
Linspace(1,5)
 Logspace: It generates again certain vectors which are logarithmically spaced of 50
logarithmically equally spaced points between decades 10^X1 and 10^X2.
Logspace(x1,x2)
 Identity Matrix: Matlab allows you to find identity Matrix by ‘eye’ command
Eye(n)……where n = order of the matrix

 DET Determinant: DET(X) is the determinant of the square matrix X.

 INV Matrix inverse: INV(X) is the inverse of the square matrix X.


 EYE(N) is the N-by-N identity matrix. EYE(M,N) or EYE([M,N]) is an M-by-N matrix
with 1's on the diagonal and zeros elsewhere.
 DIAG(x): Diagonal matrices and diagonals of a matrix
 RAND random number: RAND (N) returns an N-by-N matrix containing
pseudorandom values drawn from the standard uniform distribution on the open
interval(0,1).
 RAND(M,N) or RAND([M,N]) returns an M-by-N matrix.
 E.g rand(1,3)
 RANDPERM(N) : A random permutation of the integers 1 to n can be generated using
the RANDPERM(N) function

Page # 03 10ES07
SHUHABUDDIN KHOSO

 randperm(5)
 To create a random number between interval a and b, we can use following command
 a=1; b=5
 a+(b-a)*rand(1,3)

Saving arrays to a file:


 To save arrays for example M v, type type the following command on MATLAB
command prompt
 save myvar M v
Here myvar is name of the MATLAB file. Data files in MATLAB have
extension .mat; the file myvar.mat would be saved in the current directory.
 To restore the variables, type the following command on MATLAB command prompt
load myvar
 To restore any specific variable(s) from the saved file, type the following command on
MATLAB command prompt
load myvar M

Basic Plotting Commands and Codes:


Continous Time Plots:
 Plot command: PLOT command is used to plot continuous tome signal defined in time
domain as shown in figure.
Plot(t,y)…… where ‘y’ is desired function and t is time is seconds

 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 1 st
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,’*’)

Page # 04 10ES07
SHUHABUDDIN KHOSO

 Plotting with symbols: To plot certain distinct values on the continuous graph use the
following code.
Plot(t,’*’).. just points in form of * are plotted
Plot(t,y,’*’)… Graph along function and distinct points are plotted

 Bar charts:
Bar(x)

Discrete Plots: Use


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

Page # 05 10ES07
SHUHABUDDIN KHOSO

Y-LABEL Command: It gives x-axis appropriate label


ylabel(‘particular function’)
TITLE: It gives suitable title to your graph.
title(‘Electronics’)
TEXT: 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
Legend: LEGEND(‘string1’, ‘string2’ ,’ string3’ , ...) puts a legend on the current plot using the
specified strings as labels.

HOLD ON: hold on holds the current plot and all axis properties so that subsequent graphing
commands add to the existing graph. hold on sets the Next Plot property of the current figure and
axes to "add".

HOLD OFF: hold off returns to the default mode whereby PLOT commands erase the previous
plots and reset all axis properties before drawing new plots. hold off sets the Next Plot property
of the current axes to “replace".

HOLD: hold by itself, toggles the hold state. hold does not affect axis auto ranging properties.

HOLD ALL: hold all holds the plot and the current color and line style so that subsequent
plotting commands will not reset the color an line style.

Programs to Explore (just observe the result):


1.linspace(1,0.01,10) and Linspace(1,10)
2. t=0:0.005:10; 3.t=0:0.01:7;
q=sin(3); p=cos(t)
plot(t,q) q=sin(p);
xlabel(‘sine function) plot(t,p);subplot(2,1,1)
text(0.6,1.2,’Graph’) plot(t,q);subplot(2,1,2)
title(‘Plotting of sine function’) plot(t,p,t,q)

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)

Page # 06 10ES07
SHUHABUDDIN KHOSO

Lab Activity: (To be Submitted)


You are supposed to perform them on MATLAB and copy them to word along attached printouts.

[]
1

[ ]
1 1 2 3
z=
0 M= 2 3 1
1. If v = [2 4 7 5], and w = [1 3 8 9];
0 and 4 6 5
Investigate the effect of the following command and explain operation/function:
i) v(2)
Ans = 4 It Gives The 2nd Value Of Vector.
ii) sum = v + w
sum = [3 7 15 14] The Vector V and W are Added.
iii) diff = v – w
diff = [1 1 -1 -4] The Vector w is subtracted from v.
iv) vw = [v w]
vw = [2 4 7 5 1 3 8 9] The both vectors are combined in
Single ROW Vector vw.
v) vw(2: 6)
ans = [4 7 5 1 3] it shows the elements of row vector vw from # 2
to 6.
vi) v’
ans =[2 It Transpose The Vector.
4
7
5]

vii) z’
ans = [1 1 0 0]
viii) z*v
ans = [2 4 7 5 It Multiplies The Both Vector If # of Columns Of
2 4 7 5 1st Vector = # of Rows Of 2nd Vector.
0 0 0 0
0 0 0 0]
ix) [v; w]
ans = [2 4 7 5 This Command Shows 1st vector in 1st row and 2nd
1 3 8 9] Vector in 2nd row.
x) v*z
ans = 6 Multiplications
xi) z + v’
ans = [3
5
7
5]

Page # 07 10ES07
SHUHABUDDIN KHOSO

xii) [z; v’]


ans =[1 1st members of z then New Row then 2nd
1 members of v transpose.
0
0
2
4
7
5]

xiii) M(1,1)
ans = 1 it gives value of 1st Member of M at Position 1x1.
xiv) M(1:2,1:2)
ans = [1 2 it gives value of members in 1st row from # 1 to 2.
2 3] & in 2nd row from # 1 to 2.
xv) M(:,1)
ans = [1 it gives the value of 1st column.
2
4]
xvi) M(2,:)
ans = [2 3 1] it gives the value of 2nd row.
xvii) M(:,end)
ans = [3 it gives the values of end(last) Column.
1
5]
xviii) M(:,end-1)
ans = [2 it gives the values of end-1( 2nd last) Column.
3
6]
xix) M(:,[1 3])
ans = [1 3 it gives the values columns that are mentioned 1,3.
2 1
4 5]
xx) M(:,:)
ans = [1 2 3 it gives the value of vector.
2 3 1
4 6 5]
xxi) M(:,2)=[]
ans = [1 3 it gives the values of columns except mentioned 2.
2 1 and make it new vector of same name.
4 5]
xxii) fliplr(M)
ans = [3 1 it flips right and left column.
1 2
5 4]

Page # 08 10ES07
SHUHABUDDIN KHOSO

xxiii) flipud(M)
ans = [4 5 It Flips Up and Down rows.
2 1
1 3]

xxiv) M(:)
ans = [ 1 convert matrix column vector
2
4
3
1
5]
xxv) M=M(:)’ convert matrix to row vector
ans =[ 1 2 4 3 1 5] 1st converts in columns matrix then transposed
xxvi) M=reshape(M,2,3) convert back to matrix
ans =[ 1 2 it reshapes the vector in new dimensions of 2x3
43
15]

2. Apply DIAG function to vector v and matrix M mentioned in exercise#1. Explain the
results
ans = [0 2 0 0 0
0 0 4 0 0
0 0 0 7 0
0 0 0 0 5
0 0 0 0 0]

3. Generate the following row vector:


a=1:5;
Investigate the following commands and explain:
i. a(2)=6;
a = [1 6 3 4 5] it replace the 2nd member value to 6.
ii. a([ 1 3])=0
a = [0 6 0 4 5] it replaces the 1st and 3rd member value to 0.
iii. a(1,2)=100
a= [0 100 0 4 5] it replaces the value of member at position 1x2 to 100.
iv. a(3)=[ ]
a= [0 100 4 5] it removes the 3rd element of row vector.
v. a([1 4])=[]
a= [100 4] it removes the given element like 1 4.

Page # 09 10ES07
SHUHABUDDIN KHOSO

4. Write a matlab code to plot following functions( attach graphs)


Using t=1:0.01:10

(i) cos (pi*t)


t=1:0.01:10;
x=cos (pi*t);
plot(t,x)

(ii) sin(t)
t=1:0.01:10;
x=sin (t);
plot(t,x)

(iii) cos(t)
t=1:0.01:10;
x=cos (t);
plot(t,x)

(iv) cosec(2*pi*t)
t=1:0.01:10;
x=sin (2*pi*t);
y=1./x;
plot(t,y)

Page # 010 10ES07


SHUHABUDDIN KHOSO

5. Repeat problem 4 using subplot command for plotting the following functions.

t=1:0.01:10;
u=cos (pi*t);
v=sin (t);
w=cos (t);
y=sin(2*pi*t);
x=1./y;
subplot(2,2,1),plot(t,u)
subplot(2,2,2),plot(t,v)
subplot(2,2,3),plot(t,w)
subplot(2,2,4),plot(t,x)

6. Repeat problem 4 using the command plot(t,y1,t,y2…..) (NOTE: Write your roll
number , xlabel, ylabel on each graph.

t=1:0.01:10;
u=cos (pi*t);
v=sin (t);
w=cos (t);
plot(t,u,t,v,t,w)
legend('Cosine Function',
'Sine function','Cosine Function 2 ');
xlabel('Time')
ylabel('Funtion');
title('10ES07');

Page # 011 10ES07


SHUHABUDDIN KHOSO

7. Use text command to plot sine(x*y) where x=your roll number*2, y=cos(your roll
number) and it should contain distinct points ‘*’ in the code with a suitable text (Your
Name) at position 0.2 and 0.2

t=0:.001:1;
x=14;
y=cos(7);
z=sin(t*x*y);
plot(t,z,'*');
text(.2,.2,
'SHUHABUDDIN')

8. Differentiate Linspace(1,10) and logspace(1,10) .


Answer:-
Linspace will divide the given range from 1 to 10 into 100 equal spaces while logspace
divides the given range into 50 equal spaces.

9. How can you save a program in MATLAB? What is the advantage of writing a program
in M-file and if any error occurs in M-file how you can check that error. What role
command window plays here?
Answer:-
We can save our programs with “.m” extension. We can write a Matlab codes in M-file
because we can save, edit and troubleshoot our program easily. We can check error on
command window…. Here command window tell us about the error occur in m-file

Page # 012 10ES07

You might also like