The Mandelbrot set - python code

|



###################################################

## Python 2.7

## The Mandelbrot set

## Written by Hasla

## Last Edited May 2nd 2012.

###################################################

from matplotlib.pyplot import imshow, show, savefig

from matplotlib import colors,cm

from numpy import zeros, sqrt, log

###################################################

def f(x,const):## Given function f

return x*x  + const

###################################################

def solution(const):## solve with starting point x+yj and return some values about reached solution

z = 0 + 0j

value = 1.0

count = 100

for i in range(100):

z = f(z,const)

value = sqrt(z.real**2 + z.imag**2)

count +=1

if(value>=2.0):

return log(count)## Log of iteration makes structure finer.

break

else:##This "else" only works when "for loop" finished with out "break"

return 0

###################################################


#####-----Set the Range-----#####

maxran= 2 

binval = 0.01

#################################


#####-----Set the Color map-----#####

cmap = colors.ListedColormap(['black', 'white'])

bounds = [0,0.1,700]

norm = colors.BoundaryNorm(bounds, cmap.N)

extent = (-2.,2.,-2.,2.)

#####################################


#####-----Initialize-----#####

maximum = 2*int(maxran/binval)+1

board = zeros([maximum,maximum])

##############################


#####-----Calculate-----#####

x = -2.00001

count =0

for x_count in range(0,maximum):

y = -2.00001

for y_count in range(0,maximum):

board[y_count][x_count] = solution(complex(x,y))

count +=1

y += binval

x += binval

#############################


#####-----Output-----#####

#imshow(board, cmap=cmap, norm=norm, extent=extent)##Black & White 

imshow(board, cmap=cm.hot, extent=extent)##Color

savefig('result.png')

show()

##########################

 


And