Lab 2
Lab 2
CLO5: USE MODERN TOOL (E.G. MATLAB) FOR SIGNAL REPRESENTATION, VISUALIZATION AND
PROCESSING IN BOTH TIME AND FREQUENCY DOMAIN .
1. Plotting Graphs:
Plotting in MATLAB is done by command plot(x values, y values, ‘style option’). It is important to
note that x-values and y-values should have same size. ‘Style Option’ is optional. It is usually used
when you are plotting more than one plot on same axis. Different style options are listed below:
Test Yourself
Plot above three graphs using hold on command:
Subplots:
You can hold more than one set of axes on one figure window. The command subplot(m,n,p)
subdivides the current figure window into an m-by-n matrix of plotting areas and chooses the pth
area to be active.
Test Yourself
Run following commands and plot the output:
x = -2*pi:pi/10:2*pi;
y = x.^2;
z = sin(x);
y1 = cos(x);
z1 = exp(x);
subplot(2,2,1),plot(x,y)
grid
subplot(2,2,2),plot(x,z)
grid
subplot(2,2,3),plot(x,y1)
grid
subplot(2,2,4),plot(x,z1)
grid
(write instructions for axis label, graph title by yourself)
Note: After using subplot commands, if you again want to view single graph, use clf (Clear Figure)
command.
Logarithmic Scale Graphs:
Graphs can also be plotted on Logarithmic Scale. Commands are:
loglog→Both axis Logarithmic Scale
semilogx→ x-axis Logarithmic Scale
semilogy→ y-axis Logarithmic Scale
Function Description
area Creates a filled area plot
bar Creates a bar graph
barh Creates a horizontal bar graph
comet Makes an animated 2-D plot
compass Creates arrow graph for complex numbers
contour Makes contour plots
contourf Makes filled contour plots
errorbar Plots a graph and puts error bars
Function Description
feather Makes a feather plot
fill Draws filled polygons of specified color
fplot Plots a function of single variable
hist Makes histograms
loglog Creates plot with log scale on both x and y axes
pareto Makes pareto plots
pcolor Makes pseudo color plot of matrix
pie Creates a pie chart
plotyy Makes a double y-axis plot
plotmatrix Makes a scatter plot of a matrix
polar Plots curves in polar coordinates
quiver Plots vector fields
rose Makes angled histograms
scatter Creates a scatter plot
semilogx Makes semi-log plot with log scale on the x-axis
semilogy Makes semi-log plot with log scale on the y-axis
stairs Plots a stair graph
stem Plots a stem graph
There is one more command fprinf for output. It has more variety of options. Explore it by
yourself by doc fprintf
If you want to take input from user, simple command input is used. You can also display some
message while taking the input. For example if you write in M-File:
a=input('Enter first digit:')
The message in quotes will be printed, user will enter an input that will be stored in variable a.
Test Yourself
Write a M-File program that will ask user for two input digits one by one and then will ask what
is sum of those numbers. If the sum is correct, it will print “Correct” and otherwise it will print
“Wrong”. (If you wish you can also extend it so that program should keep on asking the sum till it
gets the correct sum)
A sample output is :
What number is a: 15
What number is b: 20
What is the sum of a + b: 35
answer = Correct!
M-File code Output
While Loop:
Syntax of while loop is similar to for loop:
while expression
statements
In for loop number of repetitions are known, but if you are facing condition that number of
repetitions are not known, then while loop is used.
Here is an almost trivial problem that requires a use of this loop. Suppose that the number π is
divided by 2. The resulting quotient is divided by 2 again. This process is continued till the current
quotient is less than or equal to 0.01. What is the largest quotient that is greater than 0.01?
To answer this question code will be:
q = pi;
while q > 0.01
q = q/2;
end
q
Switch-case constructions
Syntax of the switch-case construction is
switch expression (scalar or string)
case value1 (executes if expression evaluates to value1)
commands
case value2 (executes if expression evaluates to value2)
commands
.
.
.
otherwise
statements
end
Switch compares the input expression to each case value. Once the match is found it executes
the associated commands.
There is one more command fprinf for output. It has more variety of options. Explore it by
yourself by doc fprintf
If you want to take input from user, simple command input is used. You can also display some
message while taking the input. For example if you write in M-File:
a=input('Enter first digit:')
The message in quotes will be printed, user will enter an input that will be stored in variable a.
Test Yourself
Write a M-File program that will ask user for two input digits one by one and then will ask what
is sum of those numbers. If the sum is correct, it will print “Correct” and otherwise it will print
“Wrong”. (If you wish you can also extend it so that program should keep on asking the sum till it
gets the correct sum)
A sample output is :
What number is a: 15
What number is b: 20
What is the sum of a + b: 35
answer = Correct!
Example
x = [2 3 6 9]
then y should be
So
x = [1 2 3 3 2 1] is symmetric
x = [1 2 3 4 3 2 1] is symmetric as well!
x = [1 2 3 4 5]
y_correct = [1 0 0 2 0 0 3 0 0 4 0 0 5 0 0];
x = [1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 9 8 7 6 5 4 3 2 1];
LP = [6 9]
x = [1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0];
LP = [7 3];
Example:
n=3;
A = FillWithOnes(n,mat);
A=[0 0 1; 1 0 0; 0 0 0];
Examples:
Input x = [2 5 8 3 7 1];
Output y = 4;
Input x = [2 5 9];
Output y = 2;
So if n = 3, then return
[1 2 2 3 3 3]
[1 2 2 3 3 3 4 4 4 4 5 5 5 5 5]
1 2 3 4
5 5 6 5
should become:
0 0 0 4
0 0 6 0
0 9 0 0
If a row has the same maximum in different columns , display the first occurrence:
In the below example, the second row has twice the number 8 as maximum , the output will
display the first 8 (column2) :
x =
5 4 5
2 8 8
should become :
5 0 0
0 8 0
A = [1 2 3
3 2 1]
B = [1 1 1 1 1
1 1 2 3 1
1 3 2 1 1
or
A = 4
B = [ 1 1 1
1 4 1
1 1 1]
V=[7 1 2 -3]
there is one sign change (from 2 to -3). So, the function you write must return N=1.
V=[5 9 -2 7];
there are two sign changes, one from 9 to -2 and a second from -2 to 7, thus N=2.
Similarly
1 Output:
- A a vector with iterations+1 elements such as the i+1 element depend on the previous
element as follows :
A(i+1) = 2*A(i)+1
Example :
A = 1;
iterations = 4;
A = [1 3 7 15 31]