Download Matlab Deliverable 1: The Mandelbrot Set

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Infinity wikipedia , lookup

Law of large numbers wikipedia , lookup

Addition wikipedia , lookup

Hyperreal number wikipedia , lookup

Large numbers wikipedia , lookup

Georg Cantor's first set theory article wikipedia , lookup

Real number wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Fundamental theorem of algebra wikipedia , lookup

Collatz conjecture wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Sequence wikipedia , lookup

Transcript
-1
-0.8
-0.6
-0.4
-0.2
0
D
A
0.2
0.4
0.6
0.8
1
-1.5
C
-1
B
-0.5
0
0.5
Matlab Deliverable 1: The Mandelbrot Set
The Mandelbrot set is the most famous fractal in the world – images of the Mandelbrot set can be
found everywhere, and it’s likely that an image of the Mandelbrot set comes to mind when the word
fractal comes up. Like most fractals, the Mandelbrot set is computed using a few very simple rules
which can easily be programmed in Matlab. The fundamental rule of the Mandelbrot set can be
stated mathematically as follows:
𝑧𝑛+1 β†’ 𝑧𝑛2 + 𝑐
(1)
where c is a complex number and z0 = 0. The rule is a method for generating a series of numbers,
where each number in the sequence depends upon the preceding number. For some values of c, the
sequence converges, and for other values it β€œblows up”. If a value of c results in a converging series,
it is said to be part of the β€œMandelbrot set”. In the figure above, the values of c that are in the
Mandelbrot set are plotted in gray, while those that are not are plotted in white. The real part of c is
plotted along the x axis and the imaginary part is plotted along the y axis.
To see this rule in action, let us take a simple example: c = 0 + 0j, which is the point A in the figure
above. Following the rule gives
𝑧1 = 0
𝑧2 = 02 + 0
𝑧3 = 02 + 0 = 0
𝑒𝑑𝑐.
(2)
1
Clearly, this sequence converges to zero, so it is part of the set. Now try another example: c = -1 +
0j (the point D in the figure above).
𝑧1 = βˆ’1
𝑧2 = (βˆ’1)2 βˆ’ 1 = 0
𝑧3 = 02 βˆ’ 1 = βˆ’1
𝑧4 = (βˆ’1)2 βˆ’ 1 = 0
𝑧5 = 02 βˆ’ 1 = βˆ’1
(3)
This sequence is bouncing around between -1 and 0, but at least it isn’t blowing up to infinity! Thus,
the point D is also in the Mandelbrot sequence. Now examine the point C in the figure, which is at c
= -1 + 1j. To compute the sequence, it is easiest to use Matlab. First, type
c = -1 + 1j
at the command prompt. Matlab will answer with
c = -1.0000 + 1.0000i
proving that it understands complex numbers. Now type the first three numbers in the sequence
z0 = 0
z1 = z0^2 + c
z2 = z1^2 + c
and Matlab will respond with
z2 = -1.0000 - 1.0000i
So far, so good. But if you keep adding numbers to the sequence, you’ll find that
z6 = -5.2570e+03 + 1.0011e+04i
In other words, the sequence is blowing up, rather quickly! By the sixth number in the sequence the
magnitude is over 11,000, and it won’t take long before we reach the maximum number allowable in
Matlab. Thus, the point C is not part of the Mandelbrot set, and is plotted in white. Finally, let us
examine the point B, which is quite interesting. It lies at c = 0 + 1j, which is surrounded by white in
the figure. At first glance, we would guess that B should blow up and not be part of the set, since
the points around it are also not part of the set. However....
z2
z3
z4
z5
z6
= -1.0000 + 1.0000i
= 0.0000 - 1.0000i
= -1.0000 + 1.0000i
= 0.0000 - 1.0000i
= -1.0000 + 1.0000i
This sequence bounces around between -1+1j and 0-1j, and does not blow up, thus it is part of the
Mandelbrot set and is rendered in gray. This is what makes the set so interesting – there are
β€œislands” of the set surrounded by points that blow up. If we zoom in on this point, we’ll find that
there are shapes similar to the original β€œcardioid” shape that emerge at ever smaller scales. This
β€œself-similarity” is what makes the Mandelbrot set a fractal.
2
The Challenge: Plot the Mandelbrot Set
Create an algorithm that determines whether a given complex number, c, converges within a
reasonable time. The easiest way to determine convergence is to check whether the magnitude of z
is less than 2 at a given iteration. If it is greater than 2, the sequence is guaranteed to blow up a few
iterations later. Each value of c corresponds to a pixel on the plot – the x coordinate of the pixel is
the real part of c and the y coordinate is the imaginary part. If c is not part of the set, plot a white
pixel, and if it is part of the set, plot a black pixel.
1. Write a Matlab program that plots the Mandelbrot set in the range (x→ -2 : 1) (y→ -1 : 1). The
plot should be 601 x 401 pixels in size.
2. Write a Matlab program that plots the Mandelbrot set in the range (x→ -0.05 : -0.01) (y→ 0.77 :
0.81). The plot should be 501 x 501 pixels in size.
Use the imshow command to plot your image and set the maximum number of iterations for each
pixel to 200.
3. Bonus – extra credit! Produce the plots given above in color, with the color of each pixel
dependent upon the number of iterations it takes for the sequence to β€œblow up”.
Appendix: A Few Words about Complex Math
While Matlab can store and manipulate complex numbers, your program will be more efficient if
you stick with real variables. For a given step in the sequence we must calculate z2, where z is a
complex number. Let
𝑧 = π‘Ž + 𝑗𝑏
(4)
𝑧 2 = (π‘Ž + 𝑗𝑏)(π‘Ž + 𝑗𝑏)
= π‘Ž2 + 2π‘—π‘Žπ‘ βˆ’ 𝑏 2
(5)
Then
If the complex number under evaluation is
𝑐 = π‘₯ + 𝑗𝑦
(6)
𝑧 2 + 𝑐 = π‘Ž2 βˆ’ 𝑏 2 + π‘₯ + 𝑗(2π‘Žπ‘ + 𝑦)
(7)
π‘Žπ‘›+1 = π‘Žπ‘›2 βˆ’ 𝑏𝑛2 + π‘₯
(8)
𝑏𝑛+1 = 2π‘Žπ‘› 𝑏𝑛 + 𝑦
(9)
Then
That is, the new real part is
and the new imaginary part is
3
Finally, the magnitude of the complex number is given by
|𝑧| = βˆšπ‘Ž2 + 𝑏 2
(10)
For example, let us examine the point c = -1 + 1j.
𝑧0 = βˆ’1 + 1𝑗
𝑧1 = [(βˆ’1)2 βˆ’ 12 βˆ’ 1] + [2(βˆ’1)(1) + 1]𝑗 = βˆ’1 βˆ’ 1𝑗
𝑧2 = [(βˆ’1)2 βˆ’ (βˆ’12 ) βˆ’ 1] + [2(βˆ’1)(βˆ’1) + 1]𝑗 = βˆ’1 + 3𝑗
𝑧3 = [(βˆ’1)2 βˆ’ (3)2 βˆ’ 1] + [2(βˆ’1)(3) + 1]𝑗 = βˆ’9 βˆ’ 5𝑗
𝑧4 = [(βˆ’9)2 βˆ’ (βˆ’5)2 βˆ’ 1] + [2(βˆ’9)(βˆ’5) + 1]𝑗 = 55 + 91𝑗
𝑧5 = [(55)2 βˆ’ (91)2 βˆ’ 1] + [2(55)(91) + 1]𝑗 = βˆ’5257 + 10011𝑗
(11)
These are the same numbers we calculated earlier using Matlab.
4