import numpy # will use Numpy package to represent grid of points
a = numpy.zeros((3,3)); a # makes a 3x3 array of all zeros
a[0,0] # can access elements similarly to lists
a[1,0] = 5 # can assign values to particular elements
a
height = 3
width = 3
test_array = numpy.zeros((height,width))
test_array
test_array[1,1]=1; test_array[2,1]=0.5; test_array
matrix_plot(test_array) # Sage comand to plot array of numbers between 0 and 1
Below is the code to draw Julia set of quadratic polynomial $f(z)=z^2+c$.
The Julia set of a polynomial $f$ is defined as the boundary of the escaping set $$\{z: \lim_{n\to\infty} f^{\circ n}(z) = \infty\}$$
# Code to draw Julia set of quadratic polynomial f(z)=z^2+c
# (Should only use with |c|<2)
import numpy
height = 600 # height in pixels of image
width = 600
c=complex(0.3,0.4) # python complex number 0.3 + 0.4i
# polynomial is z^2 + c
max_iter=30 # max number of iterations
max_abs=2 # if |z|>2 then the iterates f(z),f(f(z)),... escape to infinity
xmin=-2; xmax=2 #smallest, largest x-coordinates to check
ymin=-2; ymax=2
xinc=(xmax-xmin)/width # width of each pixel
yinc=(ymax-ymin)/height
julia = numpy.zeros((height,width)) # initialize a numpy array of all zeros
for x in range(width):
for y in range(height):
z = complex((xmin + xinc*x),(ymin + yinc*y))
iters = 0 # count of number of iterations
while iters<max_iter:
z = z*z + c
if abs(z)>max_abs:
# in this case, known escape by Claim in class
julia[y,x] = 1 # color black in this case
# need this order of x,y to plot correctly
break
else:
iters += 1
matrix_plot(julia, origin='lower')