Lecture 9-1
2-D and 3-D Plotting
Summer School , 2013
School of Information Technologies
The University of Sydney, Australia
https://www.doczj.com/doc/da11242363.html,.au/~engg1801
engg1801help@https://www.doczj.com/doc/da11242363.html,.au
Jason Chan
j.chan@https://www.doczj.com/doc/da11242363.html,.au
ENGG1801 Engineering Computing 1
?Plotting
–How to show complex results using clear plots
3
ENGG1801 Engineering Computing
4
?Matlab makes plotting very easy!
ENGG1801 Engineering Computing
-8
-6
-4
-2
2
4
6
8
-1-0.8-0.6-0.4-0.200.2
0.40.60.8112345678910
0.51
1.5
2
2.5
3
-4
-2
2
4
-4
-2
2
4
05
1015
20
-3
-2
-1
1
2
3
-3
-2
-1
1
2
3
0246
810
121416
18x
z = x 2 + y 2
y
z
5
?plot(x,y)is given the x and y coordinates of points (as arrays) and connects the adjacent points
–x and y should have the same number of elements
ENGG1801 Engineering Computing
-8
-6
-4
-2
02
4
6
8
-1-0.8-0.6-0.4-0.200.20.40.60.81
x
c o s (x )
cos of x
6
% Make an array containing values from -2*pi to % 2*pi, in increments of 0.05x = -2*pi:0.05:2*pi;
% Make an array containing % the corresponding cosine % of each value in array 'x'y = cos(x);
% Plot, giving the x and y % values of each point plot(x,y);xlabel('x');
ylabel('cos(x)');title('cos of x');
ENGG1801 Engineering Computing
-8
-6
-4
-2
02
4
6
8
-1-0.8-0.6-0.4-0.200.20.40.60.81x
c o s (x )
cos of x
7
?The minimum and maximum x and y values of the axes are set using axis()axis([xmin xmax ymin ymax])
ENGG1801 Engineering Computing
-6
-5
-4
-3
-2
-1
-1
-0.8-0.6-0.4-0.200.20.40.60.81x c o s (x )
cos of x
-6
-5
-4
-3
-2
-1
-0.5
-0.4-0.3-0.2-0.100.10.20.30.40.5x c o s (x )
cos of x
axis(-2*pi 0 -1 1);axis(-2*pi 0 -0.5 0.5);
8
% Make an array containing values from -2*pi to % 2*pi, in increments of 0.05x = -2*pi:0.05:2*pi;% Make some arrays whose % values are a function of % the values in array 'x'y1 = cos(x);
y2 = cos(x-0.5);y3 = cos(x-1);
% Plot 3 curves, whose % points have the given x % and y coordinates plot(x,y1,x,y2,x,y3);xlabel('x');
legend('cos(x)','cos(x-0.5)','cos(x-1)');
ENGG1801 Engineering Computing
-8
-6
-4
-2
02468
-1-0.8-0.6-0.4
-0.200.20.40.60.81x
cos(x)cos(x-0.5)cos(x-1)
?plotyy(x1,y1,x2,y2)plots x1vs y1and
x2vs y2on the same figure
–y1 axis is on the left and y2 axis on the right
9
ENGG1801 Engineering Computing
10
ENGG1801 Engineering Computing
x = -2*pi:0.05:2*pi;y1 = cos(x);y2 = sin(x);
plotyy(x,y1,x,y2);8-1-0.500.5
1
-8
-6-4-20246
-1-0.500.5
1
8
-1-0.500.5
1
-8
-6-4-20246
-2000
200
400
600
x = -2*pi:0.05:2*pi;y1 = cos(x);
y2 = sin(x) + exp(x);plotyy(x,y1,x,y2);
?plot(x,y,s)plots with a certain line style, color and marker
–s is a string composed of symbols on next
slide (max 1 from each column)
–As usual, use the help command for more
details
11
ENGG1801 Engineering Computing
12
ENGG1801 Engineering Computing
color marker type
line style
b Blue .point -Solid g Green o circle :Dotted r Red x X-mark -.Dashdot
c Cyan +Plus --Dashe
d m Magenta *Star (none)
No line
y Yellow s Square k Black d Diamond w
White
v Triangle (down)^Triangle (up)
Hexagram
13
% Create arrays containing the x and y coordinates of % points, which we will plot in different ways below x = -2*pi:0.5:2*pi;y = cos(x);
plot(x,y,'m--'); plot(x,y,'b-*'); plot(x,y,'rp');
ENGG1801 Engineering Computing
-8
-6
-4
-2
2
4
6
8
-1-0.8-0.6-0.4-0.200.20.40.60.81-8
-6
-4
-2
2
4
6
8
-1-0.8-0.6-0.4-0.200.20.40.60.81-8
-6
-4
-2
2
4
6
8
-1-0.8-0.6-0.4-0.200.20.40.60.81
14
% Plot x vs cos(x)x = -2*pi:0.5:2*pi;plot(x,cos(x),'r-');
% We want the above graph % together with graph below,% so we use 'hold on' hold on;
% So now this wont replace % the previous graph plot(x,sin(x),'k:');
?Without hold on , Matlab erases the graph from the first plot()and replaces it with the graph from the second plot()
ENGG1801 Engineering Computing
-8
-6
-4
-2
2
4
6
8
-1-0.8-0.6-0.4-0.200.20.40.60.81
?Plotting functions automatically opens a new figure window if there is no existing figure window open on screen
?If a figure window exists, it is used for plot ?If multiple figure windows exist, the “current” figure window (last used or clicked) is used for the plot
15
ENGG1801 Engineering Computing
?An existing figure window can be made the current figure window with figure(n) where n is the number of the figure
?clf clears the current figure
?clf reset clears the current figure and resets all figure properties to their default
16
ENGG1801 Engineering Computing
% Clear the existing figure
clf;
% Create array of x values
x = -2*pi:0.05:2*pi;
% Create a figure with 2 rows and 3 columns of plots,
% and do the next plot in position % number 1
subplot(2,3,1);
plot(x, sin(x));
title('plot1: sin(x)');
% Next plot in position 2
subplot(2,3,2);
plot(x, cos(x));
title('plot 2:cos(x)');% Continued on next slide...
17
ENGG1801 Engineering Computing
% ... continued from previous slide
% Next plot in position 3
subplot(2,3,3);
plot(x, tan(x));
title('plot 3: tan(x)');
% Next plot in position 4
subplot(2,3,4);
plot(x, x.^2);
title('plot 4: x^2');
% Next plot in position 5
subplot(2,3,5);
plot(x, sqrt(x));
title('plot 5: sqrt(x)');
18
ENGG1801 Engineering Computing
% ... continued from previous slide
% Next plot in position 6
subplot(2,3,6);
plot(x, exp(x));
title('plot 6: e^x');
19
ENGG1801 Engineering Computing
20
ENGG1801 Engineering Computing
-10
010
-1-0.500.51plot1: sin(x)-10
010-1-0.500.51plot 2: cos(x)
-10
010
-400-2000200400plot 3: tan(x)
-10
10
010203040plot 4: x 2
-10
0100123plot 5: sqrt(x)
-10
010
0200400
600plot 6: e x
?subplot(r,c,i)puts many plots on 1 figure –r is the number of rows
–c is the number of columns
–i is the current graph number, which increase
in number as shown below
21
ENGG1801 Engineering Computing