Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
Matlab Notes

# Matlab Notes

Ratings: (0)|Views: 2,047|Likes:

### Availability:

See more
See less

11/06/2012

pdf

text

original

An Introduction to Matlab
Version 2.3
David F. Griﬃths
Department of MathematicsThe UniversityDundee DD1 4HN
1996 by David F. Griﬃths. Amended October, 1997, August 2001, September 2005.This introduction may be distributed provided that it is not be altered in any way and that its sourceis properly and completely speciﬁed.

Contents
1 MATLAB 22 Starting Up 2
2.1 Windows Systems . . . . . . . . . . 22.2 Unix Systems . . . . . . . . . . . . . 22.3 Command Line Help . . . . . . . . . 22.4 Demos . . . . . . . . . . . . . . . . . 3
3 Matlab as a Calculator 34 Numbers & Formats 35 Variables 3
5.1 Variable Names . . . . . . . . . . . . 3
6 Suppressing output 47 BuiltIn Functions 4
7.1 Trigonometric Functions . . . . . . . 47.2 Other Elementary Functions . . . . . 4
8 Vectors 4
8.1 The Colon Notation . . . . . . . . . 58.2 Extracting Bits of a Vector . . . . . 58.3 Column Vectors . . . . . . . . . . . . 58.4 Transposing . . . . . . . . . . . . . . 5
9 Keeping a record 610 Plotting Elementary Functions 6
10.1 Plotting—Titles & Labels . . . . . . 710.2 Grids . . . . . . . . . . . . . . . . . . 710.3 Line Styles & Colours . . . . . . . . 710.4 Multiplots . . . . . . . . . . . . . . 710.5 Hold . . . . . . . . . . . . . . . . . . 710.6 Hard Copy . . . . . . . . . . . . . . 810.7 Subplot . . . . . . . . . . . . . . . . 810.8 Zooming . . . . . . . . . . . . . . . . 810.9 Formatted text on Plots . . . . . . . 810.10Controlling Axes . . . . . . . . . . . 9
11 Keyboard Accelerators 912 Copying to and from Word and otherapplications 10
12.1 Window Systems . . . . . . . . . . . 1012.2 Unix Systems . . . . . . . . . . . . . 10
13 Script Files 1014 Products, Division & Powers of Vec-tors 11
14.1 Scalar Product (
*
) . . . . . . . . . . 1114.2 Dot Product (
.*
) . . . . . . . . . . . 1114.3 Dot Division of Arrays (
./
) . . . . . 1214.4 Dot Power of Arrays (
.^
) . . . . . . 12
15 Examples in Plotting 1316 Matrices—Two–Dimensional Arrays 13
16.1 Size of a matrix . . . . . . . . . . . . 1416.2 Transpose of a matrix . . . . . . . . 1416.3 Special Matrices . . . . . . . . . . . 1416.4 The Identity Matrix . . . . . . . . . 1416.5 Diagonal Matrices . . . . . . . . . . 1516.6 Building Matrices . . . . . . . . . . . 1516.7 Tabulating Functions . . . . . . . . . 1516.8 Extracting Bits of Matrices . . . . . 1616.9 Dot product of matrices (
.*
) . . . . 1616.10Matrix–vector products . . . . . . . 1616.11Matrix–Matrix Products . . . . . . . 1716.12Sparse Matrices . . . . . . . . . . . . 17
17 Systems of Linear Equations 18
17.1 Overdetermined system of linear equa-tions . . . . . . . . . . . . . . . . . . 18
18 Characters, Strings and Text 2019 Loops 2020 Logicals 21
20.1 While Loops . . . . . . . . . . . . . . 2220.2
if...then...else...end
. . . . . . 23
21 Function mles 23
21.1 Examples of functions . . . . . . . . 24
22 Further Builtin Functions 25
22.1 Rounding Numbers . . . . . . . . . . 2522.2 The
sum
Function . . . . . . . . . . . 2522.3
max
&
min
. . . . . . . . . . . . . . . 2622.4 Random Numbers . . . . . . . . . . 2622.5
find
for vectors . . . . . . . . . . . . 2722.6
find
for matrices . . . . . . . . . . . 27
23 Plotting Surfaces 2724 Timing 2825 Online Documentation 2926 Reading and Writing Data Files 29
26.1 Formatted Files . . . . . . . . . . . . 3026.2 Unformatted Files . . . . . . . . . . 30
27 Graphic User Interfaces 3128 Command Summary 32
1

1 MATLAB
Matlab is an interactive system for doing nu-merical computations.
A numerical analyst called Cleve Moler wrotethe ﬁrst version of Matlab in the 1970s. Ithas since evolved into a successful commercialsoftware package.
Matlab relieves you of a lot of the mundanetasks associated with solving problems nu-merically. This allows you to spend more timethinking, and encourages you to experiment.
Matlab makes use of highly respected algo-rithms and hence you can be conﬁdent aboutyour results.
Powerful operations can be performed using just one or two commands.
You can build up your own set of functionsfor a particular application.
Excellent graphics facilities are available, andthe pictures can be inserted into L
A
TEX andWord documents.These notes provide only a brief glimpse of thepower and ﬂexibility of the Matlab system. For amore comprehensive view we recommend the bookMatlab GuideD.J. Higham & N.J. HighamSIAM Philadelphia, 2000, ISBN: 0-89871-469-9.
2 Starting Up
2.1 Windows Systems
On Windows systems MATLAB is started by double-clicking the MATLAB icon on the desktop or byselecting MATLAB from the start menu.The starting procedure takes the user to the Com-mand window where the Command line is indicatedwith ’
>>
’. Used in the calculator mode all Matlabcommands are entered to the command line fromthe keyboard.Matlab can be used in a number of diﬀerent ways ormodes; as an advanced calculator in the calculatormode, in a high level programming language modeand as a subroutine called from a C-program. Moreinformation on the ﬁrst two of these modes is givenbelow.Help and information on Matlab commands can befound in several ways,
from the command line by using the ’
helptopic
’ command (see below),
from the separate Help window found underthe Help menu or
from the Matlab helpdesk stored on disk oron a CD-ROM.Another useful facility is to use the ’
lookfor keyword
command, which searches the help ﬁles for the key-word. See Exercise 16.1 (page 17) for an exampleof its use.
2.2 Unix Systems
You should have a directory reserved for sav-ing ﬁles associated with Matlab. Create sucha directory (
mkdir
) if you do not have one.Change into this directory (
cd
).
Start up a new
xterm
window (do
xterm &
inthe existing
xterm
window).
Launch Matlab in one of the
xterm
windowswith the command
matlab
After a short pause, the logo will be shownfollowed by a window containing the Matlabinterface. Should you wish to run Matlab inan
xterm
window, use the command
matlab -nojvm
and, following dislpay of the logo, the Matlabprompt
>>
will appear.Type
quit
at any time
to exit from Mat-lab
.
2.3 Command Line Help
Help is available from the command line prompt.Type
help help
for “help” (which gives a brief syn-opsis of the help system),
help
for a list of topics.The ﬁrst few lines of this read
HELP topics: matlab/general - General purpose commands. matlab/ops - Operators and special char... matlab/lang - Programming language const... matlab/elmat - Elementary matrices and ma... matlab/elfun - Elementary math functions. matlab/specfun - Specialized math functions.
(truncated lines are shown with ...). Then to ob-tain help on “Elementary math functions”, for instance,type
>> help elfun
2