How to resolve the algorithm Mandelbrot set step by step in the Octave programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mandelbrot set step by step in the Octave programming language

Table of Contents

Problem Statement

Generate and draw the Mandelbrot set.

Note that there are many algorithms to draw Mandelbrot set and there are many functions which generate it .

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mandelbrot set step by step in the Octave programming language

Source code in the octave programming language

#! /usr/bin/octave -qf
global width = 200;
global height = 200;
maxiter = 100;

z0 = 0;
global cmax = 1 + i;
global cmin = -2 - i;

function cs = pscale(c)
  global cmax;
  global cmin;
  global width;
  global height;
  persistent px = (real(cmax-cmin))/width;
  persistent py = (imag(cmax-cmin))/height;
  cs = real(cmin) + px*real(c) + i*(imag(cmin) + py*imag(c));
endfunction

ms = zeros(width, height);
for x = 0:width-1
  for y = 0:height-1
    z0 = 0;
    c = pscale(x+y*i);
    for ic = 1:maxiter
      z1 = z0^2 + c;
      if ( abs(z1) > 2 ) break; endif
      z0 = z1;
    endfor
    ms(x+1, y+1) = ic/maxiter;
  endfor
endfor

saveimage("mandel.ppm", round(ms .* 255).', "ppm");

function z = mandelbrot()
    % to view the image call "image(mandelbrot())"
    width = 500; height = 500;
    z = zeros(width, height);
    c = zeros(width, height);

    xi = 1;
    for x = linspace(-2, 2, width)
        yi = 1;
        for y = linspace(-2, 2, height)
            c(yi, xi) = x+y*i; yi += 1;
        end
        xi += 1;
    end

    for iter = 1:50
        z = z.*z + c;
    end

    z = abs(z);
end

  

You may also check:How to resolve the algorithm Long multiplication step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the D programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Factor programming language
You may also check:How to resolve the algorithm Cheryl's birthday step by step in the Nim programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Perl programming language