Thicket data repository for the EEG
1{
2 "id": "https://www.tunbury.org/2015/01/19/mandlebrot-set",
3 "title": "Mandelbrot Set",
4 "link": "https://www.tunbury.org/2015/01/19/mandlebrot-set/",
5 "updated": "2015-01-19T12:41:29",
6 "published": "2015-01-19T12:41:29",
7 "summary": "The Mandelbrot set is created from this very simple formula in which both Z and C are complex numbers.",
8 "content": "<p>The Mandelbrot set is created from this very simple formula in which both Z and C are complex numbers.</p>\n\n\\[Z_{n+1}=Z_n^2+c\\]\n\n<p>The formula is iterated to determine whether Z is bounded or tends to infinity. To demonstrate this assume a test case where the imaginary part is zero and focus just on the real part. In this case, the formula is trivial to evaluate starting with Z = 0. The table below shows the outcome at C=0.2 and C=0.3 and where one is clearly bounded and the other is not!</p>\n\n\n\n \n \n <strong>Iteration</strong>\n <strong>C = 0.2</strong>\n <strong>C = 0.3</strong>\n \n \n \n \n \n 0\n 0\n \n \n 1\n 0.2\n 0.3\n \n \n 2\n 0.24\n 0.39\n \n \n 3\n 0.2576\n 0.4521\n \n \n 4\n 0.266358\n 0.504394\n \n \n 5\n 0.270946\n 0.554414\n \n \n 6\n 0.273412\n 0.607375\n \n \n 7\n 0.274754\n 0.668904\n \n \n 8\n 0.27549\n 0.747432\n \n \n 9\n 0.275895\n 0.858655\n \n \n 10\n 0.276118\n 1.037289\n \n \n 11\n 0.276241\n 1.375968\n \n \n 12\n 0.276309\n 2.193288\n \n \n 13\n 0.276347\n 5.110511\n \n \n 14\n 0.276368\n 26.41732\n \n \n 15\n 0.276379\n 698.1747\n \n \n 16\n 0.276385\n 487448.2\n \n \n 17\n 0.276389\n 2.38E+11\n \n \n 18\n 0.276391\n 5.65E+22\n \n \n\n\n<p>C=0.2 is said to be part of the set where C=0.3 is not. Typical this point is coloured by some arbitrary function of the number of iterations it took for the modulus of Z to exceed 2.</p>\n\n<p>The set is plotted on the complex number plane with the real part using the x-axis and the imaginary part using the y-axis, thus:</p>\n\n<p><img alt=\"\" src=\"https://www.tunbury.org/images/complex-plane.svg\"></p>\n\n<p>Given that computers don’t natively work with complex numbers we need to break the formula down into manageable pieces. Firstly write the formula including both the real and complex parts then expand the brackets and group the terms.</p>\n\n\\[Z_{n+1}=Z_n^2+c\\]\n\n\\[Z_{n+1}=(Z_{re}+Z_{im}i)^2+c_{re}+c_{im}i\\]\n\n\\[Z_{n+1}=Z_{re}^2-Z_{im}^2+2Z_{re}Z_{im}i+c_{re}+c_{im}i\\]\n\n\\[\\mathbb R(Z_{n+1})=Z_{re}^2-Z_{im}^2+c_{re}\\]\n\n\\[\\mathbb I(Z_{n+1})=2Z_{re}Z_{im}+c_{im}\\]\n\n<p>Here’s a Perl program to generate a PNG file. Over the years I’ve written this same program in many languages starting with Pascal at school, PostScript at University and Excel VBA and JavaScript…</p>\n\n<p>Here’s a Perl program to generate a PNG file. Over the years I’ve written this same program in many languages starting with Pascal at school, PostScript at University and <a href=\"https://www.tunbury.org/downloads/mandelbrot.xlsm\">Excel VBA</a> and JavaScript…</p>\n\n<div><div><pre><code>#!/usr/bin/perl -w\n\nuse strict;\nuse GD;\n\nmy $width = 1024;\nmy $height = 1024;\n\nGD::Image->trueColor(1);\nmy $img = new GD::Image($width, $height);\n</code></pre></div></div>\n\n<p>Focus on an interesting bit. Real should be between -2.5 and 1 and\nimaginary between -1 and 1.</p>\n\n<div><div><pre><code>my $MINre = -0.56;\nmy $MAXre = -0.55;\nmy $MINim = -0.56;\nmy $MAXim = -0.55;\n</code></pre></div></div>\n\n<p>Maximum number of iterations before the point is classified as bounded.\nI’ve used 255 because I am using this as the colour component later</p>\n\n<div><div><pre><code>my $max = 255;\n</code></pre></div></div>\n\n<p>Setup the loops to move through all the pixels in the image. The value\nof C is calculate from the image size and scale. Note that GD creates\nimages with the origin in the top left.</p>\n\n<div><div><pre><code>for my $row (1 .. $height) {\n my $Cim = $MINim + ($MAXim - $MINim) * $row / $height;\n for my $col (0 .. $width - 1) {\n my $Cre = $MINre + ($MAXre - $MINre) * $col / $width;\n</code></pre></div></div>\n\n<p>Z starts at the origin</p>\n\n<div><div><pre><code> my $Zre = 0;\n my $Zim = 0;\n my $iteration = 0;\n</code></pre></div></div>\n\n<p>Loop until the modulus of Z < 2 or the maximum number of iterations\nhave passed. Note that I’ve squared both sides to avoid a wasting time\ncalculating the square root</p>\n\n<div><div><pre><code>while ($Zre * $Zre + $Zim * $Zim <= 4 && $iteration < $max) {\n</code></pre></div></div>\n\n<p>Here’s the formula from above to calculate the next value</p>\n\n<div><div><pre><code> my $ZNre = $Zre * $Zre - $Zim * $Zim + $Cre;\n $Zim = 2 * $Zre * $Zim + $Cim;\n $Zre = $ZNre;\n</code></pre></div></div>\n\n<p>Move on to the next iteration</p>\n\n<div><div><pre><code> $iteration++;\n }\n</code></pre></div></div>\n\n<p>Determine why we finished the loop - was it bound or not - and then\ncolour the pixel appropriately</p>\n\n<div><div><pre><code> if ($iteration < $max) {\n $img->setPixel($col, $height - $row, $iteration * 0x010101);\n } else {\n $img->setPixel($col, $height - $row, 0x00);\n }\n }\n}\n</code></pre></div></div>\n\n<p>Output the PNG file to STDOUT</p>\n\n<div><div><pre><code>binmode STDOUT;\nprint $img->png;\n</code></pre></div></div>",
9 "content_type": "html",
10 "author": {
11 "name": "Mark Elvers",
12 "email": "mark.elvers@tunbury.org",
13 "uri": null
14 },
15 "categories": [
16 "perl"
17 ],
18 "source": "https://www.tunbury.org/atom.xml"
19}