You are on page 1of 20

Computational Lab in Physics:

Final Project

The Mandelbrot Set.


Rendering the Mandelbrot Set
 Defined in the Complex plane
 Use the c++ complex class.
 Set of points such that the mapping
 z -> z2 + c
does not escape to infinity.
 In code:
 must use a threshold. One can use e.g. |z|<4.
 must use a finite number of iterations. One can
use 100 or 1000 (or make the iterations
depend on the scale of the plot).

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…

 Coordinates of the center:


 Re(c) =
-.743,643,887,037,151,
 Im(c) = .
131,825,904,205,330
 Horizontal diameter of the
image:
 .000,000,000,051,299
 Magnification relative to the
initial image:
 59,979,000,000
 Note: plot not done with
ROOT. See wikipedia entry
for Mandelbrot Set for
reference.
7
Make an Animation: zoom into a small
region

 Saved directly from ROOT using


TCanvas::Print(“part3.gif+10”);
 40 Points

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 independent, so it’s portable

 ROOT macro
 read the text file

 fill in the histograms TH2D

 draw histogram in canvas

 save the gif file with TCanvas::Print

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) {

// iterate until iter>100 or |z|>4

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);

cout << cmpx1 << “ “ << cmpx2 << endl;

complex<double> cmpx3 = cmpx1*sqrt(cmpx2);

cout << cmpx3 << endl;

cmpx3+=10;
cout << cmpx3 << endl;

return 0;
}
15
Avoiding some problems with TH2D

 Make sure to match your number of bins with the


step size and number of calculations.
 Example:
 nBinsx=1000; // bins along Real (x-axis)
 nBinsy=1000; // bins along Imag (y-axis)
 xRangeMin= -2; xRangeMax= 1;
 yRangeMin = -1;yRangeMax= 1
 stepSizeX = (xRangeMax – xRangeMin )/nBinsX
 Book the histogram using above values.
 Even better book histogram with range:
 xRangeMin-stepSizeX/2 to xRangeMax-stepSizeX/2

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

 Look in TColor class:


 SetPalette

 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);

TF2 *f2 = new TF2("f2","exp(-(x^2) - (y^2))",-1.5,1.5,-1.5,1.5);


//f2->SetContour(colNum);
f2->Draw("colz");
return;
}

Plot using the above


palette
19
More control over colors:

void colorPalette() {
//example of new colors (greys) and definition of a new palette
const Int_t NRGBs = 5;
const Int_t NCont = 256;

Double_t stops[NRGBs] = { 0.00, 0.30, 0.61, 0.84, 1.00 };


Double_t red[NRGBs] = { 0.00, 0.00, 0.57, 0.90, 0.51 };
Double_t green[NRGBs] = { 0.00, 0.65, 0.95, 0.20, 0.00 };
Double_t blue[NRGBs] = { 0.51, 0.55, 0.15, 0.00, 0.10 };
gStyle->CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
gStyle->SetNumberContours(NCont);

TF2 *f2 = new TF2("f2","exp(-(x^2) - (y^2))",-1.5,1.5,-1.5,1.5);


//f2->SetContour(colNum);
f2->SetNpx(300);
f2->SetNpy(300);
f2->Draw("colz");
return;
}

20

You might also like