Mandelbrot.m

From AIMSWiki

size = 150; % The width and height of the resulting image
xrange = linspace(-2, 0.5, size);
yrange = linspace(-1.15, 1.15, size);
maxcount = 128;

% cmap determines the colouring of the resulting image
cmap = hsv2rgb([linspace(0, 1, maxcount)', ones(maxcount, 2)]);
cmap(end, :) = 0;

clear i; % Make sure that i refers to the imaginary number sqrt(-1)

% Generate the Mandelbrot fractal
im = zeros(size);
for v = 1:size
  for u = 1:size
    c = xrange(u) + yrange(v)*i;
    z = 0;
    count = 0;
    while (count < maxcount) & (abs(z) < 2)
      z = z^2+c;
      count++;
    endwhile
    im(v, u) = count;
  endfor
endfor

% Plot the image
colormap(cmap);
image(im, 1);