Last active
May 21, 2020 04:41
-
-
Save robintux/8f8b5cd9ea8ef4dc765b5fe260bd8aeb to your computer and use it in GitHub Desktop.
Metodo de Newton-Raphson para el calculo de una raiz de una funcion no lineal
This file contains 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
# ============================================================================= | |
# Abraham Zamudio [GMMNS] | |
# 2020/04/27 | |
# Lima-Peru | |
# ============================================================================= | |
def newton(f,Df,x0,epsilon,max_iter): | |
'''Aproximacion de la solucion para f(x)=0 en el intervalo [a,b] | |
por el metodo de Newton | |
Parametros de entrada | |
--------------------- | |
f : Funcion | |
Funcion a la cual se le buscara una solucion a f(c)=0 | |
Df : Funcion | |
Derivada de f(x). | |
x0 : Numero real | |
Punto inicial (punto de partida) para f(x)=0. | |
epsilon : numero real | |
El criterio de parada es abs(f(x)) < epsilon. | |
max_iter : numero entero positivo | |
Maximo numero de iteraciones para el metodo de newton | |
Salida de la funcion | |
------- | |
xn : numero real | |
IMplementacion del metodo de newton: Calcula la aproximacion lineal | |
de f(x) en xn y encuentra el intercepto x usando la formula | |
x = xn - f(xn)/Df(xn) | |
Continua hasta que abs(f(xn)) < epsilon y retorna xn | |
Si Df(xn)==0, retorna None. | |
Si el numero de iteraciones excede a max_iter, entonces retorna None | |
Ejemplos | |
-------- | |
>>> f = lambda x: x**2 - x - 1 | |
>>> Df = lambda x: 2*x - 1 | |
>>> newton(f,Df,1,1e-8,10) | |
Found solution after 5 iterations. | |
1.618033988749989 | |
''' | |
xn = x0 | |
for n in range(0,max_iter): | |
fxn = f(xn) | |
if abs(fxn) < epsilon: | |
print('Encontro solucion luego de ',n,'iteraciones.') | |
return xn | |
Dfxn = Df(xn) | |
if Dfxn == 0: | |
print('La derivada de la funcion es cero. NO se encuentra solucion') | |
return None | |
xn = xn - fxn/Dfxn | |
print('Numero de iteraciones excedido. NO se encuentra solucion') | |
return None | |
# ============================================================================= | |
# Algunas funciones de ejemplo | |
# ============================================================================= | |
# Ejemplo 1 | |
f = lambda x: x**2 - x - 1 | |
Df = lambda x:2*x-1 | |
newton(f,Df,1,1e-6,50) | |
# Ejemplo 2 | |
def func(x): | |
return x*x*x - x*x + 2 | |
Dfunc = lambda x:3*x*x-2*x | |
newton(func,Dfunc,-2,1e-6,50) | |
# Ejemplo 3 | |
def ProblemaRoot(x): | |
return x*x*x - x - 2 | |
def DerivadaRoot(x): | |
return 3*x*x-1 | |
newton(ProblemaRoot,DerivadaRoot,2,1e-9,50) | |
# Ejemplo 4 : https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/10RootFinding/bisection/examples.html | |
def raizCuad(x): | |
return x*x -3 | |
def DraizCuad(x): | |
return 2*x | |
newton(raizCuad,DraizCuad,1,1e-6,50) | |
# Ejemplo 5 : https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/10RootFinding/bisection/examples.html | |
import math as m | |
def F(x): | |
return m.exp(x)*(3.2*m.sin(x) -0.5*m.cos(x)) | |
def DF(x): | |
return m.exp(x)*(3.7*m.sin(x) + 2.7*m.cos(x)) | |
newton(F,DF,0,1e-6,50) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment