Matlab is an extremely powerful tool that is fairly simple to use. This tutorial will give a broad overview of Matlab and will help to familiarize you with many of Matlab's useful features.
0. Using Help
In Matlab, type "help" for a list of topics to get help on.
Then type "help
1. Simple Arithmetic & Mathematical Functions
Matlab can be used for any operations you could do on a calculator.
For example, if you type in ``4 * 3 + 2 * 5'' you should get back the answer
22. You can use variables instead of the actual numbers to do arithmetic,
which have the advantage of not being erased after being typed once.
So, if you type ``apples=4'' and ``oranges=2'', typing in
``cost=apples*3 + oranges*2'' will again give you the answer 22 and will
store that answer in a variable called "cost".
Matlab can also be used to find functions such as sin(x), sqrt(x)
and log(x).
2. Array Operations
One of Matlab's strengths is its integral use of matrices.
Arrays are simple one-row or one-column matrices which will demonstrate
some of the basic matrix manipulations possible.
The simplest way to create an array is to simply define all its
values. Assume we wanted to evaluate the function sin(x) from 0 to pi.
Since there are infinite points in this range, we must choose a finite
number of them; say we want to choose points at every 0.1*pi. Then we
could make an array by typing
``x=[0.1*pi .2*pi .3*pi .4*pi .5*pi .6*pi .7*pi .8*pi .9*pi pi]''
Then to find the sin of these values we can simply type ``y=sin(x)'',
which evaluates each point in the array x and creates a new array
y of the same size in which it stores the output.
We can access this array in many different ways. If we
simply wanted to access one element, we could type ``x(3)'' for the
third element in x. To access a block of elements we can use
colon notation: ``x(1:5)'' selects the first through fifth elements in x.
Similarly, ``y(4: -1: 2)'' means to select elements 4, 3, and 2 in reverse
order -- the format being
Using colon notation it is much simpler to construct arrays.
Instead of typing out every point and value of pi in the above array
we could type ``x=(0:0.1:1)*pi'', which constructs the equivalent
array given the start, end and increment values.
Alternately, we could type ``x=linspace(0, pi, 11)'', which
tells Matlab the start and end points, and the number of data points
in between.
Now we turn to manipulating the data in an array. As we have
already seen by y=sin(x), it is simple to do mathematical operations
between scalars and arrays. The scalars apply to each element of the
array; thus, ``x-3'' subtracts 3 from each element in x. Array-array
operations are only slightly more complicated. When the two arrays are
the same size, addition, subtraction, multiplication and division can
be applied on an element-by-element basis. However, when doing
element-by-element multiplication or division the operator must
be preceded by a . (.* or ./), otherwise the matrix multiplication
or division operation is done. Another manipulation we can do with
an array (and any matrix in general) is to change its orientation from
rows to columns or vice versa using the transpose operation ('). Thus
typing `` z=x' '' would create a column vector with the same values
as the row vector x.
For more information on matrices type "help elmat" or "help specmat"
at the Matlab console.
3. Plotting
It is extremely simple to plot in Matlab, since it can
can automatically choose limits for the axes, mark individual points
and draw straight lines between them. You can also optionally plot
multiple sets of data on the same graph, use different line types,
mark only the data points, or use different colors for the curves.
If we want to plot sine wave array that we constructed earlier,
all we would have to do is type "plot(x,y)". where x is the array of
increments of pi and y=sin(x) (also an array). We can plot both sine
and cosine on the same graph; to do this we first need to make an
array to hold the cosine values by typing "z=cos(x)". Then we will plot
both sets of data with "plot(x,y,x,z,'+')". The optional '+' tells Matlab to
mark the data points of x,z using a plus so we can distinguish the two
curves. We can also easily label the graphs, using the commands
"xlabel('independent variable')" and "ylabel('dependent variables')",
and title our graph with "title('your title here')". It is just as
simple to create a 3D graph, using the command "plot3(y,z,x), grid".
The optional ",grid" command simply a grid at the tick
marks on the graph.
4. Scripts and M-Files
Matlab uses external files (called M-files because they end in
a .m) to execute user-defined scripts and functions. To write a script
M-file, simply place all the commands you would normally type at the
console in a text file with a name ending in .m.
Matlab uses M-files for user-created functions. These functions
can be quite useful as they can be called any number of times with
different arguments, and can be passed to other functions which require
functions as inputs (numerical analysis of ordinary differential equations,
for example, requires the input of a function which provides derivatives).
The format of an M-file is:
function