Professional Documents
Culture Documents
Final Project
2
Part 1: Render the Mandelbrot Set
Use the complex class to simplify your life,
all computations are in the complex plane.
complex<double> cmpx(1,1);
Do a program that will loop over the
complex plane in the range:
-2 < Real Part < +1
-1 < Imaginary Part < +1
For each complex number in the loop, use it
as the parameter in the recurrence relation:
z -> z2 + c
Initial value of z=(0,0).
3
Part 1: Continued, Render M set
Test to determine if a point is in the set or not:
if |z| is always < 4 after 100 iterations, the point is
considered to be in the set.
if |z| goes beyond 4, it is not in the set.
The number of iterations can be used as a
“distance” measure of the point to the set.
Use this to create a 2-D histogram
Each cell represents a choice of the c parameter:
x-axis : Real Part of c
y-axis : Imaginary Part of c
Fill each cell with a weight equal to the number of
iterations done by the algorithm for that value of c.
4
Result from Part 1:
Obtain a plot of the Mandelbrot Set
Save it as a gif file from root:
“part1.gif”
60 points
5
Part 2: Look at the fine detail.
Now that you can render the Mandelbrot Set, have some fun with it:
It is easy to look at a smaller region, all you do is change the region you
will loop over:
RealPartMin < Real Part < RealPartMax
ImagPartMin < Imaginary Part < ImagPartMax
Result from part 2:
Choose a new range, magnification of at least 10,000
(5 points)
Save as part2a.gif
Magnfication of 1,000,000 or more
(5 points)
Save as part2b.gif
Note:
number of iterations can be adjusted.
can try a different color palette in ROOT.
6
Magnificent magnifications…
8
Final Project Summary
Part 1: you rendered it full scale,
name it “LastnamePart1.gif”
60 points
Part 2: you zoomed into a small region.
5 + 5 = 10 points
Name them “LastnamePart2a.gif” and “LastnamePart2b.gif”
Part 3: you can render many plots between the full
Mandelbrot set and the zoomed in region. Use the one with
the largest magnification factor.
Modify the program to make 20 plots for 20 choices of the
complex plane range.
Save each plot into an animated gif:
Use TCanvas::Print(“LastnamePart3.gif+10”);
The +n in the Print command will save the frame with a delay of n x 10 ms
with respect to the previous frame.
30 points
Extra credit: Continuos Animation, use Gimp or Photoshop.
9
Extra points: Continuous Animations,
Zoom in & out and repeat…
Saved with ROOT using
TCanvas::Print(“part3.gif+10”);
Edited with the GIMP to produce continuous In+Out
animation that repeats over and over.
Photoshop works too, if you have it. (GIMP is free.)
10 extra points.
10
Suggestions:
One can do the programs all in root.
Suggest to split into two parts:
C++ Program to produce a text file with all the
values needed to make a plot.
compile it so it’s fast
ROOT macro
read the text file
11
Useful code Snippets.
12
Text output into many files: ofstream
#include <fstream>
char filename[255];
sprintf(filename,"MandelbrotRunDataAdapIter%i.txt",i);
cout << filename << endl;
ofstream mandelOutput(filename);
for (double c_real=RealPartMin[i];
c_real<RealPartMax[i];c_real+=stepSizeRe) {
for (double c_imag=ImagPartMin[i]; c_imag<ImagPartMax[i];
c_imag+=stepSizeIm) {
mandelOutput << c_real << '\t' << c_imag << '\t' << iter << endl;
13
Using the complex class
Operators defined for complex numbers
+, -, *, /, -=, +=, /=, *=, =, ==, !=
Overloading of operators:
complex_ob + scalar
scalar + complex_ob
complex_ob + complex_ob
Functions:
Returning a scalar:
T abs(const complex<T> &ob) : absolute value
T arg(const complex<t> &ob) : phase angle
T conj(…) : complex conjugate
cos, cosh, exp, imag, log, log10, pow, real, sin, sinh sqrt, tan, tanh, do the
obvious
T norm(const complex<T> &ob) : magnitude squared
complex<T> polar(const T& v, const T& theta=0) : magnitude specified by
v and phase angle specified by theta
14
Demonstrating complex class
#include <iostream>
#include <complex>
using namespace std;
int main() {
complex<double> cmpx1(1,0);
complex<double> cmpx2(1,1);
cmpx3+=10;
cout << cmpx3 << endl;
return 0;
}
15
Avoiding some problems with TH2D
16
Reading data from text files
//loop to read many files that have a similar name
// and one number to index them.
for (int i=0; i<nPlots; ++i) {
char filename[255];
sprintf(filename,"MandelbrotRunData%i.txt",i);
ifstream inputFile(filename);
//read the data
// example, read 1 line:
char buffer[255]l;
inputFile.getline(buffer,255);
//example, read 3 numbers from a line
double number1, number2, number3;
inputFile >> number1 >> number2 >> number3;
// note that one must take care of end-of-lines.
// another useful loop: while(!inputFile.eof()) { // keep reading next line }
// do something with data.
}
17
Playing with Palettes
gStyle:
SetPalette(1,0);
RGB colors in the
range 51 to 99
SetPalette(51,0);
Deep blue to light blue
CreateGradientColorTa
ble
18
Example (modified from R. Brun’s in
ROOTTALK)
void colorPalette() {
//example of new colors and definition of a new palette
const Int_t colNum = 200;
Int_t palette[colNum/2];
for (Int_t i=0;i<colNum/2;i++) {
TColor *color = new TColor(501+i,0,Float_t(i)/(colNum/2.),Float_t(i)/
(colNum),"");
palette[i] = 501+i;
}
gStyle->SetPalette(colNum/2,palette);
void colorPalette() {
//example of new colors (greys) and definition of a new palette
const Int_t NRGBs = 5;
const Int_t NCont = 256;
20