ECE120L - Activity 4
ECE120L - Activity 4
ECE120L - Activity 4
LABORATORY ACTIVITY #4
2 – Dimensional Plots
I. Learning Outcomes:
At the end of the laboratory activity, the students should be able to:
1. plot 2 – dimensional multiple graphs with MATLAB
2. plot response curves in logarithmic scale
3. plot polar equations with MATLAB
II. Introduction:
The MATLAB environment offers a variety of data plotting functions to create, and modify graphic displays.
A figure is a MATLAB window that contains graphic displays (usually data plots) and UI components. Figures
can be created explicitly with the figure function, and implicitly whenever graphics are plotted and no figure is
active. By default, figure windows are resizable and include pull-down menus and toolbars.
A plot is any graphic display that can be created within a figure window. Plots can display tabular data,
geometric objects, surface and image objects, and annotations such as titles, legends, and color bars. Figures
can contain any number of plots. Each plot is created within a 2-D or a 3-D data space called an axes. Axes can
be created explicitly with the axes or subplot functions.
Description
• plot(Y) plots the columns of Y versus the index of each value when Y is a real number. For complex Y,
plot(Y) is equivalent to plot(real(Y),imag(Y)).
• plot(X1,Y1,...,Xn,Yn) plots each vector Yn versus vector Xn on the same axes. If one of Yn or Xn is a matrix
and the other is a vector, plots the vector versus the matrix row or column with a matching dimension to
the vector. If Xn is a scalar and Yn is a vector, plots discrete Yn points vertically at Xn. If Xn or Yn are complex,
imaginary components are ignored. plot automatically chooses colors and line styles in the order specified
by ColorOrder and LineStyleOrder properties of current axes.
• plot(X1,Y1,LineSpec,...,Xn,Yn,LineSpec) plots lines defined by the Xn,Yn,LineSpec triplets, where LineSpec
specifies the line type, marker symbol, and color. Xn,Yn,LineSpec triplets with Xn,Yn pairs:
plot(X1,Y1,X2,Y2,LineSpec,X3,Y3).
t = [0:1/10000:4*pi]; x = -pi:pi/10:pi;
x = 4*exp(-t).*sin(25*t); y = tan(sin(x)) - sin(tan(x));
plot(t,x,'r'), grid on plot(x,y,'--rs','LineWidth',2,...
xlabel('Time, t'), ylabel('Amplitude') 'MarkerEdgeColor','k',...
title('Plot of f(t) = 4e^-^tsin(25t)') 'MarkerFaceColor','g',...
axis([0 4*pi -4 4]) 'MarkerSize',10)
3
2
2
1
1
Amplitude
0 0
-1
-1
-2
-2
-3
-3
-4 -4 -3 -2 -1 0 1 2 3 4
0 2 4 6 8 10 12
Time, t
1.8 -1
10
1.6
1.4 -2
10
1.2
1 -3
10
0.8
0.6 -4
10
0.4
0.2
-5
10
0 0 2 4 6 8 10
0 1 2
10 10 10
PREPARED BY: RONEL V. VIDAL, PECE 2
50
10
loglog(x,exp(x),'-s') 30
grid on 10
20
10
10
10
0
10
-1 0 1 2
10 10 10 10
3. Stem Plot
The syntax stem(X,Y) plots X versus the columns of Y. X and Y must be vectors or matrices of the same size.
Additionally, X can be a row or a column vector and Y a matrix with length(X) rows. stem(...,'fill') specifies
whether to color the circle at the end of the stem. 2
1.5
%stem plot 1
x = [0:1/2:10];
0.5
y = 2*cos(x);
stem(x,y,'fill'), grid on 0
-0.5
-1
-1.5
-2
0 2 4 6 8 10
4. Stairstep graph
Stairstep graphs are useful for drawing time-history graphs of digitally sampled data. The syntax stairs(X,Y)
plots the elements in Y at the locations specified in X. 1
0.8
0.6
x = linspace(-2*pi,2*pi,40);
0.4
stairs(x,sin(x))
0.2
-0.2
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
5. Bar Plot
The syntax bar(Y) draws one bar for each element in Y. If Y is a matrix, bar groups the bars produced by
the elements in each row. The x-axis scale ranges from 1 up to length(Y) when Y is a vector, and 1 to size(Y,1),
which is the number of rows, when Y is a matrix.
The syntax bar(x,Y) draws a bar for each element in Y at locations specified in x, where x is a vector
defining the x-axis intervals for the vertical bars. The x-values can be nonmonotonic, but cannot contain
duplicate values. If Y is a matrix, bar groups the elements of each row in Y at corresponding locations in x.
10
50
8
40
6
30
4
20
2 10
0 0
1 2 3 10 11 12 13 14 15
6. Polar Plots
The command polar(theta,r) creates a two – dimensional plot using polar coordinates. The polar coordinates
are 𝜃 (the angular coordinate) and 𝑟 (the radial coordinate). A grid is automatically overlaid on a polar plot
consists of concentric circles and radial lines every 30.
The spiral of Archimedes is described by the polar coordinates(𝜃, 𝑟), where 𝑟 = 𝑎𝜃. Obtain a polar plot of
this spiral for 0 ≤ 𝜃 ≤ 4𝜋, with the parameter a = 2.
Spiral of Archimedes
90 30
120 60
% Polar Plot of Spiral of Archimedes 20
clear 150 30
10
theta = 0:pi/100:4*pi;
a = 2; 180 0
r = a*theta;
polar(theta,r,'m')
210 330
title('Spiral of Archimedes')
240 300
270
plot(X,Y,LineSpec,'PropertyName',PropertyValue)
2. Graphic Properties
• Line width
• Marker size
• Marker face and edge coloring (for filled markers)
C. Multiple Graphs
MATLAB has several commands in order to plot multiple graphs. Listed below are some commands used
in plotting 2 – dimensional multiple graphs.
1. figure(n) :plot the graphs into an individual nth figure window
2. subplot(m,n,p) :splits the figure window into an array of sub windows with m rows and n
columns, and directs the subsequent plotting commands to the pth sub window
3. hold :freezes the current plot for subsequent graphics commands
1 1
0.5 0.5
Amplitude
Amplitude
0 0
-0.5 -0.5
-1 -1
0 1 2 3 4 5 6 0 1 2 3 4 5 6
time,t time, t
Figure 1 Figure 2
t = 0:pi/20:2*pi; 0.8
x = sin(t); 0.6
y = sin(t-pi/2);
0.4
z = sin(t-pi);
plot(t,x,'-.r*'), grid on 0.2
hold on
0
plot(t,y,'--mo')
plot(t,z,':bs') -0.2
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
Amplitude
Amplitude
1.5
0 1
0.5
-50
0
-100 -0.5
-10 0 10 -10 0 10
time,t time,t
Plot of 2sin(2t)cos(2t) Plot of 2cos(t)+2sin(t)
1.5 3
1 2
Amplitude
Amplitude
0.5 1
0 0
-0.5 -1
-1 -2
-1.5 -3
-10 0 10 -10 0 10
time,t time,t
A. Plotting Functions
The table below shows daily temperatures for New York City, recorded for 6 days, degrees –
Fahrenheit.
Temperatures in New York City
Day Temperature, ℉
1 43
2 53
3 50
4 57
5 59
6 67
7 60
Construct graphs of the data above using the following plotting functions below. Include line
specifier and graphic properties to enhance your graph.
1. 2 – d line plot
2. Stem plot
3. Stairstep
4. Bar plot
1. 𝑓(𝑥) = 𝑒 𝑥
2. 𝑓(𝑥) = 10−2𝑥
D. Hold on command
Below are four trigonometric equations. Plot them in one graph using the hold on command.
Alter the color and line width of each function. Include marker if applicable. Use −4π ≤ x ≤ 4π
with a resolution of 1/100.
𝑓(𝑥) = 4 sin 𝑥
𝑔(𝑥) = 3 sin 2𝑥
ℎ(𝑥) = 2 sin 3𝑥
𝑘(𝑥) = sin 4𝑥
E. Polar Plots
1. Below are three polar equations. Obtain a polar plot of each equation in one graph using the
hold on command. Use 0 ≤ θ ≤ 2π with a resolution of π/1000. Alter the color of the plot for
each equation.
𝑟1 = 3 sin 4𝜃
𝑟2 = 2 sin 4𝜃
𝑟3 = sin 4𝜃
2. For the following polar equations below, plot them individually using the figure command.
Alter the color and line width of each plot.
1) 𝑟 = sin 𝜃 + sin3(5𝜃⁄2) 0 ≤ 𝜃 ≤ 4𝜋
2
2) 𝑟 = cos 5𝜃 + sin 3𝜃 + 0.3 0 ≤ 𝜃 ≤ 2𝜋