You are on page 1of 16

MATLAB

Revised by Clinton Wolfe


Original by David Hart

 

  

. .

MATLAB

1.

2.

3.

Matlab? ..................................................................................................................3

1.1. ........................................................................3
1.2. Matlab .....................................................................................................4
1.3. Matlab ................................................................4
..............................................................................................................................4

2.1. Matlab......................................................................................................4
2.2. ..............................................................................................................5
2.3. ...............................................................................................6

.........................................................................................7

3.1. ...............................................................7
3.1.1. ...........................................................................7
3.1.2. .................................................................................................................7
3.1.3. .......................................................................................................8

3.2. .....................................................................................8
3.2.1. .........................................................................................................8
3.2.2. .................................................................................................................9
3.2.3. ........................................................................................................................9

3.3. ..........................................................................................................10
3.4. .................................................................................10
4.

5.

6.

............................................................................................................................11

4.1.
4.2.
4.3.
4.4.

........................................................................................................11
2- ........................................................................................12
3- ........................................................................................13
....................................................................................................14

5.1.
5.2.
5.3.
5.4.

..................................................................................................14
.......................................................................................................................14
..............................................................................................15
.....................................................................................................15

.................................................................................................................14

...............................................................................................................16

MATLAB

1. Matlab

MATLAB
, ().
" " ("MATrixLABoratory")
Linpack Eispack.
, , ,
, , .
Matlab ,
, . Matlab
,
Maple.
, .
Matlab , "",
. . (
1999) Matlab 5.3 11.
1.1.


windows.
, .

.
, (.. ,
),
.
Matlab ,
UNIX
.
' , .

File Open

, Open

A = 5


Matlab

A = 5

Matlab.
.

MATLAB
1.2. Matlab

To MATLAB
.

Windows

UNIX LINUX


Windows, Macintosh, Linux.
1.3. Matlab

Matlab:

windows NT, Start Menu ->Programs->Statistics-Math->Matlab.

UNIX, matlab .
Xwindow,
DISPLAY. matlab -display
hostname:0.

Matlab 10-15 .
, :
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, type tour or visit www.mathworks.com.
>>

>> Matlab.
Matlab, ( matlab) quit
File->Exit MATLAB.

2.
2.1. Matlab

Matlab .
, .
, .
,
( Maple, ).
.
, .
Matlab , .
.
,
.

MATLAB
2.2.

Matlab .
.

2 + 3
7-5
34*212
1234/5786
2^5

ans
ans
ans
ans
ans

=
=
=
=
=

a = sqrt(2)

a = 1.4142

b = a, pi, 2 + 3i

b = 1.4142
ans = 3.1416
ans = 2.0000 + 3.0000i

c = sin(pi)
eps

c = 1.2246e-016
ans = 2.2204e-016

d = [1 2 3 4 5 6 7 8 9]
e = [1:9]
f = 1:9

d = 1 2 3 4 5 6 7 8 9
e = 1 2 3 4 5 6 7 8 9
f = 1 2 3 4 5 6 7 8 9

g = 0:2:10
f(3)
f(2:7)
f(:)

g =
ans
ans
1 2

h = [1 2 3];
h'(nothing)

ans = 1
2
3

0
=
=
3

5
2
7208
0.2173
32


.

"ans" .


.

2 4 6 8 10
3
2 3 4 5 6 7
4 5 6 7 8 9




. Pi, i, j
.
"eps"
.
"eps"
.
Matlab
(
!)
.
"d", "e", "f"
. .

":" - (
)
.

":".



( , , ),
.
";"

. " '
"

MATLAB
,
,

-
-.
h * h'
h .* h
h + h

ans = 14
ans = 1 4 9
ans = 2 6 8

g = [ 1 2 3; 4 5 6; 7 8
9]

g = 1 2 3
4 5 6
7 8 9

g(2,3)
g(3,:)
g(2,3) = 4

ans = 6
ans = 7 8 9
g = 1 2 3
4 5 4
7 8 9

g^2

ans =

g .^ 2


. *
,

.
" .* "

.
.

.

":"

.

30 36 42
66 81 96
102 126 150
ans = 1 4 9
16 25 36
49 64 81


.


.

2.3.

,
.
format .

, format compact .

15 ,
format long .

5 , format short .

,
.

, help format .
6

MATLAB
Matlab "" ( 15 )
. .

3.
3.1.
3.1.1.

. :

rand(2)
rand(2,3)

zeros(2)
ones(2)

ans = 0.9501
0.2311
ans = 0.8913
0.7621

0.6068
0.4860
0.4565 0.8214
0.0185 0.4447



0
1
2x2

(
).

ans = 0 0
0 0
ans = 1 1
1 1

I.

eye(2)

ans = 1 0
0 1

hilb(3)

ans = 1.0000 0.5000 0.3333


0.5000 0.3333 0.2500
0.3333 0.2500 0.2000

,
- help elmat .
3.1.2.

.
a = [1 2; 3 4]
a = 1 2
3 4

[a, a, a]

ans = 1 2 1 2 1 2
3 4 3 4 3 4

[a; a; a]

ans = 1
3
1
3
1
3

2
4
2
4
2
4

MATLAB
[a, zeros(2); zeros(2), a']

ans = 1
3
0
0

2
4
0
0

0
0
1
2

0
0
3
4

3.1.3.

.
"for".
for i=1:10,
for j=1:10,
t(i,j) = i/j;
end
end

,
(t(i,j) = i/j;) . , Matlab
t 100 !
.

3.2.

, +, -, *, / .
(
--),
".*" (-) , "*"
() .
3.2.1.

. Matlab
, .
- , : ("^").
a , :

b=2

b=2

a + b

ans = 3 4
5 6

a * b

ans = 2 4
6 8

b .
--.
.

MATLAB
a ^ b

ans = 7 10
15 22

a .^ b

ans = 1 4
9 16

- a*a
--.

3.2.2.

( ). Matlab
- -,
.
, Matlab ,
. Matlab
:

v = [1 2 3]
u = [3 2 1]

v = [1 2 3]
u = [3 2 1]

v * u

Error

v * u'

ans = 10

dot(v,u)

ans = 10

cross(v,u)

ans = -4 8 -4


.

.

.

3- .

3.2.3.

Matlab ,
.

k = 16 2 3
5 11 10
9 7 6

k = 16 2 3
5 11 10
9 7 6

rref(k)

ans = 1
0
0

rank(k)

ans = 3

det(k)

ans = -136

inv(k)

ans =

0
1
0

0
0
1

0.0294 -0.0662 0.0956


-0.4412 -0.5074 1.0662
0.4706 0.6912 -1.2206

MATLAB
[vec,val] =
eig(k)

vec = -0.4712 -0.4975 -0.0621


-0.6884 0.8282 -0.6379
-0.5514 0.2581 0.7676
val = 22.4319
0
0
0 11.1136
0
0
0 -0.5455


.
"vec"
,

"val"

. help matfun
.
3.3.


. a
, x - "", b
" ", , a x =b
.
, Matlab

. , Matlab
:
x = a \ b

" x a- b."

a = [1 2 3; 4 5 6; 7 8 10]; b = [1 1 1]';


x = -1 1 0

, :
a*x, a*x - b, eps

:
ans = 1 1 1
ans = 1.0e-015 *
-0.1110
-0.6661
-0.2220
ans = 2.2204e-016

a*x - b eps -
.
, " " ( a*x - b
).
a(3,3) = 9; b = [1 1 0]';

( b) ,
.
.
3.4.

Maple Mathematica, Matlab


("worksheet"). Matlab,
10

MATLAB
.
, "logging"

diary 'c:\scratch\session.txt'
diary '~/session.txt' UNIX.

NT

,
.
. diary
.
"x"
"x.value"
save x.value x -ascii

"mysession.mat"
,
save mysession

,
load mysession

, , UNIX
:
% more session.txt
% more x.value

Windows NT, Notepad.

4.

Matlab (
). ,
Matlab
Maple Mathematica.
4.1.

Matlab,
. , 2, 3 4
, .

.
Matlab , .
-
. Matlab
, .
, Matlab
. , Matlab
.

11

MATLAB
Matlab
. Matlab, ()
.
, .
,
.
4.2. 2-

,
. ,
.

x = -10:0.5:10;
y = x .^ 2;

1x41
-10 10,
1x41

.

()

.
, <x(1), y(1)>
, <x(2), y(2)>
, .

plot(x,y)

t = 0:0.1:2*pi;
x = cos(t);
y = sin(t);

1x63
.

()

.
.
.

plot(x,y)

t = 0:pi/5:2*pi;
u = cos(t);
v = sin(t);

1x11
.
"" .

()


, .

, 11
.

figure plot(u,v)


,
, .

plot(x,y, 'ro-')

12

MATLAB
,
,


.

plot(x,y, 'r-',
u,v,'b*:')

.
, ,
.
.
.
.
.
.

figure
subplot(1,2,1)
plot(x,y)
title('Fine Mesh')
subplot(1,2,2)
plot(u,v)
title('Coarse Mesh')

: help plot, help


help fill .

comet, help semilogy

Matlab .
, ,
.
4.3. 3-

t
x
y
z

()

...

=
=
=
=

-4*pi:pi/16:4*pi;
cos(t);
sin(t);
t;

... ..

plot3(x,y,z)

[x, y] = meshgrid(3:0.1:3,-3:0.1:3);
z = x.^2 - y.^2;

()

mesh(x,y,z)


"" .

surf(x,y,z)

13

MATLAB

,
.

plot3(x,y,z)

4.4.

, Matlab .
, :
help slice , help movie , help getframe , help graph2d , help graph3d , help graphics
.

5.
5.1.

Matlab editor
,
. "m-" (Matlab )
".m" ( . . foo.m). m-
Matlab,
, .
File -> New ->M-file editor/debugger
Matlab, editor .
sketch.m:
[x y] = meshgrid(-3:.1:3, -3:.1:3);
z = x.^2 - y.^2;
mesh(x,y,z);

, Matlab, directory m-
path. pathtool directory .
( addpath directory .)

sketch

,
.
:
mymatrix.m directory
A = [2 3 4; 5 6 7; 8 9 0];

mymatrix . ,
, save load Matlab.
5.2.

m-, ,
%session% (

14

MATLAB
).
editor
sqroot.m, .
function sqroot(x)
% Compute square root by Newton's method
% Initial guess
xstart = 1;
for i = 1:100
xnew = ( xstart + x/xstart)/2;
disp(xnew);
if abs(xnew - xstart)/xnew < eps, break, end;
xstart = xnew;
end

Matlab
format long
sqroot(19)

.
:

"workspace"
, ( xstart
xnew, ) ,
workspace .
, " M-" ,
Matlab.

: ,
clear function_name
, .

5.3.

MATLAB "batch mode", .


"test.m" ( ),
UNIX :
% matlab < test.m > homework.out

" MATLAB, test.in, test.out."


--m",
quit .
5.4.

"m-" MATLAB !
path.
demos.
demo
15

MATLAB
Matlab.
help function .

6.

Matlab :
http://itia.math.uch.gr/~mav/matlab/start/index.html
.
UNIX, Using Math Software under UNIX :
http://www.indiana.edu/~statmath/math/all/mathunix/index.html
. .
Matlab ,
.

104.

16