====================================================
Getting Started with Matlab
===========================
(Peter Lynch, UCD Meteorology & Climate Centre)
====================================================
Introduction
============
These are basic notes to help you get started.
The best way to learn MATLAB is to start using it.
How to begin:
(1) Double-click on the MATLAB icon.
OR
(2)
Type "matlab &" at prompt
How to stop:
>> quit or >> exit (where >> is the prompt).
At the beginning, enter
>> helpdesk
This will open a window with extensive documentation.
Click on 'Getting Started' for an outline description.
To see more MATLAB examples, select
Examples and Demos
under the menu, or type
>> demo
at the MATLAB prompt. From the menu displayed, run
the demos that interest you, and follow the
instructions on the screen.
There are two ways to use MATLAB:
(1) Interactively, where you type commands for
immediate execution, or
(2) M-files, containing sets of commands.
We start with the interactive use.
====================================================
(1) Examples of Interactive use of MATLAB.
======================================
Variables, Vectors, Matrices
============================
Variables have names beginning with a letter. They are
case-sensitive. Thus 'Geopotential' and 'geopotential'
are different. They are given values by statements
like
>> Geopotential = 5564
The value is echoed by MATLAB.
To suppress it, end the command with a semi-colon;
>> Geopotential = 5564; % No Echo.
Type declarations and dimension statements are
not required in MATLAB.
Some numbers are pre-defined. For example, try
>> pi or >> i
to see this.
Expressions are mostly self-explanatory, e.g.:
>> phi = (1+sqrt(5))/2
Vectors are defined thus:
>> rowvec = [ 1, 2, 3 ];
>> colvec = [ 2; 3; 5 ];
Then
>> rowvec*colvec
gives 23 and
>> colvec*rowvec
gives a 3 by 3 matrix:
2 4 6
3 6 9
5 10 15
Now try
>> rowvec*rowvec
You get
??? Error using ==>> *
Inner matrix dimensions must agree.
IMPORTANT:
To operate on vectors or matrices component by
component, precede the operator by a dot. Thus:
>> rowvec.*rowvec
gives the vector with squares of the components:
1 4 9
You can get the sum of the components
>> sum(rowvec);
or the mean value
>> mean(rowvec);
or the standard deviation
>>std(rowvec);
There are many other statistical functions.
Matrices are defined like this
>> amat = [ 1, 2, 3; 4, 5, 6; 7, 8, 10]
giving
1 2 3
4 5 6
7 8 10
We get the inverse using
>> ainv = inv(a)
Check that
>> amat*ainv
gives the 3 by three identity matrix.
The transpose of amat is amat'. Be careful: if amat
is complex, this is the hermitian conjugate.
Components of amat are indicated in an obvious way:
>> amat(3,2) = 8.
Try other combinations yourself.
Ranges: The colon operator
==========================
A range of numbers is defined by the colon operator.
For example,
>> 1:6 gives
1 2 3 4 5 6
The interval need not be 1:
>> 1:3:12 gives
1 4 7 10
Or the series can go backwards.
>> 8:-3:-5 gives
8 5 2 -1 -4
The colon by itself refers to all the elements in a
row or column of a matrix. So
>> amat(:,2)
is the second column and
>> amat(3,:) is the third row.
>> linspace(0,pi,101)
generates a sequence of 101 numbers, evenly spaced
between zero and pi.o
Graphics
========
MATLAB has extensive facilities for displaying
vectors and matrices as graphs, as well as annotating
and printing these graphs.
For a start, try the following:
>> t = 0:pi/100:2*pi;
>> x = sin(t); y = cos(t);
>> plot(t,x); hold on; plot(t,y);
The two graphs can also be plotted together:
>> plot(t,x,t,y);
You can also plot functions of variables:
>> plot(t,x,'b');
>> hold on;
>> plot(t,x.*x,':r');
Try other combinations yourself!
To open a new graphics window, type
>> figure
Subplots are easy. Try the following:
>> subplot(2,2,1); plot(t,x)
>> subplot(2,2,2); plot(t,y)
>> subplot(2,2,3); plot(t,x.*x)
>> subplot(2,2,4); plot(t,y.*y)
A title can be added, and axes labelled in an obvious way:
>> title('My first plot')
>> xlabel('time'); ylabel(' sin(t)');
Getting Help
============
Help for a specific function can be obtained.
For example,
>> doc sqrt
gives the HTML documentation on the square root
function.
Flow Control
============
Flow through a script can be controlled in several
ways. We consider only the simplest example of
a for-loop:
>> factorial(1)=1;
>> for n=2:10
>> factorial(n)=factorial(n-1)*n;
>> end
Then
>> y=factorial
will echo the value of n! with n going from 1 to 10.
[Purists say you should avoid for-loops,
but they are often useful.]
====================================================
(2) M-Files
=======
You can create your own programs using M-files, which
are text files containing MATLAB code. Just create a
file containing the same statements you would type at
the MATLAB command line. Save the file under a name
such as bobo.m that ends with the extension .m.
You can use the MATLAB editor by typing
>> edit bobo
to edit an existing file.
It is convenient to keep the edit window open while
running the MATLAB program. For testing, you can
then switch back and forward easily between running
and editing.
====================================================
****************************************************
Look through the online documentation to get a
general idea of the extensive assistance available.
****************************************************
====================================================