"""
Template for Newton fractal computation and plotting.
Feel free to understand and modify the code.
More info about Newton fractals can be found i.e. at https://en.wikipedia.org/wiki/Newton_fractal
This file contains two parts doing the same thing. The first one is easier
to understand, whereas the second one is more optimized (though it can be
surely optimized better) and is a typical example of using the NumPy library.
PLEASE USE ONLY ONE PART (whichever you like) !!!
Disclaimer: This is only a template. It's NOT guaranteed that it will work as
it is for any f. So feel free to experiment with settings and the code.
"""
#---------BEGIN OF 1ST PART---------
import math
import numpy as np
import matplotlib.pyplot as plt
def newton(f,df,z0,maxiter=100,tol=1e-12):
"""
Implementation of Newton method
f: function of which we're trying to find a root
df: derivative of f
z0: initial guess
maxiter: maximum number of iterations (try to change)
tol: tolerance for estimating if method converged
"""
for i in range(maxiter):
if i == maxiter-1:
zlast = z0
z0 = z0 - f(z0)/df(z0)
# we guess if method has converged
converged = (abs(zlast-z0)