Created
July 26, 2012 23:12
-
-
Save hectorh30/3185171 to your computer and use it in GitHub Desktop.
Problema 1 - Proyecto 1 - Métodos numéricos - Ciclo 2 2012 - UVG: Definición de los 2 métodos cerrados y los 2 abiertos, y código driver para resolución del problema planteado
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Wed Jul 18 15:56:11 2012 | |
@author: hectorh30 | |
""" | |
def punto_fijo(f,g,x_init,max_error=0.01,max_iterations=100): | |
""" | |
INPUT | |
- Functions f and g: functions required by the method | |
- Float x: initial estimation | |
- Float max_error, max_iterations: self-explanatory | |
OUTPUT | |
A tuple of (final_result,iteration_number,final_relative_porcentual_error) | |
""" | |
error = max_error + 1.0 | |
i = 0 | |
x0 = x_init | |
x1 = x0 | |
while (error > max_error and i < max_iterations): | |
x1 = g(x0) | |
error = abs(((x1 - x0)/x1)*100) | |
x0 = x1 | |
i += 1 | |
return x1,i,error | |
def newton_raphson(f,f1,max_error=0.01,max_iterations=100): | |
""" | |
INPUT | |
- Functions f and g: functions required by the method | |
- Float x: initial estimation | |
- Float max_error, max_iterations: self-explanatory | |
OUTPUT | |
A tuple of (final_result,iteration_number,final_relative_porcentual_error) | |
""" | |
error = max_error + 1.0 | |
i = 0 | |
x0 = x_init | |
x1 = x0 | |
while (error > max_error and i < max_iterations): | |
x1 = x0 - (f(x0)/f1(x0)) | |
error = abs(((x1 - x0)/x1)*100) | |
x0 = x1 | |
i += 1 | |
return x1,i,error | |
def biseccion(f,lower_x,upper_x,max_error=0.01,max_iterations=100): | |
""" | |
INPUT | |
- Function f: function required by the method | |
- Float lower_x,upper_x: boundaries of the initial interval | |
- Float max_error, max_iterations: self-explanatory | |
OUTPUT | |
A tuple of (final_result,iteration_number,final_relative_porcentual_error) | |
""" | |
error = max_error + 1.0 | |
i = 0 | |
left_x = lower_x | |
right_x = upper_x | |
continue_flag = True | |
if (f(left_x)*f(right_x) < 0): | |
x0 = left_x | |
while (error > max_error and i < max_iterations and continue_flag): | |
xr = (left_x + right_x)/2 | |
if (f(left_x)*f(xr) < 0): | |
right_x = xr | |
elif (f(left_x)*f(xr) > 0): | |
left_x = xr | |
else: | |
continue_flag = False | |
error = abs(((xr - x0)/xr)*100) | |
i += 1 | |
x0 = xr | |
return xr,i,error | |
else: | |
return lower_x,-1.0,-1.0 | |
def falsa_posicion(f,lower_x,upper_x,max_error=0.01,max_iterations=100): | |
""" | |
INPUT | |
- Function f: function required by the method | |
- Float lower_x,upper_x: boundaries of the initial interval | |
- Float max_error, max_iterations: self-explanatory | |
OUTPUT | |
A tuple of (final_result,iteration_number,final_relative_porcentual_error) | |
""" | |
error = max_error + 1.0 | |
i = 0 | |
left_x = lower_x | |
right_x = upper_x | |
continue_flag = True | |
if (f(left_x)*f(right_x) < 0): | |
x0 = left_x | |
while (error > max_error and i < max_iterations and continue_flag): | |
xr = right_x - (f(right_x)*(left_x - right_x))/(f(left_x)-f(right_x)) | |
if (f(left_x)*f(xr) < 0): | |
right_x = xr | |
elif (f(left_x)*f(xr) > 0): | |
left_x = xr | |
else: | |
continue_flag = False | |
error = abs(((xr - x0)/xr)*100) | |
i += 1 | |
x0 = xr | |
return xr,i,error | |
else: | |
return lower_x,-1.0,-1.0 | |
""" | |
Driver para métodos abiertos | |
""" | |
print '' | |
print '-------------------------' | |
print ' -- Métodos abiertos' | |
print '-------------------------' | |
print '' | |
x_init = float(raw_input("Ingrese una estimación inicial: ")) | |
max_error = float(raw_input("Ingrese el porcentaje de error deseado: ")) | |
max_iteraciones = int(raw_input("Ingrese el numero máximo de iteraciones: ")) | |
""" | |
Método del punto fijo | |
""" | |
print '-------------------------' | |
print ' -- Metodo de punto fijo' | |
print '-------------------------' | |
def f(x): | |
return -20.0 + 10.0/(1.0+x) + 10/((1.0+x)**2) + 10/((1.0+x)**3) | |
def g(x): | |
return (-1.0/2.0) + 1.0/(2.0+2.0*x) + 1.0/(2*(1+x)**2) | |
x1_pf,i_pf,error_pf = punto_fijo(f,g,x_init,max_error,max_iteraciones) | |
print 'Cantidad de iteraciones: ',i_pf | |
print 'Resultado final: ',x1_pf | |
print 'Error final: ',error_pf | |
""" | |
Método de Newton-Raphson | |
""" | |
print '-------------------------' | |
print ' -- Metodo de Newton-Raphson' | |
print '-------------------------' | |
def f(x): | |
return -20.0 + 10.0/(1.0+x) + 10/((1.0+x)**2) + 10/((1.0+x)**3) | |
def f1(x): | |
return -10.0/((1.0+x)**2)-20.0/((1.0+x)**3)-30.0/((1.0+x)**4) | |
x1_nr,i_nr,error_nr = punto_fijo(f,g,x_init,max_error,max_iteraciones) | |
print 'Cantidad de iteraciones: ',i_nr | |
print 'Resultado final: ',x1_nr | |
print 'Error final: ',error_nr | |
""" | |
Driver para métodos cerrados | |
""" | |
print '' | |
print '-------------------------' | |
print ' -- Métodos cerrados' | |
print '-------------------------' | |
print '' | |
left_x_init = float(raw_input("Ingrese un x menor que la raíz: ")) | |
right_x_init = float(raw_input("Ingrese un x mayor que la raíz: ")) | |
max_error = float(raw_input("Ingrese el porcentaje de error deseado: ")) | |
max_iteraciones = int(raw_input("Ingrese el numero máximo de iteraciones: ")) | |
""" | |
Método de bisección | |
""" | |
print '-------------------------' | |
print ' -- Método de bisección' | |
print '-------------------------' | |
def f(x): | |
return -20.0 + 10.0/(1.0+x) + 10/((1.0+x)**2) + 10/((1.0+x)**3) | |
x1_b,i_b,error_b = biseccion(f,left_x_init,right_x_init,max_error,max_iteraciones) | |
if i_b < 0: | |
print "El intervalo no define un cambio de signo explícito" | |
else: | |
print 'Cantidad de iteraciones: ',i_b | |
print 'Resultado final: ',x1_b | |
print 'Error final: ',error_b | |
""" | |
Método de falsa posición | |
""" | |
print '-------------------------' | |
print ' -- Método de falsa posición' | |
print '-------------------------' | |
def f(x): | |
return -20.0 + 10.0/(1.0+x) + 10/((1.0+x)**2) + 10/((1.0+x)**3) | |
x1_fp,i_fp,error_fp = falsa_posicion(f,left_x_init,right_x_init,max_error,max_iteraciones) | |
if i_b < 0: | |
print "El intervalo no define un cambio de signo explícito" | |
else: | |
print 'Cantidad de iteraciones: ',i_fp | |
print 'Resultado final: ',x1_fp | |
print 'Error final: ',error_fp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment