Newton's Method to Find Complex Roots and Discovering a Fractal

Landing page with pretty pictures, will update


import math

max_iter = 100000 # maximum number of interations i'm allowing
usr_input = input ("Enter info for Newton's method (x_start, epsilon) : ")
x_start, eps = usr_input.split (",")
x_start = float(x_start)
eps = float(eps)

# polynomial we want the roots of
def f(x):
 return (2*x*x + x + 3 - math.exp(x))

# get slope of tangent line at a point x with derivative
def slope(x):
 return ((f(x+eps)-f(x))/eps)

# find x value at which line tangent to curve intersects x-axis
def get_xcross(x):
 return (x - (f(x)/slope(x)))

# iterate to perform newton's method
x = x_start
y = 100 #distance from 0
cnt = 0
while abs(y) > eps:
 x = get_xcross(x)
 print("x is", x)
 y = f(x)
 cnt = cnt + 1
 if cnt > max_iter:
  print("oops this function seems to not have roots")

if cnt < max_iter:
 print("this is the root of your function: ", x)


<