Computing and Networking

Using MATLAB


MATLAB in UNIX

MATLAB is a high-performance interactive software package for scientific and engineering numeric computation. MATLAB integrates numerical analysis, matrix computation, signal processing, and graphics. This is a very brief sketch of some of the things that you can do in MATLAB. If you need more information, a manual is available in the reference area located in Room 141 of the Computer Center.

This document covers the following information:


Starting MATLAB

To start MATLAB simply type :


Matrix Operations

To create a matrix like the one below with a name of 'A':
A = Type in the following line:
If 'A' and 'B' are two nxn square matrices, the A/B results in: While A\B will give you: You can also compute the inverse of A by inv(A).

Addition and subtraction of matrices are defined whenever the matrices have the same dimensions:
Also, a scalar can be added or subtracted from all the element:
Multiplication and division of matrices are defined whenever the first operand has as many rows as the columns of the second operand. Also, a scalar can be multiplied or divided:


Complex Numbers and Matrices

Complex numbers are entered using the special functions "i" or "j". Complex numbers can be created by typing in:
There are at least two convenient ways to enter complex matrices. They are created by:
Complex numbers and matrices are allowed in all mathematical operations and functions in Matlab.

NOTE: Avoid any blank spaces, when entering a matrix like "1 + 5*i".


Graphing

There are many available types of graphs in MATLAB, such as linear graph, log graph, 3-D graph, and etc.

A linear plot of X versus Y can be created by enter the values of x and y, and execute plot:
The plot can be labeled, and has the grid turned on by:


M-Files

MATLAB is capable of executing sequences of commands that are stored in files, called M-files. M-files are created by using regular unix editors such as, gemacs and vi. They must have a file type of ".m".

There are two types of M-files:
  1. Scripts
    Scripts contain long sequences of commands (MATLAB statements). The variables in a script file operate globally on the data in the workspace.
    Example:
       % An M-file to calculate Fibonnaci numbers
       f= 1 1:; i=1;
       while f(i)+f(i+1)<100
        f(i+2)=f(i)+f(i+1);
        i=i+1;
       end
       plot(f)
    
    This script file is saved under the name " fibno.m ". To execute this file, simply type fibno at the MATLAB prompt.

  2. Functions
    A function differs from a script in that arguments may be passed, and variables inside the file are local to the function. A function file is recognized when the first line of an M-file contains the word "function ".
    Example:
       function y=mean(x)
       %MEAN Average or mean value. For vectors, MEAN(x)
       %returns the mean value.  For matrices, MEAN(x) is
       %a row vector containing the mean value of each column.
       m,n:=size(x);
       if m == 1
         m = n;    %Handle isolated row vector
       end
       y=sum(x)/m;
    
    This is saved as " mean.m ". The mean value function can be used by typing:
    where Z is a vector of the integers from 1 to 99:


Control Flows

FOR loops

MATLAB has its own version of the "DO" or "FOR" loop like other computer languages. MATLAB's for loop allows a statement, or group of statements, to be repeated a fixed number of times.

The general form of a for loop is:
           for v = expression
             statements
           end
WHILE loops

MATLAB's while loop allows a statement, or a group of statements to be repeated under control of a logical condition.

The general form of a while loop is:
           while expression
             statements
           end
IF and BREAK statements

The general form of an if statement is:
           if expression
             statements
           end
           if expression
             statements
           elseif expression
             statements
           else
             statements
           end
Here are some of the commands and functions that you can use with MATLAB:

User Commands

For More Information

Contact the Information Center at 646-1840


NMSU Home Directory Search Comments Help
Computing & Networking Library
Phonebook

Last Modified: Sunday January 13, 2008
webmaster@nmsu.edu