This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 14:440:127– Introduction to Computers for Engineers Notes for Lecture 4 Rutgers University, Fall 2008 Instructor Blase E. Ur 0.1 2D Plotting Matlab has quite a number of functions builtin that allow you to plot graphs, making it quite easy to create scientific reports using Matlab. 0.1.1 Creating XY Plots When creating XY Plots, which are the most basic type in Matlab, the key is to realize how to input the points. You will use the plot command, which takes as input two vectors. The first vector is the set of all x points, and the second vector is the set of all y points. xpoints = [ 1 2 3 4 5 ]; ypoints = [ 6 8 9 12 15 ]; plot(xpoints, ypoints) The code above will plot the points (1,6), (2,8), (3,9), (4,12), and (5,15), and connect the points with a line. The following graph is displayed: Of course, it often doesn’t make sense to type in all of the x points and all of the y points by hand. Thus, if you wanted to plot y = x 3 , you’re more likely to type the following set of commands. Note that you first create a vector of all the x points, usually using either the colon operator or the linspace function. You then perform mathematical operations on x to get y, usually using dot multiplication when you want to multiply. x = linspace(0,20,100); y = x.^3; plot(x, y) 0.1.2 Titles, Labels, Grids To start making your graph look nicer, you can add things like titles and labels for the axes. To add a title to your graph, simply type title(’My First Graph’) write after you use the plot command. To add a label to the x axis, just type xlabel(’My X Axis’) , and similarly type xlabel(’My Y Axis’) for the y axis. For the title and labels, be sure to put the descriptions in single quotes since they are character strings. To add grid lines to your plot, type grid on . By default, the grid is off. After turning the grid on, you can turn it off again using the command grid off . Here is a set of commands that plots a graph with some extra embellishments, followed by the final product: x = linspace(0,20,100); y = x.^3; plot(xpoints, ypoints) title(’X Cubed’) xlabel(’X Points’) ylabel(’Y Points’) grid on 0.1.3 Changing Line Colors Like a MySpace Kid You can also change the color of the line in the graph, or even whether the line is solid, dashed, etc., using an extra argument (input) to the plot function. Rather than just typing plot(x,y) , you can type something like plot(x,y,’–dg’) . This command specifies that the lines in the plot should be dashed (–), that all of the points in the plot should be highlighted as diamonds (d), and that the line should be green (g), although the black and white lecture notes won’t show the true glory of that decision. Here’s what that looks like: x = linspace(0,20,100); y = x.^3; plot(xpoints, ypoints,’dg’) 0.1.4 Multiple Graphs in Multiple Windows If you run a plot command in Matlab, and then run another plot command a few minutes later, the first plot will be overwritten and replaced by the second plot. However, if you instead wouldthe first plot will be overwritten and replaced by the second plot....
View
Full
Document
This note was uploaded on 11/09/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Fall '08 term at Rutgers.
 Fall '08
 Finch

Click to edit the document details