Advertisement
cd62131

Newton's method

Dec 27th, 2018
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.30 KB | None | 0 0
  1. #!/usr/bin/python3
  2. def nsolve(n, a):
  3.     e = 1e-16
  4.     x = float(a)
  5.     while True:
  6.         x2 = x - (x**n - a) / (n * x**(n - 1))
  7.         if abs(x2 - x) < e:
  8.             break
  9.         x = x2
  10.     return x
  11.  
  12.  
  13. for n, a in [(2, 5), (4, 2)]:
  14.     x = nsolve(n, a)
  15.     print(f'x**{n} = {a}, x = {x}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement