Last active
May 21, 2020 04:41
-
-
Save robintux/aea2fa8cc36b686c3a862afa1d011208 to your computer and use it in GitHub Desktop.
Metodo de la secante 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 secant(f,a,b,N): | |
'''Aproximacion de la solucion para f(x)=0 en el intervalo [a,b] | |
por el metodo de la secante | |
https://en.wikipedia.org/wiki/Secant_method | |
Parametros de entrada | |
---------- | |
f : function | |
La función para la cual estamos tratando de aproximar una solución f(x)=0. | |
a,b : numeros reales | |
El intervalo [a,b] es donde se busca la solucion. La funcion retorna | |
None si f(a)*f(b) >= 0 pues a solucion no esta garantizada | |
N : Entero positivo | |
NUmero de iteraciones. | |
Returns | |
------- | |
m_N : Numero real | |
El intercepto con el eje X de la linea secante en el n-esimo intervalo | |
m_n = a_n - f(a_n)*(b_n - a_n)/(f(b_n) - f(a_n)) | |
Si los signos de f(a_n), f(b_n) y f(m_n) son iguales en alguna iteracion, | |
entonces el metodo de la secante falla . | |
Ejemplos | |
-------- | |
>>> f = lambda x: x**2 - x - 1 | |
>>> secant(f,1,2,5) | |
1.6180257510729614 | |
''' | |
if f(a)*f(b) >= 0: | |
print("El metodo de la secante falla.") | |
return None | |
a_n = a | |
b_n = b | |
for n in range(1,N+1): | |
m_n = a_n - f(a_n)*(b_n - a_n)/(f(b_n) - f(a_n)) | |
f_m_n = f(m_n) | |
if f(a_n)*f_m_n < 0: | |
a_n = a_n | |
b_n = m_n | |
elif f(b_n)*f_m_n < 0: | |
a_n = m_n | |
b_n = b_n | |
elif f_m_n == 0: | |
print("Se encontro solucion exacta.") | |
return m_n | |
else: | |
print("El metodo de la secante FALLA.") | |
return None | |
return a_n - f(a_n)*(b_n - a_n)/(f(b_n) - f(a_n)) | |
# ============================================================================= | |
# Algunas funciones de ejemplo | |
# ============================================================================= | |
# Ejemplo 1 | |
f = lambda x: x**2 - x - 1 | |
secant(f,1,2,25) | |
# Ejemplo 2 | |
def func(x): | |
return x*x*x - x*x + 2 | |
secant(func,-2,0,50) | |
# Ejemplo 3 | |
def ProblemaRoot(x): | |
return x*x*x - x - 2 | |
secant(ProblemaRoot,1,2,50) | |
# Ejemplo 4 : https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/10RootFinding/bisection/examples.html | |
def raizCuad(x): | |
return x*x -3 | |
secant(raizCuad,1,2,50) | |
# Ejemplo 5 : https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/10RootFinding/bisection/examples.html | |
import math as m | |
def raizCuad(x): | |
return m.exp(x)*(3.2*m.sin(x) -0.5*m.cos(x)) | |
secant(raizCuad,3,4,50) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment