Mandelbrot Set

Style[Row@{"The Mandelbrot set is the set of complex numbers ",TraditionalForm[c]," for which the function ",TraditionalForm[ fx==z^2+c]," does not diverge when iterated from ",TraditionalForm[z==0]},12]

define a function

Practically, we don' t iterate the function for infinite times, rather we set a threshold and observe the level of divergence when the maximum iteration reaches
lets define a function called Mandelbrot, which has 3 parameters:
- the maximum iteration n
- the power p (which can be 2 or any number)
- the set of complex number c

Mandelbrot[n_,p_,c_]:=
Module[{z=ConstantArray[0+0I,Dimensions@c]},
Do[z=z^p+c,n];
Clip[Abs@z,{0,2}]*0.5
]

we use a grid of complex number as c, ranging from -2.5 to 2.5 for real part and -1.5 to 1.5 for imaginary part, with resolution of 0.01

c=Table[i+j*I,{j,Range[-1.5,1.5,0.01]},{i,Range[-2.5,2.5,0.01]}];

let’s run our function

Image@Mandelbrot[20,2,c]

compile the function

We can compile the above function, so that it runs faster
we use if condition to filter those z that are diverged to prevent numerical error

MandelbrotCompile=Compile[{{n,_Integer},{p,_Real},{c,_Complex,2}},
Module[{z=ConstantArray[0+0I,Dimensions@c],dim=Dimensions@c},
Do[If[Abs@z[[i,j]]<4,z[[i,j]]=z[[i,j]]^p +c[[i,j]]],{i,1,dim[[1]]},{j,1,dim[[2]]},{n}];
Clip[Abs@z,{0,2}]*0.5
]
]

let’s run our compiled function

Image@MandelbrotCompile[20,2,c]

play

lets play with the iteration n

list=ParallelTable[Image@MandelbrotCompile[i,2,c],{i,1,40}];
Manipulate[list[[i]],{i,1,Length@list,1}]

change the power number p

list2=ParallelTable[Image@MandelbrotCompile[40,i,c],{i,1,10,0.1}];
Manipulate[list2[[i]],{i,1,Length@list2,1}]

or zoom in by changing c

scale=0.8;
zoomin=40;
cs=Table[c*scale^k-1.405+0.01I,{k,1,zoomin}];

list3=ParallelTable[Image@MandelbrotCompile[40,2,c],{c,cs}];
Manipulate[list3[[i]],{i,1,Length@list3,1}]