You might want to try the stuff on MATLAB as you read it below to convince yourself of how easy it can really be... And once you've mastered the basics, you can use MATLAB's help system to learn about other commands, built-in functions, and variations of the basic commands below.
This page is now stable, but not static. I will continue to add useful information as it is requested or as it occurs to me.
Last Update: Added section on curve fitting.
athena% add matlabThe MATLAB command window will then appear. Your graphical output will appear in other ad hoc windows when you issue the appropriate commands.
athena% matlab &
or
plot(xdata1,ydata1,S1)
Read on about this most useful of features...
p returns the parameters p(1)..p(n) for which you fitted, and
Yfitted returns the values of y predicted from the actual
values of x according to the solved for parameters
p.
NOTE: Chances are you'll have to succesively refine your initial guesses
if MATLAB reports that that it is having trouble getting a clean fit.
Getting help
Once you're inside MATLAB, you can get help by using the help
command. You can use the following commands:
Echoing Commands
When MATLAB finishes executing a command, it typically echoes
whatever value was calculated in that command. Sometimes this is useful
in checking that MATLAB is doing what you intend it to do. At other
times it is inconvenient. If you add a semicolon (;) at the end
of your command, MATLAB will not print the value it has just
calculated. Compare:
>> a=5
a =
5
>> a=5;
>> a
a =
5
For more info...
2. Matrices
MATLAB deals exclusively with matrices. Matrices can be of any size
including empty (zero rows and/or columns), one-by-one
(scalar), n-by-1 (column vector), 1-by-m
(row vector), or n-by-m (proper
rectangular). Matrices can be real or complex, and sparse or full,
although in most laboratory applications you won't need to worry about
this.
Creating vectors
>> a=[5 7.8 pi 12 5+i]
a =
Columns 1 through 4
5.0000 7.8000 3.1416 12.0000
Column 5
5.0000 + 1.0000i
>> b=1:5
b =
1 2 3 4 5
>> bb=[-100:-6:-200]
bb =
Columns 1 through 12
-100 -106 -112 -118 -124 -130 -136 -142 -148 -154 -160 -166
Columns 13 through 17
-172 -178 -184 -190 -196
Creating matrices
>> c=[1 19.5
0 3*20]
c =
1.0000 19.5000
0 60.0000
or putting a semicolon between rows:
>> d=[1776 1492; 1984 2010]
d =
1776 1492
1984 2010
Manipulating matrices
>> M=magic(6)
M =
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
>> a=[2 5 6];
>> b=[3 6 1];
>> M(a,b)
ans =
7 25 3
34 16 30
29 11 4
The colon by itself stands for the entire column or row. For instance,
M(5,:) is the entire fifth row of M, and M(:,:) is just M itself.
3. Graphing
XY Plots
y yellow . point
m magenta o circle
c cyan x x-mark
r red + plus
g green - solid
b blue * star
w white : dotted
k black -. dashdot
-- dashed
For example, PLOT(X,Y,'c+') plots a cyan plus at each data point.
hold on (this prevents the next plot
command from erasing the figure window)
plot(xdata2,ydata2,S1)
Titles and labels
Errorbars
errorbar(x,y,l,u,symbol) draws the points xy using the format
in symbol, with errorbars that extend a distance of l below the
corresponding point and u above the corresponding point. X, y, l and u
are vectors of the same size.
3-D Plots
See MESH, SURF, WATERFALL.
4. Importing and Exporting Data
The following information is taken verbatim from the online help:
Saving files
SAVE Save workspace variables on disk.
SAVE fname saves all workspace variables to the binary "MAT-file"
named fname.mat. The data may be retrieved with LOAD. Omitting the
filename causes SAVE to use the default filename "matlab.mat".
SAVE fname X saves only X.
SAVE fname X Y Z saves X, Y, and Z.
SAVE fname X Y Z -ascii uses 8-digit ASCII form instead of binary.
SAVE fname X Y Z -ascii -double uses 16-digit ASCII form.
SAVE fname X Y Z -ascii -double -tabs delimits with tabs.
If fname is "stdio", SAVE sends the data to standard output.
Loading Files
LOAD Retrieve variables from disk.
LOAD fname retrieves the variables from the MAT-file 'fname.mat'.
LOAD, by itself, loads from the file named 'matlab.mat'.
LOAD xxx.yyy reads the ASCII file xxx.yyy, which must contain a
rectangular array of numeric data, arranged in m lines with n values
in each line. The result is an m-by-n matrix named xxx.
To load an ASCII file that does not have a filename extension, use
LOAD fname -ascii. Otherwise MATLAB adds the extension '.mat' and
tries to load it as a MAT-file. To load a MAT-file that does NOT
have a '.mat' extension, use LOAD fname.ext -mat.
If fname is "stdio", LOAD reads from standard input.
Loading raw data
Have your data in a text file organized in whatever way you want in
matrix form, that is, tabs or blanks to separate columns, and newlines
to separate rows. Then, once you're in MATLAB, do
load filename
-ascii.
This will load your data into a matrix of the appropriate
size whose name is filename.
Curve-Fitting
MATLAB has impressive and quick! facilities for curve fitting.
If you really want to understand the theory behind curve fitting (and I
suggest that you do, because it's really quite nifty) pick up a copy of
Numerical Recipes. Look through the sections on
Levenberg-Marquadt. You'll probably learn a lot. You can find this book,
among other places, at the Hayden basement cluster. It's Athena
rackware, so you can't take it, but pursuant with copyright restrictions
you can copy brief passages for your own academic, non-profit use.Polynomial fits
With your data in the the vectors, say, x and y, the
command
>> [p, S]=polyfit(x,y,n);
will fit your data with a polynomial of degree n, and return the
coefficients in p (in descending powers of x) and an error-related
matrix in S.Fitting to arbitrary functions
Again with your data pairs in x and y, the
command
>> [p, Yfitted] = curvefitnl(x,y,fstring,guess);
will fit an arbitrary function to your data. fstring is a
string containg the symbolic function to fit; for instance 'p(1)*sin(
p(2).*x ).^2' would be used to fit a squared sine wave of amplitude p(1)
and angular frequency p(2). The vector guess is just a row
vector with your initial guessses for the parameters p(1)..p(n); for
instance, [1 2*pi].
This page written by: Victor Chudnovsky (vchudnov@mit.edu).