You are on page 1of 19

Numerical Analysis

Lab 3

Joshua Cook
October 28, 2014

Preliminaries
0.1

Generalization of Newton fractals

A generalization of Newtons iteration is


zn+1 = zn a

p(zn )
p0 (zn )

where a is any complex number. The special choice a = 1 corresponds to the Newton fractal.
The fixed points of this map are stable when a lies inside the disk of radius 1 centered at 1. When a is outside
this disk, the fixed points are locally unstable, however the map still exhibits a fractal structure in the sense
of the Julia set. If p is a polynomial of degree n, then the sequence zn is bounded provided that a is inside a
disk of radius n centered at n.
More generally, Newtons fractal is a special case of a Julia set.

0.2

Import Statement

Include libraries for processing.


import math
import numpy as np
from scipy.misc import derivative
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
%matplotlib inline

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

1
Write a Python function that takes as input the integer n and returns a 1d numpy array of length n holding
the n roots of unity; that is, the n roots of the polynomial
zn 1 = 0
def unity_roots(n):
roots = np.zeros((n,1), dtype=complex)
for i in range(n):
theta = 2*i*math.pi/n
re = math.cos(theta)
im = math.sin(theta)
roots[i] = re + im*1j
return roots

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

2
Write a Python function named nfractal that takes as input four arguments: K, pixel, tol, and n, and
1. generates a mesh of the complex plane consisting of pixel points along the x-axis and 0.75*pixel
points along the x-axis, with x, y [1.5, 1.5].
k)
2. applies the iteration zk+1 = zk a pp(z
0 (z ) to each z = x + iy on the mesh K times and records the number
k
of times that the iterated value is within tol distance to one of the roots of the polynomial p(z).
3. colors each point in the mesh grid according to the number of times it is within a distance tol of one
of the roots of the polynomial p(z).

2.1

Define Function

def nfractal(K, pixel, tol, n,a=1):


# generate mesh
x = np.linspace(-1.5,1.5,pixel)
y = np.linspace(-1.5,1.5,0.75*pixel)
[Re,Im] = np.meshgrid(x,y)
# onesmatrix is a math construct for use in algorithm
onesmatrix = np.ones((0.75*pixel,pixel),float)
# B stores convergence/divergence values
B = np.zeros((0.75*pixel,pixel),float)
# C stores our iterated values i.e. our z_n+1 for each location on the complex plane
C = np.zeros((0.75*pixel,pixel),complex)
C = Re + Im*1j
Cn = C
def p(x): return (x**n) - 1
def pp(x): return n*x**(n-1)
roots = unity_roots(n)
for i in range(K):
Cn = Cn - a*p(Cn)/pp(Cn)
for j in range(n):
B = B + (np.abs(Cn-onesmatrix*roots[j])< tol)
plt.pcolormesh(x,y,B,cmap='spring')
return Re, Im, B

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 1: frac = nfractal(10,1000,10E-6,2)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 2: frac = nfractal(10,1000,10E-6,3)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 3: frac = nfractal(10,1000,10E-6,4)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 4: frac = nfractal(10,1000,10E-6,3,1.1)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 5: frac = nfractal(10,1000,10E-6,3,1.2)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 6: frac = nfractal(10,1000,10E-6,7,1+.2j)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 7: frac = nfractal(10,1000,10E-6,6,1)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Convergence Toward a Specific Root

def nfractal(K, pixel, tol, n,a=1, invert=0):


# generate mesh
x = np.linspace(-1.5,1.5,pixel)
y = np.linspace(-1.5,1.5,0.75*pixel)
[Re,Im] = np.meshgrid(x,y)
# onesmatrix is a math construct for use in algorithm
onesmatrix = np.ones((0.75*pixel,pixel),float)
# B stores convergence/divergence values
R = np.zeros((0.75*pixel,pixel),float)
G = np.zeros((0.75*pixel,pixel),float)
B = np.zeros((0.75*pixel,pixel),float)
im = np.zeros((0.75*pixel,pixel,3),float)
# C stores our iterated values i.e. our z_n+1 for each location on the complex plane
C = np.zeros((0.75*pixel,pixel),complex)
C = Re + Im*1j
Cn = C
def p(x): return (x**n) - 1
def pp(x): return n*x**(n-1)
roots = unity_roots(n)
for i in range(K):
Cn = Cn - a*p(Cn)/pp(Cn)
if (invert == 1):
for j in range(n):
if (j%3 == 0):
R = R + (np.abs(Cn-onesmatrix*roots[j])>tol)
if (j%3 == 1):
G = G + (np.abs(Cn-onesmatrix*roots[j])>tol)
if (j%3 == 2):
B = B + (np.abs(Cn-onesmatrix*roots[j])>tol)
if (invert == 0):
for j in range(n):
if (j%3 == 0):
R = R + (np.abs(Cn-onesmatrix*roots[j])<tol)
if (j%3 == 1):
G = G + (np.abs(Cn-onesmatrix*roots[j])<tol)
if (j%3 == 2):
B = B + (np.abs(Cn-onesmatrix*roots[j])<tol)
R = R *(510/K)

Numerical Analysis
Lab 3

G = G *(510/K)
B = B *(510/K)
im[:,:,0] = R
im[:,:,1] = G
im[:,:,2] = B
plt.imshow(im)
return Re, Im, B
frac = nfractal(60,1000,10E-6,2)

Joshua Cook
October 28, 2014

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 8: frac = nfractal(60,1000,10E-6,2)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 9: frac = nfractal(60,1000,10E-6,3)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 10: frac = nfractal(60,1000,10E-6,4)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 11: frac = nfractal(60,1000,10E-6,5)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 12: frac = nfractal(60,1000,10E-6,4,1+.8j)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 13: frac = nfractal(60,1000,10E-6,3,1-.7j,1)

Numerical Analysis
Lab 3

Joshua Cook
October 28, 2014

Figure 14: frac = nfractal(60,1000,10E-6,4,.2-.1j,1)