import math
a = 10
x = 4
finished = False
while not finished:
print(x)
y = (x + a / x) / 2
if abs(y - x) < 0.00000000000000001:
finished = True
x = y
4 3.25 3.1634615384615383 3.1622778816927752 3.162277660168387 3.162277660168379
def square_root(a):
finished = False
x = -10
while not finished:
print(x)
y = (x + a / x) / 2
if abs(y - x) < 0.00000000000000001:
finished = True
x = y
return x
square_root(10)
-10 -5.5 -3.659090909090909 -3.196005081874647 -3.16245562280389 -3.162277665175675 -3.162277660168379
-3.162277660168379
math.sqrt(10)
3.1622776601683795
def newton(f, df, x0, e):
while abs(0 - f(x0)) > e:
x0 = x0 - f(x0) / df(x0)
return x0
def f(x):
return x ** 2 - 10
def df(x):
return 2 * x
newton(f, df, 1, 0.000000001)
3.1622776601683795
import scipy.optimize
scipy.optimize.newton(f, 4)
3.162277660168379