Greek Mat Lab

of 16/16
ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ ΤΜΗΜΑ ΜΑΘΗΜΑΤΙΚΩΝ Ξεκινώντας με το MATLAB Revised by Clinton Wolfe Original by David Hart Μ. Βάβαλη και Τ. Κατελανή
  • date post

    08-Nov-2014
  • Category

    Documents

  • view

    11
  • download

    3

Embed Size (px)

Transcript of Greek Mat Lab

MATLABRevised by Clinton Wolfe Original by David Hart

. .

MATLAB

1. 1.1. ........................................................................3 1.2. Matlab .....................................................................................................4 1.3. Matlab ................................................................4 2.1. Matlab......................................................................................................4 2.2. ..............................................................................................................5 2.3. ...............................................................................................6 3.1. ...............................................................7 3.1.1. ...........................................................................7 3.1.2. .................................................................................................................7 3.2. .....................................................................................8 3.2.1. .........................................................................................................8 3.2.2. .................................................................................................................9 3.3. ..........................................................................................................10 3.4. .................................................................................10 4. ............................................................................................................................11 4.1. 4.2. 4.3. 4.4. 5. ........................................................................................................11 2- ........................................................................................12 3- ........................................................................................13 ....................................................................................................14 ..................................................................................................14 .......................................................................................................................14 ..............................................................................................15 .....................................................................................................15 3.2.3. ........................................................................................................................9 3.1.3. .......................................................................................................8 ..............................................................................................................................4 Matlab? ..................................................................................................................3

2.

3.

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

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

5.1. 5.2. 5.3. 5.4. 6.

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

2

MATLAB

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

1. Matlab

windows. , . . , (.. , ), . Matlab , UNIX . ' , . File OpenA = 5

, Open Matlab Matlab. .

A = 5

3

MATLAB 1.2. Matlab

To MATLAB . Windows UNIX LINUX

Windows, Macintosh, Linux. 1.3. Matlab windows NT, Start Menu ->Programs->Statistics-Math->Matlab. UNIX, matlab . Xwindow, DISPLAY. matlab -display hostname:0.

Matlab:

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. Matlab . , . , . , ( Maple, ). . , . Matlab , . . , . 2.1. Matlab

4

MATLAB 2.2.

Matlab . . 2 + 3 7-5 34*212 1234/5786 2^5 a = sqrt(2) ans ans ans ans ans = = = = =

5 2 7208 0.2173 32

. "ans" . . . Pi, i, j . "eps" . "eps" . Matlab ( !) . "d", "e", "f" . . ":" - ( ) . ":". ( , , ), . ";" . " ' "

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

0 = = 3

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

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

ans = 1 2 3

5

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 ans = 6 ans = 7 8 9 g = 1 2 3 4 5 4 7 8 9

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

g^2

ans =

g .^ 2

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

, . format . , format compact . 15 , format long . 5 , format short . , .

2.3.

, help format . 6

MATLAB Matlab "" ( 15 ) . .

3. 3.1. . : rand(2) rand(2,3)

3.1.1. ans = 0.9501 0.2311 ans = 0.8913 0.7621

0 1 2x2 ( ). I.

0.6068 0.4860 0.4565 0.8214 0.0185 0.4447

zeros(2) ones(2)

ans = 0 0 0 0 ans = 1 1 1 1 ans = 1 0 0 1 ans = 1.0000 0.5000 0.3333 0.5000 0.3333 0.2500 0.3333 0.2500 0.2000

eye(2)

hilb(3)

, - help elmat . 3.1.2.

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

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

[a; a; a]

7

MATLAB[a, zeros(2); zeros(2), a'] ans = 1 3 0 0 2 4 0 0 0 0 1 2 0 0 3 4

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

3.1.3.

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

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

3.2.

. Matlab , . - , : ("^"). a , : b=2 a + b

b=2 ans = 3 4 5 6 ans = 2 4 6 8

b . --. .

a * b

8

MATLABa ^ b ans = 7 10 15 22 ans = 1 4 9 16

- a*a --.

a .^ b

( ). Matlab - -, . , Matlab , . Matlab : v = [1 2 3] u = [3 2 1] v * u v * u'

3.2.2.

v = [1 2 3] u = [3 2 1] Error ans = 10

. . . 3- .

dot(v,u)

ans = 10

cross(v,u)

ans = -4 8 -4

Matlab , . k = 16 2 3 5 11 10 9 7 6 rref(k) k = 16 2 3 5 11 10 9 7 6 ans = 1 0 0 ans = 3 ans = -136 ans = 0.0294 -0.0662 0.0956 -0.4412 -0.5074 1.0662 0.4706 0.6912 -1.2206 0 1 0 0 0 1

3.2.3.

. . . . .

rank(k) det(k) inv(k)

9

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 = -1 1 0 a = [1 2 3; 4 5 6; 7 8 10]; b = [1 1 1]'; x = a \ b

, : :ans = 1 1 1 ans = 1.0e-015 * -0.1110 -0.6661 -0.2220 ans = 2.2204e-016 a*x, a*x - b, eps

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

Maple Mathematica, Matlab ("worksheet"). Matlab, 10

MATLAB . , "logging" diary 'c:\scratch\session.txt' diary '~/session.txt' UNIX.

NT

, . . diary . "x" "x.value" "mysession.mat" , , , , UNIX : Windows NT, Notepad.% more session.txt % more x.value load mysession save mysession save x.value x -ascii

Matlab ( ). , Matlab Maple Mathematica. 4.1.

4.

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 . . , , , .

plot(x,y)

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

()

1x63 . . . .

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

()

1x11 . "" . , . , 11 . , , .

plot(x,y, 'ro-')

12

MATLABplot(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')

comet, help semilogy

: help plot, help help fill .

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

MATLABplot3(x,y,z)

, .

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

4.4.

5. 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);

5.1.

, Matlab, directory m- path. pathtool directory . ( addpath directory .) , . : mymatrix.m directory mymatrix . , , save load Matlab. 5.2. A = [2 3 4; 5 6 7; 8 9 0]; sketch

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 , .

MATLAB "batch mode", . "test.m" ( ), UNIX : " MATLAB, test.in, test.out." --m", quit . 5.4. % matlab < test.m > homework.out

5.3.

"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