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 05 Rutgers University, Fall 2009 Instructor- Blase E. Ur 1 2-D Plotting 1.1 X-Y Line Plots Matlab has a number of functions built-in that allow you to plot graphs, making it quite easy to create scientific reports using Matlab. The key idea is that Matlab uses vectors of numbers to decide which points to plot. For instance, if you ask Matlab to plot([1 2 3],[5 6 7]) , then the points (1,5), (2,6), and (3,7) are plotted. When creating X-Y Plots, which are the most basic type in Matlab, you must remember how to in- put the points. You will use the plot command, which takes as input two 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. Note that xpoints and ypoints are just variable names. 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 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 the need to create those vectors. The fplot function takes a function (as a character string , in single quotes) 1 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. 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