# Learning by examples

tutorial,example,tips,maple,matlab,C++,visual C++,java,C#,php,mysql,database,programming,computer

## Tags

Posted on Monday, May 24, 2010 at 2:37 PM

# Matlab Tutorial 4 - Plotting

Matlab Tutorial We end our discussion on the basic features of MATLAB by introducing the commands for data visualization (i.e. plotting). By typing <code>help plot</code> you can see the various capabilities of this main command for two-dimensional plotting, some of which will be illustrated below.

If <code>x</code> and <code>y</code> are two vectors of the same length then <code>plot(x,y)</code> plots <code>x</code> versus <code>y</code>.<!--more-->

For example, to obtain the graph of y = cos(x) from - π to π, we can first define the vector x with components equally spaced numbers between - π and π, with increment, say 0.01. <pre name="code" class="js">&gt;&gt; x=-pi:0.01:pi;</pre> We placed a semicolon at the end of the input line to avoid seeing the (long) output. Note that the smallest the increment, the "smoother" the curve will be. Next, we define the vector y <pre name="code" class="js">&gt;&gt; y=cos(x);</pre> (using a semicolon again) and we ask for the plot <pre name="code" class="js">&gt;&gt; plot(x,y)</pre> At this point a new window will open on our desktop in which the graph (as seen below) will appear.

<img class="alignnone" title="Picture 1" src="http://i818.photobucket.com/albums/zz102/imgstores/LearnByExamples/matlab-tutorial-4-plotting_1.jpg" alt="" width="561" height="420" />

It is good practice to label the axis on a graph and if applicable indicate what each axis represents. This can be done with the <code>xlabel</code> and <code>ylabel</code> commands. <pre name="code" class="js">&gt;&gt; xlabel('x') &gt;&gt; ylabel('y=cos(x)')</pre> Inside parentheses, and enclosed within single quotes, we type the text that we wish to be displayed along the <code>x</code> and <code>y</code> axis, respectively.

We could even put a title on top using <pre name="code" class="js">&gt;&gt; title('Graph of cosine from - \pi to \pi')</pre> as long as we remember to enclose the text in parentheses within single quotes. The back-slash ( \ ) in front of pi allows the user to take advantage of LaTeX commands. If you are not familiar with the mathematical typesetting software LaTeX (and its commands), ignore the present commend and simply type <pre name="code" class="js">&gt;&gt; title('Graph of cosine from - pi to pi')</pre> Both graphs are shown below. These commands can be invoked even after the plot window has been opened and MATLAB will make all the necessary adjustments to the display. <table> <tr> <td><img src="http://i818.photobucket.com/albums/zz102/imgstores/LearnByExamples/matlab-tutorial-4-plotting_2.jpg" width="280" height="210"/> </td> <td><img src="http://i818.photobucket.com/albums/zz102/imgstores/LearnByExamples/matlab-tutorial-4-plotting_3.jpg" width="280" height="210"/></td> </tr> </table> Various line types, plot symbols and colors can be used. If these are not specified (as in the case above) MATLAB will assign (and cycle through) the default ones as given in the table below. <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td width="36" valign="top">y</td> <td width="124" valign="top">yellow</td> <td width="67" valign="top">.</td> <td width="92" valign="top">point</td> </tr> <tr> <td width="36" valign="top">m</td> <td width="124" valign="top">magenta</td> <td width="67" valign="top">o</td> <td width="92" valign="top">circle</td> </tr> <tr> <td width="36" valign="top">c</td> <td width="124" valign="top">cyan</td> <td width="67" valign="top">x</td> <td width="92" valign="top">x—mark</td> </tr> <tr> <td width="36" valign="top">r</td> <td width="124" valign="top">red</td> <td width="67" valign="top">+</td> <td width="92" valign="top">plus</td> </tr> <tr> <td width="36" valign="top">g</td> <td width="124" valign="top">green</td> <td width="67" valign="top">-</td> <td width="92" valign="top">solid</td> </tr> <tr> <td width="36" valign="top">b</td> <td width="124" valign="top">blue</td> <td width="67" valign="top">*</td> <td width="92" valign="top">star</td> </tr> <tr> <td width="36" valign="top">w</td> <td width="124" valign="top">white</td> <td width="67" valign="top">:</td> <td width="92" valign="top">dotted</td> </tr> <tr> <td width="36" valign="top">k</td> <td width="124" valign="top">black</td> <td width="67" valign="top"><sub>—.</sub></td> <td width="92" valign="top">dashdot</td> </tr> <tr> <td width="36" valign="top"></td> <td width="124" valign="top"></td> <td width="67" valign="top"><sub>--</sub></td> <td width="92" valign="top">dashed</td> </tr> </tbody> </table> So, to obtain the same graph but in <code>green</code>, we type <pre name="code" class="js">&gt;&gt; plot(x,y,'g')</pre> where the third argument indicating the color, appears within single quotes. We could get a <code>dashed </code>line instead of a <code>solid </code>one by typing <pre name="code" class="js">&gt;&gt; plot(x,y,'--')</pre> or even a combination of line type and color, say a <code>blue dotted</code> line by typing <pre name="code" class="js">&gt;&gt; plot(x,y,'b:')</pre> Multiple curves can appear on the same graph. If for example we define another vector <pre name="code" class="js">&gt;&gt; z=sin(x);</pre> we can get both graphs on the same axis, distinguished by their line type, using <pre name="code" class="js">&gt;&gt; plot(x,y,'r--',x,z,'b:')</pre> The resulting graph can be seen below, with the <code>red dashed</code> line representing <code>y = cos(x)</code> and the <code>blue dotted</code> line representing <code>z = sin(x)</code>. <img src="http://i818.photobucket.com/albums/zz102/imgstores/LearnByExamples/matlab-tutorial-4-plotting_4.jpg"/>

When multiple curves appear on the same axis, it is a good idea to create a <code>legend </code>to label and distinguish them. The command <code>legend </code>does exactly this. <pre name="code" class="js">legend('cos(x)','sin(x)')</pre> The text that appears within single quotes as input to this command, represents the legend labels. We must be consistent with the ordering of the two curves, so since in the <code>plot </code>command we asked for <code>cosine </code>to be plotted before <code>sine</code>, we must do the same here. <img src="http://i818.photobucket.com/albums/zz102/imgstores/LearnByExamples/matlab-tutorial-4-plotting_5.jpg"/> At any point during a MATLAB session, you can obtain a hard copy of the current plot by either issuing the command <code>print </code>at the MATLAB prompt, or by using the command menus on the plot window. In addition, MATLAB plots can by <code>copied </code>and <code>pasted </code>(as pictures) in your favorite word processor (such as Microsoft Word). This can be achieved using the Edit menu on the figure window. Another nice feature that can be used in conjunction with <code>plot </code>is the command <code>grid</code>, which places grid lines to the current axis (just like you have on graphing paper). Type <code>help grid</code> for more information.

Other commands for data visualization that exist in MATLAB include <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td width="76" valign="top"><sup>subplot</sup></td> <td width="340" valign="top">create an array of (tiled) plots in the same window</td> </tr> <tr> <td width="76" valign="top"><sup>loglog</sup></td> <td width="340" valign="top">plot using log-log scales</td> </tr> <tr> <td width="76" valign="top"><sup>semilogx</sup></td> <td width="340" valign="top">plot using log scale on the x-axis</td> </tr> <tr> <td width="76" valign="top"><sup>semilogy</sup></td> <td width="340" valign="top">plot using log scale on the y-axis</td> </tr> <tr> <td width="76" valign="top"><sup>surf</sup></td> <td width="340" valign="top">3-D shaded surface graph</td> </tr> <tr> <td width="76" valign="top"><sup>surfl</sup></td> <td width="340" valign="top">3-D shaded surface graph with lighting</td> </tr> <tr> <td width="76" valign="top">mesh</td> <td width="340" valign="top">3-D mesh surface</td> </tr> <tr> <td width="76" valign="top"><sup>subplot</sup></td> <td width="340" valign="top">create an array of (tiled) plots in the same window</td> </tr> </tbody> </table> It is left to the reader to further investigate the above commands through MATLAB's <code>help </code>command. We illustrate here how to obtain one of the surface pictures on the cover of this guide: <pre name="code" class="js"> >> [x,y] = meshgrid(-3:.1:3,-3:.1:3); >> z = 3*(1-x).^2.*exp(-(x.^2)-(y+1).^2)... - 10*(x/5-x.^3-y.^5).*exp(-x.^2-y.^2)... - 1/3*exp(-(x+1).^2-y.^2); >> surf(z) >> xlabel('x') >> ylabel('y') >> zlabel('z') >> title('Peaks') </pre> <img src="http://i818.photobucket.com/albums/zz102/imgstores/LearnByExamples/matlab-tutorial-4-plotting_6.jpg"> Type <code>help meshgrid</code>, <code>help surf</code> and <code>help peaks</code> for more information on the above surface.

<strong>Catch up</strong> <a href="http://learnbyexamples.org/matlab/matlab-tutorial-1-the-basic-features.html">Matlab Tutorial 1 – The basic features</a> <a href="http://learnbyexamples.org/matlab/matlab-tutorial-2-vectors-and-matrices.html">Matlab Tutorial 2 – Vectors and matrices</a> <a href="http://learnbyexamples.org/matlab/matlab-tutorial-3-built-in-functions.html">Matlab Tutorial 3 – Built-in functions</a> Matlab Tutorial
Bookmark:   Share: By Email

0 comments on "Matlab Tutorial 4 - Plotting"

Subject:

Comment:

Because you do not have or are not logged in to your Programmer's Heaven account, please enter your name.

Name:

To help prevent comment SPAM, please enter the magic code '369' in the box:

Posting Rules
• Do not post anything that is racist, hate speech or of a sexual or adult nature.
• Do not post or link to anything that infringes copyrighted laws.
• Posting about security or legal topics is fine so long as you are not glorifying or encouraging people to perform illegal activities.
• Both the author of this blog and the Programmer's Heaven administrators may delete any inappropriate comments without notice at their own discretion.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic