Question
This question asks you to write MATLAB code to generate interesting fractal patterns. A fractal is defined as a never-ending pattern that repeats itself at
This question asks you to write MATLAB code to generate interesting fractal patterns. A fractal is defined as a never-ending pattern that repeats itself at different scales and exhibits a property called self-similarity. Though fractals are quite complex, they are made by repeating a simple process over and over again (fractalfoundation.org). Let us consider a fractal game that generates the Sierpinski triangle shown in Fig. 1. It is a fractal pattern with the overall shape of an equilateral triangle, sub-divided recursively into smaller equilateral triangles. The pattern is reproducible at any magnification or reduction. It is named after the Polish mathematician Waclaw Sierpinski, but appeared as a decorative pattern many centuries prior to his work. 1 Figure 1: The Sierpinski triangle created by repeating a simple set of rules. 1For a more detailed description, please see the Wikipedia entry for the Sierpinski triangle. 1 To get you started, we have provided some skeleton code within the template file which is reproduced below. Lines 3 through 16 generate and plot an equilateral triangle in which each side measures one unit in length. The vertex B is placed at the origin (0, 0). Lines 19 through 22 place the first mark at a random location inside the triangle as a red dot. You must not modify this portion of the code. 1 % Generate the equilateral triangle of unit length with 2 % vetex B placed at the origin (0, 0). 3 x_b = 0; y_b = 0; % Coordinates of vertex B (x_b, y_b) 4 a = 1; % Length of one side of the triangle 5 x_a = x_b + a/2; y_a = y_b + a*sind(60); % Coordinates of vertex A 6 x_c = x_b + a; y_c = y_b; % Coordinates of vertex C 7 8 % Plot the triangle and label the vertices. 9 figure; 10 plot([x_a, x_b, x_c, x_a], [y_a, y_b, y_c, y_a], 'b-'); 11 axis([-0.1 1.1 -0.1 1.1]); 12 text(x_b, y_b - 0.05, 'B'); % Label vertex B 13 text(x_a, y_a + 0.05, 'A'); % Label vertex A 14 text(x_c, y_c - 0.05, 'C'); % Label vertex C 15 hold on; 16 title('Sierpinski Triangle'); 17 18 % Generate the first point within the triangle at random. 19 w = rand(1, 3); 20 x_o = sum(w.*[x_a, x_b, x_c])/sum(w); 21 y_o = sum(w.*[y_a, y_b, y_c])/sum(w); 22 plot(x_o, y_o, 'r.'); The above code snippet generates Fig. 2. Write MATLAB code to play the following iterative game: 1. Make a mark anywhere inside the triangle. Note that this has already been done for you in the provided skeleton code. 2. Toss a six-sided die. 3. If the die comes up 1 or 2, place a new mark in the triangle halfway between the old mark and vertex A. 4. If the die comes up 3 or 4, place a new mark halfway between the old mark and vertex B. 5. If the die comes up 5 or 6, place a new mark halfway between the old one and vertex C. 6. Using the latest mark as the reference point for placing the next mark, repeat steps two through five. After many tosses of the die, the Sierpinski pattern starts to emerge, as shown in Fig. 3. Here, each red dot represents a mark. Run your code for 104 tosses of the die to generate the Sierpinski triangle shown in Fig. 1. It is 2 0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 B A C Figure 2: The equilateral triangle with unit sides whose vertex B is rooted at the origin (0, 0). The location of the initial mark is shown as a red dot. Note that this mark is placed at random within the triangle for each run of the program. 0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 B A C Figure 3: The Sierpinski triangles after 100 and 1000 tosses of the die. interesting to note that the same pattern emerges irrespective of the location at which the initial mark is placed. In other words, even though a random die is involved, the result of the game is always almost exactly the same whenever it is played. If you are curious to learn how such a simple game like this can produce a structure with as much detail as the Sierpinski triangle, read: Daniel Kaplan and Leon Glass, Understanding Nonlinear Dynamics, Springer-Verlag, 1995, pp. 105142.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started