Much of this material is based on the Introduction to Matlab pages on the University of Queensland website, by Graeme Chandler.


Basic Scalar Arithmetic
The simplest way to start with Matlab is to use it for simple calculations. Matlab has a wide range of functions and is able to use complex numbers as well as reals. The following simple commands have obvious meanings. Type them in and see what happens. (In the examples below, all the text after the  % is just a comment or an explanation. You do not have to type it in. It would just be ignored by Matlab.)

(-1+2+3)*5 - 2/3        % The four arithmetic operations

2^3                     % Means 2 to the power 3

exp( sin( pi/2 ) )      % The usual functions are provided
                        %  log, log10, cos, tan, asin, ...
                        %   Note Matlab always uses radians.

Basic Matrix Arithmetic

A = rand(3,4) 	% A 3x4 matrix filled with random numbers
x = rand(4,1)
b = A*x		% This will give you a 3x1 matrix, normal matrix multiplication.
Usually, working with data means we want to mutliply two matrices (for example, multiplying density and temperature to find pressure), but we would like to multiply ELEMENT by ELEMENT. To do this, use the '.' operator.
A = rand(3,4) 	% A 3x4 matrix filled with random numbers
x = rand(3,4)
b = A.*x	% This will give you a 3x4 matrix, element multiplication.

Roundoff Error
Like a calculator, Matlab does all calculations correct only to about 16 significant decimal digits. This is enough accuracy for most purposes. For convenience, usually only the first 5 significant digits are displayed on the screen. Some more examples.

pi
22/7                  % pi is not the same as 22/7!
11*(15/11) - 15       % This shows there is roundoff error
                      %   when Matlab uses fractions.

Functions
Often we need to work with functions. For example, we may want to find the minimum of a function, its zeros, or the definite integral of the function. This section explains how to create functions and work with them in Matlab.

To set up functions in Matlab, we need to create a file containing the Matlab code that evaluates the function. These files are called function files. The name of the file must always end with the `.m' extension (for example fred.m, funct.m, etc.). Suppose we need to work with the function
f(x) = 1
(x-.3)2+.01
+ 1
(x-.9)2+.04
-6.
As this function has been called `f', we will create the file f.m using the Matlab editor. To open the editor press the new file icon (the left icon above the command window). (Note: It is possible to use your favourite editor not just the inbuilt Matlab editor. I like 'vi'.) Once in the editor, type the following lines into the file.

                         % As the function is called `f'
                         % put this code in the file `f.m'

function  y  =  f( x ) 
%                      
%  This  is the  test function.
%
   term1   =  1 ./( (x-.3).^2 + .01 )  ;
   term2   =  1 ./( (x-.9).^2 + .04 ) ;
   y = term1 + term2 - 6  ;
 

After the lines have been typed in, save the file (by pressing the disk icon). You will be asked for a name; in this case use f.m. Then return to the command window.

If the code has been typed into the file correctly, we should be able to use f the same way we use standard functions like sin and cos etc. For instance, can evaluate f(.3), f(.9), and plot f.

f(.3)        %  Check the code gives the correct answers
f(.9)        %      at   two test points.
 
x = (-1 : .05 : 2)';       % Plot  f  between  -1  and  2 .
plot( x , f(x) ) ;

To guard against errors in the code we should check the answers. Pencil and paper shows  
f(.3) = 1
02+.01
+ 1
(-.6)2+.04
-6 = 100+ 1
.4
-6 = 96.5 ;
and similarly f(.9) = 21.7027. The Matlab answers should be the same. If these commands do not work or give wrong answers, there is probably a typing error. If so, go back into the file and correct the error. Save the file then try to use the function in Matlab again.

Sometimes Matlab does not notice the changes to the code and continues to report errors after we have corrected them. This is usually because we have forgotten to save the file to disk. Annother common cause is that we are editing a file in one directory and Matlab is reading the file in another directory. But occasionally this is due to a Matlab installation bug. To correct this bug, after changing code give the command

clear functions

Try it yourself
Write a function that will take arrays of density and temperature and calculate pressure. Some test variables:

rho(1:3) = 1.226
T = [280 288 300]
With the above values, and R = 287, pressure = [985.2 1013.4 1055.6]*1e2.