INTRODUCTION TO MATLAB
Preface
This is a tutorial to help you get started in Matlab. To find more details see the
very
helpful book Mastering MATLAB 7 by Duane Hanselman and Bruce Little field.
Examples
of Matlab code in this pamphlet are in typewriter font like this . As you read
through the
chapters below type and execute in Matlab all of the examples, either at the
>>command
line prompt or in a test program you make called test.m. Longer sections of code
have boxes
around the code.
This booklet can also be used as a reference manual
because it is short, it has lots of
examples, and it has a table of contents and an index. It is almost true that
the basics of
Matlab are in chapters 1-9 while physics applications are in chapters 9-17.
Contents
Preface i
Table of Contents iii
1 Running Matlab 1
1.1 Starting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 1
1.2 It's a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 1
1.3 Making Script Files . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 2
1.4 Running Script Files . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 2
1.5 Pause command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 3
1.6 Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 3
1.7 Making Matlab Be Quiet . . . . . . . . . . . . . . . . . . . . . . . . . . .
. 4
1.8 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 4
1.9 Arranging the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 4
1.10 Sample Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 5
1.11 Breakpoints and Stepping . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 6
2 Variables 7
2.1 Numerical Accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 7
2.2 π. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 8
2.3 Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . .
. . . 8
2.4 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 8
2.5 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 9
3 Input, Calculating, and Output 11
3.1 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 11
3.2 Calculating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 11
3.3 Add and Subtract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 12
3.4 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 12
3.5 Arithmetic with Array Elements . . . . . . . . . . . . . . . . . . . . . . .
. 13
3.6 Complex Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 13
3.7 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 14
3.8 Housekeeping Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 14
3.9 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 15
4 Arrays and x-y Plotting 19
4.1 Colon (:) Command . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 19
4.2 xy Plots, Labels, and Titles . . . . . . . . . . . . . . . . . . . . . . . .
. . . 20
4.3 Generating Multiple Plots . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 20
4.4 Overlaying Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 21
4.5 xyz Plots: Curves in 3-D Space . . . . . . . . . . . . . . . . . . . . . . .
. . 22
4.6 Logarithmic Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 22
4.7 Controlling the Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 23
4.8 Greek Letters, Subscripts, and Superscripts . . . . . . . . . . . . . . . .
. . 23
4.9 Changing Line Widths, Fonts, Etc. . . . . . . . . . . . . . . . . . . . . .
. . 24
5 Surface, Contour, and Vector Field Plots 25
5.1 Meshgrid and Ndgrid . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 25
5.2 Contour Plots and Surface Plots . . . . . . . . . . . . . . . . . . . . . .
. . 27
5.3 Evaluating Fourier Series . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 30
5.4 Vector Field Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 31
6 Vector Products, Dot and Cross 33
7 Linear Algebra 35
7.1 Solve a Linear System . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 35
7.2 Max and Min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 36
7.3 Matrix Inverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 36
7.4 Transpose and Hermitian Conjugate . . . . . . . . . . . . . . . . . . . . .
. 37
7.5 Special Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 37
7.6 Determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 38
7.7 Norm of Vector (Magnitude) . . . . . . . . . . . . . . . . . . . . . . . . .
. 38
7.8 Sum the Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 38
7.9 Selecting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . .
. 39
7.10 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . .
. . . 39
7.11 Fancy Stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 39
8 Polynomials 41
8.1 Roots of a Polynomial . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 41
8.2 Find the polynomial from the roots . . . . . . . . . . . . . . . . . . . . .
. . 41
8.3 Multiply Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 41
8.4 Divide Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 42
8.5 First Derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 42
8.6 Evaluate a Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 42
8.7 Fitting Data to a Polynomial . . . . . . . . . . . . . . . . . . . . . . . .
. . 43
9 Loops and Logic 45
9.1 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 45
9.2 Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 48
9.3 Secant Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 50
9.4 Using Matlab's Fzero . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 53
10 Derivatives and Integrals 55
10.1 Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 55
10.2 Definite Integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 57
10.3 Matlab Integrators . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 59
11 Interpolation and Extrapolation 63
11.1 Linear Interpolation and Extrapolation . . . . . . . . . . . . . . . .
. . . . . 63
11.2 Quadratic Interpolation and Extrapolation . . . . . . . . . . . . . . . . .
. 64
11.3 Interpolating With polyfit and polyval . . . . . . . . . . . . . . . . . .
. 65
11.4 Matlab Commands Interp1 and Interp2 . . . . . . . . . . . . . . . . . . . .
66
12 Make Your Own Functions: Inline and M- les 71
12.1 Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 71
12.2 M- le Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . 72
12.3 Derivative Function derivs.m . . . . . . . . . . . . . . . . . . . . . . .
. . . 72
12.4 Definite Integral Function defint.m . . . . . . . . . . . . . . . . . . . .
. . 74
12.5 Indefinite Integral Function indefint.m . . . . . . . . . . . . . . . . . .
. . 75
13 Fast Fourier Transform (FFT) 77
13.1 Fourier Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 77
13.2 Matlab's FFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 77
13.3 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 81
13.4 Using the FFT to Compute Fourier Transforms . . . . . . . . . . . . . . . .
83
14 Fitting Functions to Data 87
14.1 fminsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 87
15 Systems of Nonlinear Equations 91
15.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 91
16 Ordinary Differential Equations 93
16.1 Decay of a Radioactive Sample . . . . . . . . . . . . . . . . . . . . .
. . . . 93
16.2 Simple Harmonic Oscillator . . . . . . . . . . . . . . . . . . . . . . . .
. . . 94
16.3 Euler's Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . 95
16.4 Second- order Runge -Kutta . . . . . . . . . . . . . . . . . . . . . . . . .
. . . 96
16.5 Matlab's Differential Equation Solvers . . . . . . . . . . . . . . . . . .
. . . 98
16.6 Event Finding with Matlab's Differential Equation Solvers . . . . . . . . .
. 102
17 Publication Quality Plots 107
17.1 Creating an EPS File . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 107
17.2 Controlling the Appearance of Figures . . . . . . . . . . . . . . . . . . .
. . 109
17.3 Controlling the Size of Exported Graphics . . . . . . . . . . . . . . . . .
. . 112
17.4 Making an EPS Suitable for Publication . . . . . . . . . . . . . . . . . .
. . 113
17.5 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 115
17.6 Making Raster Versions of Figures . . . . . . . . . . . . . . . . . . . . .
. . 117
Chapter 1
Running Matlab
1.1 Starting
Get on a department PC or buy Student Matlab for your own machine and start
the
program.
1.2 It's a Calculator
You can use Matlab as a calculator by typing commands at the >>prompt, like
these. Try
them out.
1+1
2*3
5/6
exp(-3)
atan2(-1,2)
And just like many hand calculators, ans in Matlab means the last result calculated.
sin(5)
ans
Note that Matlab's trig functions are permanently set to
radians mode. Note also that
the way to enter numbers like 1.23 × 1015 is
1.23e15
And here's another useful thing. The up-arrow key ↑ will
display previous commands .
And when you back up to a previous command that you like, hit Enter and it will
execute.
Or you can edit it when you get to it (use ←, →, and Del), then execute it. Try
doing this
now to re-execute the commands you have already typed.
1.3 Making Script Files
Most of the work you will do in Matlab will be stored in les called scripts
containing Matlab
commands to be executed over and over again. To make a script, first browse or
type in the
current directory window on the tool bar to put yourself in the directory where
you want
to store your Matlab scripts. Then open a new text le in the usual way by
clicking on the
empty document on the tool bar, or open an old one to be edited. A script can be
filled with
a sequence of Matlab commands that will be executed from top to bottom just as
if you had
typed them on the command screen. These les have .m extensions (automatically
added
in Windows) and can be executed by typing their names (without the .m extension)
in the
command window. For example, if you create a script called test.m you can
execute it in
the command window by typing test. Do not choose le names that start with
numbers,
like 430lab1a.m. When Matlab receives the start of a number on the command line
it
thinks a calculation is coming , and since 430lab1a is not a valid calculation
Matlab will
give you an error. Also, do not use a space or a period in the le name. If you
want to
separate words in your le name, you can use the underscore character (e.g. my
script.m).
During a session keep this le open so you can modify and
debug it. And remember
to save the changes you make (Ctrl-s is a quick way) or Matlab in the command
window
won't know that you have made changes to the script.
Document your code by including lines in it that begin
with %, like this. (Note: don't
try to execute these lines of code, they just illustrates how to put comments
in.)
% This is a comment line
Or you can put comments at the end of a line of code like this:
f=1-exp(-g*t) % compute the decay fraction
You may need to type lines into your script that are too
long to see well. To make
the code look better you can continue program lines onto successive lines by
using the ...
syntax. (Note: don't try to execute these lines of code, they just illustrate
how to continue
long lines.)
a=sin(x)*exp(-y)*...
log(z)+sqrt(b),
Finally, nearly always begin your scripts with the clear
command. This makes sure
that you don't have leftover junk active in Matlab that will interfere with your
code.
1.4 Running Script Files
Before you can execute a script that you have written and saved, you need to
point Matlab's
current directory to the place where your script is saved. Matlab displays the
current
directory in an editable box on the toolbar. You can change the directory by
typing in the
box or clicking the button next to the current directory box (i.e. the button
with the three
dots in it).
Once your directory is set to the right place, you can go
to the window with the Matlab
command prompt >>and type the name of your le without the .m extension, like
this:
test
and your script will then run.
A convenient alternative for running a script is to use
the "Save and Run" shortcut key,
F5, while in the m- file editor window. This shortcut will save your script file,
ask you if you
want to switch the current directory to where your script le is located (if it
isn't already
pointed there), and then run the script for you.
Prev | Next |