newton.py

Created by tom-h

Created on April 15, 2018

641 Bytes


from kandinsky import *

# drawing area
xa = -1.0
xb = 1.0
ya = -1.0
yb = 1.0

# complex function
def f(z):
  return z * z * z - 1.0

# draw
def newton(m=20, h=1e-6, eps=1e-3):
  for y in range(222):
    zy = y * (yb - ya) / (221) + ya
    for x in range(320):
      zx = x * (xb - xa) / (319) + xa
      z = complex(zx, zy)
      i = 0
      ok = True
      while i < m*ok:
        # derivative
        dz = (f(z + complex(h, h)) - f(z)) / complex(h, h)
        z0 = z - f(z) / dz
        ok = abs(z0 - z) > eps
        z = z0
        i = i + 1
      col = color(i % 4 * 64, i % 8 * 32, i % 16 * 16)
      set_pixel(x, y, col)