## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

http://jitkomut.lecturer.eng.chula.ac.th/matlab/graphic.html

Plotting graphs

Plotting graphs is a very common tool for illustrating results in science. MATLAB has many commmands that can be used for creating various kinds of plots.

Easy plots

The simplest buit-in function for plotting an explicit function is ezplot command. For example, we want to plot a parabola on the interval [-1,1]

**We can simply use the following command:
**

>> ezplot('x^2',[-1 1])

The first input of ezplot command is a string describing the function. The second input (which is optional) is the interval of interest in the graph. Note that the function description is not necessarily a function in x variable. We can use any variable we like:

>> ezplot('sin(b)',[-2 3])

**If the function definition involves some constant parameter, write it explicitly:
**

>> ezplot('x^2+2')

**In this case, if we assign a value 2 to a variable first, says y=2, when executing:
**

>> y = 2; >> ezplot('x^2+y')

MATLAB will interpret y as a variable (not a value of 2). Hence, it will create a contour plot of the function

Another related command is fplot. It is used to plot a function with the form specified limits.

between

Plot command

1 of 10

4/13/2014 2:22 AM

'linespec'.1:5.chula. The simplest form of the command is: plot(x.'PropertyName'. For example: >> x = 0:0. if any are desired. 2 of 10 4/13/2014 2:22 AM .th/matlab/graphic.y.y) where x and y are each a vector. >> plot(x. Specifier - The line color specifiers are: Line Color red green blue cyan magenta yellow black white Specifier r g b c m y k w The marker type specifiers are: Marker Type plus sign circle asterisk point Specifier + o * . The line style specifiers are: Line Style solid (default) dashed dotted dash-dot -: -. Both vector must have the same number of elements.Plotting graphs — MATLAB documentation http://jitkomut. The plot command has additional arguments that can be used to specify the color and style of the line and the color and type of markers. With these options the command has the form: plot(x.y) Once the plot command is executed. >> y = exp(-x).lecturer.ac. the figure Window opens and the plot is displayed The plot appears on the screen in blue which is the default line color.html The plot command is used to create a two-dimensional plot.PropertyValue) Line Specifiers (linespec) Line specifiers are optional and can ce used to define the style and color of the line and the type of markers (if markers are desired).eng.

lecturer. The Property Name is typed as a string.'--sb'.. all inside the plot command. b) For example.html Marker Type cross triangle (pointed up) triangle (pointed down) square diamond five-pointed star sixed-pointed star triangle (pointed left) triangle (pointed right) Specifier x ^ v s d p h < > The specifiers are typed inside the plot command as strings.g.th/matlab/graphic.and y-axes. plot(x.y..ac.. Property Name and PropertyValue Properties are optional and can be used to specify the thickness of the line. 3 of 10 4/13/2014 2:22 AM .8. For example: >> x = 0:5..y.. y = exp(-x).Plotting graphs — MATLAB documentation http://jitkomut. 5.'-or') This command creates a plot with solid red line and the marker is a circle. the command: >> x = linspace(0.'markersize'. Within the string. respectively after we have plotted a graph. the specifiers can be typed in any order and the specifiers are optional. Formatting a plot Plots can be formatted by using MATLAB command that follow the plot or fplot commands.*sin(8*x). The marker has green filling.. Here we will explain the the first method which is more useful since a formatted plot can be created automatically every time the program is executed.g. b) the color of the filling for filled markers (e. or interactively by using the plot editor in the Figure Window.3. The xlabel and ylabel command xlabel and ylabel create description on the x. Property Name linewidth markersize markeredgecolor markerfacecolor Description the width of the line (default 0. the size of the marker..pi. followed by a comma and a value for the property.50). r . >> plot(x.2.'linewidth'.) the size of the marker (e.^x.'markerfacecolor creates a plot with a blue dashed line and squares as markers. ) the color of the edge line for filled marker (e. y = 2. and the colors of the marker’s edge line and fill.chula.2. possible values are 1.eng. The linewidth is 2 points and the size of the square markers is 8 points. r.6.5.g..

legend('distance') creates a plot shown on the right. bold. ylabel.^2/2. ) For example. The legend shows a sample of the line type of each graph that is plotted. beside the line sample.PropertyValue) Some of the PropertyName are: Property Name Rotation FontSize Description the orientation of the text (in degree) the size of the font (in points) 4 of 10 4/13/2014 2:22 AM .Plotting graphs — MATLAB documentation http://jitkomut. The usage is: >> legend('string 1'. and color. s = t. The text command A text label can be placed in the plot with the text or gtext commands: >> text(x.. the command: >> >> >> >> >> >> t = linspace(0.ac. specified by the user. Formatting the texts The texts in the xlabel.. etc.100). style (italic.'text as string'.'text as string') >> gtext('text as string') The text command places the text in the figure such that the first character is positioned at the point with the coordinates x. xlabel('Time (sec)').lecturer.'PropertyName'. Formatting can be done by adding optional PropertyName and PropertyValue arguments following the string inside the command.y.).html The usages are: >> xlabel('text as string') and: >> ylabel('text as string') The title command A title can be added to the plot with the command: >> title('text as string') The text will appear on the top of the figure as a title. title. ylabel('distance (m)').s).y.chula.eng. text and legend commands can be formatted to customize the font. size. For example: >> text(x.. The legend command The legend command places a legend on the plot.'string 2'.th/matlab/graphic. The gtext command places the text at a position specified by the user (with the mouse). and places a label.10. title('Distance as a function of t'). plot(t.y (according to the axes of the figure).

ymax]) axis equal axis square axis tight Description sets the limits of both x and y sets the same scale for both axes sets the axes region to be square sets the axis limits to the range of the data The grid command grid on adds grid lines to the plot.lecturer. For example: >> fplot('x^2+4*sin(2*x)-1'. italic style.) Some formatting can also be done by adding modifiers inside the string. or \rm. and with normal font. grid off removes grid lines from the plot.18) Greek characters Greek characters can be included in the text by typing \ (back slash) before the name of the letter.eng.Plotting graphs — MATLAB documentation http://jitkomut. ymin. For example: >> title('\bf The values of X_{ij}'. A long superscript or subscript can be typed inside { } following the _ or the ^. b. 'FontSize'. A single character can be displayed as a subscript or a superscript by typing _ (the underscore character) or ^ in front of the character. normal.th/matlab/graphic. The axis command can be used to change the range of the axes.[-3 3]) >> grid on >> axis([-2 2 0 5]) 5 of 10 4/13/2014 2:22 AM . For example. bold) the color of the text (e. Character Letter \alpha \beta \gamma \theta \pi \sigma The axis command When the plot(x. Here are some possible forms of the axis command: Commands axis([xmin. rexpectively.html Property Name FontWeight Color Description the weight of the characters (light. r .ac.y) command is executed. \it. adding \bf. xmax. etc. respectively.. MATLAB creates axes with limits that are based on the minimum and maximum values of the elements of x and y.g. will create a text in bold font.chula.

A script file that creates The default colors of multiple graphs in MATLAB start from blue.y. Using the hold on. There are two methods to plot multiple graphs in one figure. hold on plot(x.20). y = 3*x.'second derivative'). Then the hold on command is typed.y. The loglog function plots both x and y data 6 of 10 4/13/2014 2:22 AM . and t vs s . This keeps the Figure Window with the first plot open. The semilogx function plots x data on logarithmic axes and y data on linear axes.'first derivative'.t.'--rx'.'-bo'. and green. the other is by using the hold on.'--r'). dy = 9*x.v.ddy. 2. hold off commands. One is by using the plot command. and its . For example.th/matlab/graphic.s.t) creates 3 graphs: y vs x.dy.eng. With the data from the previous example. all in the same plot. including the axis properties and the formatting.v. . For example: >> plot(x.y. hold off command To plot several graphs using the hold on.ac.ddy). It returns MATLAB to the default mode in which the plot command erases the previous plot and resets the axis properties. v vs u with a dashed red lines with cross signs.y.u. v vs u.dy.'g:s') plots y vs x with a solid blue line and circles.x. ddy = 18*x.x. ddy = 18*x. The vector of each pair must have the same length.s. It is also possible to add line specifiers following each pair. t vs s with a dotted green line and square markers. MATLAB automatically plots the graphs in different colors so that they can be identified. respectively. we can plot y and its derivatives by typing commands shown in the script: >> >> >> >> >> plot(x.':k').^3-26*x+10. plot(x. The hold off command stops this process.'-b').u.Plotting graphs — MATLAB documentation http://jitkomut.4. red. all in the same plot. and second derivatives .^2-26.chula. For example: >> plot(x. one graph is plotted first with the plot command. 1. hold off commands. plot(x. legend('y'.lecturer.html Plotting multiple graphs In may situations there is a need to make several graphs in the same plot. Additional graphs can be added with plot commands that are typed next. hold off Logarithmic Scales The semilogy function plots x data on linear axes and y data on logarithmic axes. Using the plot command Two or more graphs can be created in the same plot by typing pairs of vectors inside the plot command. we plot the function first derivative for these graphs can be written as: >> >> >> >> >> >> >> x = linspace(-2.

subplot(2. The subplots are arranged like elements in a mxn matrix where each element is a subplot. y = x.100). >> r = 3*cos(theta/2). The command subplot(m.3).^2.eng.3.html on logarithmic axes.1). subplot(2.2*pi. we create a plot that has 2 rows and 2 columns: >> >> >> >> subplot(2. The command has the form: >> polor(theta. To plot a function domain.2.ezplot('exp(-x)').chula. For example.'linespec') where theta and radius are vectors whose elements define the coordinates of the points to be plotted. For example. This means that the next plot command will create a plot in this subplot.ezplot('sin(x)/x').2).n.y) (The logspace creates a vector of 100 elements in log scale with the first element 10^0 and the last element 10^(3).y) loglog(x.2.lecturer.ac. The subplots are numbered from 1 through mn.4).n.ezplot('x^2'). The line specifiers are the same as in the plot command. For example. a vector for values of calculation. semilogy(x. a plot of the function with the corresponding values of in a certain is created first. subplot(2. and then a vector is created using element-wise is done by: >> theta = linspace(0.ezplot('sin(x)').radius. from the first row to the last.2.200).r) Multiple plots on the same window Multiple plots on the same page can be created with the subplot command. which has the form: >> subplot(m.p) The command divides the Figure Window into mxn rectangular subplots where plots will be created.) Polar plots The polar command is used to plot functions in polar coordinates. Multiple Figure Windows 7 of 10 4/13/2014 2:22 AM .p) makes the subplot p current.2. notice the difference between the two commands: >> >> >> >> x = logspace(0.th/matlab/graphic.^2+theta. The number increases from left to right within a row. >> polar(theta.Plotting graphs — MATLAB documentation http://jitkomut.

xlabel('Year').eng. The figure command can also have an input argument that is an integer figure(n) . Stem plots Function format: stem(x. 1.ac. sle = [8 12 20 22 18 24 27]. Rk = 0. Stairs plots Function format: stairs(x. 3. It is also possible to open additional Figure Window. Horizontal bar plots Function format: barh(x. The number corresponds to the number of a corresponding Figure Window. Vertical bar plots Function format: bar(x.y): >> >> >> >> yr = [1988:1994]. >> pie(grade) 6.5. >> stairs(k.Rk).Plotting graphs — MATLAB documentation http://jitkomut. Every time the command figure is entered.th/matlab/graphic.y): >> k=-5:5.'r'). >> stem(k. Several forms of the commmand are: close closes the active Figure Window close(n) closes the nth Figure Window close all closes all Figure Windows that are open Other types of plots (bar. If a Figure Window is already open and the plot command is executed. Pie plots Function format: pie(x) : >> grade=[11 18 26 9 5].html When the plot or any other commands that generates a plot is executed. Histograms 8 of 10 4/13/2014 2:22 AM .^abs(k). 2.cdf).chula. ylabel('Sales (Millions)'). >> cdf =cumsum(pmf). This is done by typing the command figure. 5. histogram) We illustrate various kinds of plots by examples. 4. a new plot will replace the existing plot. barh(yr.xlabel('Year').sle).y): >> k=1:5.lecturer. the Figure Window opens and displays the plot.sle.pmf = [1/4 1/4 1/4 1/8 1/8]. Figure Windows can be closed with the close command.y): >> >> >> >> yr = [1988:1994]. bar(yr.ylabel('Sales (Millions)'). sle = [8 12 20 22 18 24 27]. MATLAB opens a new Figure Window and after than we can enter the plot command to generate a plot in the last active Figure Window.

surf or contour to create the plot.100) term in 3-D plots Surface. as a function of time of a particle that moves along a straight line is given by Use subplot command to make the three plots of position. >> z = exp(-0. For example. we generate 10000 random numbers according to Gaussian distribution.chula. a user must create three equal-sized arrays x.5]. To plot data. says .y arrays required for these plots. and M is the number of bins.5*(x. and the vector z contains the function values associated with every point of x and y.Plotting graphs — MATLAB documentation http://jitkomut. Finally we call function mesh. Plot the histogram of the data and discuss the result. velocity.y. Then we evaluate the function values to plot at each of those (x. and acceleration on the same page.lecturer. MATLAB function meshgrid makes it easy to create the vector x. 2. mesh. The position in log scale with 200 points. Write a MATLAB script to plot the magnitude of where versus from 0 to in steps of . The height of the bar corresponds to the number of data points in the bin. >> hist(y. The vector x and y contain the x values and the y values. The simplest form of the command is: >> hist(y. Its histogram should look like a bell curve (due to the density function).z) Exercises 1.05:3).y) locations. For example. Write a MATLAB script to plot circular marker. >> mesh(x.th/matlab/graphic.1). 9 of 10 4/13/2014 2:22 AM . Generate 10000 random numbers according to a uniform distribution on the interval [-5. and contour plots are convenient ways to represent data that is a function of two independent variables.html Histograms are plots that show the distribution of data. The overall range of a given set of data points is divided to smaller subranges (bins).eng.y.ac. Plot the graph in log scale for both x and y axes. and z. and the histogram shows how many data points are in each bin. 4. we plot the 2-dimensional Gaussian density function This is done by typing: >> [x.y]=meshgrid(-3:0.^2)).^2+y. The points should be connected by a 2-pt red line and each point should be marked with a 6-pt wide blue for to 3. This is done by typing: >> y = randn(10000.M) where y is a vector with the data points.

Plotting graphs — MATLAB documentation http://jitkomut.lecturer.html 10 of 10 4/13/2014 2:22 AM .th/matlab/graphic.chula.eng.ac.

- DIPUM2E_Chapter03
- BACAMAR1
- SAP Material Number Ranges and Formating 20090806
- Stress
- quiz1 1
- 02 c Examples
- Fight
- module 10-graphs of functions
- MS Word Quiz 2003
- CalcI_AreaBetweenCurves
- Matlab Documentation
- Ch01 edit 1
- 3
- DIP - Lab 05 - Intesnsity Slicing and Equalizing
- PMR
- Rotation Derivation
- tobor man
- math final questions
- Essentials.pdf
- Fall2013 AlternateMidterm(With Answers)
- 1second Quarter Exam Grade 8 Sy 2013-2014
- tpgs.pdf
- Signals and Systems
- Business Calculus.pdf
- 4 Ext.for. prob. 4
- SemCad Tutorial 2009-01-29
- Discrete Mathematics
- Cod Ility
- ch06_3
- Pattern Stories Droujkova Moss 2010

- UT Dallas Syllabus for math2312.521.07u taught by William Scott (wms016100)
- UT Dallas Syllabus for math2v90.001.08s taught by Noureen Khan (nak033000)
- UT Dallas Syllabus for phys5301.501.11f taught by Paul Mac Alevey (paulmac)
- High School Mathematics Grade 10-12
- Tmp 7550
- tmp9817.tmp
- frbrich_wp01-8.pdf
- UT Dallas Syllabus for math2312.002.09s taught by (mxf091000)
- UT Dallas Syllabus for phys5302.001.09s taught by Paul Macalevey (paulmac)
- Comparative Analysis of Power System Stabilizer using Artificial Intelligence Techniques
- frbrich_wp90-11.pdf
- tmp3183.tmp
- tmp664
- UT Dallas Syllabus for math2312.002.11f taught by Manjula Foley (mxf091000)
- tmp5D76.tmp
- tmp28B2.tmp
- UT Dallas Syllabus for math2413.003.11s taught by Wieslaw Krawcewicz (wzk091000)
- tmp5DD5.tmp
- tmpF8BB
- UT Dallas Syllabus for math2312.001.09f taught by William Scott (wms016100)
- tmp2B1C.tmp
- UT Dallas Syllabus for math2312.701.11f taught by Manjula Foley (mxf091000)
- tmp82D3.tmp
- UT Dallas Syllabus for math1314.002 06f taught by Paul Stanford (phs031000)
- tmpAE76.tmp
- ON Fuzzy Linear Programming Technique Application
- Space Shuttle Trajectory Guide
- tmpD53B.tmp
- UT Dallas Syllabus for math3303.501 05f taught by Thomas Butts (tbutts)
- UT Dallas Syllabus for phys5401.501.08f taught by Paul Macalevey (paulmac)

- 2014 Spring Career Fair Brochure
- Pierre Alain BaraisM@RT 07
- Mechanical Requirements
- Cal1415 Gs Exp sda
- Sample Cover Letter
- cal1314-gs-s
- RS - Resume
- Scanning Electron Microscopy (SEM)
- Migration Certificate Instructions
- ME 6200 Course Objectives and Information
- cal1314-gs-s
- Fem to CAD Integration
- cal1314-gs-s
- - HomeworkSolution#9

Sign up to vote on this title

UsefulNot usefulClose Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Close Dialog## This title now requires a credit

Use one of your book credits to continue reading from where you left off, or restart the preview.

Loading