Skip to main content
Logo image

Section 6.6 Plotting Multiple Curves

Frequently it is desirable to display more than one curve in a single plot. There are several ways in MATLAB to create such a figure. We will discuss three methods in this section:

Subsection 6.6.1 Method 1: Plotting Pairs of Vectors

The plot command accepts more than one pair of vectors.
>> x = -2:0.01:4;
>> y = 3.5.^(-0.5*x).*cos(6*x);
>> u = x;
>> v = y+0.5;
>> t = x;
>> h = y-0.5;
>> plot(x,y, u,v, t,h)
This creates a single plot with curves of y vs. x, v vs. u, and h vs. t. Each curve is given a separate color to distinguish it.
Additionally, line specifiers for each curve are possible:
>> plot(x,y,'b', u,v,'--r', t,h,'m:', 'linewidth',1.5)

Subsection 6.6.2 Method 2: Using the hold on Command

The hold on and hold off commands allow control over whether the Figure Window is erased or not.
The standard usage is to first issue a plot command, then the hold on command, several more plot commands, and finally the hold off command.
The effect is that the first plot command sets up the axes and displays the first curve, the hold on command ensures that the rest of the plot commands go to the current figure until the hold off command is issued.
>> x = 0:pi/24:3*pi;
>> y1 = cos(x);
>> y2 = sin(x);
>> y3 = sin(x).*cos(x);
>> plot(x,y1)
>> hold on
>> plot(x,y2, 'r')
>> plot(x,y3, '-go')
>> hold off

Subsection 6.6.3 Method 3: Using the line() Command

You can also use the line() command, which adds a curve to the existing plot.
The difference between line() and plot() is that plot starts a new figure while line does not.
>> x = 0:pi/24:3*pi;
>> y1 = cos(x);
>> y2 = sin(x);
>> y3 = sin(x).*cos(x);
>> plot(x,y1)
>> line(x,y2,'color','r')
>> line(x,y3,'color','g', 'linestyle','-', 'marker','o')
Must use PropertyName and PropertyValue

Activity 6.1.

Plot the function
\begin{equation*} f(x) = \frac{5\,\sin{(x)}}{x + e^{-0.75x}} - \frac{3}{5}x \end{equation*}
for x between -5 and 10 using a red solid line of linewidth 2. Can you find the coordinates of the maximum that occurs around x = 1? If so, mark this maximum on your plot with a green filled-in diamond. Be sure to make this diamond large enough to be easily seen.
Please paste your code into the textbox.