Zach466920 Zach466920 - 3 months ago 22
C++ Question

How to Improve Accuracy of Iteration?

I'm trying to make code, c++, to plot the Mandlebrot Set. However, whenever I run my code, see below, the convergence is poor. How do I fix that? I provided some code below. There's also a screenshot of the Mandelbrot Set, red, and my code's approximation, greyscale.

#include <GL/glut.h>

void renderscene(void) {

double x=0;
double y=0;
double ix=0;
double iy=0;
int n=1;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

for(x=-3;x<3;x=x+0.01){
for(y=-3;y<3;y=y+0.01){

for(n=1;n<50;n=n+1){
ix=ix*ix-iy*iy+x;
iy=2*iy*ix+y;
if(ix*ix+iy*iy>4){
break;
}
}

ix=0;
iy=0;

glPointSize(1);
glColor3f(0.1*n,0.1*n,0.1*n);
glBegin(GL_POINTS);
glVertex2f(x*0.4,y*0.4);
glEnd();
}
}

glutSwapBuffers();

}


int main(int argc, char **argv) {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(300,200);
glutInitWindowSize(500,500);
glutCreateWindow("Hello");

glutDisplayFunc(renderscene);

glutMainLoop();

return 1;
}


enter image description here

enter image description here

Answer

You are using the wrong ix in the computation of the new iy.

Try

double nextix=ix*ix-iy*iy+x;
iy=2*iy*ix+y;
ix = nextix;