In [4]:
# Package code from last time in function to make it easier to use

import numpy # Python package used for arrays

def draw_julia(c, max_iter, xmin, xmax, ymin, ymax):
    """Draws Julia set for z^2+c. 
    Should only use with |c|<2. 
    max_iter is max number of iterations
    xmin, xmax smallest, largest x-coordinates to check."""
    
    height = 600  # height in pixels of image
    width = 600
 
    max_abs=2 # if |z|>2 then the iterates f(z),f(f(z)),... escape to infinity

    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, know orbit escape by Claim in class
                    julia[y,x] = 1  # color black in this case
                    # need this order of x,y to agree with math convention
                    break
                else: 
                    iters += 1
    return matrix_plot(julia, origin='lower')
    
In [2]:
# "basilica" julia set
draw_julia(complex(-1,0), 30, -2, 2, -2, 2)
Out[2]:
In [3]:
# zoom in near 0 + 0.708i
# should increase iterations as zoom in
draw_julia(complex(-1,0), 40, 0-0.005, 0+0.005, 0.708-0.005, 0.708+0.005)
Out[3]:
In [5]:
# Code to draw colored Julia set 

import numpy

def draw_julia_color(c, max_iter, xmin, xmax, ymin, ymax):
    """Draws colored Julia set for z^2+c. 
    Color is hotter for points that take longer to escape.
    Should only use with |c|<2. 
    max_iter is max number of iterations
    xmin, xmax smallest, largest x-coordinates to check."""
   
    height = 600  # height in pixels of image
    width = 600
 
    max_abs=2 # if |z|>2 then the iterates f(z),f(f(z)),... escape to infinity

    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 and abs(z)<max_abs:
                z = z*z + c 
                iters += 1
            # if don't escape, iters = max_iter
            # if it does escape, iters is lower the faster it escapes
            julia[y,x] = iters/max_iter
    return matrix_plot(julia, cmap='hot', origin='lower')  #'hot' is a particular color profile
    
In [6]:
draw_julia_color(complex(-1,0), 30, -2, 2, -2, 2)
Out[6]:
In [7]:
# Feigenbaum julia set
draw_julia_color(complex(-1.401,0), 30, -2, 2, -2, 2)
Out[7]:
In [8]:
# This Julia set is so small that it is invisible without color
draw_julia(complex(0.5,0.7), 30, -2, 2, -2, 2)
Out[8]:
In [9]:
# With color, we can see where the Julia set is
draw_julia_color(complex(0.5,0.7), 30, -2, 2, -2, 2)
Out[9]: