In many ways, the Mandelbrot Set is similar to the Julia Sets we just discussed.
Code
function MandelbrotSet(r,w,makeVideo)
% Draws a Mandelbrot Set with iterations r and resolution w. Set makeVideo
% to 1 if you want to record a video.
if makeVideo==1
writerObj = VideoWriter('mandelbrotSet4.avi'); % Name it.
writerObj.FrameRate = 2;
writerObj.Quality=100;
open(writerObj)
end
% Create the real and imaginary parts of each number in a temporary
% variable
temp=zeros(w,w,2);
for i=1:w
temp(i,:,1)=linspace(-2,1,w);
temp(:,i,2)=linspace(-1.25,1.25,w);
end
% Set the initial values based on their real and imaginary components
values=zeros(w,w,2);
for i=1:w
for j=1:w
values(i,j,1)=temp(i,j,1)+1i*temp(i,j,2);
end
end
% Initialize the output
output=zeros(w,w);
% Create the figure
figure('Position',[100,100,1400,800],'Color',[0 0 0]);
% For each iteration, cycle through data and perform the desired
% computatation
for i=1:r
for m=1:w
for k=1:w
values(m,k,2)=(values(m,k,2))^2+values(m,k,1);
% Essentially, set output(m,k) equal to the current
% iteration if the series diverges
if abs(values(m,k,2))>10 && output(m,k)==0
output(m,k)=i;
end
end
end
% Plot the data
clims=[0,r];
imagesc(output(:,:),clims);
colormap('hot');
axis off
if makeVideo==1
frame = getframe(1);
writeVideo(writerObj, frame)
end
pause(.001)
end
if makeVideo==1
for i=1:10
frame = getframe(1);
writeVideo(writerObj, frame)
end
close(writerObj)
end
end