Вы находитесь на странице: 1из 8

function r= Biseccion(fdex,a,b)

error=0.0005;
k=0;
m=0;
fxa= feval(fdex,a);
fxb=feval(fdex,b);
if fxa*fxb<=0
fprintf('\tk\t\ta\tb\t\t\tm\n',k,a,b,m);
while abs(b-a)/2>error
m=(a+b)/2;
fprintf('%5d%12.6f%12.6f%12.6f\n',k,a,b,m)
k=k+1;
fxm=feval(fdex,m);
if fxa*fxm<=0
b=m;
fxb=fxm;
else
a=m;
fxa=fxm;
end
end
r=m;
else
fprintf('Cambiar limites\n');
end

>> f=inline('2*x.^3-3*x-1')

f=

Inline function:
f(x) = 2*x.^3-3*x-1

>> raiz=Biseccion(f,-1.3,-0.7)
Error: File: Biseccion.m Line: 24 Column: 1
Illegal use of reserved keyword "else".

>> raiz=Biseccion(f,-1.3,-0.7)
k

0 -1.300000 -0.700000 -1.000000


1 -1.300000 -1.000000 -1.150000
2 -1.150000 -1.000000 -1.075000
3 -1.075000 -1.000000 -1.037500
4 -1.037500 -1.000000 -1.018750
5 -1.018750 -1.000000 -1.009375
6 -1.009375 -1.000000 -1.004688
7 -1.004688 -1.000000 -1.002344
8 -1.002344 -1.000000 -1.001172
9 -1.001172 -1.000000 -1.000586

raiz =

-1.0006

>> Biseccion(f,-0.5,-0.2)
k

0 -0.500000 -0.200000 -0.350000


1 -0.500000 -0.350000 -0.425000
2 -0.425000 -0.350000 -0.387500
3 -0.387500 -0.350000 -0.368750
4 -0.368750 -0.350000 -0.359375
5 -0.368750 -0.359375 -0.364062
6 -0.368750 -0.364062 -0.366406
7 -0.366406 -0.364062 -0.365234
8 -0.366406 -0.365234 -0.365820

ans =

-0.3658

>> Biseccion(f,1.3,1.5)
k

1.300000

1.500000

1.400000

1.300000

1.400000

1.350000

1.350000

1.400000

1.375000

1.350000

1.375000

1.362500

1.362500

1.375000

1.368750

1.362500

1.368750

1.365625

1.365625

1.368750

1.367188

1.365625

1.367188

1.366406

ans =

1.3664

function r= PuntoFijo(g,x)
tol=0.0005;
maxiter=50;
k=0;
fprintf('%5d%12.6f\n',k,x);
while 1
x0=x;
x=feval(g,x0);
k=k+1;
difer=x-x0;
fprintf('%5d%12.6f%12.6f\n',k,x,difer);
if (abs(x-x0)<=tol)|(k==maxiter)
break
end
end
if k<maxiter
r=x;
else
fprintf('Se supero el limite de iteraciones cambiar punto
inicial\n');
end

>> g=inline('1+x-x^2/4')

g=

Inline function:
g(x) = 1+x-x^2/4

>> PuntoFijo(g,-2.05)
0 -2.050000
1 -2.100625 -0.050625
2 -2.203781 -0.103156
3 -2.417944 -0.214163
4 -2.879558 -0.461614
5 -3.952522 -1.072964
6 -6.858130 -2.905608
7 -17.616615 -10.758486

8 -94.202898 -76.586283
9-2311.749390-2217.546492
10-1338357.060292-1336045.310902
11-447801243564.440310-447799905207.380000
12-50131488434912603000000.00000050131488434464802000000.000000
13-628291533174943990000000000000000000000000000.000000628291533174943990000000000000000000000000000.000000
14986875626648304380000000000000000000000000000000000000000000000
00000000000000000000000000.000000986875626648304380000000000000000000000000000000000000000000000
00000000000000000000000000.000000
15243480875618120850000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000.000000243480875618120850000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000.000000
16

-Inf

-Inf

17

-Inf

NaN

18

-Inf

NaN

19

-Inf

NaN

20

-Inf

NaN

21

-Inf

NaN

22

-Inf

NaN

23

-Inf

NaN

24

-Inf

NaN

25

-Inf

NaN

26

-Inf

NaN

27

-Inf

NaN

28

-Inf

NaN

29

-Inf

NaN

30

-Inf

NaN

31

-Inf

NaN

32

-Inf

NaN

33

-Inf

NaN

34

-Inf

NaN

35

-Inf

NaN

36

-Inf

NaN

37

-Inf

NaN

38

-Inf

NaN

39

-Inf

NaN

40

-Inf

NaN

41

-Inf

NaN

42

-Inf

NaN

43

-Inf

NaN

44

-Inf

NaN

45

-Inf

NaN

46

-Inf

NaN

47

-Inf

NaN

48

-Inf

NaN

49

-Inf

NaN

50

-Inf

NaN

Se supero el limite de iteraciones cambiar punto inicial


>> PuntoFijo(g,1.9)

1.900000

1.997500

0.097500

1.999998

0.002498

2.000000

0.000002

ans =

2.0000

function r= Newton_Raphson(f1,x)
tol=0.0005;
maxiter=50;
k=0;
fprintf('%5d%12.6f\n',k,x);
while 1
x0=x;
x=x0-f1(x0)/f1deriv(x0);
k=k+1;
fprintf('%5d%12.6f\n',k,x);
if(abs(x-x0)<=tol)|(k==maxiter)% mientras ocurra cualquiera de ellas
se hace falso el while
break
end
end
if k< maxiter
r=x;
else
fprintf('Se supero el limite de iteraciones cambiar punto
inicial\n');
end

>> f1=inline('x^5-2')

f1 =

Inline function:
f1(x) = x^5-2

>> Newton_Raphson(f1,1)
0

1.000000

1.200000

1.152901

1.148729

1.148698

ans =

1.1487

Вам также может понравиться