Académique Documents
Professionnel Documents
Culture Documents
Lecture 4
1
Graphics
2
Graphics
3
EXAMPLE OF A 2-D PLOT
Plot title Legend
Light Intensity as a Function of Distance
1200
Theory
y axis Experiment
label 1000
Text
Tick-mark
800
INTENSITY (lux)
600
400
Data symbol
200
0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
x axis Tick-mark label
label
4
2D plot() COMMAND
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1]; 5
>> plot(x,y)
4
-1
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
6
Basic Plotting
7
Basic Plotting
>>x = 0:pi/100:2*pi;
>>y = sin(x);
1
>>plot(x,y) 0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
8
Specifying Line Styles and
Colors
9
Specifying Line Styles and
Colors
10
Specifying Line Styles and
Colors
11
Specifying Line Styles and
Colors
>> x1 = 0:pi/100:2*pi;
>>x2 = 0:pi/10:2*pi;
>>plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
1
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
12
FORMATTING PLOTS
There are two methods to format a plot:
1. Formatting commands.
In this method commands, that make changes or additions to
the plot, are entered after the plot() command. This can be
done in the Command Window, or as part of a program in a
script file.
13
FORMATTING COMMANDS
title(string)
Adds the string as a title at the top of the plot.
xlabel(string)
Adds the string as a label to the x-axis.
ylabel(string)
Adds the string as a label to the y-axis.
14
FORMATTING COMMANDS
legend(string1,string2,string3)
Creates a legend using the strings to label various curves (when
several curves are in one plot). The location of the legend is
specified by the mouse.
text(x,y,string)
Places the string (text) on the plot at coordinate x,y relative to
the plot axes.
gtext(string)
Places the string (text) on the plot. When the command
executes the figure window pops and the text location is clicked
with the mouse.
15
FORMATTING COMMANDS
MATLAB enables you to add axis labels and titles. For example,
using the graph from the previous example,
Now label the axes and add a title. The character \pi creates the
symbol . Plot of the Sine
1
>> xlabel('x = 0:2\pi')
0.8
>> ylabel('Sine of x')
0.6
>> title('Plot of the Sine')
0.4
0.2
Sine of x
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
x = 0:2
16
EXAMPLE OF A FORMATTED
PLOT
Below is a script file of the formatted light intensity plot (2nd
slide).
(Some of the formatting options were not covered in the lectures,
but are described in the book)
yd=[950 640 460 340 250 180 140]; Creating a vector with
light intensity from data.
plot(x,y,'-','LineWidth',1.0)
hold on
plot(xd,yd,'ro--','linewidth',1.0,'markersize',10)
hold off
17
EXAMPLE OF A FORMATTED
PLOT
Formatting of the light intensity plot (cont.)
18
EXAMPLE OF A FORMATTED
PLOT
19
FORMATTING A PLOT IN THE
FIGURE WINDOW
Once a figure window is open, the figure can be formatted interactively.
Use the insert menu to
20
THE fplot COMMAND
The fplot command can be used to plot a function
with the form: y = f(x)
fplot(function,limits)
The limits is a vector with the domain of x, and optionally with limits
of the y axis:
[xmin,xmax] or [xmin,xmax,ymin,ymax]
Line specifiers can be added.
21
THE fplot COMMAND
22
PLOT MULTIPLE GRAPHS IN
THE SAME PLOT
23
PLOT MULTIPLE GRAPHS IN
THE SAME PLOT
>>y2 = sin(x-.25);
>>y3 = sin(x-.5); 1
>>plot(x,y,x,y2,x,y3) 0.8
sin(x)
sin(x-.25)
sin(x-.5)
>>legend('sin(x)', 0.6
'sin(x-.25)','sin(x-.5)') 0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
24
PLOT MULTIPLE GRAPHS IN
THE SAME PLOT
plot(x,y,u,v,t,h)
plot(x,y,-b,u,v,r,t,h,g:)
25
PLOT MULTIPLE GRAPHS IN
THE SAME PLOT
Plot of the function, y 3x 3 26 x 10 and its first and
second derivatives, for 2 x 4 , all in the same plot.
x = [-2:0.01:4];
y = 3*x.^3-26*x+6;
2 x 4
yd = 9*x.^2-26;
ydd = 18*x;
plot(x,y,'-b',x,yd,'--r',x,ydd,':k')
26
PLOT MULTIPLE GRAPHS IN
THE SAME PLOT
120
100
80
60
40
20
-20
-40
-2 -1 0 1 2 3 4
27
PLOT MULTIPLE GRAPHS IN
THE SAME PLOT
hold on Holds the current plot and all axis properties so that
subsequent plot commands add to the existing plot.
28
PLOT MULTIPLE GRAPHS IN
THE SAME PLOT
Plot of the function, y 3 x 3
26 x 10 and its first and
second derivatives, for 2 x 4 all in the same plot.
x = [-2:0.01:4];
y = 3*x.^3-26*x+6;
yd = 9*x.^2-26;
ydd = 18*x;
plot(x,y,'-b')
hold on
plot(x,yd,'--r')
plot(x,ydd,':k')
hold off
29
Figure Windows
31
Multiple Plots in One Figure
>>x = 0:pi/100:2*pi;
>> y=sin(x);
>> y1=sin(2*x);
>> y2=cos(x);
>> y3=cos(2*x);
>> subplot(2,2,1);plot(x,y,'b*')
>> subplot(2,2,2);plot(x,y1,'k:+')
>> subplot(2,2,3);plot(x,y2,'r--')
>> subplot(2,2,4);plot(x,y2,'c-.s')
32
Multiple Plots in One Figure
33
Basic 3-D Graphics
34
Exercise