2 Using the hold on hold off commands 10 8 6 4 2 2 4 6 8 10 3000 2000 1000 1000

# 2 using the hold on hold off commands 10 8 6 4 2 2 4

This preview shows page 103 - 115 out of 151 pages.

command. 2. Using the hold on , hold off commands. -10 -8 -6 -4 -2 0 2 4 6 8 10 -3000 -2000 -1000 0 1000 2000 3000 3x 3 9x 2 18x
1. USING THE plot() COMMAND Plots three graphs in the same plot: y versus x, y1 versus x1, and y2 versus x2. By default, MATLAB makes the curves in different colors. Additional curves can be added. The curves can have a specific style by adding specifiers after each pair, for example: >> plot(x,y,x1,y1,x2,y2) >>plot(x,y,’-b’,x1,y1,’—r’,x2,y2,’g:’)
EXAMPLE Plot the function 3x 3 and its first and second derivate for a range of x from -10 to 10. >> x=-10:1:10; % Range of x values >>y=3*x.^3; % Calculating the function 3x 3 >>dy=9*x.^2; % Calculating the first derivate 9x 2 for each x value >>dy2=18*x; % Calculating the second derivate >> plot (x,y,’r-’,x,y,’b:’,x,dy2,’g--’)
hold on Holds the current plot and all axis properties so that subsequent plot commands add to the existing plot. hold off Returns to the default mode whereby plot commands erase the previous plots and reset all axis properties before drawing new plots. 2. hold on and hold off COMMANDS TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT NOTE: This method is useful when all the information (vectors) used for the plotting is not available at the same time.
EXAMPLE Plot the function 3x3 and its first and second derivate for a range of x from -10 to 10. >> x=-10:1:10; % Range of x values >>y=3*x.^3; % Calculating the function 3x3 >> plot (x,y,’r-’) >>hold on >>dy=9*x.^2; % Calculating the first derivate >> plot (x,y,’b:’) >>dy2=18*x; % Calculating the second derivate >>plot (x,dy2,’g--’) >>hold off
FORMATTING PLOTS A plot can be formatted to have a required appearance. With formatting you can: Add title to the plot. Add labels to axes. Add legend. Change range of the axes.
FORMATTING COMMANDS 1] title(‘string’) Adds the string as a title at the top of the plot. >>x=-10:1:10; >>y=3*x.^3; >> plot (x,y); >> title (‘Title of the Plot’) 2] xlabel(‘string’) Adds the string as a label to the x -axis. >> xlabel (‘x range’)
FORMATTING COMMANDS 3] ylabel(‘string’) Adds the string as a label to the y -axis. >> ylabel (‘ y values’) 4] axis([xmin xmax ymin ymax]) Sets the minimum an maximum limits of the x - and y-axes.
FORMATTING COMMANDS 5] legend(‘string1’,’string2’,’string 3’…) For each line plotted, the legend shows a sample of the line type, marker symbol, and color beside the text label you specify. >> hold on >> dy=9*x.^2 >> plot (x,dy,’g--’) >>dy2=18*x; >>plot(x,dy2,’r:’) >>legend(‘y’, ’First derivative’, ’Second derivative’)
FORMATTING A PLOT IN THE FIGURE WINDOW Figures can be formatted interactively from the figure window Use the edit menu to edit 1] Axes properties 2] Figure properties 3] Current object properties Use the insert menu to interactively insert the 1]x label 2]y label 3] legend 4] textbox 5] colorbar
PLOTTING MULTIPLE FIGURES IN ONE PAGE subplot(m,n,p) divides the figure page into an m-by-n grid. Combined with the plot command it creates a plot in the grid position specified by p.

#### You've reached the end of your free preview.

Want to read all 151 pages?