Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In the SMDm-file, insert a figure command before the second plot command:
- 45 end
- 46 figure
- 47 plot(t,y2,’LineWidth’,3,’LineStyle’,...
- 48 ’—’,’Color’,’Blue’);
- Save and run the m-file.
- There will be two graphs created in windows labeled Figure 1 and Figure 2.
- While this example showed how to create multiple graphs from an m-file, we
- want to show both sets of data on a single graph. For this, we will need to use the
- hold command. The hold oncommand keeps the current graph open so that sub-sequent plotting commands will add to the current graph. There is also a hold off
- command that closes the graph from further additions. The use of the hold com-mand alone (without the value in parentheses) toggles the command between
- hold onand hold off. To prevent ambiguity, it is recommended that onor offbe
- specified.
- Since we will have more than one curve on the graph, we will need a legend to
- distinguish them.
- In the SMDfile, replace the figure command with a hold oncommand. Also,
- add thelegend command as shown:
- 45 end
- 46 hold on
- 47 plot(t,y2,’LineWidth’,3,’LineStyle’,...
- 48 ’—’,’Color’,’Blue’);
- 49 legend(‘Damping Coefficient = 0.1’,...
- 50 ‘Damping Coefficient = 0.2’);
- Note that in the legend command, the two curves are identified in the order in
- which they are created.
- Save and run the file. Make any desired edits (such as the font sizes of the leg-end and numbers on the axes) to the graph with the Property Editor. Click and
- drag the legend to the desired location.
- 166 CHAPTER 5 Plotting Data
- The finished graph is shown in Figure 5.60.
- Another useful MATLAB plotting command is fplot. To use fplot, the function to
- be plotted must be written as a MATLAB function m-file. The general form of
- the fplot command is as follows:
- fplot(‘functionname’,[x
- lower
- x
- upper
- ])
- where x
- lower
- and x
- upper
- are the lower and upper limits of the function arguments.
- That is, x
- lower
- and x
- upper
- define the domain of the independent variable. The fplot
- command can be used to plot built-in MATLAB functions in addition to function
- files that you generate. For example, we can plot the sin function for values of
- zero to 2 π radians by entering this command:
- >> fplot(‘sin’,[0 2*pi])
- To plot the displacement of our spring-mass-damper system, we must first create
- a function file.
Add Comment
Please, Sign In to add comment