Sie sind auf Seite 1von 48

MATLAB

200900

2004

: .., ..-..,

: . , ..., . ..

:

. , , , , .

2004 .

.......................................................................................................... 3
..................................................................................................... 4
1........................................................................................ 5
MATLAB ........................................................................... 5
1. .......................................... 29
2...................................................................................... 32
................................................ 32
2. .......................................... 44
1 .................................................................................................... 46
..................................... 46
2 .................................................................................................... 47
MATLAB.............................................................. 47
3 .................................................................................................... 47
........................................................... 47
......................................................................................................... 48

MATLAB ,
.
, .
.
MATLAB, . , -,
.
MATLAB.
. ,
( ,
), , .
MATLAB: , , ,
, . : , , , Symbolic Math Toolbox.

5
1

MATLAB
MATLAB , , C.
(Command Window), matlab.exe. (Workspace), ,
.
.
MATLAB. : , .
>>. , , . . = .
Enter
. :
a=2+51-37
Enter,
:
a = 16
, , ,
MATLAB (Workspace). clc ,
.
, clear clear(1, 2, ).

6
, 1 2. who ,
.

Enter.
MATLAB . MATLAB, File Save Workspace As .
mat, -.
:
File Load Workspace .
double
MATLAB
, , 2.85093+11,
10. double. MATLAB short
, .
:
res=5.345*2.868/3.14-99.455+1.274
:
res = -93.2990
res,
:
format long

res

7
Enter :
res = -93.29900636942675

MATLAB.

, ( Enter) :
format short
.
double
: +, -, *, /
^ .
. , .

,
Enter.
MATLAB
.
, (
, )
() .
help elfun. 1 .
, :
2*asin(1)
, :
ans = 3.1416,

8
. MATLAB
: pi. ( MATLAB
2).
MATLAB , , ( : round,
: fix). mod
, sign , lcm , perms
nchoosek .
, .
double .
. ( 1):
1

<

<=

>

>=

~=

==

1,
0. , .

:
a=1; b=2; c=3;
res=(a<b)+(c~=b)+(b==a)
:
res = 2

9
,
2:
2

&

(),
().
( ) ( ). ( ),
1 (); 0 (). 0 ()
, ( ) .
.
.

,
double .
i j. ,
, , . ,
:
2+3i; -6.789+0.834e-2*i; 4-2j; x+y*i;
. :
res=sin(2+3i)*atan(4i)/(1-6i)
:

10
-1.8009 - 1.9190i
: abs ( ), conj ( ), imag ( ), real ( ), angle ( ),
isreal (, ). 1.

( ) . .
.
, :
c=2+3i; d=2i;
c>d
, .
( ), .

, [ ].

:
al=[1 2 3]; d=[1+2i,2+3i,3-7i];

, .
. , :

11
al(3)=789;
. , :
al(2)=(al(1)+al(3))/2;

.
:
al(4)=7;
1 length,
, :
length(al)
ans = 4
1 -
:
al=[al 7];
, :
a3(1)=67; a3(2)=7.8; a3(3)=0.017;
.

, (
).
, () . :
diap=3.7:0.3:8.974;
,
( ) .
, , length
( ).
()
. -

12
,
.
:
a=[1 2; 3 4; 5 6]
ENTER, :
a=
1

6
3x2 ( ,

) -:
a=[[1 2];[3 4];[5 6]];
-:
a=[[1;3;5],[2;4;6]];

whos( ), ndims,
size.
, . , ,
. :
a(1,1)=1; a(1,2)=2; a(2,1)=3;
a(2,2)=4; a(3,1)=5; a(3,2)=6;
,
, ones (m,n) zeros(m,n),
(m , n ).

,

13
, .
.
,
, , reshape (,
, N), M N
, ,
MATLAB . : ,
.. ( ) : (, double), , .
. , reshape .
, .' ( ). ,
A=[1 1 1; 2 2 2; 3 3 3];
B=A.'
B=
1

' ()

.
, MATLAB, . MATLAB ,

14
. MATLAB , MATLAB
,
. , ( 8 16 ), MATLAB .

, . - MATLAB
. MATLAB
,
.
( ) + -.
:
A=[1 1 1; 2 2 2; 3 3 3]; B=[0 0 0; 7 7 7; 1 2 3];
A+B
,
, , .
+ ( )
, .
A+5
ans = 6

15

, ,
, : .* , ./,
.^. , * /
.
./, ,
.\.
. ./ (k, m)
/ (k, m), .\ (k, m) / (k, m).
*
.
\ MATLAB .
,
Ay = b,
N x N, b N, -
\ b ( : A1 B ).
,
,
, MATLAB. ,
cross, :
u=[1 2 3]; v=[3 2 1];
cross(u,v)
ans =
-4

-4

16

sum, (
). , ,
() . , :
sum(u.*v)
u v. : u*v.
, :
sqrt(sum(u.*u))
.
,
. , , , .
, , eye, , rand,
, 0 1.
,
F=rand(3)
33 ,
0 1.
, R=rand(2,3),
R 2x3.
rand .

17
det.
, ,
sum, prod, sum, , . max min .
, , . sort ,
.
,

MATLAB , , .
,
MATLAB,
. ,
x=0:0.01:pi/2; y=sin(x);
sin 158 , x
158 .

MATLAB . ( ).
y:
x=0:0.01:2; y=sin(x);
:
plot(x,y)
(. 1).

18

. 1. y=sin(x)
MATLAB
, Figure.
,
z=cos(x);
plot(x,z)
(
clf, cla 0 1).
,
plot hold
on, :
x=0:0.01:2; y=sin(x);
plot(x,y)

19
z=cos(x);
hold on
plot(x,z)
(. 2), :
x=0:0.01:2; y=sin(x); z=cos(x);
plot(x,y,x,z)

. 2. y=sin(x), z=cos(x),
, , .
. plot figure,

.

subplot. -

20
,
.
, sin cos ,
exp() (. 3):
w=exp(x);
subplot(1,2,1); plot(x,y,x,z)
subplot(1,2,2); plot(x,w)

. 3. y=sin(x), z=cos(x) w=exp(x),


. subplot ,
, , (

21
). subplot :
subplot(1,1,1)
,
.
semilogx, semilogy loglog.
(. 4) polar.
phi=0:0.01:2*pi; r=sin(3*phi);
polar(phi,r)

. 4. r=sin(3*phi)
,
,
. ,
x=0:0.1:3; y=sin(x);
plot(x,y,'r-',x,y,'ko')

22
(. 5),
.
plot ,
. 'r-',
( r), . , 'ko', ( k) ( o) .

. 5. y=sin(x)
plot (x1, y1, s1, x2, y2, s2, ) y1(x1), y2(x2),
s1, s2, . .
s1, s2, , ().
( 3). ( 4).
( 5).
. , . ,

23
, , 'r+-' '-+r'
.
3.
,

--

-.

4
,

5
,

, , , .
, . MATLAB
, . MATLAB
. -

24
MATLAB,

.
axis ( [ xmin, xmax, ymin, ymax ] )

xlabl, ylabl, title text. xlabl , ylabl (
).
, text.
title. , grid on, . (. 6):

. 6. y=sin(x), ,

25
x=0:0.1:3; y=sin(x);
plot(x,y,'r-',x,y,'ko')
title('Function sin(x) graph');
xlabel('xcoordinate'); ylabel('sin(x)');
text(2.1, 0.9, '\leftarrowsin(x)'); grid on
text ,
.
, ,
.

\ ( ).


MATLAB . , .
.
, ,
, ,
, - . plot3,

(. 7).
:
t=0:pi/50:10*pi;
x=sin(t);
y=cos(t); plot3(x,y,t); grid on
, .

26

. 7. , plot3

plot3
, , , , .
:
u=-2:0.1:2; v=-1:0.1:1;
[X,Y]=meshgrid(u,v);
z=exp(-X.^2-Y.^2);
plot3(X,Y,z)
(. 8).
plot3 ,
, yOz.
MATLAB ,
.

27

. 8. , plot3
m-.
,
, m- MATLAB ( m). script-m- ( )
, MATLAB, .
MATLAB. MATLAB File/New/M-file (
,
). script- , script- ( ). , , ,
MATLAB, , , , -

28
, script.

. MATLAB. File/Set Path . Path/Add to path.

29

1.
1. :
3, 25 1, 07 2,34

10,10 0, 25 4, 78 .
5, 04 7, 79 3,31

2. 33 , 0 1.
3. :
A + 10 B,

A B, BT ,

,
( ),
,
.
4. ,
: ones zeros:
0

5.71 .

3.61

5. :

A X = C .
6. D:

D = sin( A) + B 3/ 5 .
7. :


a = {3, 2; 2,8; 1, 4} b = {0, 6; 3, 2; 4,8}

   
a
: b ; a b ;


a .

8. :

30

y = e x

z = arctg( x1/ 2 )
x [ 0, 4 ]

, ,
. .

9. y(x) z(x)
. .

10. :

f ( x, y ) = ln( x 2 + y 2 xy )
x, y [1, 2]


1. ?
2. MATLAB ?
3. MATLAB ?
4. MATLAB.
5. MATLAB.
6. , .
7. .
8. .
9. ?
10. ?
11. ?

31
12. ?
13. , ? ?
14. ?
?
15. m-? , m-?

32



MATLAB / \.

A y = B ,
A NxN, a B
N, \

A\ B .
\ ,
A B A \ B , inv( A) * B . inv( A) ,
A .
/ . A / B B * inv( A) . ,

y A = B .

F ( x) = 0 , , :

fzero(name, x0) .
, , . fzero name
x 0 . , , cos( x )
0 pi. x0 = 1 .

33

x=fzero('cos', 1)
x = 1.5708
, ( MATLAB) MATLAB
, , .
, , cos( x ) = x ,

y = cos( x) x , MATLAB
. (
- MATLAB) :

function y=MyFunction1(x)
y=cos(x)-x;
MyFunction1.m,
MATLAB ( cd). fzero:

x=fzero('MyFunction1',pi/2)
x = 0.7391
,
. , .
, fzero :

fzero( name, x0, tol ),


tol ().
, (
):

format long
= fzero('MyFunction1',pi/2, le-8)

34

x =0.73908513263090
MyFunction1(x)
ans = 9.778322596076805e-010,
, .
, fzero . ,
. MATLAB roots, . , 4 3 + 2 - + 2,
(1 2) (i -i),
(
):

Coef = [ 1, -3, 3, -3, 2 ],


roots:

= roots( Coef )
= 2.00000000000000
0.00000000000000 +1.OOOOOOOOOOOOOi
0.00000000000000 -1.OOOOOOOOOOOOOOi
1.00000000000000
,
. roots
fzero ( ).


MATLAB
. -

35
, .
fmin:

fmin( name, x0, xl ).


name , ,

x0 xl .

fmins:

xmin = fmins( name, x0 ).


name , , x0 - ,
. (0,0).

function = MyFunc2 ( )
= x(1)^2+ x(2)^2;
MyFunc2.m
MATLAB. fmins:

xmin = fmins( 'MyFunc2', [1,1] );


xmin :

xmin(1)
ans =-2.102352926236483-005
xmin(2)
ans =2.548456493279544-005
, .
, , ,
.

36


MATLAB trapz:

Integ = trapz ( , ) ;
()
.
. ,
( ). : ,
.

i/10.

cos( x)dx .
0

dx = pi/10;
= 0:dx:pi;
y=cos(x);
I1 = trapz(x,y);
I1 = 5.5511e-017
, .

.
MATLAB : quad ( c) quad8 ( - 8- ). -

37

. ,
, .
.
quad8 quad, , (
). MATLAB, quad quad8 .

quad8(name, x1, x2)


: name,
x1 x2. , . , (
), Inf.
,
, . , quad, quad8 ( trapz) .
.
:

= sin(t); = 2cos(t); z =3t; 0 <= t <= 2


, 5. -

M = 5ds ,
C

38

:
2

M = 5

( x ) 2 + ( y ) 2 + ( z ) 2 dt .

function z = MyFunc321( t )
z = sqrt( cos(t).^2 + 4*sin(t).^2 + 9 ),
MyFunc321.m, quad:

= 5 * quad( 'MyFunc321', 0, 2 );
= 34.2903
MATLAB dblquad. :
1

( x sin( y) + y sin( x))dxdy .


0

function z = Fof2Var( x, )
z = x.*sin(y) + y.*sin(x);
( Fof2Var .m) dblquad:

J = dblquad( 'Fof2Var', 0, 1, 1, 2 );
J = 1.1678

MATLAB : ode23, ode45, odell3, odel5s, ode23s, ode23t

ode23tb.
s -

39

ode45, - 45- (
).
n , 1, 2, ..., n (
):

1' = Fl( , 1, 2, ..., n );


2' = F2( , 1, 2, ..., n );
...
n' = Fn( , 1, 2, ..., n).
- Y F, 1, 2, ..., n Fl, F2, ..., Fn,
. :

Y' = F( , Y ).
ode45, F( , Y ).
, , :
y + y + K x 2 = 0
:
y (0) = 1
.
y (0) = 0

:
1' = 2 + * * ;
2' = -1.
1(0) = 0, 2(0) = 1. - . = 0
. , .

40
- Y :
Y = [ 1, 2 ].
1 2
, 1 2 -. F , = 0.01,
MyDifEql:

function F = MyDifEql( , )
F = [ 0.01 * * + y(2); -y(l) ];
MyDifEql.m.
, ,
, .
ode45,
[0,1] [0,20].

[ X, Y ] = ode45( 'MyDifEql',[0,20],[0,1] );
:

plot (X, Y(:, 1), X, Y(:, 2))


(Y(:, 1) , Y(:, 2)
).
,
-:
, .
. .
--, :

41
1' = 2;
2' = -y1 + * (1 - 1 * 1) * 2
1(0) =2, 2(0) = 0. = 1000 - . MyVanDerPol, :

function F = MyVanDerPol( , )
F = [ (2); -(1) + 1000*( 1 - (1)^2 ) * (2) ].
-- [0, 3000] odel5s:

[X, Y] = odel5s ( 'MyVanDerPol', [0,3000], [2,0] ),


.

.

Symbolic Math Toolbox ,
.
MATLAB ,
. sym syms. sym
- , :
sym x .
: ( simplify)

x2 y 2
.
x y
sym x;
sym y;
simplify((x^2-y^2)/(x-y))
ans = +
syms , , :

42

syms x y z;
, ,
expand,
:

rez=expand(S) ,
S , ;
rez . :

syms x y;
rezl=expand(sin(x+y) }
rezl = sin (x) *cos () +cos (x) *sin (y)
diff,
:

diff(y(x)),
() - . ,
:

syms x;
D=diff (x^2+4*x^5)
D = 2*x+20*x^4
n ,
:

D =diff (S, 'v' ,n),


S , v .
S .

syms x ;
S=x^3*y^2+sin(x*y) ;
D=diff (S,'x' ,2)
D = 6*x*y^2-sin (x*y)*y^2

43
MATLAB
dsolve, :

1. y=dsolve( 'Dy(x)' ) ,
D() -; - dsolve .

2. y=dsolve ('Dy (x)' , ' ' ) ,


D() -; - .
D, D2 .
dsolve . :

[f,g]=dsolve('Df(x),Dg(x)', ''),
Df(x) ,Dg(x) - ; - .
: :
'() = 0.6-0.2y(x)
c (0)=0.

dsolve(' Dy = 0.6 - 0.2*y ', ' (0)=0 ')


ans = 3+exp(-l/5*t)*3.
,
:
expand ;
factor ;
det ;
inv ;
int ;
limit ;
taylor ;
solve .

44

2.
1. :

x + y z = 36
x + z y = 13 .
y+ zx=7
2. :

y = 3 x 3 ,

y = x 4 2 x 2 + 10 .

3. :
lim (cos x)

1
x sin x

x 0

4. y(x):
a)

y = ln exp(2 x) + 1

b)

x 2 y 2 + 2ln( xy ) = 4

5. :

x +1

y=

1 x x

6. :
/4

cos(2 x) + sin 2 ( x)
dx .

sin(3
x
)
/7

7. :
1

x
0

exp( x + sin( y )) cos( y )dxdy .

8. :

ln x
dx .
x

9.
y(x) [1,10]:

45

x 2 y + 3 xy + y = 1/ x
y (1) = 1
.
y (1) = 0
10. :

y = exp( x + y ) + exp( x y ) .

1. ?
2. MATLAB?
3. ?
4. ?
5. MATLAB?
6.
MATLAB?
7. ?
8. MATLAB?
9. n- ?
10. dsolve.

46



a^x

x^a

sqrt(x)

exp(x)

log(x)

log10(x)

abs(x)

fix(x)

floor(x)

ceil(x)

round(x)

rem(x,y)

x x y

mod(x,y)

x x y

sign(x)

factor(x)


sin(x)

sinh(x)

asin(x)

asinh(x)

cos(x)

cosh(x)

acos(x)

acosh(x)

tan(x)

tanh(x)

atan(x)

47
atanh(x)0

cot(x)

coth(x)

acot(x)

acoth(x)

MATLAB
i, j

pi

eps

(
2-52)

realmin

realmax

inf

NaN

ans


abs

conj

imag

real

angle

isreal

48

1. .., .. MATLAB 5.X. , ,


. .:-, 2000. 336 .
2. .., .. MATLAB 5.0/5.3.
. .: , 1999. 640 ., .
3. ., . .
. .: , 2001. 624 ., .
4. .., .., .. :
Mathcad 2000, MATLAB 5. . .: , 2001. 524 .
5. . MatLab 6. .: -, 2002.
6. . Matlab6. . .: , 2001.
7. . MATLAB. .: -, 2000

Das könnte Ihnen auch gefallen