ECE120L - Activity 4 2d plots PDF

Title ECE120L - Activity 4 2d plots
Author Raphael Ong
Course Introduction to Matlab
Institution Malayan Colleges Laguna
Pages 9
File Size 779.3 KB
File Type PDF
Total Downloads 66
Total Views 140

Summary

2dimensional plotting aasdfasdfasdfsadfsadfasdfadfasdfasdflkjasdfklja asd flkasjdflkasjflkjasdf asldkjfaslkjfsalkdjfa...


Description

ECE120L – INTRODUCTION TO MATLAB 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.

A. 2 Dimensional Plotting Function The following function syntax are some available MATLAB 2 – D plot functions. 1. 2 – D Line Plot Syntax: plot(Y) plot(X1,Y1,...,Xn,Yn) plot(X1,Y1,LineSpec,...,Xn,Yn,LineSpec) plot(X1,Y1,LineSpec,'PropertyName',PropertyValue) 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).

PREPARED BY: RONEL V. VIDAL, PECE

1



plot(X1,Y1,LineSpec,'PropertyName',PropertyValue) manipulates plot characteristics by setting lineseries properties (of lineseries graphics objects created by plot). Enter properties as one or more name and value pairs. x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)

t = [0:1/10000:4*pi]; x = 4*exp(-t).*sin(25*t); plot(t,x,'r'), grid on xlabel('Time, t'), ylabel('Amplitude') title('Plot of f(t) = 4e^-^tsin(25t)') axis([0 4*pi -4 4]) -t

Plot of f(t) = 4e sin(10t) 4

3

3 2

2 1

Amplitude

1 0

0

-1

-1

-2 -2

-3 -4 0

2

4

6 Time, t

8

10

-3 -4

12

-3

-2

-1

0

1

2

3

4

2. The Logarithmic Plots Logarithmic scale plots are widely used in the field of engineering in order to represent data set that covers a wide range of values. 2.1 semilogx(x,y) : produces a semilog plot of y versus x with logarithmic abscissa scale 2.2 semilogy(x,y) : produces a semilog plot of y versus x with logarithmic ordinate scale 2.3 loglog(x,y) : produces a plot of y versus x with both abscissa and ordinate in logarithmic scale

%semi logarithmic plot x = [0:0.01:10]; a =exp(x) – .25; y = semilogx(x,a,'r','linewidth',2) grid on, axis([10^0 10^2.5 0 2*10^4])

grid on 10

4

2

%semi logarithmic plot x = [0:0.1:10]; y = semilogy(x,(exp(-x)),'m','linewidth',1.5)

x 10

1.8

10

0

-1

1.6 1.4

10

-2

1.2 1

10

-3

0.8 0.6

10

-4

0.4 0.2

10 0

0

10

1

10

-5

0

2

4

6

8

10

2

10

PREPARED BY: RONEL V. VIDAL, PECE

2

50

10

%loglog scale plot x = logspace(-1,2); loglog(x,exp(x),'-s') grid on

40

10

30

10

20

10

10

10

0

10

-1

0

10

1

10

2

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 x = [0:1/2:10]; y = 2*cos(x); stem(x,y,'fill'), grid on

1 0.5 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

x = linspace(-2*pi,2*pi,40); stairs(x,sin(x))

0.6 0.4 0.2 0 -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. PREPARED BY: RONEL V. VIDAL, PECE

3

y = [4 6 3; 5 9 12; 7 12 8]; bar(y,'group'), axis([0 4 0 13])

y = [34 56 67 23 22 45]; bar(x,y) 70

12 60 10 50 8 40 6

30

4

20

2

10

0

1

2

3

0

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 Arc himedes 90

30 60

120

% Polar Plot of Spiral of Archimedes clear theta = 0:pi/100:4*pi; a = 2; r = a*theta; polar(theta,r,'m') title('Spiral of Archimedes')

20 30

150 10

180

0

210

330

240

300 270

B. Line Specification and Graphic Properties plot(X,Y,LineSpec,'PropertyName',PropertyValue) 1. Line Specification (LineSpec) Line specification describes how to specify the properties of lines used for plotting. MATLAB graphics give the user control over these visual characteristics: • Line style • Color • Marker type

PREPARED BY: RONEL V. VIDAL, PECE

4

Line styles, marker types, and colors using string specifiers Table #. Line Style Specifiers Specifier Line Style Solid Line (default) -Dashed Line : Dotted Line -. Dash – dot Line Table #. Color Specifiers Specifier Color b Blue (default) r Red g Green c Cyan m Magenta y Yellow k Black w White 2. • • •

Table #. Marker Specifiers Specifier Marker Type + Plus sign o Circle * Asterisk . Point (see note below) x Cross ‘square’ or s Square ‘diamond’ or d Diamond ^ Upward pointing triangle v Downward pointing triangle > Right – pointing triangle < Left – pointing triangle ‘pentagram’ or p Five – pointed star (pentagram) ‘hexagram’ or h Six – pointed star (hexagram) Note The point (.) marker type does not change size when the specified value is less than 5.

Graphic Properties Line width Marker size Marker face and edge coloring (for filled markers)

LineWidth or linewidth — Specifies the width (in points) of the line. MarkerEdgeColor or markeredgecolor — Specifies the color of the marker or the edge color for filled markers (circle, square, diamond, pentagram, hexagram, and the four triangles). 3. MarkerFaceColor or markerfacecolor — Specifies the color of the face of filled markers. 4. MarkerSize or markersize— Specifies the size of the marker in points (must be greater than 0). 1. 2.

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

PREPARED BY: RONEL V. VIDAL, PECE

5

1. The Figure command Consider the following functions:

𝑥1 = sin 2𝑡 𝑥2 = 2 cos 𝑡 sin 𝑡 Plot the given functions from 0 to 2𝜋 with a resolution of 𝜋 ⁄20 using the figure(n) command. % Plotting Multiple Graphs with Figure command t = 0:pi/20:2*pi; x1 = sin(2*t); x2 = 2*cos(t).*sin(t); figure(1), plot(t,x1,'mo-','linewidth',2,'markeredgecolor','k','markerfacecolor','c','markersize',8), grid on, title('Plot of sin(2t)'), xlabel('time,t'), ylabel('Amplitude'), axis([0 2*pi -1.25 1.25]) figure(2), plot(t,x2,'rd--','linewidth',2,'markeredgecolor','b','markerfacecolor','y','markersize',8) grid on, title('Plot of cos(2t)sin(2t)'), xlabel('time, t'), ylabel('Amplitude'), axis([0 2*pi -1.25 1.25])

Plot o f cos(2t)sin(2t)

1

1

0.5

0.5 Amplitude

Amplitude

Plot o f sin(2t)

0

0

-0.5

-0.5

-1

-1

0

1

2

3 time, t

4

Figure 1

5

6

0

1

2

3 time, t

4

5

6

Figure 2

2. The hold Command hold: freezes the current plot for subsequent graphics commands Syntax: hold on hold off Description: • The hold function determines whether new graphics objects are added to the graph or replace objects in the graph. • hold on retains the current plot and certain axes properties so that subsequent graphing commands add to the existing graph. • hold off resets axes properties to their defaults before drawing new plots. hold off is the default.

PREPARED BY: RONEL V. VIDAL, PECE

6

1

t = 0:pi/20:2*pi; x = sin(t); y = sin(t-pi/2); z = sin(t-pi); plot(t,x,'-.r*'), grid on hold on plot(t,y,'--mo') plot(t,z,':bs') hold off

0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0

1

2

3

4

5

6

7

3. The subplot Command The subplot(m,n,p) command creates several smaller “subplots” in the same figure. The variable m is for rows, n for columns and p the position of each subplot. Consider the functions: 𝑥1 = 𝑒0.5𝑡 sin 2𝑡 𝑥2 = 1 + cos 4𝑡 𝑥3 = 2 sin 2𝑡 cos 2𝑡 𝑥4 = 2 cos 𝑡 + 2 sin 𝑡 Plot the given functions from –10 to +10, with a resolution of 0.001 using the subplot command. % Plotting Multiple Graphs with subplot command t = [-10:0.001:10]; x1 = exp(0.5*t).*sin(2*t); x2 = 1 + cos(4*t); x3 = 2*sin(2*t).*cos(2*t); x4 = 2*cos(t)+2*sin(t); subplot(2,2,1), plot(t,x1,'r','linewidth',1.5), grid on, title('Plot of e^0^.^5^tsin(2t)') xlabel('time,t'), ylabel('Amplitude'), axis([-10,10,-100,100]) subplot(2,2,2), plot(t,x2,'g','linewidth',1.5), grid on, title('Plot of 1 + cos(4t)') xlabel('time,t'), ylabel('Amplitude'), axis([-10,10,-0.5,2.5]) subplot(2,2,3), plot(t,x3,'m','linewidth',1.5), grid on, title('Plot of 2sin(2t)cos(2t)') xlabel('time,t'), ylabel('Amplitude'), axis([-10,10,-1.5,1.5]) subplot(2,2,4), plot(t,x4,'linewidth',1.5), grid on, title('Plot of 2cos(t)+2sin(t)') xlabel('time,t'), ylabel('Amplitude'), axis([-10,10,-3,3])

PREPARED BY: RONEL V. VIDAL, PECE

7

0.5t

Plot of e

Plot of 1 + cos(4t)

sin(2t) 2 Amplitude

50 0 -50 -100 -10

Amplitude

2.5

1 0.5 0

0 time,t Plot of 2sin(2t)cos(2t)

10

-0.5 -10

1.5

3

1

2

0.5 0 -0.5 -1 -1.5 -10

III.

1.5

Amplitude

Amplitude

100

0 time,t Plot of 2cos(t)+2sin(t)

10

0 time,t

10

1 0 -1 -2

0 time,t

10

-3 -10

Laboratory Exercises: *Note: For each of the exercises, always include the x – label, y – label and the title of each plot. A. Plotting Functions The table below shows daily temperatures for New York City, recorded for 6 days, degrees – Fahrenheit. Temperatures in New York City Temperature, ℉ Day 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

PREPARED BY: RONEL V. VIDAL, PECE

8

B. Figure Command Given the 3 functions below, plot them individually using the figure command. Set the horizontal axis as indicated for each function and with a resolution of 1/100. Alter the color, line width and include marker if applicable for each plot. 1 −4𝜋 ≤ 𝑥 ≤ 4𝜋 1) 𝑓(𝑥) = 2 + cos 𝑥 + 2 cos 2𝑥 −𝑥 2) 𝑓(𝑥) = 𝑒 sin 10𝑥 0 ≤ 𝑥 ≤ 3𝜋 3) 𝑓(𝑥) = 2 sin 2𝑥 cos 2𝑥 −4𝜋 ≤ 𝑥 ≤ 4𝜋 C. Logarithmic Plots Using the subplot command Given the function below, plot it in linear, semilog x, semilog y, and log x & y scale using the subplot command. Alter the color, the line style and line width of the plot with a resolution of 1/100. 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. 0 ≤ 𝜃 ≤ 4𝜋 1) 𝑟 = sin 𝜃 + sin3(5𝜃⁄2 ) 2) 𝑟 = cos2 5𝜃 + sin 3𝜃 + 0.3

0 ≤ 𝜃 ≤ 2𝜋

PREPARED BY: RONEL V. VIDAL, PECE

9...


Similar Free PDFs