Saturday, December 26, 2015

Mandelbrot Set using Sagemath

I generated this picture using Sagemath. Here is the code if you want to do the same

# Author: Sumant
# Design is to have the window size of -1 and 1 on x axis and -1 and 1 on y axis
# Parameter L controls number of points its for ex L = 10 means from -10 to 10 ie 20 pts
# So total of 20 times 20 = 400 pts will be used
# Dec-26-2015
z = var('z')
j=0
x=[] # Array to store 0 and 1 which is later changed to a matrix
X=70 # This is Precision choose from 10 and above, for 100 it takes around 1 minute 40 seconds to generate
Y=70
M=n(1/X) # For scaling the numbers
kX=range(-3*X,n(X/2)) #3 parts of negative x axis and 1/2 part of positive x axis
kY=range(-Y,Y)
def f(c):
    z=0 # Recall Mandebrot set always begins with zero
    for i in range(80): #This number calculates the number of iterations we have to do until it it exceeds 2
       z = z^2+c # This is the Mandelbrot set z = z^2+c, we start with
       if n(abs(z)) > 2:
            break
 
    if abs(z) > 2:
        return 0
    else:
        return 1

for i in kX:
    for j in kY:
        x.append(f((M*i+M*j*I)))# Populating the array

B=matrix(ZZ,3.5*X,x)# 3.5 is to compensate 3 parts of negative x axis and 1/2 part of positive x axis
matrix_plot(B.transpose())# Plotting the transpose image


0 Comments:

Post a Comment

<< Home

Site Meter