Materials Files-628255 Lecture4Graphing
Materials Files-628255 Lecture4Graphing
Introduction to Computing
for Engineers and
Scientists
LECTURE 4: GRAPHING
Objectives
2
Making a plot
>> x = 1:10;
>> y = x.^2;
>> plot(x, y)
plot(x, y) takes the two
vectors x and y and plots the
coordinate pairs (x(1),
y(1)), (x(2), y(2)), … in
the figure window
3
An example graph
>> x = 0 : 1 : 10;
>> y = sin(x);
>> plot(x, y)
A smoother graph
The linspace(x, y, n)
function makes a list of n values
between x and y
Adding labels and
annotations
figure opens a new figure window, which all further plot commands
will draw inside
10
Multiple plots on the
same figure window
◦ Add another set of inputs for each additional graph
◦ plot(x1, y1, ‘linespec’, x2, y2, ‘linespec’, …)
◦ By default, this makes each plot a different color
The axis command changes the minimum and maximum values for
each of the axes. You can use this to zoom in on a specific part of a plot
x=linspace(-
10,10,200);
y=x.^3-3*x.^2-
28*x+60;
plot(x,y)
Modifying the plotted
range
x=linspace(-
10,10,200);
y=x.^3-3*x.^2-
28*x+60;
plot(x,y)
axis([-10,10,-
100,100])
Adding a legend
For example, if you have three sets of data, and you want the legend to be in the
bottom right of the graph, the command is
legend(‘Set 1’,‘Set 2’,‘Set 3’,‘Location’,‘SouthEast’)
Place options:
◦ North, East, South, West,
NorthEast, NorthWest, SouthEast, SouthWest
◦ Add Outside to put the legend outside the plot box
◦ Best looks for a location that won’t overlap the data
Example
17
Example
Cannon range
% Define the constants 1200
g = 9.8; 50 m/s
100 m/s
v1 = 50; 1000
v2 = 100;
% Define the angle vector
800
angle = 0 : 0.05 : pi/2;
Range (m)
% Calculate the range
600
R1 = v1^2/g*sin(2*angle);
R2 = v2^2/g*sin(2*angle);
400
% Plot the results
plot(angle,R1,angle,R2,':')
200
title('Cannon range')
xlabel('Cannon angle')
0
ylabel('Range (m)') 0 0.5 1 1.5 2
Cannon angle
legend('50 m/s', '100 m/s')
18
Subplots
c=3
subplot(r, c, n) divides
the current figure window into a n=1 n=2 n=3
grid of r rows and c columns, and
directs all the following plotting
commands to the nth sub- r=3 n=4 n=5 n=6
window
n=7 n=8 n=9
19
Subplots
x = -10:0.2:10;
y1 = x.^3;
y2 = abs(x);
y3 = floor(x);
y4 = 1./x;
subplot(2, 2, 1)
plot(x, y1, 'r')
subplot(2, 2, 2)
plot(x, y2, 'b')
subplot(2, 2, 3)
plot(x, y3, 'g')
subplot(2, 2, 4)
plot(x, y4, 'k')
20
Complex subplots
subplot(3, 3, [1,2,4,5])
plot(x, y1, 'r')
subplot(3, 3, [3,6])
plot(x, y2, 'b')
subplot(3, 3, [7:9])
plot(x, y3, 'g')
21
Polar plots
Example:
t=linspace(0,2*pi,200);
r=2*sin(2*t);
polar(t,r)
Bar and pie charts
x = [1, 2, 5, 4, 8];
y = [8, 4, 5, 2, 1];
subplot(2,2,1)
bar(x)
subplot(2,2,2)
bar([x; y])
subplot(2,2,3)
bar3([x;y])
subplot(2,2,4)
pie(x)
24
Three-dimensional plots
SPACE CURVES SURFACES
25
Space curves
Space curve
Types of 3D surfaces
mesh(x,y,z) surf(x,y,z)
contour(x,y,z) surfc(x,y,z)
28
Surfaces
A simple example
Customizing 3D plots
32
Colormaps for 3D plots
33
Publishing a script with
plots
34
Publishing a script with
plots
35
Next week
36