Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED MATLAB HELP! (10 points) Using MATLAB and for loops, provide an animation t

ID: 3564948 • Letter: N

Question

NEED MATLAB HELP!

(10 points) Using MATLAB and for loops, provide an animation that looks like below. Create a pink filled square with length 2 units. Locate the initial lower left corner of the square at the origin (0,0) of the figure. At each animation step, add another square to your figure, where each new square is smaller, and translated in a diagonal (approximately). Also, make the pink color fade with each new square. Provide a total of 11 squares in your animation. They will look like below.

Explanation / Answer

Have a look at the two values of x alone to simplify this investigation. After your first iteration, x will be 2and 1, in the second iteration 1 and 0.5. This means you are approaching zero with squares that are getting smaller and smaller, the opposite of what you intended to do.

How about you start big close to the origin and shrink as you go further away? You could initialize x = [0, 2]; and y = [0, 2];. We're using only two elements here because for a square that's aligned with your axes, that's all we need. The first iteration may start with a shift by the edge length of the previous square as in x = x + x(2) - x(1);. The square will have to shrink though as well, so you could move the left corners by some small fraction of the edge length, e.g. x(1) = x(1) + (x(2) - x(1)) * 0.1;. To summarize, your loop would look like

Note that we replaced x(2) - x(1) by edge_len. Then we another problem of setting your color. You could use a color vector c = [1, k / 10, k / 10] to create a gradient from red to almost white. Then instead of fill(..., 'r'); you'd use fill(..., c);

With this, there won't be any fill outside the loop. That used to cover all your interesting graphing in the code block you show in the question.