0% found this document useful (0 votes)
30 views9 pages

Khizran 3

Uploaded by

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

Khizran 3

Uploaded by

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

LAB NO: 03

GENERATION OF VARIOUS SIGNALS AND THEIR PLOTTING IN MATLAB


Plotting
Plotting is one of the most useful applications of a math package to plot experimental or
generated data.
Creating Simple Plots:
The MATLAB command to plot a graph is plot(x,y).
The vectors x = (1; 2; 3; 4; 5; 6) and y = (3;-1; 2; 4; 5; 1) produce the picture shown in Figure
below
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot (x,y)

Note: The plot functions have different forms depending on the input arguments. If y is a vector
plot (y) produces a piecewise linear graph of the elements of y versus the index of the elements
of y. If we specify two vectors, as mentioned above, plot (x,y) produces a graph of y versus x.
Basic 2 D plotting:
Plotting a function in MATLAB involves the following three steps:
1. Define the function
2. Specify the range of values over which to plot the function
3. Call the MATLAB plot(x, y) function.
Example:
1- Define the function
For plotting the function
y= cos(x) over a range of 0≤x≤pi

2- Specify the range of values over which to plot the function


To start, we have to define this interval and tell MATLAB what increment to use. The
interval is defined using square brackets [ ] that are filled in the following manner:
Syntax: [start: interval: end]
Example:
x=0: pi/10:2* pi
To assign this range to a variable name, we use the assignment operator. We also do this
to tell MATLAB what the dependent variable is and what function we want to plot.
Hence the command
y = cos(x), returns the values of cos(x) to the variable y for the values of x:
3- Call plot(x, y) function
Now we can plot the function by entering the following command in the command window:
>> Plot (x, y)
After a moment MATLAB will open a new window on the screen with the caption

Figure 1. The plot is found in this window.


>> x = [0:pi/10:2*pi];
>> y = cos(x);

Fig. 2: Plotting the data


Adding axis command: MATLAB allows us to adjust the axes used in two- dimensional plots
in the following way
 Adding axis square to the line containing the plot command, this will cause MATLAB to
generate a square plot.
 Adding axis equal, then MATLAB will generate a plot that has the same scale factors and spacing
on both axes.
 To set the plot for user defined range call the function axis in the following way: axis([xmin xmax
ymin ymax])

Multiple plots: To plot multiple functions, we simply call the plot(x, y) command with multiple
pairs x, y defining the independent and dependent variables used in the plot in pairs. This is
followed by a character string enclosed in single quotes to tell us what kind of line to use to
generate the second curve.

Multiple (x, y) pairs arguments create multiple graphs with a single call to plot.
For example,
these statements plot three related functions of
x: y1 = 2 cos(x), y2 = cos(x), and y3 = 0.5 *cos(x), in the interval 0: x: 2pi
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,’--’,x,y2,’-’,x,y3,’:’)
>> xlabel(’0 \leq x \leq 2\pi’)
>> ylabel(’Cosine functions’)
>> legend(’2*cos(x)’,’cos(x)’,’0.5*cos(x)’)
>> title(’Typical example of multiple plots’)
>> axis([0 2*pi -3 3])
Adding colors: The color of each curve can be set automatically by MATLAB or we
can manually select which color we want. This is done by enclosing the appropriate
letter assigned to each color used by MATLAB in single quotes immediately after the
function to be plotted is specified. Let’s illustrates with an example.
For plotting the first series in red color with dashed lines and second series with dot-
dashed lines in blue color.
>> plot (t1, f, ’r--’, t2, g, ‘b.-’);
Attributes for plot:

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.
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])
title(‘Sine Exponential Function’)
y = exp(-2*x).*cos(10*x);
subplot(1,2,2)
plot(x,y)
xlabel('x')
ylabel('exp(–2x)*cos(10x)')
axis([0 5 -1 1])
title(‘Cosine Exponential Function’)
Stem Function in MATLAB
Stem() method in MATLAB is a type of plotting method to represent any type of
data in a discrete form. This method generates a plot in the form of vertical lines
being extended from the bases line, having little circles at tips which represents
the exact value of the given data. Unlike plot() function, it does not join the
values points with each other to create a continuous graph, rather it emphasizes
the fact that the data points are discrete, not continuous .

clc
clear all
close all
x = linspace(-pi,pi,50);
y = sin(x);
subplot(1,2,1)
plot(x,y)
xlabel({'-\pi','0','\pi'})
title('Plot function of Sin(x)')
grid on
subplot(1,2,2)
stem(x,y)
xlabel({'-\pi','0','\pi'})
title('Stem function of Sin(x)')
grid on

LAB TASK
1. Plot a sine function, cosine function and an exponential function (both increasing and decaying)
by multiplying each function with your registration number. (e.g 12*sin(x) if your reg no. =12)
2. Add any other function in the above given functions and plot addition of those two functions.
(e.g sin(x)+cos(x))
3. Plot all these functions in discrete form as well using stem command.
4. Use subplot command to plot the discrete version of each function alongside its continuous form.
5. Plot the following functions on the same graph
1) y = sin(2πt) *sin(2π(20t))

2) z= cos(2πt) *cos(2π(20t+ π/4))


MATLAB CODINNG:

t=0:0.200:3*pi;
y1=6*sin(t);
y2=6*cos(t);
y3=6*exp(t);
y4=-6*exp(t);
plot(t,y1,"-",t,y2,"--",t,y3,"+",t,y4,"*");
legend("6*sin(t)","6*cos(t)","6*exp(t)","-6*exp(t)");
title("Different Functions");
xlabel("Time Axis");
ylabel("Amplitude Axis");
axis([0 6 -200 300]);

t=0:0.200:3*pi;
y1=6*sin(t);
y2=6*cos(t);
y3=6*exp(t);
y4=-6*exp(t);
x= sin(t);
z=y1+y2;
plot(t,y1,"-",t,y2,"--",t,y3,"+",t,y4,"*",t,x,".-.",t,z);
legend("6*sin(t)","6*cos(t)","6*exp(t)","-6*exp(t)");
title("Different Functions");
xlabel("Time Axis");
ylabel("Amplitude Axis");
axis([0 4 -100 100]);

clc
t=0:0.200:3*pi;
y1=6*sin(t);
y2=6*cos(t);
y3=6*exp(t);
y4=-6*exp(t);
x= sin(t);
z=y1+y2;
subplot(3,2,1);
stem(t,y1,"k*");
title("Sine WAve");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,2);
stem(t,y2,"k.-.");
title("Cos Wave");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,3);
stem(t,y3,"k*");
title("exponential Function");
xlabel("Amplitude Axis");
ylabel("y axis");
subplot(3,2,4);
stem(t,y4,"k*");
title("exponential Functions");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,5);
stem(t,x,"k--");
title("Sine WAve");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,6);
stem(t,z,"k*");
title("Adding Functions");
xlabel("Time Axis");
ylabel("Amplitude Axis");

clc
t=0:0.200:3*pi;
y1=6*sin(t);
y2=6*cos(t);
y3=6*exp(t);
y4=-6*exp(t);
x= sin(t);
z=y1+y2;
subplot(3,2,1);
plot(t,y1,"k*");
title("Sine WAve");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,2);
plot(t,y2,"k.-.");
title("Cos Wave");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,3);
plot(t,y3,"k*");
title("exponential Function");
xlabel("Amplitude Axis");
ylabel("y axis");
subplot(3,2,4);
plot(t,y4,"k*");
title("exponential Functions");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,5);
plot(t,x,"k--");
title("Sine WAve");
xlabel("Time Axis");
ylabel("Amplitude Axis");
subplot(3,2,6);
plot(t,z,"k*");
title("Adding Functions");
xlabel("Time Axis");
ylabel("Amplitude Axis");

You might also like