0% found this document useful (0 votes)
9 views6 pages

EA1 Fall 24 HW4

This homework assignment focuses on iterated functions and the Mandelbrot set using Matlab. Students will create a function to compute and visualize the Mandelbrot set by calculating escape times and values for complex numbers. The assignment includes detailed instructions on setting up the function, handling inputs, and utilizing vectorization for efficient computation.

Uploaded by

noelbenyella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

EA1 Fall 24 HW4

This homework assignment focuses on iterated functions and the Mandelbrot set using Matlab. Students will create a function to compute and visualize the Mandelbrot set by calculating escape times and values for complex numbers. The assignment includes detailed instructions on setting up the function, handling inputs, and utilizing vectorization for efficient computation.

Uploaded by

noelbenyella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EA1 Homework Program 4: Iterated functions and the Mandelbrot set

Please make sure to download and install the Image Processing Toolbox before attempting this home-
work assignment. To get it, click on Add-Ons in the main window of Matlab (under ENVIRONMENT on
the HOME tab). This should open the Add-On Explorer. There, search for the Image Processing Toolbox
and install it.

1 Complex numbers in Matlab

In this assignment you will be working with complex numbers, that is, numbers having both real and
imaginary parts. Don’t worry if you don’t remember much about complex numbers—Matlab will be doing
all the work for you!
In Matlab, you can create complex numbers using either i or j to denote the square root of −1. For
example, typing z = 2-5i in the command window will create a variable z whose value is a complex
number having real part 2 and imaginary part −5. Vectors and matrices can contain complex elements as
well: typing rand(2,3)+1i*rand(2,3) will create a 2-by-3 matrix having random real and imaginary
parts. Note that we used 1i instead of just i in this expression; this is because if you would
√ happen to
redefine i as a variable (thus overwriting Matlab’s definition), then 1i would still mean −1 whereas i
would mean whatever you assigned it to mean. For this reason it is best to avoid using i or j directly and
instead use 1i or 1j.
The ordinary arithmetic operators in Matlab work as expected with complex numbers. Also, the p absolute
value function abs works with complex numbers: if x and y are real, then |x + iy| is defined to be x2 + y 2 ,
that is, the distance from x + iy to the origin in the complex plane. For example, abs(3+4i) returns 5.

2 The Mandelbrot set

For a given complex parameter c, we generate a sequence {z0 , z1 , z2 , . . . } of complex numbers as follows:
2
we set z0 = 0 and for each index k = 1, 2, 3, . . . we set zk = zk−1 + c. Thus we have

z0 = 0
z1 = z02 + c = c
z2 = z12 + c = c2 + c
z3 = z22 + c = (c2 + c)2 + c
2
z4 = z32 + c = (c2 + c)2 + c + c

and so on. The complex parameter c does not change during this iteration, but for each different choice for c
we end up with a different sequence {z0 , z1 , z2 , . . . }. The following table shows the beginning part of the
sequence for some different choices for the parameter c:
c z0 z1 z2 z3 z4 z5 z6
−1 0 −1 0 −1 0 −1 0
−2 0 −2 2 2 2 2 2
i 0 i −1 + i −i −1 + i −i −1 + i
1 0 1 2 5 26 677 458330
Notice that for the first three choices for c in this table, the sequence is bounded, that is, the numbers in the
sequence do not get larger and larger as the sequence progresses. In contrast, the sequence for the fourth

1
choice c = 1 is unbounded (the numbers explode in size as the sequence progresses). Let us now define the
following set of complex numbers:

M = the set of all complex numbers c that generate a bounded sequence {z0 , z1 , z2 , . . . }.

According to our table, the numbers −1, −2, and i all belong to M, but the number 1 does not. This set M
is called the Mandelbrot set, and it is famous for being remarkably complicated considering the simplicity of
2
the iteration zk = zk−1 + c. The purpose of this assignment is to write a Matlab function that will compute
visualizations of this Mandelbrot set M.

3 Visualizing the Mandelbrot set

It turns out that for any choice for c, the generated sequence {z0 , z1 , z2 , . . . } is bounded (that is, c belongs
to the Mandelbrot set M) if and only if |zk | ⩽ 2 for all k. We can therefore assign two numbers to each
complex value of c, its escape time and its escape value: if {z0 , z1 , z2 , . . . } is the sequence generated by c
as described above, then the escape time for c is the smallest index k such that |zk | > 2, and its escape value
is |zk |. For example, looking at the above table, we see that c = 1 has an escape time of k = 3 and an escape
value of |z3 | = 5. If c belongs to M (as do the first three choices for c in the table), then we say its escape
time is infinite and its escape value is undefined. We can represent infinite and undefined values in Matlab
using the constants Inf (meaning “infinity”) and NaN (meaning “not a number”), respectively.
The escape time measures how slowly or quickly the sequence blows up: if the escape time is small, it
blows up quickly, and if the escape time is large, it blows up slowly. We can visualize M over a rectangular
region in the complex plane by dividing the region into equally spaced points (with each point representing
a single pixel in the image), calculating the escape time for each point, and assigning different colors to
different escape times. Using the escape values as well in the color assignment reduces color quantization
and generally results in nicer images. The following three images show such visualizations of M for different
rectangular regions in the complex plane:

In these images, the black points represent values of c that actually belong to M (i.e., values of c whose escape
times are infinite), the dark red points represent values of c that have large escape times (the corresponding
sequences blow up slowly), and the dark blue points represent values of c that have small escape times (the
corresponding sequences blow up quickly). Colors in between represent escape times in between.
To calculate the escape time for a particular value of c, we simply generate elements of the sequence
{z0 , z1 , z2 , . . . } one by one until we reach a value of k for which |zk | > 2. This final value of k is then the

2
escape time and |zk | is the escape value. In practice we must stop at some maximum value of k (otherwise
this would go on forever if c happened to belong to M). For example, the following code will calculate
the escape time and escape value for c = −0.575 − 0.47i and store them in variables called escTime and
escVal (respectively), using a maximum of 1000 iterations:
1 c = -0.575 - 0.47i;
2 maxEscTime = 1000;
3 escTime = Inf;
4 escVal = NaN;
5 z = 0;
6 k = 0;
7 while abs(z) <= 2 && k < maxEscTime
8 z = zˆ2 + c;
9 k = k+1;
10 end
11 escTime = k;
12 escVal = abs(z);

If you run this in Matlab, the variables escTime and escVal should end up having the values 107 and
2.8690, respectively. Likewise, if you change the value of c to be something inside M (such as c = 1i),
then after running lines 2 through 12 again, the variable escTime should end up having the value set by
maxEscTime. Thus if you put this code inside a loop which loops over all pixels in an image, storing each
calculated escape time and escape value in a corresponding array, then at the end you will have the data
you need to create pictures like those above. The problem with this approach is that it will be very slow in
Matlab for large-resolution images. For example, for an image of size 1000 by 1000 pixels, there will be
106 different c values, which means we would need to execute the above while loop a total of 106 different
times. Fortunately, there is a much faster way that uses logical indexing and vectorization. Logical indexing
lets you select/access only those elements of an array that satisfy some logical criterion. Vectorization lets
you operate on entire arrays at once without explicitly looping over the elements.

4 The assignment

You will create a function m-file in this assignment, unlike the script m-files you created in previous
assignments. A function m-file begins with a function declaration, which is a line starting with the keyword
function and containing information about the outputs, name of the function, and its inputs. Your function
should be called mandelbrot, and the file name should be mandelbrot.m. There will be four inputs to
your function:

limits This is a 4-element vector specifying a rectangular region in the complex plane. It has the form
[XMIN XMAX YMIN YMAX], where XMIN and XMAX are the minimum and maximum real parts and YMIN
and YMAX are the minimum and maximum imaginary parts. For example, [-1 2 -2 3] specifies a
rectangular region in the complex plane with −1 − 2i as the lower left corner and 2 + 3i as the upper
right corner. This is similar to the input to the axis function. Default is [-2.0 0.5 -1.2 1.2].

nx The number of points (pixels) in the x-direction. Default is 1000.

ny The number of points (pixels) in the y-direction. Default is 1000.

maxEscTime The maximum number of iterations in the sequence allowed when calculating the escape
times. This will be the maximum effective escape time besides Inf. Default is 1000.

3
There will also be three outputs to your function:

EscTime An ny-by-nx array containing the escape times for each pixel.

EscVal An ny-by-nx array containing the escape values for each pixel.

Image An array containing the color data for the image.

You will write your own code to calculate EscTime and EscVal, and we will provide you with the code to
calculate Image.

1. Begin your file with the function declaration, followed by comments to document your function. After
the function documentation, enter a blank line followed by comments with your name, etc., as in
% Homework Program 4
% Name: Granger , Hermione
% Date: 10/25/2024

2. Set each of the four input variables to its default value if it is missing, i.e., not supplied by the function
call. You will use features of an arguments block, which in addition allows checking for bad inputs
causing Matlab to throw errors when that happens. Moreover, instead of identifying the inputs by their
position in the function call, as you learned in class, here we will use named inputs identified by some
keywords, which in addition to setting the default values allows the inputs to be specified in any order.
Therefore, the first line of your function would look something like
function [output1 , output2 , ...] = mandelbrot (kw)

followed by an arguments block containing lines of code that check for bad inputs and supply default
values. See Matlab documentation on arguments for more details. For example:
arguments
kw. limits (1 ,4) { mustBeNumeric , mustBeReal , mustBeFinite } = [ -2.0
0.5 -1.2 1.2]
end

which declares the keyword limits to be a 1 by 4 matrix, i.e., a 4-column row vector, checks for
valid input values, and specifies default values. Include lines of code like this for each of the other
three inputs. Note that this method of assigning default values to inputs is more flexible than the
method demonstrated in Example 6.3 of the Chapman text (pages 254–256), which is based on simply
checking the value of nargin. For example, the above method allows you to call your function as
mandelbrot(nx=100) to get a low-resolution image with the default values for limits, ny, and
maxEscTime.

3. Create an ny-by-nx array called C that contains all of the c values in the rectangular region. The
functions linspace and meshgrid are useful here, and in fact the following code will do what you
want (just copy and paste it):
x = linspace ( limits (1) ,limits (2) ,nx);
y = linspace ( limits (4) ,limits (3) ,ny);
[X,Y] = meshgrid (x,y);
C = X + 1i*Y;

4
This code will produce an array C of complex numbers. The element C(1,1) represents the upper left
corner of the region, which has the value XMIN+1i*YMAX. Likewise, C(ny,1) represents the lower
left corner of the region, which has the value XMIN+1i*YMIN. Similarly, C(1,nx) represents the
upper right corner of the region, which has the value XMAX+1i*YMAX. Finally, C(ny,nx) represents
the lower right corner of the region, which has the value XMAX+1i*YMIN. All other elements of C are
equally spaced between these corner values.
4. Create the ny-by-nx arrays EscTime and EscVal containing the escape times and escape values for
each corresponding point in C. This main part of the assignment consists of the following steps:
(a) Initialize the array EscTime to be an ny-by-nx array of all Inf, and initialize the array EscVal
to be an ny-by-nx array of all NaN. Note that Inf(3,5) and NaN(3,5) will create 3-by-5 arrays
of all Inf values and all NaN values, respectively.
(b) Create an ny-by-nx array called Z of all zeros. This array Z will store the current values of the
sequences {z0 , z1 , z2 , . . . }. Because there is a different sequence for each different c, we need Z
to be of the same size and shape as C. Also, because every sequence starts at z0 = 0, we want Z
to be all zeros in the beginning.
(c) Create an ny-by-nx array called done of all logical false values. This array will flag as “done”
any values of c for which we already know the escape time. In the beginning we don’t know
anything, so done should contain all false values. Note that false(3,5) will create a 3-by-5
array of all false values.
(d) Initialize the sequence index variable k to 0.
(e) Create a while loop that will evaluate the loop body whenever k is less than maxEscTime and
not all of the entries in the done array are true. Note that all(done(:)) will return true if and
only if all entries in the done array are true. Inside the loop body, do the following:
i. Replace each element of Z with its square plus the corresponding element of C. In other
words, replace the current z with z 2 + c for each different value of c. Do this using
vectorization: do not explicitly loop over all values of Z, but instead use appropriate array
operations to process the whole array Z at once.
ii. Increment the sequence index variable k.
iii. Create an ny-by-nx logical array called new whose elements are true when the correspond-
ing element of abs(Z) is greater than 2 and the corresponding element of done is false.
Again, use vectorization here instead of explicitly looping through all values of Z and done.
The true elements of this array new will represent all “newly escaped” sequences, that is,
all sequences whose escape times are equal to the current value of the loop variable k.
iv. Use logical indexing to assign the current value of k to those elements of EscTime given by
the true elements of new. Likewise, assign the current absolute values of those elements
of Z given by the true elements of new to the corresponding elements of EscVal.
v. Update the done array by setting each element to true if either it was already true or the
corresponding element of new is true. Again, use vectorization, not explicit loops.
5. Create the Image array and display the image. You do not have to understand how to do this. Instead,
just call the function showMandelbrot provided for you in the file showMandelbrot.m, which you
can download from Canvas from the same folder you got this assignment. The function call is simply:
% Plot result as a color image
%
Image = showMandelbrot (EscTime ,EscVal , limits );

5
Test your function in the command window by running it first for the default input values, i.e., by
typing mandelbrot on the command line and hitting return. Be patient—it can take a while to generate
the image (perhaps tens of seconds on a slower computer). Then, test your function for the following input
values for the limits: [-0.7489 -0.7484 -0.06081 -0.0604], [-0.58 -0.572 -0.475 -0.463],
and [-0.75 -0.732 -0.2 -0.177]. Again, be patient! The above limits values should generate the
pretty pictures shown on page 2 of this document.

On Canvass where you downloaded your homework assignment, you will also find a Matlab file:
exploreMandelbrot.m and a zoom_mandelbrot.mp4 file. The former will allow you to interact with
your program and explore the pretty images it produces by selecting a rectangular box in the current image
with your cursor. The latter is a movie compiled from such images by Prof. Freeman.

Submit only your m-file mandelbrot.m, not any of the figures or pictures it generates.

You might also like