Advertisement
cardel

Untitled

Jun 17th, 2023
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. """
  2. 0.05 = (x/(x+1))*raiz(6/(2+x))
  3.  
  4. f(x) = (x/(x+1))*raiz(6/(2+x)) - 0.05
  5.  
  6. """
  7. import math
  8.  
  9. def f(x):
  10.     return (x/(x+1))*math.sqrt(6/(2+x))-0.05
  11.  
  12. def df(x):
  13.     return (math.sqrt(6/(2+x)) - (x/(x+1))*(1/math.sqrt(6/(2+x))))/(x+1)**2
  14.  
  15. def newtonRaphson(x0, tol, n):
  16.     print('\n\n*** Metodo de Newton-Raphson ***')
  17.     i = 1
  18.     while i <= n:
  19.         x1 = x0 - f(x0)/df(x0)
  20.         print('Iteracion-%d, x1 = %0.6f y f(x1) = %0.6f' % (i, x1, f(x1)))
  21.         if abs(f(x1)) < tol:
  22.             print('\nLa raiz es: %0.8f' % x1)
  23.             return x1
  24.         x0 = x1
  25.         i = i + 1
  26.     print('\nNo se encontro la raiz')
  27.     return None
  28.  
  29. x0 = float(input('Ingrese el valor inicial: '))
  30. tol = float(input('Ingrese la tolerancia: '))
  31. n = int(input('Ingrese el numero maximo de iteraciones: '))
  32. newtonRaphson(x0, tol, n)
  33. print(f(0.02995472))
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement