Herramientas Personales
Usted está aquí: Inicio materias Álgebra Lineal: Aplicaciones Físicas Numerico newton.py

newton.py

newton.py — Python Source, 1Kb

Contenido del Archivo

#  EL SIGUIENTE SCRIPT PERMITE BUSCAR RAICES DE POLINOMIOS CUBICOS
#  DE LA FORMA:
#  
#     C0 + C1 x + C2 x^2 + C3 x^3
#  
#  A TRAVES DEL METODO DE NEWTON.
#  
#  (https://en.wikipedia.org/wiki/Newton%27s_method)

C0 =  10.
C1 =  -1.
C2 = -10.
C3 =  -2.
print("Comenzando la busqueda de raices del polinomio:","\n","("+str(C0)+")"+" + "+"("+str(C1)+")"+" x + "+"("+str(C2)+")"+" x^2 + "+"("+str(C3)+")"+" x^3","\n")
#[1]

def polinomio(x):
  p = C0 + C1*x + C2*x**2 + C3*x**3
  return p
#[2]


def polinomio_derivado(x):
  p = C1+2*C2*x+3*C3*x**2
  return p

x = 23
print("Valor inicial de la busqueda: x="+str(x),"\n")
e = 100
while e > 0.0001:
#[3]
  x_previo = x
  print("Valores sondeados:",x)
  x = x - polinomio(x)/polinomio_derivado(x)
  e = abs(x-x_previo)/abs(x_previo)
  #[4]

print("\n"+"La raiz encontrada es x="+str(x),"con precision del "+str(e*100)+"%")

#  REFERENCIAS
#  [ 1 ]  PRINT: https://micro.recursospython.com/recursos/la-funcion-print.html
#  [ 2 ]  FUNCIONES: https://uniwebsidad.com/libros/python/capitulo-4/definiendo-funciones
#  [ 3 ]  WHILE: http://webs.ucm.es/info/aocg/python/lenguaje_programacion/index.html?highlight=bucle#bucle-while
#  [ 4 ]  ABS: http://www.w3big.com/es/python3/python3-func-number-abs.html
Acciones de Documento