<?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="id31497041">



<name>Basic operations in MATLAB</name>
<metadata>
  <md:version>1.3</md:version>
  <md:created>2006/02/20 13:57:47 US/Central</md:created>
  <md:revised>2006/02/21 23:21:44.061 US/Central</md:revised>
  <md:authorlist>
      <md:author id="Anders">
      <md:firstname>Anders</md:firstname>
      
      <md:surname>Gjendemsjø</md:surname>
      <md:email>gjendems@NO-SPAM.iet.ntnu.no</md:email>
    </md:author>
      <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>Basic operations</md:keyword>
    <md:keyword>MATLAB</md:keyword>
  </md:keywordlist>

  <md:abstract>This module covers basic  operations in MATLAB.</md:abstract>
</metadata>
<content>

  <section id="id31833631">
    <name>Basic Operations on Numbers</name>
    <para id="id31833641">MATLAB has many
    arithmetic operations and functions built in. Most of them are
    straightforward to use. The <cnxn target="commonMatS">Table</cnxn> below lists some commonly used
    scalar operations; in this table, <code>x</code> and <code>y</code> are scalars.  (A scalar is a single number.)
    <table frame="all" id="commonMatS">
      <name>Common scalar mathematical operations in MATLAB</name>
      <tgroup cols="2" align="left" colsep="1" rowsep="1">
        <colspec colnum="2" colname="c2"/>
        <colspec colnum="4" colname="c4"/>
        <thead valign="top">
          <row>
            <entry align="center">Operation</entry>
            <entry align="center">MATLAB</entry>
          </row>
        </thead>
        <tbody valign="top">
          <row>
            <entry align="center"><m:math><m:apply><m:minus/><m:ci>x</m:ci><m:ci>y</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>x-y</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:plus/><m:ci>x</m:ci><m:ci>y</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>x+y</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:times/><m:ci>x</m:ci><m:ci>y</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>x*y</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:divide/><m:ci>x</m:ci><m:ci>y</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>x/y</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:power/><m:ci>x</m:ci><m:ci>y</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>x^y</code></entry>
          </row>
          <row> 
            <entry align="center"><m:math><m:apply><m:power/><m:ci>e</m:ci><m:ci>x</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>exp(x)</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:ci type="function">log10</m:ci><m:ci>x</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>log10(x)</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:ci type="function">ln</m:ci><m:ci>x</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>log(x)</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:ci type="function">log2</m:ci><m:ci>x</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>log2(x)</code></entry>
          </row>
        </tbody>
      </tgroup>
    </table>
    
    </para>
    
    <para id="id113">
    Expressions are formed from numbers,  variables,  and these operations. The operations have different precedences. The <code>^</code> operation has the highest precedence; <code>^</code> operations are evaluated before any other operations.  Multiplication and division have the next highest precedence, and addition and subtraction have the lowest precedence. Precedence is altered by parentheses; expressions within parenthesesare evaluated before expressions outside parentheses.
    </para>

    <example id="example119">
      <para id="example119p">
      The <cnxn target="example119t">Table</cnxn>  below shows several mathematical formulas, the corresponding  MATLAB expressions,  and the values that MATLAB would compute for the expressions.
    <table frame="all" id="example119t">
      <name>Example MATLAB Expressions</name>
      <tgroup cols="3" align="left" colsep="1" rowsep="1">
        <colspec colnum="2" colname="c2"/>
        <colspec colnum="4" colname="c4"/>
        <colspec colnum="6" colname="c6"/>
        <thead valign="top">
          <row>
            <entry align="center">formula</entry>
            <entry align="center">MATLAB Expression</entry>
            <entry align="center">Computed Value</entry>
          </row>
        </thead>
        <tbody valign="top">
          <row>
            <entry align="center">
              <m:math>
                <m:apply>
                  <m:plus/>
                    <m:apply><m:power/><m:cn>5</m:cn><m:cn>2</m:cn></m:apply>
                    <m:apply><m:power/><m:cn>4</m:cn><m:cn>2</m:cn></m:apply>
                </m:apply>
              </m:math>
            </entry>
            <entry align="center"><code>5^2+4^2</code></entry>
            <entry align="center"><code>41</code></entry>
          </row>
          <row>
            <entry align="center">
              <m:math>
                <m:apply>
                  <m:power/>
                    <m:apply><m:plus/><m:cn>5</m:cn><m:cn>4</m:cn></m:apply>
                    <m:cn>2</m:cn>
                </m:apply>
              </m:math>
            </entry>
            <entry align="center"><code>(5+4)^2</code></entry>
            <entry align="center"><code>81</code></entry>
          </row>
          <row>
            <entry align="center">
              <m:math>
                <m:apply>
                  <m:divide/>
                    <m:apply><m:plus/><m:cn>2</m:cn><m:cn>3</m:cn></m:apply>
                    <m:apply><m:minus/><m:cn>4</m:cn><m:cn>5</m:cn></m:apply>
                </m:apply>
              </m:math>
            </entry>
            <entry align="center"><code>(2 + 3)/(4 - 5)</code></entry>
            <entry align="center"><code>-5</code></entry>
          </row>
          <row>
            <entry align="center">
              <m:math>
                <m:apply>
                  <m:ci type="function">log10</m:ci>
                    <m:cn>100</m:cn>
                </m:apply>
              </m:math>
            </entry>
            <entry align="center"><code>log10(100)</code></entry>
            <entry align="center"><code>2</code></entry>
          </row>
          <row>
            <entry align="center">
              <m:math>
                <m:apply>
                  <m:ci type="function">ln</m:ci>
                  <m:apply> 
                          <m:times/>
                            <m:cn>4</m:cn>
                            <m:apply><m:plus/><m:cn>2</m:cn><m:cn>3</m:cn></m:apply>
                  </m:apply>
                </m:apply>
              </m:math>
            </entry>
            <entry align="center"><code>log(4*(2+3))</code></entry>
            <entry align="center"><code>2.9957</code></entry>
          </row>
        </tbody>
      </tgroup>
    </table>
      </para>
    </example>

  </section>
  <section id="id111">
    <name>Basic Operations on  Matrices</name>
    <para id="id112">
    In addition to scalars, MATLAB can operate on matrices. Some common matrix operations are shown in the <cnxn target="commonMatM">Table</cnxn> below; in this table, <code>M</code> and <code>N</code> are  matrices.
    <table frame="all" id="commonMatM">
      <name>Common matrix mathematical operations in MATLAB</name>
      <tgroup cols="2" align="left" colsep="1" rowsep="1">
        <colspec colnum="2" colname="c2"/>
        <colspec colnum="4" colname="c4"/>
        <thead valign="top">
          <row>
            <entry align="center">Operation</entry>
            <entry align="center">MATLAB</entry>
          </row>
        </thead>
        <tbody valign="top">
          <row>
            <entry align="center"><m:math><m:apply><m:times/><m:ci>M</m:ci><m:ci>N</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>M*N</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:power/><m:ci>M</m:ci><m:cn>-1</m:cn></m:apply></m:math></entry>
            <entry align="center"><code>inv(M)</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:power/><m:ci>M</m:ci><m:ci>T</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>M'</code></entry>
          </row>
          <row>
            <entry align="center">det(<m:math><m:ci>M</m:ci></m:math>)</entry>
            <entry align="center"><code>det(M)</code></entry>
          </row>
        </tbody>
      </tgroup>
    </table>
    </para>
    
    <para id="para120">
      MATLAB functions <code>length</code> and <code>size</code> are used to
      find the dimensions of vectors and matrices, respectively.
    </para>
    <para id="para129">
      MATLAB can perform an operation  on each element of a vector or matrix.  To perform an arithmetic operation on each element in a vector (or matrix), rather than on the
      vector (matrix) itself, then the operator should be preceded by
      ".", e.g <code>.*</code>, <code>.^</code> and <code>./</code>.
    </para>
    <example id="example4">
      <para id="example4p2">
        Let <m:math>
        <m:apply>
          <m:eq/>
          <m:ci>A</m:ci>
          <m:matrix>
            <m:matrixrow>
              <m:cn> 1 </m:cn>
              <m:cn> 1 </m:cn>
            </m:matrixrow>
            <m:matrixrow>
              <m:cn> 1 </m:cn>
              <m:cn> 1 </m:cn>
            </m:matrixrow>
          </m:matrix>
        </m:apply>
        </m:math>. 
        Then <code>A^2</code> will return
        <m:math>
          <m:apply>
            <m:eq/>
            <m:ci>AA</m:ci>
            <m:matrix>
              <m:matrixrow>
                <m:cn> 2 </m:cn>
                <m:cn> 2 </m:cn>
              </m:matrixrow>
              <m:matrixrow>
                <m:cn> 2 </m:cn>
                <m:cn> 2 </m:cn>
              </m:matrixrow>
            </m:matrix>
          </m:apply>
          </m:math>,
          while <code>A.^2</code> will return
          <m:math>
            <m:apply>
              <m:eq/>
              <m:matrix>
                <m:matrixrow>
                  <m:apply><m:power/><m:cn>1</m:cn><m:cn>2</m:cn></m:apply>
                  <m:apply><m:power/><m:cn>1</m:cn><m:cn>2</m:cn></m:apply>
                </m:matrixrow>
                <m:matrixrow>
                  <m:apply><m:power/><m:cn>1</m:cn><m:cn>2</m:cn></m:apply>
                  <m:apply><m:power/><m:cn>1</m:cn><m:cn>2</m:cn></m:apply>
                </m:matrixrow>
              </m:matrix>
              <m:matrix>
                <m:matrixrow>
                  <m:cn> 1 </m:cn>
                  <m:cn> 1 </m:cn>
                </m:matrixrow>
                <m:matrixrow>
                  <m:cn> 1 </m:cn>
                  <m:cn> 1 </m:cn>
                </m:matrixrow>
              </m:matrix>
              
            </m:apply>
            </m:math>.
      </para>
    </example>

    <example id="example5">
      <para id="example5p2">
        Given a vector x, compute a vector y having elements
        <m:math>
          <m:apply>
            <m:eq/>
            <m:apply><m:ci>y</m:ci><m:ci>n</m:ci></m:apply>
            <m:apply>
              <m:divide/>
              <m:cn>1</m:cn>
              <m:apply><m:sin/><m:apply><m:ci>x</m:ci><m:ci>n</m:ci></m:apply></m:apply>
            </m:apply>
          </m:apply>
          </m:math>.

          This can be easily be done in MATLAB by typing <code>y=1./sin(x)</code>
          Note that using <code>/</code> in place of <code>./</code> would result in the (common) error
          <code>Matrix dimensions must agree</code>.
      </para>
    </example>

  </section>


  <section id="id31827532">
    <name>Complex numbers</name>
    <para id="id31827542">MATLAB has excellent support for complex
    numbers with several built-in functions available. The imaginary
    unit is denoted by <code>i</code> or (as preferred in electrical engineering) <code>j</code>.
    To create complex variables 
    <m:math>
      <m:apply>
        <m:eq/>
        <m:ci><m:msub><m:mi>z</m:mi><m:mi>1</m:mi></m:msub></m:ci>
        <m:apply>
          <m:plus/>
          <m:cn>7</m:cn>
          <m:imaginaryi/>
        </m:apply> 
      </m:apply>
    </m:math>
    and

    <m:math>
      <m:apply>
        <m:eq/>
        <m:ci><m:msub><m:mi>z</m:mi><m:mi>2</m:mi></m:msub></m:ci>
        <m:apply>
          <m:times/>
          <m:cn>2</m:cn>
          <m:apply>
            <m:power/>
            <m:ci>e</m:ci>
            <m:apply>
              <m:times/>
              <m:imaginaryi/>
              <m:pi/>
            </m:apply>
          </m:apply> 
        </m:apply>
      </m:apply>
    </m:math>
    simply enter
    <code>z1 = 7 + j</code> and <code>z2 = 2*exp(j*pi)</code></para>

    <para id="id31827626">The <cnxn target="table2">Table</cnxn> below  gives an overview of the basic
    functions for manipulating complex numbers, where <m:math><m:ci>z</m:ci></m:math> is a complex number.</para>

    <table frame="all" id="complexNum">
      <name>Manipulating complex numbers in MATLAB</name>
      <tgroup cols="2" align="left" colsep="1" rowsep="1">
        <colspec colnum="2" colname="c2"/>
        <colspec colnum="4" colname="c4"/>
        <thead valign="top">
          <row>
            <entry align="center"/>
            <entry align="center">MATLAB</entry>
          </row>
        </thead>
        <tbody valign="top">
          <row>
            <entry align="center">Re(<m:math><m:ci>z</m:ci></m:math>)</entry>
            <entry align="center"><code>real(z)</code></entry>
          </row>
          <row>
            <entry align="center">Im(<m:math><m:ci>z</m:ci></m:math>)</entry>
            <entry align="center"><code>imag(z)</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:abs/><m:ci>z</m:ci></m:apply></m:math></entry>
            <entry align="center"><code>abs(z)</code></entry>
          </row>
          <row>
            <entry align="center">Angle(<m:math><m:ci>z</m:ci></m:math>)</entry>
            <entry align="center"><code>angle(z)</code></entry>
          </row>
          <row>
            <entry align="center"><m:math><m:apply><m:power/><m:ci>z</m:ci><m:cn>*</m:cn></m:apply></m:math></entry>
            <entry align="center"><code>conj(z)</code></entry>
          </row>
        </tbody>
      </tgroup>
    </table>

  </section>
  <section id="otherDet">
    <name>Other Useful Details</name>
    <para id="p1other">
      <list type="bulleted" id="id11814914"><item>A <emphasis>semicolon</emphasis> added at the end of a line tells MATLAB to suppress
        the command output to the display.</item>
        <item>MATLAB Version 7 is <emphasis>case sensitive</emphasis> for both variables and functions; for example, <code>b</code> and <code>B</code> are different variables and MATLAB will recognize the built-in function <code>sum</code> but not  <code>SUM</code>.  In previous versions, MATLAB was not case sensitive for function names.</item>
        <item>Often it is useful to <emphasis>split a statement</emphasis> over multiple lines. To split a
        statement across multiple lines, enter three periods <code>...</code> at the end of
        the line to indicate it continues on the next line.</item>
      </list>
    </para>
    <example id="exSplit">
      <para id="exSplitPara">Splitting 
      <m:math>
        <m:apply>
          <m:eq/>
          <m:ci>y</m:ci>
          <m:apply>
            <m:plus/>
            <m:ci>a</m:ci>
            <m:ci>b</m:ci>
            <m:ci>c</m:ci>
          </m:apply>
        </m:apply>              
        </m:math> over multiple lines.
        <code type="block">
  y = a...
  + b...
  c;</code>
      </para>
    </example>

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