This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-04-20 08:32:23

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Coding the Mandelbrot Set

This is a guide on plotting the Mandelbrot Set. It's divided into 3 parts: What is the Mandelbrot Set?, Understanding the Algorithm, and Programming the algorithm. Here is a project on plotting it, if you don't get it.

What is the Mandelbrot Set?
The Mandelbrot Set (M-Set in short) is a fractal. It is plotted on the complex plane. It is an example of how intricate patterns can be formed from a simple math equation. It is entirely self-similar. Within the fractal, there are mini-Mandelbrot Sets, which have their own M-Sets, which have their own M-Sets, which have their own M-sets, etc.
Though most representations of the M-Set have color, only the black bit is part of the set. The color is to basically show how long it took to prove that that point wasn't in the set. However, these form cool patterns, too.
Here are some pictures:
Only the set:
http://www.olympus.net/personal/dewey/points1.png

With color:
http://2.bp.blogspot.com/_c7S0Y3wBP9g/S7kL6nCsBwI/AAAAAAAAB70/gAlP6_tW7g0/s400/Mandelbrot_set.jpg



Understanding the Algorithm
The M-Set is generated using the algorithm:

Z{n+1}=z{n}^2 + C

Here, both Z and C are complex numbers. What are complex numbers? They're, put simply, square roots of negative numbers. Since negative numbers can't have square roots, we created 'complex' or 'imaginary' numbers to deal with it. 'i' (pronounced iota) is the symbol for √-1. Complex numbers are expressed as multiples of i, like 3i. They are graphed on a number line perpendicular to the number line we all know. The resultant plane is called the complex plane, and is where we will graph the Mandelbrot Set.

Code:

The complex plane:
   1i
-1  0 +1
  -1i

Complex numbers are defined as the sum of a real number and an imaginary number. Examples are 3i + 1 or 4i - 2.

In this expression, C is the complex number for which you are testing whether or not it's in the M-Set (it will define a single point on the complex plane — C is a real number plus an imaginary one, remember?). Here's how you use it: You set Z to 0. Then set Z to Z^2 + C. We call this action iteration. For example, if C was 3 (I'm using a real number for simplicity), z would be:

0
0^2 + 3 = 3
3^2 + 3 = 12
12^2 + 3 = 147
etc.

If you did this many, many times, there are two possibilities for Z — it escapes to infinity, or it doesn't. If it doesn't escape, it is in the set. This looks hard to calculate-how can we know whether it reaches infinity? For all we know at 1000000000 iterations it'll be a normal, but after 1000000001 iterations it starts constantly doubling. Fortunately, we know 2 other things:
•It has been proved that if Z ever gets higher than 2, it will escape to infinity
•If it does escape, it'll do so normally within 50 iterations. More will make a more accurate picture, but it will slow the script down considerably. 50 is a good number.
So now, all we need to do is repeat Z^2 + C 50 times and see how high it is. Great!



Programming the Algorithm
That's all very nice, but there's a catch (isn't there always?) — this uses complex numbers, and Scratch—make that any programming language—doesn't allow square roots of negative numbers. Try it yourself. You'll get a red Error!. So how do we avoid this? Well, remember how a complex number is a real number plus an imaginary number, and an imaginary number is just √-1? Well, that means we can split any variable, say Q, into two variables: q-Real and q-Complex, or abbreviated, qR and qX. We also know that qX squared is real, because i^2 is -1 which is real, and the coefficient is real anyway (here, coefficient is the 3 in 3i). This means now we know how to square complex numbers to get real numbers. So, let's take a look at the algorithm:

Z{n+1} = Z{n}^2 + C
=(zR + zX)^2 + cR + cX
=zR^2 + zX^2 + 2zR*zX {by opening the brackets} + cR + cX

If you think carefully, zX can only be represented by its coefficient. This is because zX^2 is real, and 2*zR*zX + cX (all the complex terms) can be represented by the coefficient, which is again because zX^2 is real. So, we never 'need' the value of i, which is a huge relief.
Now we can start coding!
We need a sprite to move through every point on the stage. That's easy:

Code:

set x to -240
set y to 180
repeat 360
    set x to -240
    change y by -1
        repeat 480
            change x by 1
            •••Insert coding here•••
        end repeat
end repeat

See where I put •••Insert coding here•••? That's where we need to code our algorithm. From now on, all the coding I show you will be in that segment. Starting off,

Code:

set cR to x position/120 {because real numbers are on the horizontal number line. '/120' is to set the magnification.}
set cX to y position/120 {because imaginary numbers are on the vertical number line. '/120' is to set the magnification.}
set zR to 0
set zX to 0
set r to 0 {r will be the number of repetitions. You'll se why this will be helpful pretty soon}

This'll set up all our variables. Great. Now for the hard part.
For the calculation, we need to set zR to zR^2 + (zX^2*-1 {because i^2 is -1}) + cR, and set zX to 2*zR*zX + cX. We get this just by grouping them on the basis of their complexness. Complex values get added into zX, the rest into zR.

Code:

set old_zR to zR {this is because zR will change, and when evaluating zX we'll get the wrong value of zR}
set zR to (zR*zR) + -1*(zX*zX) + cR
set zX to 2*old_zR*zX + cX
change r by 1 {'cause r counts the repeats}

But, before we put that in, we need to know how many times to iterate. Iterate means repeat, so we need to put this into a repeat. We know that by 50 iterations, we can be pretty sure whether zR will ever exceed 2 or not. The problem is that it may exceed 2 very early, and approach infinity, causing Scratch to freeze because the numbers get out of hand. So, we need a 'if I'm over 2, stop me right away' repeat. This is exactly why we needed r.

Code:

repeat until [r > 50] or [zR > 2]

will do the job. [r > 50] says it repeats 50 times, [zR > 2] says if it's over 2, stop repeating.

Now, we can finally tell whether the point you chose is in the Mandelbrot Set or not. Whew! This part is really simple, so I'm not going to explain it (much).

Code:

if zR > 2
    set pen color to [black]
    pen down
    pen up
else
    set pen color to [r] {because non-set points are colored based on how long it took to establish C wasn't part of the M-Set}
    set pen shade to [50] {because black has a shade of 0, and so all colors will end up black}

And we're done! Congratulations!

You can get the whole project here.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#2 2011-04-20 08:46:14

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: Coding the Mandelbrot Set

holy!

You are smart 0.o


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#3 2011-04-20 10:49:35

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Coding the Mandelbrot Set

ssss wrote:

holy!

You are smart 0.o

Thanks!


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#4 2011-04-20 13:59:46

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Coding the Mandelbrot Set

excellent tutorial! Maths can create such beautiful patterns, both simple and complex, symmetrical and asymmetrical!


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#5 2011-04-21 19:47:22

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Coding the Mandelbrot Set

You actually managed to teach complex numbers to someone in Algebra I. (me)  yikes

Offline

 

#6 2011-04-22 02:22:04

Taneb
Scratcher
Registered: 2009-07-07
Posts: 100+

Re: Coding the Mandelbrot Set

I did something like this before, but it was very slow. I think yours is quicker because where you used 2 I used 100. Yeah, I'm an idiot sometimes.

Offline

 

#7 2011-04-22 08:26:00

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Coding the Mandelbrot Set

I made this once...

Rather than working it out I nicked the pseudocode from Wikipedia.


/* No comment */

Offline

 

#8 2011-04-25 04:51:52

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Coding the Mandelbrot Set

Bump.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#9 2013-02-23 21:39:02

3sal2
Scratcher
Registered: 2012-03-22
Posts: 100+

Re: Coding the Mandelbrot Set

By the way, mathematicians write the real part before the imaginary part when writing complex numbers, e.g. -1+i instead of i-1.


http://scratch.mit.edu/static/projects/3sal2/3120946_sm.png In 2012, scientists at the LHC discovered the Higgs boson, which explains the source of the masses of the W+, W-, and Z bosons, as well as fermions.

Offline

 

#10 2013-02-24 11:12:04

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Coding the Mandelbrot Set

Necropost aside, I remember this topic! It was back in my youth when I didn't know what a complex number was, so I refrained from posting. Looking back, I think this hasn't received nearly enough views as what it deserved from your extensive explanations of the mandelbrots  big_smile

(Correction, it has quite a few views, but nobody actually read the whole thing because it was so complicated  tongue  even I had to go get an ice pack after a few formulae...)

Last edited by LS97 (2013-02-24 11:13:18)

Offline

 

#11 2013-02-24 20:01:17

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Coding the Mandelbrot Set

LS97 wrote:

even I had to go get an ice pack after a few formulae...)

big_smile  I do that to people sometimes, I wonder why?  tongue


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#12 2013-02-25 11:01:32

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Coding the Mandelbrot Set

Hardmath123 wrote:

LS97 wrote:

even I had to go get an ice pack after a few formulae...)

big_smile  I do that to people sometimes, I wonder why?  tongue

Your username seems to suggest something like that...  tongue

Offline

 

Board footer