You are on page 1of 39

MATLAB

..

2005
1

1.

Matlab;

1.1.

Matlab;

1.2.

Command Window Matlab

1.3.

Matlab

1.4.

Matlab

1.5.

11

2.

15

2.1.

Gauss

15

2.2.

Gauss_Jordan

17

2.3.

18

2.4.

18

3.

20

3.1.

20

3.2.

20

3.3.

22

3.4.

Newton-Raphson

23

4.

26

4.1.

26

5.

29

5.1.

29

5.2.

30

6.

34

6.1.

34

39



.

Matlab.

.

.

.


.
,

.



Computational Analysis

.

Matlab.

,
.


Matlab. ,
,

.

.

,
,
e-mail.
. .
enistaz@aegean.gr

1.

Matlab;

1.1

Matlab;

Matlab
.
-

.
.


- .
,
, :
(1) help
(2) lookfor.
Matlab
.

,
.
help.

,
.
abs

absolute=.
Command Window Matlab:
help abs
:
ABS

Absolute value.

ABS(X) is the absolute value of the elements of X. When


X is complex, ABS(X) is the complex modulus (magnitude) of
the elements of X.
See also SIGN, ANGLE, UNWRAP.

:
() Matlab
(.. C/C++)
, ,
.
() , .. =+i,
abs(X)=sqrt(^2+^2)
.
() help

( abs)
, ,
help.

Matlab lookfor.
. matlab .

.

abs.
Command Window Matlab:
lookfor absolute
help file
absolute . lookfor,
:
MAUIFINDEXE Return the absolute pathname to a MAUI executable.
ABS

Absolute value.

CIRCLEPICK Pick bad triangles using an absolute tolerance


MAD

Mean absolute deviation.


.
help .
Matlab
-
.
Matlab (help lookfor)
Command Window. ;

1.2

Command Window Matlab;

Matlab
.

.
components ,
.
desktop
Matlab x,y x,y version number .

Command Window.
Matlab
- .

,
.


Editor.

1.3

Matlab;

Commad Window edit.


, Editor
Matlab. Editor
directory Matlab.
,
,
. atlab
. , Command Window,
directory
.

, :
edit
directory
:
cd sub-directory

sub-directory
:
cd ..
Command
Window
Editor.

1.4

Matlab;

, Matlab
-
.
Editor. Editor ,

, directory . ,
File

Save as..
directory
.
,
Command Window directory.

.
.
deyterobathmia.m :
1:
%%% We solve the Equation ax^2+bx+c=0

clear;
clc;
close all;

a = 1;
b = 1;
c = 1;

D = sqrt(b^2-4*a*c);

p1 = (-b+D)/(2*a);
p2 = (-b-D)/(2*a);

p1r = real(p1);
p1i = imag(p1);

p2r = real(p2);
p2i = imag(p2);

fprintf('p1=%.4f %.4fi\np2=%.4f %.4fi\n',p1r,p1i,p2r,p2i);

%
.

clear
.

clc ,
.

close all
.

.
a 1, b 1 c 1.

. ,
, x2+x+1=0.

D.
sqrt .

, p1 p2.

(p1r, p2r) (p1i, p2i)


.

. %.4f
4 f (float) .
\n .


Matlab. ,
(..
C/C++) .
:
o
o ( C/C++,
)
o compile .
Matlab
.
Matlab
.
33
.
grammiko1.m :
2:
%%% a11x + a12y + a13z = a14
%%% a21x + a22y + a23z = a24
%%% a31x + a32y + a33z = a34

clear;
clc;
close all;

for(i=1:1:3)
for(j=1:1:4)
fprintf('a%d%d=',i,j);
a(i,j)=input('');
fprintf('\n');
end
end

for(i=1:1:3)
for(j=1:1:3)
Dxyz1(i,j)=a(i,j);
end
end

for(i=1:1:3)

for(j=1:1:4)
if(j==4)
k=1;
else
k=j;
end
Dx1(i,k)=a(i,j);
end
end

for(i=1:1:3)
for(j=1:1:4)
if(j==4)
k=2;
else
k=j;
end
Dy1(i,k)=a(i,j);
end
end

for(i=1:1:3)
for(j=1:1:4)
if(j==4)
k=3;
else
k=j;
end
Dz1(i,k)=a(i,j);
end
end

Dxyz = det(Dxyz1);
Dx = det(Dx1);
Dy = det(Dy1);
Dz = det(Dz1);

x = Dx/Dxyz;
y = Dy/Dxyz;
z = Dz/Dxyz;

10

fprintf('x=%.4f\ny=%.4f\nz=%.4f\n',x,y,z);

,
.

a(i,j)
3 .

33
x,y,z.

det
a(i,j)
a i j .

,
Matlab
. ,

det. , input
( ,
).

, ,
. Matlab ,

.
()

.

1.5


-.
. ,
.

. directory
,
function
.

11

2
.
a11, a12,
34
x,
y, z.
.
grammiko2.m,
fun1.m fun2.m. , ,
3
4 5.
:
3:
%%% a11x + a12y + a13z = a14
%%% a21x + a22y + a23z = a24
%%% a31x + a32y + a33z = a34

clear;
clc;
close all;

li = 3;
co = li+1;

[a]=fun1(li,co);

for(i=1:1:li)
for(j=1:1:co-1)
Dxyz1(i,j)=a(i,j);
end
end

for(i=1:1:li)
for(j=1:1:co)
if(j==4)
k=1;
else
k=j;
end
Dx1(i,k)=a(i,j);
end

12

end

for(i=1:1:li)
for(j=1:1:co)
if(j==4)
k=2;
else
k=j;
end
Dy1(i,k)=a(i,j);
end
end

for(i=1:1:li)
for(j=1:1:co)
if(j==4)
k=3;
else
k=j;
end
Dz1(i,k)=a(i,j);
end
end

fun2(Dxyz1, Dx1, Dy1, Dz1);

4:
function [b]=fun1(lin,col);

for(i=1:1:lin)
for(j=1:1:col)
fprintf('a%d%d=',i,j);
b(i,j)=input('');
fprintf('\n');
end
end

13

5:
function a=fun2(Dxyz2, Dx2, Dy2, Dz2);

Dxyz = det(Dxyz2);
Dx = det(Dx2);
Dy = det(Dy2);
Dz = det(Dz2);

x = Dx/Dxyz;
y = Dy/Dxyz;
z = Dz/Dxyz;

fprintf('x=%.4f\ny=%.4f\nz=%.4f\n',x,y,z);



. ,


.

directory .

.

Matlab .


.
, :
(1)

(2)

(3)

(4)

(5)

14

2.

2.1

Gauss

Gauss

.
,
,
.

:
(1)

,
,
.

.

.
, ,
. ,
.
:
fun1.m 4.
, a
.

Gauss 6
gauss1.m.
6:
%%% a11x + a12y + a13z = a14
%%% a21x + a22y + a23z = a24
%%% a31x + a32y + a33z = a34

clear;
clc;
close all;

li = 3;
co = li+1;

[a]=fun1(li,co);

15

[a]=fun3(li,co,a);

[r]=fun4(li,co,a);

for(j=1:1:li)
fprintf('x%d=%.4f\n',j,r(j));
end

fun1.m
fun3.m fun4.m
.
. 7 8 ,
fun3.m fun4.m:
7:
function [a]=fun3(lin,col,a);

for(diag=1:1:lin-1)
for(i=diag+1:1:lin)
a_temp=-a(i,diag)/a(diag,diag);
for(k=1:1:col)
a(i,k)=a(i,k)+a(diag,k)*a_temp;
end
end
end

8:
function [re]=fun4(lin,col,a);

for(i1=lin:-1:1)
sum = 0;
for(c1=i1+1:1:col-1)
sum = sum+a(i1,c1)*re(c1);
end
re(i1)=(a(i1,col)-sum)/a(i1,i1);
end

16


. ,
.

Gauss-Jordan.

2.2

Gauss-Jordan

Gauss

.

.
( )
Gauss, . ,

( gauss1.m)
fun3.m fun4.m .

fun3_b.m fun4_b.m
gauss2.m.
fun3_b.m fun4_b.m 9
10
9:
function [a]=fun3_b(lin,col,a);

for(diag=1:1:lin)
for(i=1:1:lin)
if(i~=diag)
a_temp=-a(i,diag)/a(diag,diag);
for(k=1:1:col)
a(i,k)=a(i,k)+a(diag,k)*a_temp;
end
end
end
end

17

10:
function [re]=fun4_b(lin,col,a);

for(i1=1:1:lin)
re(i1) = a(i1,col)/a(i1,i1);
end

(Gauss) (Gauss-Jordan)

.

.

2.3

Gauss Gauss-Jordan

.
,
.



.


.
.

2.4


:
= =
.

33.

18

.
33 :

a11 a12 a13


A = a21 a22 a23
a31 a32 a33
:

b11 b12 b13


B = b21 b22 b23
b31 b32 b33
:

a11 a12 a13 b11 b12 b13 b11 b12 b13 a11 a12 a13 1 0
a a a b b b = b b b a a a = 0 1
21 22 23 21 22 23 21 22 23 21 22 23
a31 a32 a33 b31 b32 b33 b31 b32 b33 a31 a32 a33 0 0

0
0 .
1

a11 a12 a13 b11 b12 b13 1 0


a a a b b b = 0 1
21 22 23 21 22 23
a31 a32 a33 b31 b32 b33 0 0

0
0
1


:
a11 a12 a13 b11 1
a a a b = 0
21 22 23 21
a31 a32 a33 b31 0

a11 a12 a13 b12 0


a a a b = 1
21 22 23 22
a31 a32 a33 b32 0

a11 a12 a13 b13 0


a a a b = 0 .
21 22 23 23
a31 a32 a33 b33 1

bij
i,j=1..3 B.

.
19

3.

3.1


, Boltzano.
:
Boltzano:
f [a,b]
f(a)f(b)<0
(a,b) f(x)=0.

,
:
f ( x) = x 5 + x + 1
f(-1)=-1 f(1)=3 f(-1)f(1)<0

(-1,1).

(a,b)


.

.
Matlab.

3.2

,
.
Boltzano
,
.

:

20

. , a b,
f
(f(a)f(b)<0).
Boltzano
.
(a,b)
c=(a+b)/2. f(c)
(a,c) f(a)f(c)<0, (c,b)
f(c)f(b)<0.
Boltzano
.

.

. :
()

Boltzano.
()

()


Boltzano.
()


.
matlab
dixotomish1.m :
11:
clear;
clc;
close all;

epsilon = 1e-6;
a = -1;
b = 1;

while(abs(b-a)>epsilon)
fa = fx(a);
fb = fx(b);
c=(a+b)/2;
fc = fx(c);

21

if(fa*fc<0)
b=c;
else
a=c;
end
end

fprintf('a=%.7f\nb=%.7f\nf(a)=%.7f\nf(b)=%.7f\n',a,b,fa,fb);


fx. f(x)
.
:
12:
function [f]=fx(x);

f = x^5+x+1;

matlab

.

.

3.3

,

.
a
Boltzano , .


. ,

( )
( ).
Newton-Raphson
.

22

3.4.

Newton Raphson


Newton - Raphson x
f(x) ( f()=0), f
Taylor . ,

f ( ) = f ( x) + f ( x)( x) + ...
f() = 0,
f,
, . ,
, ,

= x

f ( x)
+ ...
f ( x)

,
x - xn, (
) xn+1,
. , NewtonRaphson :

xn+1 = xn

f ( xn )
f ( xn )

NewtonRaphson.

.



.
matlab Newton-Raphson,
nr1.m :
13:
clear;
clc;
close all;

23

diaf=1;
dx=1e-2;

epsilon=1e-4;
a=1;

while(diaf>epsilon)
fa = fx(a);
dfa = dfx(a,dx);
anew = a - fa/dfa;
diaf = abs(anew-a);
a = anew;
end

fprintf('a=%.7f\nf(a)=%.7f\n',a,fa);

: fx
dfx. ( fx) .
( dfx) fx x.
x :

f ( x) =

f ( x + dx) f ( x)
dx

dx .
,
. Matlab
:
14:
function [dfun]=dfx(x,dx);

f1 = fx(x+dx);
f2 = fx(x);
dfun = (f1-f2)/dx;


fx.
(nr1.m) (dfx)
(fx).

24

dfx
dx.


f x.
dx->0

.
.
.

,
.

.

, , Matlab
. ,

,
. . Matlab .

.

25

4.

4.1


y(x)
(xi,yi).

yi = yi y( xi ) = 0
y(x)
(xi,yi) (xi+1,yi+1).
.

y(x).

.
, (x1,y1) (x2,y2), x1 x2

y 2 y1
x 2 x1

y ( x ) = y1 + ( x x1 ) = y1 +

y 2 y1
( x x1 )
x 2 x1

(),
3 :
(xi,yi), i=1,2,3, x1 x 2
y(x) 2 .

x3



.
. , 3
:
(xi,yi), i=1,2,...,, x1 x 2 ... x N
y(x) (-1)- .

26



.
:
()
3
(x1,y1), (x2,y2) (x3,y3).
()
2 .
: f(x)=ax2+bx+c. (xi,yi)
.
()

:
y1=ax12+bx1+c
y2=ax22+bx2+c
y3=ax32+bx3+c

33
a,b,c.

.
,
.

,
.

parembolh1.m
:
15:
clear;
clc;
close all;
fprintf('arithmos shmeiwn=');
numb1 = input('');
for(n1=1:1:numb1)
fprintf('x%d=',n1);
x(n1) = input('');
fprintf('y%d=',n1);
y(n1) = input('');
end
for(m=1:1:numb1+1)
for(k=1:1:numb1)
if(m<=numb1)
matr(k,m) = x(k)^(numb1-m);

27

else
matr(k,m) = y(k);
end
end
end

, .


.
, .

. ,
Matlab .

.
, , Matlab
,
. ,

.

28

5.

5.1

f ( x) =

f ( x + dx) f ( x)
dx

dx .
dx

.


dx.

dx.
dx.
parag1.m
16 :
16:
%% function f(x)=6x^10
%% first derivative f'(x)=60x^9

clc;
clear;
close all;
i=1;

x=8;
for(dxn=0:-0.1:-15)
dx = 10^dxn;
dxplot(i) = dx;
fx = 6*x^10;
fxdx= 6*(x+dx)^10;
dfx=60*x^9;
df = (fxdx-fx)/dx;
sf(i) = abs(df-dfx)/dfx;
i=i+1;
end
loglog(dxplot,sf);
xlabel('dx');
pause(0);

29



. :

dx
10-8 10-7. , ,
.
2-5%
dx .

.
,
,
. .
.

5.2

dx

30

.
dx .

.
.

.
.

, ,

,
.
:
()


.
()
dx
.

diff1.m 17
:
17:
clc;
clear;
close all;
fprintf('Paragwgos sto x=');
sdiff = input('');
fprintf('dx=');
dx = input('');
fprintf('Arithmos shmeiwn ths f(x)=');
numb1 = input('');
for(i=1:1:numb1)
fprintf('x%d=',i);
xy(i,1)=input('');
fprintf('y%d=',i);
xy(i,2)=input('');
end
for(m=1:1:numb1+1)
for(k=1:1:numb1)
if(m<=numb1)
matr(k,m) = xy(k,1)^(numb1-m);
else
matr(k,m) = xy(k,2);
end
end

31

end
[xyz]=grammiko3(numb1,matr);
[res] = diff2(xyz,sdiff,dx);
fprintf('x=%.10f\ndx=%.15f\nf(%.10f)=%.8f\nf`(%.10f)=%.8f\n',...
sdiff,dx,sdiff,fun5(xyz,sdiff),sdiff,res);


.
grammiko3.m diff2.m 18 19 .
18:
function [xyz]=grammiko3(numb,a)
for(i=1:1:numb)
for(j=1:1:numb)
Dxyz(i,j,numb+1)=a(i,j);
end
end
for(det_number=1:1:numb)
for(i=1:1:numb)
for(j=1:1:numb+1)
if(j==numb+1)
k=det_number;
else
k=j;
end
Dxyz(i,k,det_number)=a(i,j);
end
end
end

for(root_number_1=1:1:numb+1)
Detxyz(root_number_1) = det(Dxyz(:,:,root_number_1));
end
for(root_number_2=1:1:numb)
xyz(root_number_2) = Detxyz(root_number_2)/Detxyz(numb+1);
end

19:
function [dres] = diff2(xyz,x,dx);
dres = (fun5(xyz,x+dx) - fun5(xyz,x))/dx;

19 (diff2.m) , fun5.m
20 .

32

20:
function [fx] = fun5(xyz,x);
len1 = length(xyz);
fx = 0;
for(i=1:1:len1)
fx = fx + xyz(i)*x^(len1-i);
end

3 :
()

.
.
()


(;).
()
, ,
(dx)
.

.
.

.


.
.

33

6.

6.1

,
, , .
.
.

.

. :
b

I = f ( x)dx
a


f(x)
xx .
(
: , ,
Simpson )
.
f(x)
a
b ( ) f(a).

.
,
.
, , .

a b

.


. dx.
:
dx; . ,


.
dx
.

34

int1.m 21
:
21:
clc;
clear;
close all;
fprintf('a=');
aint = input('');
fprintf('b=');
bint = input('');
fprintf('dx=');
dx = input('');
fprintf('Arithmos shmeiwn ths f(x)=');
numb1 = input('');
for(i=1:1:numb1)
fprintf('x%d=',i);
xy(i,1)=input('');
fprintf('y%d=',i);
xy(i,2)=input('');
end
for(m=1:1:numb1+1)
for(k=1:1:numb1)
if(m<=numb1)
matr(k,m) = xy(k,1)^(numb1-m);
else
matr(k,m) = xy(k,2);
end
end
end
[xyz]=grammiko3(numb1,matr);
[res] = int2(xyz,aint,bint,dx);
fprintf('a=%.3f\nb=%.3f\ndx=%.15f\nsum(%.3f,%.3f)=%.8f\n',...
aint,bint,dx,aint,bint,res);


grammiko3.m int2.m.
( 18) 22
:
22:
function [res] = int2(xyz,aint,bint,dx);
numb_dx = ceil(bint-aint)/dx;
dx = ceil(bint-aint)/numb_dx;
res = 0;

35

for(x=aint:dx:bint)
res = res + fun5(xyz,x)*dx;
end

fun5.m
.
dx
.
.
. dx

dx
,
.
dx
dx
.
. ,

.
.


.


. ,

.
.
int3.m 23 :
23:
clc;
clear;
close all;
icount=1;
aint = 0;
bint = 2;
for(dxn=0:-0.5:-7)
dx = 10^dxn
dxpl(icount) = dx;
res = 0;
for(x=aint:dx:bint)
res = res + (x^6)*dx;
end
result1(icount) = res;
result2(icount) = (bint^7-aint^7)/7;
result3(icount) = abs(result1(icount)result2(icount))/result2(icount);

36

icount = icount + 1;
end
loglog(dxpl,result3);

. ,
,
(
) , (dx)
.
.
dx;
.
, dx
. , ,
.

dx 23.

( )
dx ( ) 10-7
. 10-7

.
37


.
.

dx.

38

1.

, .. .. ,
, 2000.

2.

Numerical Methods Using Matlab, G. Lindfield and J. Penny, Prentice Hall,


2000.

3.

Mathematical Explorations with Matlab, K. Chen, P. Giblin and A. Irving,


Cambridge University Press, 1999.

4.

, .. .. ,
, 1997.

5.

,
G.E. Forsythe, M.A. Malcolm and C.B. Moler, ..
.. , , 2000.

6.

Numerical Mathematics, A. Quarteroni, R. Sacco and F. Saleri, Springer,


1991.

7.

Numerical Mathematics and Computing, W. Cheney and D. Kincaid,


Thomson Brooks/Cole, 2004.

8.

, .. , , 2002.

39

You might also like