No BSD License  

Highlights from
Compare 6 different algorithms to calculate the mandelbrot set

Compare 6 different algorithms to calculate the mandelbrot set

by

 

03 Nov 2008 (Updated )

Compares five different algorithms to calculate the Mandelbrot Set

mandel_fast
function mandel_fast
% This script compares different algorithms for the calculation of the
% Mandelbrot Set.

%Example mandel_fast will execute the program.
step=10;
maxiter=10;
%xmax=2;
xmin=-2;
xmax=2;
ymin=-2;
ymax=2;
%test1='Simple Script to calculate the Mandelbrot Set'
for iii=1:6
    iii
   
    steps=step*iii;
    numberOfSteps(iii)=steps;
    maxiter=maxiter*iii;
    tic
    Z=0;
     X=[-xmax:2*xmax/(steps-1):xmax];
     Y=[-xmax:2*xmax/(steps-1):xmax];
     
     for m=1:steps
        c=i*(-xmax+2*xmax*m/steps);
        for n=1:steps
            c=-xmax+2*xmax*n/steps-.5 + i*imag(c);
            z=c;
            for r=0:maxiter
                z=z*z+c;
                if abs(z)>10000 break
                end
            end
            Z(m,n)=sqrt(r/maxiter);
        end
    end
    colormap(hsv(maxiter));
    mesh(X,Y,Z)
f(iii)=getframe  
time(iii,1)=toc;   

%test2='Improved Script to calculate the Mandelbrot Set'
tic    
%Allocating the memory is going to speed up the program
Z=zeros(steps);
      for m=1:steps
         ci=(-xmax+2*xmax*m/steps);
         for n=1:steps
            cr=-xmax+2*xmax*n/steps-.5;
            zr=cr;
            zi=ci;
            rmax=maxiter;
            for r=0:maxiter
                zrn=zr*zr-zi*zi+cr;
                zin=2*zi*zr+ci;
                zi=zin;
                zr=zrn;
                if (zr*zr+zi*zi)>10000,
                    rmax=r;
                    break
                end
            end
            Z(m,n)=rmax;
        end
    end
    colormap(hsv(maxiter));
    Z=sqrt(Z/maxiter);
    imagesc(Z)
    axis off
    axis equal
f(iii)=getframe 
time(iii,2)=toc;

  
     
%test3='Further improvements on the script to calculate the Mandelbrot Set'     
    
%xmin=-2;
%xmax=2;
%ymin=-2;
%ymax=2;
%This is the fasted algorithm, when run from this M file. However, when I
%put that code in an GUI it runs slower than algorithm 4. I can' really
%explain that, but I would like to understand why that can happen.
tic
     Z=zeros(steps);
     dy=(ymax-ymin)/(steps-1);
    
     for m=1:steps
         ci=ymin+dy*m;
         for n=1:steps
            cr=xmin+dy*n;
            zr=cr;
            zi=ci;
            rmax=maxiter;
            for r=0:maxiter
                zrn=zr*zr-zi*zi+cr;
                zin=2*zi*zr+ci;
                zi=zin;
                zr=zrn;
                if (zr*zr+zi*zi)>10000,
                    rmax=r;
                    break
                end
            end
            Z(m,n)=rmax;
 
         end
    end
    colormap(hsv(maxiter));
    Z=sqrt(Z/maxiter);
    imagesc(Z)
    axis off
    axis equal
time(iii,3)=toc;
f(iii)=getframe 
%This is the vectortized and it runs better than the other algorithms when transferred to an GUI. 
tic
    %compute other constants  
    Rwidth=xmax-xmin;
    Iwidth=ymax-ymin;
    slR=Rwidth/(steps-1);
    slI=Iwidth/(steps-1);
    [x,y]=meshgrid((0:steps-1)*slR+xmin,(0:steps-1)*slI+ymin);
    Zvalues=ones(size(x));
    initZ=zeros(size(x));
    c=(x+i*y);
    obergrenze=steps*steps;
    z=initZ;
    %ddx=1/maxiter;
    %h = waitbar(0,'Please wait...');
    h_z=1:(obergrenze);
    for counter=1:maxiter 
     % ddx=ddx+ddx;
     % waitbar(ddx)  
      z(h_z)=z(h_z).^2+c(h_z);
      h_z= h_z(logical(abs(z(h_z))<2));
      Zvalues(h_z)=Zvalues(h_z)+1;
    end
   % close(h);
    %imagesc(Z)
    f(iii)=getframe 
    mesh(x,y,Zvalues);
f(iii)=getframe 
%Comparison of the algorithms
time(iii,4)=toc;
%s='Generalized script for the calcualtion of the mandelbrot set.'

xmin=-2;
xmax=2;
ymin=-2;
ymax=2;
tic
     Z=zeros(steps);
     dy=(ymax-ymin)/(steps-1);
     dx=(xmax-xmin)/(steps-1);
     for m=1:steps
         ci=(ymin+dy*m);
         for n=1:steps
            cr=(xmin+dx*n);
            zr=(cr);
            zi=(ci);
            rmax=maxiter;
            for r=0:maxiter
                zrn=(zr*zr-zi*zi+cr);
                zin=(2*zi*zr+ci);
                zi=(zin);
                zr=(zrn);
                if (zr*zr+zi*zi)>10000,
                    rmax=r;
                    break
                end
            end
            Z(m,n)=rmax;
 
         end
    end
    %colormap(hsv(maxiter));
    %Z=sqrt(Z/maxiter);
    imagesc(Z)
    axis off
    axis equal
f(iii)=getframe 
    time(iii,5)=toc;    
 xmin=-2;
xmax=2;
ymin=-2;
ymax=2;
tic
     Z=zeros(steps);
     dy=(ymax-ymin)/(steps-1);
     dx=(xmax-xmin)/(steps-1);
     for m=1:steps
         ci=single(ymin+dy*m);
         for n=1:steps
            cr=single(xmin+dx*n);
            zr=single(cr);
            zi=single(ci);
            rmax=maxiter;
            for r=0:maxiter
                zrn=single(zr*zr-zi*zi+cr);
                zin=single(2*zi*zr+ci);
                zi=single(zin);
                zr=single(zrn);
                if single(zr*zr+zi*zi)>10000,
                    rmax=r;
                    break
                end
            end
            Z(m,n)=rmax;
 
         end
    end
    %colormap(hsv(maxiter));
    %Z=sqrt(Z/maxiter);
    imagesc(Z)
    axis off
    axis equal
f(iii)=getframe 
    time(iii,6)=toc;      
s='Elapsed time for the algorithms:'
time
end
numberOfSteps
time
mesh(time)

Contact us