Due Date:
Work should be submitted via WISE Monday, April 5, 2010 before the beginning of class.
The solutions can be found here (pdf) but it is important that you try to do these without first looking at the solutions!
setup()
illustrates how you would use this function to multiply two complex numbers.
If you look at the functions in the Complex number class, you will see the function for cMult. Also note that the cAdd, cSub, and cDiv functions are commented out and not fully implemented. Following the pattern in cMult together with what you know about complex number operations, uncomment and complete the code for the cAdd, cSub, and cDiv functions. Test your code by using the problems you solved in the above Complex Number Problems. Note, you should be able to use the code to compute the solutions for all of the problems. This will both test the correctness of your cAdd, cSub, and cDiv functions, but will also give you practice using the syntax.
Plotting Complex numbers: To see the numbers plotted on the complex plane, create a new Processing sketch containing the code below. Also, copy your Complex class from the previous problem above and paste it at the bottom of this code. When you run the code, you will get the image below on the left. You only need to follow the code in drawComplex() (ignore the rest of the code). Try plotting other operations (e.g. mult, subtract).
![]() |
|
Iterating: Replace the drawComplex() function above with the following:
![]() |
|
The code starts with plotting a complex number c, and then plots m*c, m*m*c, etc, where m is a second complex number. This is an example of iteration - each time through the loop, the value of c is updated based on the previous value of c.
Do you see why the complex numbers increase in length and angle, and why the angle rotates in a counter-clockwise direction?
Modify the code (do you see how?) so that you get the following images:
![]() | ![]() |
Here, the complex numbers do not change in length although the angle does change. | Here the complex numbers shrink in length. |
Rescaling: So far, we have been drawing numbers to scale. That is, the line representing the complex number 10+0i will be 10 pixels long. Sometimes, we don't want to draw things to scale. For example, if you are an architect, your drawing plans for a house will not be the actual size of the house. Instead, it will be scaled (e.g. 10 feet = 1 inch) so that the drawing will fit on a normal piece of paper! Often, we want to do this as well. For example, when we implement the Mandelbrot set, we will be working in a region of the complex plane that extends from approximately from -2 to 2 units along the real axis and imaginary axes. It would not make sense to have 1 unit = 1 pixel because the image would not be visible. In this case, we need to increase the scale of the region so that it fits the entire window.
For example, if want to represent the region from 0 to 1 (on both the real and imaginary axis), and if our window size is 100 pixels in height and width, then each pixel corresponds to .01 = (1/100) units or, we say that 1 pixel = .01 units in length in the complex plane. The origin may also be shifted. The question we need to figure out is, if we have a point in our complex region (e.g. .2+.7i), where will this go in our 100x100 pixel window? This all may sound confusing, but Processing makes it easy to do the rescaling by providing us with a map() function. Look in the online reference to see what the map() function does.
For example, suppose we want to plot the region of the complex plane for 0 to 1 (real and imaginary) and that the window is 20x20 pixels as shown below. Where would the complex number .75 + .5i be placed in the window? To figure it out, we use the map function to determine the pixel associated with (.75,.5) as follows
![]() |
|
Orbits: Create a new Processing sketch and paste in the code below. You will also need to copy in the Complex number class from the previous part of the lab. In class, we will go over exactly what this code is doing. However, in a nutshell, it is generating an orbit generated by iterating on z = z2 + c. The final point is identified by a white dot (depending on the initial point z, this may or may not be visible in the window. Below are some examples of images you get for different starting values.
|
![]() | ![]() | ![]() |
Iteration Example - The Barnsley Fern: This example doesn't actually make use of complex numbers, but it is a good example of iteration and rescaling. The Barnsley fern is described here.
Complete the code below based on the equations given at the web page. You need to add the remaining 3 parts of the if-else seequence and also fill in the appropriate values for the map function. Note, each of the 4 sets of equations (for each part of the if-else segments) is randomly executed some fraction of the time (Eqs1: 1%, Eqs2: 7%, Eqs3: 7%, and Eqs4: 85%). The way this is implemented is by generating a random number between 0 and 1000. The generated number will be below 100 approximately 1% of the time (do you see why?). You should get the image on the left.
If you set the color to a different value for each if-else, you may be surprised at what you get.
![]() |
|
Monday, April 5, before class:
The work in this lab is largely preparation for lab 9 so there is not a lot to turn in. Only Parts 6 and 7 have items to submit: