You are on page 1of 45

ANALISIS NUMERICO

C U R SO

A NA L I SI S NU M E R I C O

D O C E NT E

: D R . WI L SO N M A C O V.

A L U M NO

G O I C O C H E A P R I NC I P E , E VE R I VA N

T RAB AJ O

APL I C AC I N D E PR OG R AMA C ON E L
M E T O D O D E L A B I SE C C I O N

CICLO

IV

T RU J I L L O- PE R
2010

PROGRAMAS MEJORADOS
%METODO DE LA BISECCION
function biseccion
clear;
clc;
fprintf('\calculo de la raiz de una ecuacion por mtodo de
Biseccin\n\n');
y=input('Dame la funcion:','s');
% Se ingresa la funcin
x1=input('Dame el intervalo inferior :'); % Se ingresa el intervalo
inferior
xu=input('Dame el intervalo superior :');
% Se ingresa el intervalo
superior
e=input('Dame el porciento del error :');
% Se ingresa el porciento
de error
xi=0;
ea=100;
c=1;
x=x1;
a=eval(y);
% Evalua la funcin en el intervalo ingresado
x=xu;
b=eval(y);
% Evalua la funcin en el intervalo ingresado
cc=a*b;
if cc>0;
fprintf('\n\nLos intervalos que ha ingresado para hacer el cculo
de la raiz\n\n');
fprintf('\nde la ecuacin no son los adecuados por no existir una
raz entre \n\n');
fprintf('\nestos, Ejecute de nuevo el programa, por favor.\n\n');
return
end
while ea>e
% Repite las declaraciones segun el numero definido de
veces
xr=(x1+xu)/2;
x=x1;
y1=eval(y);
x=xr;
yr=eval(y);
ea=abs((xr-xi)/xr)*100; % Establece la tolerancia
z=y1*yr;
if z<0
xu=xr;
end
if z>0
x1==xr;
end
if z==0
fprintf('\n\n\n\nLa raiz exacta es: %d',xr);
fprintf('\n\nNumero de iteraciones: %d',c);
break
end
xi=xr;
c=c+1;
end
if ea<e
fprintf('\n\n\n\nLa raiz exacta es: %d',xr);
fprintf('\n\nNumero de iteraciones: %d',c);
end

%fprintf: Convierte el resultado en una cadena de caracteres que


devuelve
%como valor de retorno.
% while: Repite las declaraciones un numero definido de veces
% if: Es un condicional que sirve apra ejecutar sentencias (es decir,
% si se cumple la condicin se realiza el ajuste indicado en la
% instruccion, caso contrario no se ejecuta).
% abs: valor absoluto

function biseccion1
f=input(' Ingrese la funcion f(x)=','s'); % Se ingresa la funcin
a=input(' Ingrese el Limite Inferior del intervalo que contiene a la
raiz buscada:'); % Se ingresa el intervalo inferior
b=input(' Ingrese el Limite Superior del intervalo que contiene a la
raiz buscada:'); % Se ingresa el intervalo superior
n=input(' Ingrese el Numero de Iteraciones:'); % Se ingresa el numero
de iteraciones
xmi=a; xma=b; % define como minimo al limite inferior y maximo al
limite superior
x=a; fa=eval(f); % Evalua la funcion en en el limite inferior
x=b; fb=eval(f); % Evalua la funcion en el limite superior
if fa*fb>0
% Evalua si cumple la condicion indicada
fprintf('En el intervalo dado no existen raices');
fprintf('\n')
else
i=1; % contador, i=1
while i<=n
r=(a+b)/2; % asgina a la variable r la operacion de biseccion
(pto medio)
x=a; fa=eval(f);
x=b; fb=eval(f);
x=r; fr=eval(f);
fprintf('\n') % Pasa a la siguiente linea
fprintf('%3.0f%10.6f%10.6f%10.6f%10.6f%\n',i,a,b,r);
if fr==0;
break
fprintf('la raiz es:\n'),r
else
if fa*fr<0 % si el producto de funciones es menor que
cero,
b=r;
% entonces b=r
else
% caso contrario,
a=r;
% se evalua para a=r
end
end
i=i+1; % se va iterando de modo consecutivo
end
fprintf('\nLa raiz es:\n'),r
end
x=xmi:0.1:xma; % define el intervalo para el domnio (con un paso de
0.1)
y=eval(f(x)); % asigna a la variable y la evaluacion de la funcion
f(x)

plot(x,y); % Grafica la funcion f(x)


xlabel('X') % Etiqueta el eje horizontal
ylabel('Y') % Etiqueta el eje vertical
title(['Grafica f(x)=',f]); % Establece un titulo a la grafica
end % finalizar
%else : comando que ejecuta las condiciones del if (es decir,"si se
cumple
% la condicion se ejecuta, caso contrario se evalua lo siguiente").
% plot : crea un grafico a partir de vectores y/o columnas de
matrices, con
% escalas lineales sobre ambos ejes.
% for : repite las declaraciones un numero determinado de veces.

Ejemplos de Aplicacin de los


Programas Biseccion y
Biseccin 1 realizados en clase
para comprobar la rapidez del
programa.
A continuacin se muestran algunos ejemplos que he desarrollado
del Burden usando el programa Biseccion hecho en clase:

1- Aplique el mtodo de biseccin Para encontrar soluciones


exactas dentro de 10-5 para los siguientes problemas.

* teniendo en cuenta con el nmero


de itraciones que deseamos hacer
tenemos:
=

Log

< 10-5

< -5

-n log 2 < -5
n >5/0.3 >1 6.66
n= 17

por tanto el Numero de iteraciones: 2>> 17

Ejemplo No1
Ingrese la funcion f(x)=x^2 - 4
Ingrese el Limite Inferior del
intervalo que contiene a la raiz
buscada:1
Ingrese el Limite Superior del
intervalo que contiene a la raiz
buscada:2,5
Ingrese el Limite Superior del
intervalo que contiene a la raiz
buscada:2
Ingrese el Numero de Iteraciones:17
TABLA DE VALORES DE LA BISECCION
1 1.000000 2.000000 1.500000
2 1.500000 2.000000 1.750000
3 1.750000 2.000000 1.875000
4 1.875000 2.000000 1.937500
5 1.937500 2.000000 1.968750
6 1.968750 2.000000 1.984375

7 1.984375 2.000000 1.992188


8 1.992188 2.000000 1.996094
9 1.996094 2.000000 1.998047
10 1.998047 2.000000 1.999023
11 1.999023 2.000000 1.999512
12 1.999512 2.000000 1.999756
13 1.999756 2.000000 1.999878
14 1.999878 2.000000 1.999939
15 1.999939 2.000000 1.999969
16 1.999969 2.000000 1.999985
17 1.999985 2.000000 1.999992
La raiz es:
r=
2.0000

http://www.euiti.upm.es/index/departamentos/
matematicas/webpersonal/webolga/Matematic
as_Especialidad/Ecuaciones_no_lineales/Tema_
2/Biseccion.htm
EJEMPLO No2 (libro de Burden pgina 108 ejemplos
No8)

Ingrese la funcin f(x)=(4.x-7)/(x-2)=0


Ingrese el limite Inferior:1.7
Ingrese el numero superior:1.9
Ingrese el numero de iteraciones:2

x=

1.7000
EJEMPLO No 3 ( libro de Burden pagina 42)
Ingrese la funcion f(x)=2.x^3 -x^2 +x -1
Ingrese el limite Inferior:-1
Ingrese el numero superior:6
Ingrese el numero de iteraciones:4

x=

-1

EJEMPLO No 4( libro de Burden pagina 44)


Ingrese la funcion f(x)=x^4-2x^34x^2+4x+4 =0
Ingrese el limite Inferior:-2
Ingrese el numero superior:2
Ingrese el numero de iteraciones:6

x=

-2
EJEMPLO No 5.b( libro de Burden pagina 44)
Ingrese la funcion f(x)=e^x+2^-x+2cos(x)-6
=0
Ingrese el limite Inferior:-1
Ingrese el numero superior:3
Ingrese el numero de iteraciones:2

x=

-1
EJEMPLO No6
Ingrese la funcion f(x)=sin(2.x)+tan(x)
Ingrese el limite Inferior:-2
Ingrese el numero superior:4
Ingrese el numero de iteraciones:3

x=

-2
EJEMPLO No7
Ingrese la funcion f(x)=(2.^(-x))-x
Ingrese el limite Inferior:0
Ingrese el numero superior:1
Ingrese el numero de iteraciones:17

x=

fa =

x=

fb =

-0.5000

1 0.000000 1.000000 0.500000


2 0.500000 1.000000 0.750000
3 0.500000 0.750000 0.625000

4 0.625000 0.750000 0.687500


5 0.625000 0.687500 0.656250
6 0.625000 0.656250 0.640625
7 0.640625 0.656250 0.648438
8 0.640625 0.648438 0.644531
9 0.640625 0.644531 0.642578
10 0.640625 0.642578 0.641602
11 0.640625 0.641602 0.641113
12 0.641113 0.641602 0.641357
13 0.641113 0.641357 0.641235
14 0.641113 0.641235 0.641174
15 0.641174 0.641235 0.641205
16 0.641174 0.641205 0.641190
17 0.641174 0.641190 0.641182
la raiz es:

r=

0.6412

EJEMPLO No
8( libro de
Burden
pagina 44)
Ingrese la funcion f(x)=(x.^4)(2*(x.^3))-(4*(x.^2))+4*x+4
Ingrese el limite Inferior:-2
Ingrese el numero superior:1
Ingrese el numero de iteraciones:7

x=

-2

fa =

12

x=

fb =

EJEMPLO No 9( libro de Burden pagina 44)


Ingrese la funcion f(x)=(x.^4)(2*(x.^3))-(4*(x.^2))+4*x+4
Ingrese el limite Inferior:-2
Ingrese el numero superior:1
Ingrese el numero de iteraciones:7

x=

-2

fa =

12

x=

fb =

En el intervalo dado no existen raices


Ingrese la funcion f(x)=tan(x)
Ingrese el limite Inferior:4
Ingrese el numero superior:4.5
Ingrese el numero de iteraciones:7

x=

fa =

1.1578

x=

4.5000

fb =

4.6373

En el intervalo dado no existen raices

EJEMPLO No 10( libro de Burden pagina 44)


Ingrese la funcion f(x)=x -2^-x
Ingrese el limite Inferior:0
Ingrese el numero superior:1
Ingrese el numero de iteraciones:17

x=

fa =

-1

x=

fb =

0.5000

1 0.000000 1.000000 0.500000


2 0.500000 1.000000 0.750000
3 0.500000 0.750000 0.625000
4 0.625000 0.750000 0.687500

5 0.625000 0.687500 0.656250


6 0.625000 0.656250 0.640625
7 0.640625 0.656250 0.648438
8 0.640625 0.648438 0.644531
9 0.640625 0.644531 0.642578
10 0.640625 0.642578 0.641602
11 0.640625 0.641602 0.641113
12 0.641113 0.641602 0.641357
13 0.641113 0.641357 0.641235
14 0.641113 0.641235 0.641174
15 0.641174 0.641235 0.641205
16 0.641174 0.641205 0.641190
17 0.641174 0.641190 0.641182
la raiz es:

r=

0.6412

EJEMPLO No11 (libro Burden pagina 44)

Ingrese la funcion f(x)=tan(2*x)


Ingrese el limite Inferior:4
Ingrese el numero superior:4.9
Ingrese el numero de iteraciones:7

x=

fa =

-6.7997

x=

4.9000

fb =

0.3939

1 4.000000 4.900000 4.450000


2 4.450000 4.900000 4.675000
3 4.675000 4.900000 4.787500
4 4.675000 4.787500 4.731250
5 4.675000 4.731250 4.703125
6 4.703125 4.731250 4.717188
7 4.703125 4.717188 4.710156
la raiz es:

r=

4.7102

EJEMPLO No12 (libro Burden pagina 44)

Ingrese la funcion f(x)=tan(pi*x)


Ingrese el limite Inferior:3
Ingrese el numero superior:4.5
Ingrese el numero de iteraciones:7

x=

fa =

-3.6739e-016

x=

4.5000

fb =

1.8146e+015

1 3.000000 4.500000 3.750000


2 3.750000 4.500000 4.125000
3 3.750000 4.125000 3.937500

4 3.937500 4.125000 4.031250


5 3.937500 4.031250 3.984375
6 3.984375 4.031250 4.007813
7 3.984375 4.007813 3.996094
la raiz es:

r=

3.9961

EJEMPLO No13 (libro Burden pagina 44)


Ingrese la funcion f(x)=tan(2*pi*x)+2
Ingrese el limite Inferior:2
Ingrese el numero superior:5
Ingrese el numero de iteraciones:7

x=

fa =

2.0000

x=

fb =

2.0000

EJEMPLO No14
Ingrese la funcion f(x)=3*x + tan(x)
Ingrese el limite Inferior:1
Ingrese el numero superior:4
Ingrese el numero de iteraciones:7

x=

fa =

4.5574

x=

fb =

13.1578

EJEMPLO No15
Ingrese la funcion f(x)=2*x^3 + tan(2*x)
Ingrese el limite Inferior:2
Ingrese el numero superior:4
Ingrese el numero de iteraciones:7

x=

fa =

17.1578

x=

fb =

121.2003

En el intervalo dado no existen raices

EJEMPLO No16
Ingrese la funcion f(x)=tan(2*x)
+cos(x)
Ingrese el limite Inferior:-1
Ingrese el numero superior:3

Ingrese el numero de iteraciones:7

x=

-1

fa =

2.7253

x=

fb =

-1.2810

1 -1.000000 3.000000 1.000000


2 -1.000000 1.000000 0.000000
3 0.000000 1.000000 0.500000
4 0.500000 1.000000 0.750000
5 0.750000 1.000000 0.875000
6 0.750000 0.875000 0.812500
7 0.750000 0.812500 0.781250
la raiz es:

r=

0.7813

EJEMPLO No17
Ingrese la funcion f(x)=sin(pi*x)
Ingrese el limite Inferior:-1
Ingrese el numero superior:3
Ingrese el numero de iteraciones:7

x=

-1

fa =

-1.2246e-016

x=

fb =

3.6739e-016

1 -1.000000 3.000000 1.000000


2 -1.000000 1.000000 0.000000
la raiz es:

r=

EJEMPLO No17

Ingrese la funcion f(x)=sin(2*pi*x)


+tan(2*x)
Ingrese el limite Inferior:2
Ingrese el numero superior:4
Ingrese el numero de iteraciones:7

x=

fa =

1.1578

x=

fb =

-6.7997

1 2.000000 4.000000 3.000000


2 2.000000 3.000000 2.500000
3 2.000000 2.500000 2.250000
4 2.250000 2.500000 2.375000
5 2.250000 2.375000 2.312500
6 2.312500 2.375000 2.343750
7 2.343750 2.375000 2.359375
la raiz es:

r=

2.3594

EJEMPLO No18
Ingrese la funcion f(x)=sin(2*pi*x)
+tan(2*x)
Ingrese el limite Inferior:2
Ingrese el numero superior:4
Ingrese el numero de iteraciones:7

x=

fa =

1.1578

x=

fb =

-6.7997

1 2.000000 4.000000 3.000000


2 2.000000 3.000000 2.500000
3 2.000000 2.500000 2.250000

4 2.250000 2.500000 2.375000


5 2.250000 2.375000 2.312500
6 2.312500 2.375000 2.343750
7 2.343750 2.375000 2.359375
la raiz es:

r=

2.3594

Ingrese la funcion f(x)=sin(2*pi*x)


+tan(2*x) +3
Ingrese el limite Inferior:2
Ingrese el numero superior:6
Ingrese el numero de iteraciones:7

x=

fa =

4.1578

x=

fb =

2.3641

En el intervalo dado no existen raices

EJEMPLO No19
Ingrese la funcion f(x)=cos(-pi*x)+3
Ingrese el limite Inferior:-1
Ingrese el numero superior:2

Ingrese el numero de iteraciones:7

x=

-1

fa =

x=

fb =

En el intervalo dado no existen raices

EJEMPLO No 20
Ingrese la funcion f(x)=cos(x)+(x.^4)(2*(x.^3))-(4*(x.^2))+4*x+4
Ingrese el limite Inferior:-1
Ingrese el numero superior:2
Ingrese el numero de iteraciones:7

x=

-1

fa =

-0.4597

x=

fb =

-4.4161

En el intervalo dado no existen raices

You might also like