2D & 3D PLOTTING OF MATRICES(SIGNALS) / LAB # 5

LAB # 5

2D & 3D PLOTTING OF MATRICES(SIGNALS)

Objective

  • To generate 2D plotting of matrices (signals) using M-File.
  • To generate 3D plotting of matrices (signals) using M-File.

Theory

5.1 PLOTTING OF ARRAY AND MATRICES

Using wide range graphic capabilities of MATLAB, 2D and 3D plots can be generated and customized.

The MATLAB environment provides a wide variety of techniques to display data graphically. Interactive tools enable you to manipulate graphs to achieve results that reveal the most information about your data. You can also annotate and print graphs for presentations, or export graphs to standard graphics formats for presentation in Web browsers or other media. The process of visualizing data typically involves a series of operations.

5.2 CREATING A GRAPH

There are two basic ways to create MATLAB graphs:

  • Use plotting tools to create graphs interactively.
  • Use the command interface to enter commands in the Command Window or create plotting programs.

5.3 THE PLOT FUNCTION

The most common function for plotting 2-D data is the plot function. This versatile function sets of data arrays on appropriate axes and connects the points with straight lines:

EXAMPLE 5.1

Plot Sine Wave

% Script M-file gra_plotting.m

x = linspace(0,2*pi,30);

y = sin(x);

plot(x,y)

title('Figure 26.1: Sine Wave')

Note: save M- file names -- gra_plotting.m

This example creates 30 data points over 0 ≤ x ≤ 2π to form the horizontal axis of the plot and create another vector y containing the sine of the data points in x. The plot function opens a graphic window, called a Figure window, scales the axes to fit the data, plots the points, and then connects the points with straight lines.

CLASS ACTIVITY

Show the output graph of all the given examples:

EXAMPLE 5.2

Generation of 2D plotting of Matrices (Signals)

% Script M-file example1.m

t = 0:0.1:1;

x = exp(-10*t); % two array t and x, each of size 1*11 are generated.

plot(t,x)

figure

plot(t,x,'ro')

% previous fig. is overwritten by this plot which shows

%discrete points in the form of red os.

clc % clears the screen

figure % create new figure window

stem(t,x) %discrete plot is generated

figure

stem(t,x,'filled')%discrete plot is generated with filled markers

figure

subplot(121);

% A new figure window is generated and is broken into 1*2 grid of sub

% windows.it's a sort of matrix-concept with one row and two colums. now

% two plots can be put side by side in the same figure window. Third digit

% (1 in 121 and 2 in 122) shows whoich of two sub-windows is active. in

% this plot, t is on x-axis while x is on y-axis.

plot(t,x)

subplot(122);

% This plot is generated in the second (from left) sub-window of the figure.

plot(x,t) % In this plot, x is on x-axis while t is on y-axis.

EXAMPLE 5.3

Co-plotting of signals in the same window

% Script M-file example2.m

x = 0:1:50;

y1 = sin(2.*pi*x/8);

y2 = cos(2.*pi*x/8); %sin and cos functions are generated

figure

plot(x,y1,x,y1) % The two are co-plotted in the same figure

EXAMPLE 5.4

Co-plotting of signals using Hold command

% Script M-file example3.m

x = 0:1:50;

y1 = sin(2*pi*x/8);

y2 = cos(2*pi*x/8); %sin and cos functions are generated

figure

plot(x,y1,'r'); hold on;% first plot and hold on

plot(x,y1,'g'); hold off;% After second plotting, hold off

axis([0 10 -1 1])

xlabel('time')

ylabel('amplitude')

title('Two sine wave')

HOME TASK

  1. Plot Sine and Cosine Wave

% Script M-file gra_ploting1.m

x = linspace(0,2*pi,30);

y = sin(x);

z = cos(x);

plot(x,y,x,z)

title('Figure: Sine and Cosine Wave')

Show the output of the above program also write the function of “linspace” ?

Function of linepace:

It stands for linearly spaced vector, LINSPACE(X1,X2) generates a row vector of 100 linearly EQUALLY SPACED POINTS BETWEEN X1 AND X2 .For N point between X1 and X2 .LINESPACE(X1,X2,N).For N<2,linespace returns x2.

  1. Grid and Box commands

% Script M-file gra_ploting2.m

x = linspace(0,2*pi,30);

y = sin(x);

z = cos(x);

plot(x,y,x,z)

grid on %turn axes grid lines on

box on % turn on the axes box

xlabel('Independent Variable X') % label horizontal axix

ylabel('Dependent Variable Y and Z') % label Vertical axix

title('Figure: Sine and Cosine Wave, Added Label') % tilte

Show the output of the above program also write the function of “box on”?

Function of box on:

Box is use to set the Box property of an axes .Box On adds a box to the current axes. Box OFF takes if off. It toggles the box state of the current axes by itself.

  1. Generate a plot of e-0.7y, sin (ωy), with ω = 15 rad/s and 0 ≤ y ≤ 15 with an increment of 0.1. Keep the ranges of x-axis and y-axis as (0, 200) and (-1, 1) respectively. Label the axes and title the plot.
  1. Suppose we have a function z = e-x-y which is depending on two variables x and y. let’s say the range of x and y is (-2,2). We want a 3D plot of z w.r.t. the two variables x and y.

[X,Y]=meshgrid([-2:0.1:2]);

Z = X.*exp(-X -Y);

mesh(X,Y,Z);

figure; meshc(X,Y,Z)

figure; meshz(X,Y,Z)

figure; surf(X,Y,Z)

figure; surfl(X,Y,Z)

figure; surfc(X,Y,Z)

figure; pcolor(X,Y,Z)

Show the output graph of the above program also write the comment on every line and write the function of mesh, meshc, meshz, surf, surfl, surfc and pcolor?

Function of meshmeshcmeshzsurfsurflsurfc:

It plots the colored parametric mesh defined by matrix arguments. The axis labels are determinedby the range of X, Y and Z or by the current setting of the axis.

It stands for combination mesh or contour plot. By this a contour plot is drawn beneath the mesh. As contour does not handle irregularly spaced data, thisroutine only works for surfaces defined on a rectangular grid. The matrices or vectors X and Y define the axis limits only.

It creates “curtain “or reference plane is drawn beneath .This routine only works for surfaces defined on a rectangular grid. Themetrics X and Y define the axis limits only.

It plots the colored parametric surface defined by four matrix arguments. Surf returns a handle to surface plot object.

It produces a colored lighted surface using the LIGHT object.

It stands for combination surf/contour plot. By this a contour plot is drawn beneath the surface.

It stands for pseudocolor.It specify the color in each cell of the plot.

CE 308: Communication System / 1