1
©2009 by L. Lagerstrom
Plotting Multiple Curves
• Two curves on the same plot
• Positioning the legend
• Specifying colors
• Multiple plots using 2D arrays
• One plot, 2 y axes
• Multiple figure windows
• Multiple plots in one window (subplots)
• Logarithmic plots
©2009 by L. Lagerstrom
Note re. Code and Display
•
In the code examples that follow, we will often have the results
displayed simply by leaving off the semi-colon at the end of
assignment statements. (The results will be displayed in a
simulated Command window.)
•
In addition, we will assume that the display format has been set
to “format bank” (i.e., results displayed to 2 decimal places),
simply to save space.
•
Finally, the display of the results will have blank lines in it for
readability, but the code that would create those blank lines
(e.g.,
disp(‘ ’)
) will not be shown in the code examples
(again, for readability’s sake).
Matlab code
Figure window display
©2009 by L. Lagerstrom
1. Two Curves on the Same Plot
%To put two curves on the same
%plot, first calculate the
%function's values.
x = 0:0.1:5;
%A set of x values
f1 = x.^2 - 3*x + 2; %Curve 1 values
f2 = 2*x.^2 + x - 3; %Curve 2 values
%Then list both x,y pairs as the
%parameters in the plot function
figure(1)
clf
plot(x,f1,x,f2)
title('Two Curves on One Plot')
xlabel('x')
ylabel('y')
grid
%For three curves, it would be
%plot(x,f1,x,f2,x,f3), and so on.
0
1
2
3
4
5
-10
0
10
20
30
40
50
60
Two Curves on One Plot
x
y
Matlab code
Figure window display
©2009 by L. Lagerstrom
1. Two Curves on the Same Plot, cont.
%The x values do not need to be
%the same for each curve
x1 = 0:0.1:5;
%A set of x values
x2 = 0.5:0.05:4.5; %Another set
f1 = x1.^2 - 3*x1 + 2; %Curve 1
f2 = 2*x2.^2 + x2 - 3; %Curve 2
%Do the plot (note also the addition
%of the legend function)
figure(1)
clf
plot(x1,f1,x2,f2)
title('Two Curves on One Plot')
xlabel('x')
ylabel('y')
legend('Curve 1','Curve 2')
grid
0
1
2
3
4
5
-10
0
10
20
30
40
50
Two Curves on One Plot
x
Curve 1
Curve 2