CS211 Programming Exercise 1
Spring 2008 - 100 Points
Due: Lesson T12 at 1630 (11 Feb)

Objectives:

Due Date/Time: 

Help Policy:

AUTHORIZED RESOURCES: 

      Any, except another cadet’s program.

NOTES: 

Requirements For Documentation:

Problem Overview:

This programming exercise requires you to create a MATLAB program that will calculate and display images of the Mandelbrot set, a fractal which is described below. Your program will read in four(4) inputs from the user, display a single image, and output some statistics about the image in the command window. Part of this assignment is to help you develop strong programming practices. Please note that 40% of the points for this assignment are based on good programming techniques, independent of the program's correctness.

The Mandelbrot Set

The Mandelbrot Set, shown in the image below, is a set of points in the complex plane that have some very interesting properties. For example, the shapes created by the set of points are self-similar -- that is, they create many copies of the same shape at different scales. In addition, the set of points has an infinite complexity -- regardless of how much you zoom into the set of points you see new and interesting shapes.

A point in the complex plane, c = (a + bi), is considered part of the Mandelbrot Set if repeated squaring and additions of the point using the equation zn = z2n-1 + c, do not result in an infinite value, where z0 (the first value of z) is always 0. (Mathematicians call this a divergent function if it goes to infinity-- but this is not a math class, so we will skip most of the math details.) If you repeatedly perform this operation on a value c, and the result always stays below 2.0, then you can be fairly certain that the value will never grow to infinity. If you ever get a value greater than or equal to 2.0, you know immediately that the repeated calculation will produce an infinite result.

Technically, a complex number c is either in (or not in) the Mandelbrot Set, as described above. However, you can create some very nice images by assigning different colors to each complex number in a region based on how many iterations of the equation it takes to get a value greater than or equal to 2.0. This is what you will do for this assignment.

MATLAB Images

MATLAB has many built-in functions that produce graphical output. One such function is the image() function that will treat each value in a 2D array as a color index value and display a "picture" of the array data. For example, the two MATLAB commands below will produce a 300-by-300 pixel image of random colors. (We will cover more details about graphical output in future lessons and only introduce some basic concepts for this assignment.)

My_colors = floor(rand(300) * 64);
image(My_colors);

The color index values, by default, are indexes into a color map containing 64 colors, as shown in the figure below. You can see (and edit) the color values in the color map using the MATLAB command colormapeditor(). (We highly recommend that you do not edit the default color map for this assignment! If you do change the color map, it can be restored to its original values using the command colormap('default') )

 

Program Description:

Your MATLAB program should be a single function that implements the following steps. You should implement and test each step before going to the next step. Please read the Helpful Hints section below BEFORE you begin programming.

  1. Clear the command window.
     

  2. Display a useful introduction to the program's user in the command window.
     

  3. Get the limits on the x (real) axis and the y (imaginary) axis that define a rectangular region in the complex plane.

    a. prompt for and get from the user the left limit of the x axis; the input can be any floating point number and does not need to be validated
    b. prompt for and get from the user the right limit of the x axis; the input must be validated to be greater then the left limit
    c. prompt for and get from the user the bottom limit of the y axis; the input can be any floating point number and does not need to be validated
    d. prompt for and get from the user the top limit of the y axis; the input must be validated to be greater then the bottom limit
     

  4. Create an 2D array containing all ones. The size of the array should be defined by "constants" at the top of your program.
     

  5. For every row and column position in the 2D array, do the following:

    a. Calculate (or set) a variable c equal to the complex number associated with this row/column position in the 2D array
    b. Initialize a variable  z to 0
    c. Repeatedly calculate the formula   z = z2 + c   until the magnitude of  z gets greater than or equal to 2.0 or you have done the equation at least 64 times. In addition, count the number of times the equation is calculated.
    d. Set the value of your 2D array at this row/column position to the number of times you executed the equation.
     

  6. Display the image represented by your 2D array using the image() command.
     

  7. Display text to the command window that contains the following information. Make sure your output is nicely formatted. Only values that never exceeded 2.0 in your calculation loop after 64 tried are considered part of the Mandelbrot Set. All other points are definitely not a part of the set.

    For this image:
      There are ??? points probably in the Mandelbrot Set
      There are ??? points definitely NOT in the Mandelbrot Set

     

  8. If all of the points represented in the image are part of the Mandelbrot set, display the message:

    All of the points are probably in the Mandelbrot set - not very interesting!

  9. If all of the points represented in the image generated a value greater than 2.0 on their first calculation, display the message:

    Your image is totally blue -- not very interesting!.
    Please select a region of the complex plane closer to the origin.

 

Helpful Hints:

 

Grading Information:

Proper Documentation Statement (-5 pts)?                               

Criteria

Pts

Grade

Comments

Good programming practice

40

 

 

Standard header comment block with all elements appropriately filled in

8

 

 

Appropriate commenting in the program body (section comments, variable descriptions)

8

 

 

Meaningful and consistently formatted variable names

8

 

 

Declared constants where appropriate

8

 

 

Easy-to-read program formatting (appropriate use of spaces within lines, similar statements grouped and separated with a blank line)

8

 

 

Program correctness

60

 

 

Clears window and displays program introduction

4

   

Data input and validation

16

 

 

Creation of the 2D array

3

 

 

Calculation of the color values for each position of the 2D array

22

 

 

Display of the image

1

   

Calculate and display of the image statistics

6

 

 

Calculate and display of the "all in set" message

4

 

 

Calculate and display of the "all out of the set" message

4

 

 

Totals

100

 

 

Late Penalties (-25, -50, -75 -100)

 

 

 

Final Grade