User:DX-MON/Complex Number Newton-Raphson differentiation

From Wikipedia, the free encyclopedia

This article outlines how to do Complex Number based Newton-Raphson iteration, and differentiating the complex number function to be iterated.

The Newton-Raphson iteration method is defined as:
for any arbitrary function

I am defining the following for this differentiation:


Expanding the function g(z)[edit]

The logical expansion of g(z) is:


which can be partially differentiated with respect to x or y as below:



can be used as the real component of the output of the function and the imaginary component of the output, except this is only true after switching two terms of the equations so that one is purely in terms of y and x and the other in terms of yi and x. This yealds the following for and :



Defining g'(z)[edit]

With the two partial differentiations, we can say that should be defined as:

where the output is represented by a vector who's x coordinate is the real value of the complex number and the y coordinate is the imaginary one in terms of . If only life were quite so simple as this though. The combination of the two partial differentiations is only two thirds of the picture - to have a working equation for programming this, we need to perform the full differentiation too (differentiate with respect to x and y together), so:

putting this into our definition of , we get:

Conclusion[edit]

now we have a full definition of both and , we can perform the iteration, code for doing so is presented below in Python:

from sys import argv
from PIL import Image, ImageDraw
from math import sqrt
from cmath import sin

def g_norm(z):
    return ((z ** 3) - 1)

def g_diff(z):
    return complex((3 * (z.real ** 2)) - (6 * (z.real * z.imag)) - (3 * (z.imag ** 2)),
        (3 * (z.real ** 2)) + (6 * (z.real * z.imag)) - (2 * (z.imag ** 2)))

def Julia(width, height, scale):
    Set_img = Image.new("RGB", (width, height), (255, 255, 255))
    Set = ImageDraw.Draw(Set_img)
    maxiters = 50

    w = width / 2
    h = height / 2

    print "Calculating Newton-Raphson Julia Set and drawing/colouring image"
    for x in range(-w, w):
        for y in range(-h, h):
            z = complex(x, y)
            z *= scale
            i = 0
            while i < maxiters:
                i += 1
                lz = z
                try:
                    z = z - (g_norm(z) / g_diff(z))
                except:
                    break
                if abs(z) == abs(lz):
                    break
            i = maxiters - i
            colour = int((float(i) / float(maxiters)) * 255.0)
            if colour > 10:
                if z.imag == 0.0:
                    colour = (0, colour, 0)
                elif z.imag > 0.0:
                    colour = (0, 0, colour)
                else:
                    colour = (colour, 0, 0)
            else:
                colour = (colour, colour, colour)
            Set.point((x + w, y + h), fill=colour)
            if y == (h - 1):
                print (float(x + w) / float(width)) * 100.0, "% done"
    print "100% done"
    del Set
    Set_img.save("Julia.png", "PNG")

if  __name__ == '__main__':
    if len(argv) > 3:
        Julia(int(argv[1]), int(argv[2]), float(argv[3]))
    else:
        Julia(200, 200, 1.0)

There is one problem with this though, if run it will not produce the expected Julia Set and Fatou Set. An approximation of the correct set is obtained when g_diff(z) is defined as

def g_diff(z):
    return (3 * (z ** 2))