<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
<name>Variables in M-file Environments</name>
<metadata>
  <md:version>1.3</md:version>
  <md:created>2006/02/10 11:11:37 US/Central</md:created>
  <md:revised>2006/08/03 19:11:24.276 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>LabVIEW MathScript</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Octave</md:keyword>
    <md:keyword>Variables</md:keyword>
  </md:keywordlist>

  <md:abstract>This module provides a brief introduction to the use of variables in m-file environments.</md:abstract>
</metadata>
<content>
<section id="id11633588">
<name>Variables</name>
<para id="id11557840">
A variable is a named storage location that can be set to a particular value which can be used in subsequent computations. For example, we store a value of 5 in the variable <code>a</code> with the statement <code>a=5</code>. This value remains in <code>a</code> until we store a different value (for example, using the command <code>a=100</code>) or we clear <code>a</code> using the command <code>clear a</code>. Once a variable is set to a particular value, we can get this value by using the variable name in an expression (e.g. <code>a/2</code>).
</para>

<example id="id1">
<para id="id2">
Suppose we wish to compute the circumference of a circle of diameter 5 units using the formula
<m:math>
  <m:apply>
    <m:eq/>
      <m:ci> c </m:ci>
      <m:apply>
        <m:times/>
          <m:ci> π </m:ci>
          <m:ci> d </m:ci>
      </m:apply>
  </m:apply>
</m:math>
. We could first set the variable <code>d</code> to a value of 5:
<code type="block">
&gt;&gt; d = 5

d =

    5.000

</code>
Then we could compute the circumference and assign its value to the variable
<code>c</code>:
<code type="block">
&gt;&gt; c = pi*d

c =

    15.708

</code>
In this command, the product of the value of <code>d</code>
(which is known because we earlier set it to 5) and the value of <code>pi</code> (which is a pre defined variable) is computed and the value of the product is stored in the variable <code>c</code>.
</para>
</example>

<para id="id3">
Variable names must begin with an upper- or lower-case letter. They may contain letters, digits, and underscores; they may not contain spaces or punctuation characters. Variable names are case sensitive, so <code>A</code> and <code>a</code> are different variables.
</para>
<exercise id="id4">
<problem id="id5">
<name>Valid variable names</name>
<para id="id6">
Which of the following are valid variable names?
<list id="id8" type="enumerated">
  <item><code>a</code></item>
  <item><code>B</code></item>
  <item><code>ecky_ecky_ecky_ecky_ptang_zoo_boing</code></item>
  <item><code>ecky ecky ecky ecky ptang zoo boing</code></item>
  <item><code>2nd</code></item>
  <item><code>John-Bigboote</code></item>
</list>
</para>
</problem>
<solution>
<list id="id9" type="enumerated">
  <item>Valid.</item>
  <item>Valid.</item>
  <item>Valid.</item>
  <item>Invalid, because the variable name contains spaces.</item>
  <item>Invalid, because the variable name  begins with a number.</item>
  <item>Invalid, because the variable name  contains a dash.</item>
</list>
</solution>
</exercise> 
<para id="id10">
There are several predefined variables. The most commonly used include
<list id="id11" type="bulleted">
  <item><code>ans</code> - the default variable in which computation results are stored.</item>
  <item><code>pi</code> - π.</item>
  <item><code>i</code> or <code>j</code> - 
<m:math>
  <m:apply>
    <m:root/>
      <m:cn> -1 </m:cn>
  </m:apply>
</m:math>
.</item>
</list>
Once assigned, variable names remain until they are reassigned or eliminated by the <code>clear</code> command.
</para>

<para id="id12">
Variables can contain several types of  numerical values. These types include the following:
<list id="id13" type="bulleted">
  <item>Scalar - a scalar is a single value (i.e. a number). <code>c</code> and <code>d</code> in <cnxn target="id1"/> are scalar variables.</item>
  <item>Vector - a vector is an ordered series of numbers.</item>
  <item>Matrices - a matrix is a rectangular array of numbers.  The ability to do computations on vectors and matrices gives MATLAB its name (MATrix LABoratory).  </item>
  <item>strings - variables may also contain strings of characters.</item>
</list>
</para>

</section>
</content>
</document>
