Vous êtes sur la page 1sur 5

UNIVERSIDAD DE CUENCA

Fundada en 1867

P

g
i
n
a
1

TRABAJO CODIFICACION DEL METODO DE BISECCION Y DEL METODO DE NEWTON
RAPHSON EN DIFERENTES LENGUAJES DE PROGRAMACION

METODO DE BISECCION:

a) FORTRAN
PROGRAM BISECCION
REAL a,b,tol,p
INTEGER Nmax,i

WRITE(*,*)"Ingrese el extremo inferior del intervalo:"
READ(*,*) a
WRITE(*,*) "Ingrese el extremo superior del intervalo:"
READ(*,*) b
WRITE(*,*) "Ingrese la toleracia deseada:"
READ(*,*) tol
WRITE(*,*) "Ingrese el numero maximo de iteraciones:"
READ (*,*) Nmax

i = 1
DO WHILE (i<=Nmax)
p= (a+b)/2
IF (ABS(fun(p))<=tol) THEN
WRITE(*,*) "La raiz es:" ,p
exit
End IF
IF ((fun(a)*fun(p))<0) THEN
b = p
ELSE
a = p
END IF
i = i + 1
if (i>Nmax) then
write(*,*)'Se ha sobrepasado el numero de iteraciones'
end if
END DO
END PROGRAM

FUNCTION fun(x)
fun = (x**3) + 4*(x**2) - 10
RETURN
END FUNCTION


UNIVERSIDAD DE CUENCA
Fundada en 1867

P

g
i
n
a
2

b) MATLAB
clear all
clc
disp(' METODO DE LA BISECCION ');
disp(' ');
fun=input('Ingrese la funcin: ','s');
x0=input('Ingrese el lmite inferior del intervalo: ');
x1=input('Ingrese el lmite superior del intervalo: ');
tol=input('Ingrese el porcentaje de error: ');
f=inline(fun);

if f(x0)*f(x1)<0
x=x0;
while abs(f(x))>tol
x=(x0+x1)/2;
if f(x0)*f(x)<0
x1=x;
else
x0=x;
end
end
fprintf('\nSolucin:\n x=%9f\n',x)
else
fprintf('No existe una raz en ese intervalo');
end

c) PYTHON
def f(x): # defino la funcion, para este ejemplo esta funcion polinmica
valor = x**3+4*x**2-10
return valor

def biseccion():
# se ingresan los valores iniciales
a=input('ingrese el extremo inferior:')
b =input('ingrese el extremo superior:')
Nmax = input('Ingrese el numero de interaciones deseadas:')
tol = input('Ingrese la tolerancia deseada:')

i = 1 # inicio el contador de iteraciones
while i <= Nmax: #inicio de loop
p = (a+b)/2.0 # encuentro el punto medio en el intervalo [a,b]
if abs(f(p)) < tol: # este es el caso base del loop, en el cual encuentro la raiz con
una toleracia indicada
print 'La raiz es:',p
return


UNIVERSIDAD DE CUENCA
Fundada en 1867

P

g
i
n
a
3

if f(a)*f(p) < 0: #redefino el intervalo tomando la mitad donde esta la raiz
b = p
else:
a = p
i += 1 #actualizo el contador
print 'Se ha excedido el numero maximo de iteraciones' #excepcion cuando no se
llega a cumplir el caso
base
return
biseccion()



METODO DE NEWTON RAPHSON:

a) FORTRAN
PROGRAM NEWTON
REAL x0,tol,x
INTEGER Nmax,i
Nmax = 20
WRITE(*,*) "DIGITE LA APROXIMACION INICIAL"
READ(*,*) x0
WRITE(*,*) "DIGITE LA TOLERANCIA"
READ(*,*) tol

i = 1
DO WHILE(i <= Nmax)
x = x0-(f(x0)/d(x0))
IF ((ABS(x-x0))<=tol) THEN
WRITE(*,*) "la raiz es=",x
exit
END IF
x0 = x
i = i+1
if (i>Nmax) then
WRITE(*,*) "SE HA SOBREPASADO EL NUMERO MAXIMO DE INTERACIONES"
end if
END DO
END PROGRAM

FUNCTION f(x)
f = (x**3) + 4*(x**2) 10
RETURN
END FUNCTION


UNIVERSIDAD DE CUENCA
Fundada en 1867

P

g
i
n
a
4


FUNCTION d(x)
d = 3*(x**2) + 8*x
RETURN
END FUNCTION

b) MATLAB
fprintf ('PROGRAMA NEWTON REAPSON-MATLAP\n');
f=inline(input('Ingrese la funcion entre comillas:'));%el comando inline
nos permoite leer
la
funcion(ingresada
como string) y
utilizarla como tal
d=inline(input('Ingrese la derivada de la funcion entre comillas:'));
nmax=input('Ingrese el numero maximo de iteraciones:');
tol=input('Ingrese la tolerancia deseada:');
xo=input('Ingrese el valor de xo:');

while (nmax>0);%bucle para respetar el numero de iteraciones deseado
x=xo - f(xo)/d(xo);
nmax=nmax-1;
if abs(x-xo)<tol;
fprintf('La raiz buscada es:%f\n',x);
return
end
xo=x;
end
fprintf('Se a exedido el numero de iteraciones');


c) PYTHON
def funcion(x):
valor=x**3+4*x**2-10
return valor

def derivada(x):
valor=3*x**2+8*x
return valor

def metodo_de_newton () :
valor_inicial=input('ingrese el numero inicial:')
numero_max=input('ingrese el numero que ud. desee de interaciones:')
tol=input('ingrese la toleracia q ud, desee:')



UNIVERSIDAD DE CUENCA
Fundada en 1867

P

g
i
n
a
5

i=1
while i<=numero_max:
raiz=valor_inicial-float(funcion(valor_inicial))/derivada(valor_inicial)
resto=raiz-valor_inicial
raiznueva=abs(resto)
valor_inicial=raiz
i=i+1
if raiznueva<tol:
print "la raiz de la funcion es:",raiz
return
print "se ha sobrepasado el numero de interaciones"
return
metodo_de_newton()

Vous aimerez peut-être aussi