3 ms·
Uhh, dude. Don't reinvent Newton iteration badly. from math import log EPSILON = 0.0001 def f(x): return x * log(x) + x - 6000 def i
by geophile 9y ago
Uhh, dude. Don't reinvent Newton iteration badly.
from math import log
EPSILON = 0.0001
def f(x):
return x * log(x) + x - 6000
def iterate(x):
return x - f(x) / (log(x) + 2)
def report(label, x):
print("%s: %s -> %s" % (label, x, f(x)))
x = 500
i = 0
while abs(f(x)) > EPSILON:
report(i, x)
x = iterate(x)
i += 1
report("final", x)
Output:
0: 500 -> -2392.695950788904
1: 791.2732929095519 -> 71.9490905475659
2: 782.9781524320051 -> 0.043632884977341746
final: 782.973115798371 -> 1.6199919627979398e-08