#### Archived

This topic is now archived and is closed to further replies.

# Forming the mandelbrot set

This topic is 5554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been reading about the mandelbrot set and have tried to write a simple program to generate one, however it does not work correctly and I just want to check I'm getting my maths right. Here's how I think you form a mandelbrot set: You use an iterative function z = f(z) f(z) -> z^2 + c where z is a complex number in the form x + yi and c is a constant. So you make a graph of the complex plane the y axis being i and the x axis being a real number. You then take a point on the graph such as (2,1) this would represent the complex number 2 + i. You then get your iterative function so at first z = 2 + i. Then you go over the first interation so z = z^2 + c = (4 + 4i) + -0.75 = 3.25 + 3.25i (if c is -0.75). You then work out the absolute value of z if it is below two keep on iterating if it is above two the point (2, 1 in the example) is not in the madelbrot set. You iterate over the point several times if z's absolute value stays below two you assume the point is in the mandelbrot set and plot it on your graph. Right I wrote a program that does that but I'm getting incorrect results, I've only just learnt about complex numbers so I've probably screwed up my algebra somewhere, can anyone please explain to me what I'm doing wrong? [edited by - Monder on September 23, 2003 5:59:17 PM]

##### Share on other sites
Surely (4 + 4i) + -0.75 is 3.25 + 4i, and not 3.25 + 3.25i?
Or am I missing something?

"It may not be true that all Conservatives are stupid people, but it is certainly the case that all stupid people are Conservative." - John Stuart Mill

##### Share on other sites
You´re not missing something since -0.75 = -0.75 - 0i but talking about calculation errors (typos maybe) :
(2+i)² = (2+i)(2+i) = 4 + 2i + 2i + i² = 4 + 4i - 1 = 3+4i not 4+4i.

Monder: What you are doing seems (as far as I can remember from when I did smthg like that) quite correct.

##### Share on other sites
you've got + -0.75 to the coefficant of i as well I think (or at least that's how I'm doing it).

Though I've just done a little more searching round with goolge and it appears C is meant to be the point you are testing (so it also must be a complex number) and z must start as 0 + 0i. I've done that but my program still isn't working properly, the thing I get is kinda mandelbrot set shaped, but ain't quite right.

Atheist you're quite right in my first post it should be
z = z^2 + c = (3 + 4i) + -0.75 = 2.25 + 3.25i

[edited by - Monder on September 23, 2003 6:23:24 PM]

##### Share on other sites
Right here's what my program has made

This was formed using f(z) -> z^2 + c with both z and c being complex numbers c is the point I'm testing to see if it's in the set and z starts at 0 + 0i for the first iteration and if z's absolute value goes above two during the iterations c isn't in the set.

And it's not the function that does the graph because I've just tested it with simple stuff like y = 5 and y = 2x and y = x^2 and it gives the expected results.

[edited by - Monder on September 23, 2003 6:38:00 PM]

##### Share on other sites
I remember getting something like that once when I made my mandelbrot program.

The pseudocode was something like this

zreal = calcZReal(zreal, zimag)
zimag = calcZImag(zreal, zimag)

(I was using the updated zreal instead of the old one like I should have).

Theres a slim chance that you are doing the same thing, so I thought I would point it out.

"Math is hard" -Barbie

##### Share on other sites
Well here's what I'm using to do the cacluation

Zr = (Zr * Zr) - (Zi * Zi);Zi = (2 * Zr * Zi);Zr += Cr;Zi += Ci;

Zr is the real part of Z Zi is the imaginary part of Z. It's the same thing for C and they're all doubles. Z starts at 0 + 0i and C is whatever point I'm testing on the complex plane (i.e. x + yi).

And I've just seen what's wrong (didn't read your post properly Pragma). For anyone who's in interested I had to change my above code to

TempZr = Zr;;Zr = (Zr * Zr) - (Zi * Zi);Zi = (2 * TempZr * Zi);Zr += Cr;Zi += Ci;Thanks a lot guys

[edited by - Monder on September 24, 2003 2:55:59 AM]

##### Share on other sites
double GetMandelDepth(RealValue MX, RealValue MY, int MaxDepth,
bool InsideColored, bool OutsideColored){
const RealValue OutLine=256.0;

RealValue XIter = MX;
RealValue YIter = MY;
RealValue XxX = MX*MX;
RealValue YxY = MY*MY;

int Steps = MaxDepth+1;

do{ // optimized
YIter = XIter * YIter;
YIter += YIter + MY;

XIter = XxX - YxY + MX;
XxX = XIter * XIter;
YxY = YIter * YIter;

if((YxY + XxX) > OutLine)goto OutLabel;

}while(Steps--);

if(InsideColored) return double(MaxDepth*(XxX + YxY)/OutLine);
else return 0.0;

OutLabel:
//smoth rendering
if(OutsideColored){
double Fractional=pow(XxX + YxY,0.1);
Fractional=2.0-(Fractional-1.7411013062659082)/1.296005435557;
Fractional=(pow(Fractional,3)-1.0)/7.0;
MaxDepth-=Steps;
return fabs(double(MaxDepth)+Fractional);
}else return 0.0;
}

##### Share on other sites
Right I''ve added a zoom function to my little program and so I''ve been zooming to the set, the only problem is it starts loosing details at high zoom levels look at the picture below, is this just because I''ve come to the limit of double precision? Any ideas on how to get past this?

##### Share on other sites
I''ve written a program that graphed equations on the complex plane and the loss of detail is one of the things I noticed when I zoomed in too far as well. Through debugging I did learn that the problem was definitely that I was at the edge of the precision for double.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 10
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013151
×