Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 3

Task 3.4 Detail: Put several lines on a graph

Summary of new tools and commands.

Task: Use a script to create a vector called time ranging from 0 to 10 in steps of 0.1. Create the following two vectors: y1=exp(.1*time).*cos(time).^2 and y2=3*exp(-.1*time).*cos(time).^2. Plot these two vectors on a three different plots using each of the three methods below.

There is often a need to put more than one line on a graph. There are several ways to do this. The most direct is to use the plot command with one triplet of arguments for each line on the plot. For example, the following command puts 3 lines on a plot along with asterisks on one of the lines.

plot(x1,y1,'b-',x2,y2,'r-',x3,y3,'k--',x3,y3,'*')
This command will create blue and red lines, a black dashed line with asterisks showing the data points on the dashed line. The plot command will scale the plot based on all of the input vectors.

It is not necessary that the different sets of input vectors (x,y) have the same number of points; although x1 must be the same size as y1, and so forth.

plot will produce a reasonable plot as long as the various x and y vectors have similar values. To create a plot with two lines with very different values, use plotyy(x1,y1,x2,y2) which creates a plot with two lines (each in a different color) with two different y scales. The y1 plot has its scale at the left edge of the plot while the y2 plot has its scale on the right. The y axes have the same colors as the corresponding lines.

It is possible to control the colors and line style for plotyy, but it is a bit complicated because it uses handle graphics which we will find out about in a while.

There is another general way to add lines to a plot which uses hold which freezes the scale of a plot. The procedure is

plot(x1,y1,'b-')
hold on
plot(x2,y2,'r-')
plot(x3,y3,'k--')
plot(x3,y3,'*')
hold off
The first plot command creates the graph and scales. Then the command hold on preserves the graph and allows an additional line to be added to the graph. The hold off at the end stops preserving the graph.

The hold on command in earlier versions of MATLAB would also freeze the scale of the graph based on information in the first call to plot. Current versions of MATLAB adjust the plot scale as new lines are added with different ranges of values.

If the hold on command is not used, then each use of plot will erase the existing plot and create a new plot.

Different versions of MATLAB have slightly different behaviors for hold. In earlier versions, the scales of the plot were set by the first call to plot. That meant that it had to have the correct scales for all of the lines to be added. More recent versions of MATLAB rescale the plots as new lines are added. You can always control the plot scales with xlim and ylim.

Script to complete this task:

%  task34.m
  time=0:.1:10;
  y1=exp(.1*time).*cos(time).^2;
  y2=3*exp(-.1*time).*cos(time).^2;
  figure
   plot(time,y1,'-r',time,y2,'-b')
   title('2 Bumpy Funcs')
   xlabel('time')
   ylabel('Funcs')
  figure
   plot(time,y1,'-r')
   title('2 Bumpy Funcs')
   xlabel('time')
   ylabel('Funcs')
   hold on
    plot(time,y2,'-b')
   hold off
  figure
   plotyy(time,y1,time,y2)
   title('2 Bumpy Funcs')
   xlabel('time')
   ylabel('Funcs')

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 3


email: J. Klinck