这个作业是编写代码来生成指定图形的**Matlab代写**

Engr 3: Introduction to Programming

Fall 2020 Midterm Project

Figure 1: The Mandelbrot set: −2 ≤ a ≤ 1, −1 ≤ b ≤ 1.

The white object in Figure 1 is called the Mandelbrot set. It is a famous example of how complex behavior can arise from simple rules, and has very interesting fractal properties.

In this project, you’ll write Matlab code to generate this figure, and more. You will turn in your Matlab functions mandelbrot.m and pixelate.m.

First, let’s define the Mandelbrot set. Suppose that z and c are complex numbers,and define a sequence

z0 = 0,

zn+1 = z

2n + c, n ≥ 0.

So,

z1 = z2

0 + c,

z2 = z2

1 + c,

z3 = z2

2 + c,

etc. For a given value c, there are two possible cases:

• If |z32| ≤ 2 then c is in the Mandelbrot set.

• If |z32| >2, then c is not in the Mandelbrot set.

(Note that the actual definition is that if |zn| stays finite as n → ∞, then c is in the Mandelbrot set, and if |zn| → ∞ as n → ∞, then c is not in the Mandelbrot set. But the above conditions are equivalent, and are easier to handle computationally.)

We will write c = a + ib, where i =√−1. In Figure 1, the horizontal axis corresponds to values of a (with a = −2 on the far left, and a = 1 on the far right), and the vertical axis corresponds to values of b (with b = 1 on the top, and b = −1 on the bottom – note that larger values of b are higher in this figure, a convention that we’ll use for all figures).

It was generated using the command imshow(M,’InitialMagnification’,’fit’)

where M is a matrix whose elements are 1 if the corresponding pixel is in the Mandelbrot set, and 0 if the corresponding pixel is not.

(a) (60 pts) Write a Matlab function called mandelbrot.m which takes as input the values amin, amax, bmin, bmax which are the minimum value of a, the maximum value of a, the minimum value of b, and the maximum value of b for the desired figure, respectively. We’ll assume that the desired figure has a total of 1000 pixels in the horizontal direction, and 800 pixels in the vertical direction. Note that for this project you don’t need to be super careful about ensuring that the edges of the figure correspond to these precise values; for example, the far left pixels could correspond to a = −2, or to a = −1.997, as is convenient for your program.

Your function should return M, which is a 800 × 1000 matrix whose entries are either 1 (if the pixel is in the Mandelbrot set) or 0 (if the pixel is not in the Mandelbrot set). When your function is working properly, the following lines of code should generate Figure 1:

M = mandelbrot(-2,1,-1,1);

imshow(M,’InitialMagnification’,’fit’)

To verify that your code is working properly, the following lines of code should generate Figure 2:

M = mandelbrot(-1.2,-0.5,0,0.5);

imshow(M,’InitialMagnification’,’fit’) and the following lines of code should generate Figure 3:

M = mandelbrot(-0.4,0.2,0.5,1.2);

imshow(M,’InitialMagnification’,’fit’)

Figure 2: Zooming in on the Mandelbrot set: −1.2 ≤ a ≤ −0.5, 0 ≤ b ≤ 0.5.

Figure 3: Zooming in on the Mandelbrot set: −0.4 ≤ a ≤ 0.2, 0.5 ≤ b ≤ 1.2.

(b) (40 pts) Now, let’s create pixelated images of the Mandelbrot set, such as Figure 4. In particular, write a function called pixelate.m which takes as input the matrix M (which comes from mandelbrot.m) and the variable boxsize, and returns matrix Mpixel of size 800/boxsize × 1000/boxsize, where each element of this matrix is equal to the fraction of points in the corresponding square of the original figure belong to the Mandelbrot set,as explained in the following.

The pixelization procedure can be understood by referring to Figure 5. The (m,n) pairs refer to the element of the matrix Mpixel, shown here for boxsize=100. For example,the (3,5) element is associated with the square with rows 201-300 and columns 401-500 of the M matrix. The value of the (3,5) element is assigned to be the fraction of the pixels

Figure 4: Pixelated Mandelbrot set: −2 ≤ a ≤ 1, −1 ≤ b ≤ 1, boxsize = 100.

Columns Columns Columns Columns

101− 200

Columns

101−200

Rows

(1,1) (1,2) (1,3) (1,4) (1,5)

(2,1) (2,2) (2,3) (2,4) (2,5)

(3,1) (3,2) (3,3) (3,4) (3,5)

…

…

…

…

…

…

…

…

201−300

1 − 100 201−300 301−400 401−500

1 − 100

Rows

Rows

in this square which are in the Mandelbrot set. For example, suppose that 5189 out of the 100 × 100 = 10000 pixels in this square are white, corresponding to points in the Mandelbrot set. Then we will assign Mpixel(3,5) = 0.5189

Your code should work for any value of boxsize which is sent to the function as input.

(You may assume that boxsize divides 1000 and 800 with no remainder.)

When your function is working properly, the following lines of code should generate Figure 4.

M = mandelbrot(-2,1,-1,1);

Mpixel = pixelate(M,100);

imshow(Mpixel,’InitialMagnification’,’fit’)

Similarly, these lines of code generate Figure 6:

M = mandelbrot(-2,1,-1,1);

Mpixel = pixelate(M,50);

imshow(Mpixel,’InitialMagnification’,’fit’)

and these lines of code generate Figure 7:

M = mandelbrot(-2,1,-1,1);

Mpixel = pixelate(M,20);

imshow(Mpixel,’InitialMagnification’,’fit’)

Figure 6: Pixelated Mandelbrot set: −2 ≤ a ≤ 1, −1 ≤ b ≤ 1, boxsize = 50.

For Fun: The following is not required for the Midterm Project, but if you enjoyed the above problems, you might want to try it, just for fun.

For points which are not in the Mandelbrot set, you can color them according to how many iterations are needed to reach |zn| > 2. This is how the plots in Figure 8 were generated. Note that you’ll need to modify mandelbrot.m to create three matrices R,G,B,then use the commands

img = cat(3,R,G,B);

image(img)

as in HW4 to generate the color plots in Figure 8.