In this post I'll describe an algorithm that builds images similar to this one:

We will use Mandelbrot set generation algorithm as a basis. You can find example of Matlab code for this kind of fractal on this page (thanks to Alberto Strumia). It will be used as a basis.

Mandelbrot set is generated in iterative manner.

C = X + 1i*Y; % X and Y form a grid of 2D coordinates for it = 1:10 Z = Z.^2 + C; end

Information from matrix Z is then used to display a resulting fractal.

In order to shift from standard images of fractals, let's introduce some randomness to the generation process. We will use two sources of randomness in modified algorithm:

- noise added to grid;
- noisy value of exponent in generation loop.

So, we arrive to the following code:

L = -2; C=X+1i*Y; C = C.*T; for it = 1:10 Z(isnan(Z)) = C(isnan(Z)); end

Lines 4-5 multiply grid by "small" random complex number effectively adding minor deformations to the grid. As a result, images look noisy. Commenting out these lines leads to interesting pictures too: all the curves become more clear.

In line 7 you can see that exponent for Z has changed: for each iteration exponent grows and also gets altered by a random number. Variable L denotes approximate starting value for exponent (-2 in this example). Trick in line 8 prevents us from undesired NaNs in case if we got too far in experiments with value of the exponent.

We are almost done. The last step is visualization.

shading flat;

The code is straight-forward. It transforms Z so that it lies in real domain between 0 and 1. Line 2 handles cases where absolute value for Z is close to zero: low values are replaced with information gathered from argument of corresponding elements in Z. Then a pretty colormap is selected and at last data is plotted.

You can find full source code for Matlab/Octave with some additional make-up in the github repo (m-file).

Some examples of generated images:

That's all for now. ðŸ™‚

## One thought on “Generating fractal-like images”