Matlab+Introduction+%28ang%29, studia WIP, matlab

[ Pobierz całość w formacie PDF ]
An Introduction to Matlab
Version 2.3
David F. Griths
Department of Mathematics
The University
Dundee DD1 4HN
With additional material by Ulf Carlsson
Department of Vehicle Engineering
KTH, Stockholm, Sweden
Copyright c 1996 by David F. Griths. 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 source
is properly and completely specied.
Contents
15 Examples in Plotting
13
1 MATLAB
2
16 Matrices|Two{Dimensional Arrays 13
16.1 Size of a matrix . . . . . . . . . . . . 14
16.2 Transpose of a matrix . . . . . . . . 14
16.3 Special Matrices . . . . . . . . . . . 14
16.4 The Identity Matrix . . . . . . . . . 14
16.5 Diagonal Matrices . . . . . . . . . . 15
16.6 Building Matrices . . . . . . . . . . . 15
16.7 Tabulating Functions . . . . . . . . . 15
16.8 Extracting Bits of Matrices . . . . . 16
16.9 Dot product of matrices (
.*
) . . . . 16
16.10Matrix{vector products . . . . . . . 16
16.11Matrix{Matrix Products . . . . . . . 17
16.12Sparse Matrices . . . . . . . . . . . . 17
2 Starting Up 2
2.1 Windows Systems . . . . . . . . . . 2
2.2 Unix Systems . . . . . . . . . . . . . 2
2.3 Command Line Help . . . . . . . . . 2
2.4 Demos . . . . . . . . . . . . . . . . . 3
3 Matlab as a Calculator
3
4 Numbers & Formats
3
5 Variables 3
5.1 Variable Names . . . . . . . . . . . . 3
6 Suppressing output
4
17 Systems of Linear Equations
18
17.1 Overdetermined system of linear equa-
tions . . . . . . . . . . . . . . . . . . 18
7 Built{In Functions 4
7.1 Trigonometric Functions . . . . . . . 4
7.2 Other Elementary Functions . . . . . 4
18 Characters, Strings and Text
20
8 Vectors 4
8.1 The Colon Notation . . . . . . . . . 5
8.2 Extracting Bits of a Vector . . . . . 5
8.3 Column Vectors . . . . . . . . . . . . 5
8.4 Transposing . . . . . . . . . . . . . . 5
19 Loops
20
20 Logicals 21
20.1 While Loops . . . . . . . . . . . . . . 22
20.2
if...then...else...end
. . . . . . 23
9 Keeping a record
6
21 Function m{les 23
21.1 Examples of functions . . . . . . . . 24
10 Plotting Elementary Functions 6
10.1 Plotting|Titles & Labels . . . . . . 7
10.2 Grids . . . . . . . . . . . . . . . . . . 7
10.3 Line Styles & Colours . . . . . . . . 7
10.4 Multi{plots . . . . . . . . . . . . . . 7
10.5 Hold . . . . . . . . . . . . . . . . . . 7
10.6 Hard Copy . . . . . . . . . . . . . . 8
10.7 Subplot . . . . . . . . . . . . . . . . 8
10.8 Zooming . . . . . . . . . . . . . . . . 8
10.9 Formatted text on Plots . . . . . . . 8
10.10Controlling Axes . . . . . . . . . . . 9
22 Further Built{in Functions 25
22.1 Rounding Numbers . . . . . . . . . . 25
22.2 The
sum
Function . . . . . . . . . . . 25
22.3
max
&
min
. . . . . . . . . . . . . . . 26
22.4 Random Numbers . . . . . . . . . . 26
22.5
find
for vectors . . . . . . . . . . . . 27
22.6
find
for matrices . . . . . . . . . . . 27
23 Plotting Surfaces
27
24 Timing
28
11 Keyboard Accelerators
9
25 On{line Documentation
29
12 Copying to and from Word and other
applications 10
12.1 Window Systems . . . . . . . . . . . 10
12.2 Unix Systems . . . . . . . . . . . . . 10
26 Reading and Writing Data Files 29
26.1 Formatted Files . . . . . . . . . . . . 30
26.2 Unformatted Files . . . . . . . . . . 30
13 Script Files
10
27 Graphic User Interfaces
31
14 Products, Division & Powers of Vec-
tors 11
14.1 Scalar Product (
*
) . . . . . . . . . . 11
14.2 Dot Product (
.*
) . . . . . . . . . . . 11
14.3 Dot Division of Arrays (
./
) . . . . . 12
14.4 Dot Power of Arrays (
.^
) . . . . . . 12
28 Command Summary
32
1
1 MATLAB
from the separate Help window found under
the Help menu or
Matlab is an interactive system for doing nu-
merical computations.
from the Matlab helpdesk stored on disk or
on a CD-ROM.
A numerical analyst called Cleve Moler wrote
the rst version of Matlab in the 1970s. It
has since evolved into a successful commercial
software package.
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 example
of its use.
Matlab relieves you of a lot of the mundane
tasks associated with solving problems nu-
merically. This allows you to spend more time
thinking, and encourages you to experiment.
2.2 Unix Systems
You should have a directory reserved for sav-
ing les associated with Matlab. Create such
a directory (
mkdir
) if you do not have one.
Change into this directory (
cd
).
Matlab makes use of highly respected algo-
rithms and hence you can be condent about
your results.
Powerful operations can be performed using
just one or two commands.
Start up a new
xterm
window (do
xterm &
in
the existing
xterm
window).
You can build up your own set of functions
for a particular application.
Launch Matlab in one of the
xterm
windows
with the command
Excellent graphics facilities are available, and
the pictures can be inserted into L
A
T
E
X and
Word documents.
matlab
After a short pause, the logo will be shown
followed by a window containing the Matlab
interface. Should you wish to run Matlab in
an
xterm
window, use the command
These notes provide only a brief glimpse of the
power and exibility of the Matlab system. For a
more comprehensive view we recommend the book
matlab -nojvm
Matlab Guide
D.J. Higham & N.J. Higham
SIAM Philadelphia, 2000, ISBN: 0-89871-469-9.
and, following dislpay of the logo, the Matlab
prompt
>>
will appear.
Type
quit
at any time to exit from Mat-
lab.
2 Starting Up
2.1 Windows Systems
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
On Windows systems MATLAB is started by double-
clicking the MATLAB icon on the desktop or by
selecting MATLAB from the start menu.
The starting procedure takes the user to the Com-
mand window where the Command line is indicated
with '
>>
'. Used in the calculator mode all Matlab
commands are entered to the command line from
the keyboard.
Matlab can be used in a number of dierent ways or
modes; as an advanced calculator in the calculator
mode, in a high level programming language mode
and as a subroutine called from a C-program. More
information on the rst two of these modes is given
below.
Help and information on Matlab commands can be
found in several ways,
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
from the command line by using the '
help
topic
' command (see below),
>> help elfun
2
This gives rather a lot of information so, in order to see
the information one screenful at a time, rst issue the
command
more on
, i.e.,
Command Example of Output
>>format short 31.4162
(4{decimal places)
>>format short e 3.1416e+01
>>format long e 3.141592653589793e+01
>>format short 31.4162
(4{decimal places)
>>format bank 31.42
(2{decimal places)
>> more on
>> help elfun
Hit any key to progress to the next page of information.
2.4 Demos
Demonstrations are invaluable since they give an indi-
cation of Matlabs capabilities. A comprehensive set are
available by typing the command
format|how Matlab prints numbers|is controlled by
the \format" command. Type
help format
for full list.
Should you wish to switch back to the default format
then
format
will suce.
The command
format compact
is also useful in that it suppresses blank lines in the
output thus allowing more information to be displayed.
>> demo
( Warning: this will clear the values of all current vari-
ables.)
3 Matlab as a Calculator
5 Variables
The basic arithmetic operators are
+ - * / ^
and these
are used in conjunction with brackets:
( )
. The symbol
^
is used to get exponents (powers):
2^4=16
.
You should type in commands shown following
the prompt:
>>
.
>> 3-2^4
ans =
-13
>> ans*5
ans =
-65
>> 2 + 3/4*5
ans =
5.7500
The result of the rst calculation is labelled \
ans
" by
Matlab and is used in the second calculation where its
value is changed.
We can use our own names to store numbers:
>>
Is this calculation
2 + 3/(4*5)
or
2 + (3/4)*5
? Mat-
lab works according to the priorities:
1. quantities in brackets,
2. powers
2 + 3^2
)
2 + 9 = 11
,
3.
* /
, working left to right (
3*4/5=12/5
),
4.
+ -
, working left to right (
3+4-5=7-5
),
Thus, the earlier calculation was for
2 + (3/4)*5
by
priority 3.
>> x = 3-2^4
x =
-13
>> y = x*5
y =
-65
so that
x
has the value 13 and y = 65. These can
be used in subsequent calculations. These are examples
of assignment statements: values are assigned to
variables. Each variable must be assigned a value before
it may be used on the right of an assignment statement.
4 Numbers & Formats
Matlab recognizes several dierent kinds of numbers
5.1 Variable Names
Legal names consist of any combination of letters and
digits, starting with a letter. These are allowable:
Type
Examples
Integer
1362;217897
Complex 3:21 4:3i (i =
p
1)
Inf
1:234;10:76
NetCost, Left2Pay, x3, X3, z25c5
These are not allowable:
Innity (result of dividing by 0)
NaN
Not a Number, 0=0
Net-Cost, 2pay, %x, @sign
The \
e
" notation is used for very large or very small
numbers:
-1.3412e+03
= 1:3412 10
3
= 1341:2
-1.3412e-01
= 1:3412 10
1
= 0:13412
All computations in MATLAB are done in double pre-
cision, which means about 15 signicant gures. The
Use names that reect the values they represent.
Special names: you should avoid using
eps = 2.2204e-16
= 2
54
(The largest number such
that
1 + eps
is indistinguishable from 1) and
pi = 3.14159...
= .
If you wish to do arithmetic with complex numbers,both
i
and
j
have the value
p
1 unless you change them
3
Real
>> i,j, i=3
ans = 0 + 1.0000i
ans = 0 + 1.0000i
i = 3
>> x = 9;
>> sqrt(x),exp(x),log(sqrt(x)),log10(x^2+6)
ans =
3
ans =
8.1031e+03
ans =
1.0986
ans =
1.9395
6 Suppressing output
One often does not want to see the result of intermedi-
ate calculations|terminate the assignment statement
or expression with semi{colon
exp(x)
denotes the exponential function exp(x) = e
x
and the inverse function is
log
:
>> x=-13; y = 5*x, z = x^2+y
y =
-65
>> format long e, exp(log(9)), log(exp(9))
ans = 9.000000000000002e+00
ans = 9
>> format short
z =
104
>>
the value of
x
is hidden. Note also we can place several
statements on one line, separated by commas or semi{
colons.
and we see a tiny rounding error in the rst calculation.
log10
gives logs to the base 10. A more complete list
of elementary functions is given in Table 2 on page 32.
Exercise 6.1 In each case nd the value of the expres-
sion in Matlab and explain precisely the order in which
the calculation was performed.
8 Vectors
These come in two avours and we shall rst describe
row vectors: they are lists of numbers separated by ei-
ther commas or spaces. The number of entries is known
as the \length" of the vector and the entries are often
referred to as \elements" or \components" of the vec-
tor.The entries must be enclosed in square brackets.
i)
-2^3+9
ii)
2/3*3
iii)
3*2/3
iv)
3*4-5^2*2-3
v)
(2/3^2*5)*(3-4^3)^2
vi)
3*(3*4-2*5^2-3)
7 Built{In Functions
7.1 Trigonometric Functions
Those known to Matlab are
sin, cos, tan
and their arguments should be in radians.
e.g. to work out the coordinates of a point on a circle of
radius 5 centred at the origin and having an elevation
30
o
= =6 radians:
>> v = [ 1 3, sqrt(5)]
v =
1.0000 3.0000 2.2361
>> length(v)
ans =
3
Spaces can be vitally important:
>> v2 = [3+ 4 5]
v2 =
>> x = 5*cos(pi/6), y = 5*sin(pi/6)
x =
7 5
>> v3 = [3 +4 5]
v3 =
4.3301
y =
2.5000
3 4 5
The inverse trig functions are called
asin, acos, atan
(as opposed to the usual arcsin or sin
1
etc.). The
We can do certain arithmetic operations with vectors
of the same length, such as
v
and
v3
in the previous
section.
result is in radians.
>> acos(x/5), asin(y/5)
ans = 0.5236
ans = 0.5236
>> pi/6
ans = 0.5236
>> v + v3
ans =
4.0000 7.0000 7.2361
>> v4 = 3*v
v4 =
3.0000 9.0000 6.7082
>> v5 = 2*v -3*v3
v5 =
-7.0000 -6.0000 -10.5279
>> v + v2
??? Error using ==> +
Matrix dimensions must agree.
7.2 Other Elementary Functions
These include
sqrt, exp, log, log10
4
[ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • apo.htw.pl

  •