Skip to content

Instantly share code, notes, and snippets.

@arskiy
Created October 29, 2022 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arskiy/7aee8ad2f7cfac5ff7a5bf0c0a27301c to your computer and use it in GitHub Desktop.
Save arskiy/7aee8ad2f7cfac5ff7a5bf0c0a27301c to your computer and use it in GitHub Desktop.
import numpy as np
x = var('x')
f(x) = (x-80)*(x-52.2)*(x-4.4)*(x+3)*(x+70.2)*(x+7.4)
fp = diff(f,x)
NewtonIteration(x) = x - f(x)/fp(x)
roots = []
for i in np.arange(-100.0, 100.0, 0.2):
xn = i
# avoid possible infinite cycle
for j in range(100):
xn_1 = N(NewtonIteration(xn),digits=20);
if xn == xn_1:
if xn not in roots:
roots.append(xn)
break
xn = xn_1
print(roots)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment