Question
Hi, this is for C++. Any help would be much appreciated. The Mandelbrot set is a fractal defined as the set of points on the
Hi, this is for C++. Any help would be much appreciated.
The Mandelbrot set is a fractal defined as the set of points on the complex plane such that a particular iterated function on them does not "diverge". For a given complex number c, the iterated function is as follows:
f_0(c) = (0 + 0i)
f_n(c) = f_{n-1}(c)^2 + c
where the squaring in step 2 is done by complex number multiplication. It is known that if at any point during the iteration |f_n(c)|becomes greater than 2, that the function will eventually diverge. Thus, a common visualization technique is to iterate until |f_n(c)| is greater than two, up to some maximum number of iterations, say 255. Pseudocode for this is given below:
int mandelbrotIteration(Complex c, maxIterations) { Complex z = 0 + 0i; for i=0 to maxIerations z = z*z + c; if |z| > 2 return i; return maxIterations; }
The main instructions are written below here----------------------------------------------------------------------------
Write a class called Complex that encapsulates a complex number with the following functionality:
-It should have two double variables for the real and imaginary coefficients of the number.
-Make a constructor that takes two double values for the real and imaginary coefficients of the number.
-Make a length method that returns the distance of the number from the origin (length(a+bi) = sqrt(aa + bb))
-Overload the +, =, and * operators for your class to operate properly on complex numbers.
-Overload the >> operator so you can print out complex numbers.
Write a program that will take the real and imaginary coefficients of a complex number, store them using your Complex number class, and then perform the Mandelbrot iteration on it.
-The program should terminate the iterations when the length of the z is greater than 2, or when 255 iterations are completed.
-When the program stops iterating, it should print how many iterations were performed along with the value of the iterate (z in the pseudocode above). For your output of the final z value, print 4 digits after the decimal point. Hint: use the following to print 4 digits after the decimal point (or use printf).
cout << setprecision(4) << fixed;
Sample program run
Enter the coefficients of a complex number: 0 1.5 Num iterations: 2 Final value: (-2.2500 + 1.500i)
------------This is what the main method is:-------------------
#include using namespace std;
#include "Complex.h"
int main(void) { double a, b; const int MAX_ITERATIONS = 255;
cout << "Enter the coefficients of a complex number: "; cin >> a; cin >> b;
Complex c(a,b); Complex r(0,0);
int i = 1; while (true) { r = r*r; r = r+c; //cout << i << " " << r << endl; if (r.a*r.a + r.b*r.b > 4) break; if (i == MAX_ITERATIONS) break; i++; }
cout << "Num iterations: " << i << endl; cout << "Final value: " << r << endl; }
A seperate Complex.cpp file needs to be made as well as a Complex.h file for the header. The .h file just needs to handle the double variables and the overloaded opertators. The .cpp file should hold the rest of the functionality for the program.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started