The MATLAB Environment

MATLAB is an interactive program for numerical computation and data visualisation. It was originally developed in FORTRAN as a MATrix LABoratory for solving numerical linear algebra problems. The original application may seem boring (except to linear algebra enthusiasts), but MATLAB has advanced to solve nonlinear problems and provide detailed graphics. It is easy to use, yet very powerful. A few short commands can accomplish the same results that required a major programming effort only a few years ago.

THE MATLAB WORKSPACE

The prompt for a user input is shown by the double arrow:
>>

MATLAB has en extensive on-line help facility. For example:

>> help exp

 EXP    Exponential.
        EXP(X) is the exponential of the elements of X, e to the X.
 
        See also LOG, LOG10, EXPM, ARITH, POW2.
By simply typing:
>> help
MATLAB will provide a list of commands that are available. If you do not know the exact command for the function you are after, another useful command is lookfor. This command works somewhat like an index. If you did not know the command for the exponential function was exp, you could type:
>> lookfor exponential
EXP     Exponential.
EXPM    Matrix exponential.
The command demo gives an overview of the available demo programs. These might be useful if you wish to know more about certain aspects of MATLAB.

Simple calculations

Simple calculations can be carried out in MATLAB. For example by typing

>> 2+2
MATLAB gives the (correct!) response:
ans = 
    4
However, most calculations are carried out using variables . This is illustrated below:
>> x = 2
>> z = x+x
The response is:
x =
    2
z = 
    4
Actually we can suppress MATLAB from echoing to the screen by adding a semicolon (;) after each line. For example
>> x = 2;
>> z = x+x
This time the response is:
z = 
    4

Managing you variables

You can view the variables currently in the workspace by typing:
>> who

Your variables are:

ans      x         z         
More detail about the size of the matrices can be obtained by typing:
>> whos
              Name        Size       Elements     Bytes    Density   Complex

               ans       1 by 1             1         8       Full      No 
                 x       1 by 1             1         8       Full      No 
                 z       1 by 1             1         8       Full      No 

Grand total is 3 elements using 24 bytes
Sometimes it is desirable to clear all the variables in a workspace. This is done simply by typing:
>> clear
More frequently you may wish to clear a particular variable, such as x :
>> clear x

Programming in MATLAB

Thus far we have shown the interactive features of MATLAB by entering one command at a time. Though possible it would be a very bad idea to carry out complex calculations by typing interactively at the MATLAB command prompt (What if you make a mistake? or want to do the calculation again tomorrow ?). Instead MATLAB code should be written in m-files and saved for later use.

There are two ways of generating your own MATLAB code: 1) script files and 2) function routines. Function routines will be considered later in this course. We will start by initially just considering script files:

Script files

A script file is simply a sequence of commands that could have been entered interactively. When the sequence is long, or must be performed a number of times it is much easier to generate a script file. The following example is a very simple program used to solve an equation. If we name the file myprog.m then it is run by simply typing myprog in the MATLAB command window.
% My first program
 x = 2.0;
 y = 3*sqrt(x) -7

Getting started

Get started on MATLAB by trying the exercise associated with this section.

Click HERE for next section.