This preview shows pages 1–3. Sign up to view the full content.
This 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 04 Rutgers University, Spring 2010 Instructor Blase E. Ur 1 2D Plotting Matlab has a number of functions builtin that allow you to plot graphs, making it quite easy to create scientific reports using Matlab. The key idea is that Matlab uses vectors or matrices of numbers to specify which points to plot. 1.1 XY Line Plots XY Line Plots are the most basic types of plots. The key idea is that you must give Matlab two separate (and equal size) vectors specifying the x and y points separately. If you ask Matlab to plot([1 2 3],[5 6 7]) , then the points (1,5), (2,6), and (3,7) are plotted. With the plot command, always be sure it has two input vectors of equal size. The first vector is the set of all x points, and the second vector is the set of all y points. plot([ 1 2 3 4 5 ], [ 6 8 9 12 15 ]) 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 doesnt 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 , youre more likely to type the following set of commands. Note that you first create a vector of all the x points, using either the colon operator or the linspace function. You then perform mathematical operations on x to get y, usually with element by element (dot) operators like .*, ./, +, or  x = linspace(0,20,100); % 100 points between 0 and 20 y = x.^3; plot(x, y) 1.2 Function Plots Using the plot command to plot x 2 , you needed to create a vector of x points using linspace , and then calculate the y points using y = x. 2 . However, you can also create a function plot, which obviates creating those vectors. The fplot function takes a function (as a character string , in single quotes) and vector containing the range for the x points as its inputs. Note that were taking a bit of a shortcut in the way we specify the function; well discuss this in the later lecture on symbolic math. The most important 1 change is that you should not use dot operations when specifying functions in this way. fplot(sin(x),[2*pi,2*pi]) % note that sin(x) must be in single quotes % 2pi,2pi is the interval for x. 2 Tools for All Plotting Functions 2.1 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) right after you use the plot command. You must first create the graph, and then you can change the title. To add a label to the x axis, just type xlabel(My X Axis) , and similarly type ylabel(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 ....
View Full
Document
 Spring '08
 Finch

Click to edit the document details