Πληροφορική Ι (Matlab)

102
Διαφάνειες από το Μάθημα: Πληροφορική Ι (Matlab) ΠΑΝΕΠΙΣΤΗΜΙΟ ΑΘΗΝΩΝ ΤΜΗΜΑ ΜΑΘΗΜΑΤΙΚΩΝ Αθήνα 2003-2004

Transcript of Πληροφορική Ι (Matlab)

:

(Matlab)

2003-2004

1

141. I . . . Matlab / Octave. , , .

2

() ()

3

:

. : () . , . .

4

. :

( 0 ). ( 1 /). ( ). ( ).

, , .

5

1. . 2. . 3. : . . : . 4. : . 5. . 6. .

6

7

Matlab 5 : - input disp

+

/=

if -else while, for

ti . / siia . asmn .

8

Fahrenheit Celsius

Fahrenheit Celsius: c

/

% f = 451; disp(c); : 451, 32, 5, 9 % . . . : f, c

% 451 F 32) 5/9;

c = (f

: : = : disp ;

9

451

32

5

+9.2

2.7183

0

1.0

ii sta pi . ti mf

( 6.023e+23

ae aE ) 9e 2

1.7E 308

1.e10

10

f

/

0.

/.

mtabntai , ( sta). . atinin , _. ! : : : 456 disp 22 3 _ -

si mnmh.

11

tim fn . mtabht : f. sta : 3.14 ,

area = 3.14

radius

radius ;

:

: .. +

, .

12

: +,

, / , /, ..

- : taithta .

a a a

:

1. (),

2. : 3. : +,

- .

a/b

c a/ b

c

ann sstism

:

, a aist ta xi, :

a/b a

c c

a/b

c

14

b

d

a

b

c

. ?

b

a

b ??

,/

b

a

b

13

b

a

b

4+3*4-3 (4 + 3) * (4 - 3) = 7 4 + (3*4) - 3 = 13

a/b

c ??

/

d

nt ansh

, .

, !

1. .

2. tim

=.

RAM

(, )

=

x = x + 1;

451 232.77

451

452 232.77

f

f

f

c

c

16 15

= + 1; = ( - 32)*5/9; = 451;

{

{

disp() 2 :

disp( ) disp( ) : x: disp(x)

!: disp( Matlab is cool)! 13: disp(13) disp( 13) : disp( f , c) ( ).

(.) ;, . .. f = 451 c f, c

% 451 32) 5/9 % % % , 2

c = (f

fprintf (), .

: disp(1+1)

17

18

F C % %c

F,

F,

%f

Fahrenheit Celsius 32) 5/9;

% %c

f = 0; c = (f f = 1; disp( f ); disp(c); ... f = 212; disp( f ); disp(c);

%f

Fahrenheit Celsius

disp( f ); disp(c); c = (f 32) 5/9;

f = 0; while f >

=

==

=

air_temperature > 30.0 : 25 1 : . (= ): ,

45

1: n ?

n ( ). 2 n .

n n, 1, 2, . . . , n n.

46

1%

% num_div %n n = input( ? ); num_div = 0; for i = 1: n, if rem(n,i ) == 0, end if num_div == 2, disp( ); else , disp( ); end num_div = num_div + 1; end

47

1: 2 1 : 1000000 2. > 1.

> 2: n 2, .

> n : 1 = 2 n 1 2 = n, 1 , 2 < ). n (

48

2%

% divisor % is_prime %n % % % n = input( ? ); % is_prime = 1; % if rem(n,2)==0, is_prime = 0; else = 1, = 0,

49

divisor = 3; while divisor numpass(student_friendly), student_friendly = i ; end end disp( student_friendly );63

size .

A p

q :

size (A,1) p A, size (A,2) q A.

x n . : size (x ,2)

n (

64

: list = input( ); small = list (1); large = list (1); for i =2: size ( list ,2), if list ( i ) < small , small = list ( i ); else, if end end end list ( i ) > large , large = list ( i ); % % % size

65

for i =1:100, x( i ) = i ; % end :x

{ 1 x i=1; x(i)=i; 1

{2 x i=2; x(i)=i;

{1 2 3 i=3; x(i)=i;66

(.)x = zeros (1,100); % 100 for i =1:100, x( i ) = i ; % end :0 1 1 1 0 0 2 2 0 0 0 3 0 0 0 0 x ..... x ..... x ..... x .....

0 0

0 0

0 0

67

: plot. : x , y , x , y , . . . xn , yn x

y

100

80

, plot (x, y) , x , y ,

60 40 20 0

0

0.1

0.2

0.3

0.4

0.5

x ,y

100 80 60 40 20 0

x ,y

xn , yn

!

x , y , ..., xn

, yn

0

0.1

0.2

0.3

0.4

0.5

68

-

x

.

.

x

.

.

0.6

0.7

0.8

0.9

0.6

0.7

0.8

0.9

0 1

) # %& (' $ # # % % " # & $ " "

!

-

0 0

0 0

0 0

x=zeros(1,100); i=1; x(i)=i; i=2; x(i)=i; i=3; x(i)=i;

1

1

20

15

10

5

0

5 2

... xi , yi .

69

In = input( ? ); xright = 4; x = zeros (1, n ); y = x; i = 1; for t= xleft : h: xright , x( i ) = t ; i = i + 1; end plot (x , y );

xleft =

2; xleft )/( n 1); % x % x % y

h = ( xright

y( i ) = t^3

2 t^2

5 t + 6;

70

n x

, x , x , . . . , xn

:

y

x

x

x

,

x

1

0

1

) % ,2

0 %

.

3

4

.

IIh = input( x ? ); xright = 4; x = zeros (1, n ); y = x; i = 1; for t= xleft : h: xright , x( i ) = t ; i = i + 1; end plot (x , y );

xleft =

2; xleft )/h ) + 1; % % x % y % xright 5 t + 6;

n = fix (( xright

y( i ) = t^3

2 t^2

71

Horner ( xi , yi ), :

y( i ) = t^3

2 t^2

5 t + 6;

5 (2 t^3, 2 2 t^2 1 5 t). :

y( i ) = (( t 2) t

5) t + 6;

mn 2 . : / ().

72

h x

, x

x

h, x

x

h, . . .

h = 0.25; xleft =

4; xright = 3; i = 1;

n = fix (( xright

xleft)/h ) + 1;

%

x = zeros (1, n ); y = x ; for t= xleft :h: xright , x( i ) = t ; if denom == 0,

denom = t^2

t

2;

% disp( t );

disp( : ); else , disp(x( i )); end i = i + 1; end disp(y( i ));

y( i ) = ( t^3

2 t^2

5 t + 6) / denom;

73

)

h

.

, : y

x

x

x

/ x

x

,

,

0 %

%

#

74

. , , .

75

a snthsh ( amma), , istfi .

76

()

- ( ).

- .

- : .

-, mbima .

77

. , . ?

: 1. ( Matlab) 2. .

, :

:

) ia a siasmnh snthsh it san ma t

aamt

. . . .

78

( /) , ,

interface (

79

N

function p = Factorial (n) %FACTORIAL(N) pgz ! p = 1; return

Matlab: a = Factorial (5); : function c = Combinations(n, k) %COMBINATIONS(N,K) k return

81

c = Factorial (n ) / ( Factorial (k )

for i =1:n , p = p

function [p x ] = (p ) return [ x ] = ( );

80

N

i ; end

Factorial (n k));

function E = circle_area( r ) E = pi r r ; return

function E = ring_area( inner , outer) %RING_AREA b kkk inner_area = circle_area( inner ); % circle_area outer_area = circle_area(outer ); return

ti mtabht.

hmintai . an na n .

.

aiintai antifnta .

E = outer_area

% inner , outer

k/xk k

inner_area;

82

83

r1 = input( ); r2 = input( ); disp(ring_area( r1 , r2 )); Matlab

Matlab .m

: FUNCTION 1

Matlab, .m circle_area circle_area.m ring_area ring_area.m

84

FUNCTION 2

FUNCTION 3

FUNCTION 4

FUNCTION 5

FUNCTION 6

FUNCTION 7

FUNCTION 8

85

: 1. .

2. . 3. . 4. 5. . 6. .

86

function is_prime = isPrime (n) %ISPRIME(N) p 1 p, 0 is_prime = 1; if ( rem(n,2)==0 & n>2) n==1, is_prime = 0; else divisor = 3; limit = sqrt (n)+1; while divisor