Lab 01
Lab 01
PRACTICAL HANDOUTS
Lab-01
Introduction to MATLAB Environment and Basics
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.
Page # 02 10ES07
SHUHABUDDIN KHOSO
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)
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)
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)
AXIS Command: Define x and y axis and give range for x and y axis
axis ([x1 x2 y1 y2])
Page # 05 10ES07
SHUHABUDDIN KHOSO
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.
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
[]
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
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]
Page # 09 10ES07
SHUHABUDDIN KHOSO
(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)
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');
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')
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