Solved

Testing wheter a complex number is in the mandelbrot set

Posted on 2003-10-27
3
274 Views
Last Modified: 2011-09-20
Hi,
Im trying to write some C that will test whether a complex number in the form a + bi lies within the madelbrot set.
To lie within the set C has to be iterated 200 times without the result going over 2.
to start with the values are set to 0 and the first iteration is does on the zero values, then the real and imaginary parts of C are added and if the modulus of the new answer is less then 2 then the loop continues, if not it outputs that C is not in the mandelbrot set.

For some reason my code always outputs
Time until 2 is exceeded = 2

I cant quite see where Im going wrong, I am pretty new to C.

Here is my code.
Thanks for any help


#include <stdio.h>
#include  <math.h>

int main() {
    mSetTest(2.0, 2.0);

          return (0);
}


int mSetTest(double c_re, double c_im)
{
  int i, count;
  float real = c_re;
  float imag = c_im;
  float zreal, zimag = 0;
  float zrealans,zimagans,testreal,testimag;
  //Perform 200 Iterations to test wheter C is in Mandelbrot set
  for(i=1; i<200; i++)
  {

     zrealans = (zreal*zreal)-(zimag*zimag);

     zimagans = (zreal * zimag) * 2;

     testreal = zrealans+real;

     testimag = zimagans + imag;


  if (sqrt((testreal*testreal) + (testimag*testimag))>2)
  {   /*Increase the counter and print how many
              iterations were performed before exceeding 2
          */
        count ++;
        printf("Times until 2 is exceeded =%i\n", count);
          return (0);
  }
  else
  {   /*Increase the counter and set the initial variables to the new ones */
        count ++;
        zreal=testreal;
          zimag=testimag;

  }
return (i);
}
 /*If iterations go greater than 200 then C is in the mandelbrot set*/
        printf("C is in the mandelbrot set");
}
0
Comment
Question by:welsh_boy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Expert Comment

by:GaryFx
ID: 9629185
On the first iteration:
    real is  2.0
    imag is 2.0
    zreal is 0
    zimag is 0

Therefore
    zrealans is 0
    zimagans is 0
    testreal is 2.0
    testimag is 2.0

Therefore
   sqrt((testreal*testreal) + (testimag*testimag)) is sqrt ((2*2) + (2*2) which is sqrt (8) which is > 2.   Thus you always take the first branch, print the message, and return.

Gary

0
 

Author Comment

by:welsh_boy
ID: 9629357
I have changed my code slighty my mSetTest now looks like below.
But now, no matter what values I pass it for c_re and c_im my program always outputs Time until 2 is exceeded = 1

!!

int mSetTest(double c_re, double c_im)
{
  int i;
  int count = 0;
  float real = c_re;
  float imag = c_im;
  float zreal, zimag = 0;
  float zrealans,zimagans,testreal,testimag;
  //Perform 200 Iterations to test wheter C is in Mandelbrot set
  for(i=0; i<199; i++)
  {

     zrealans = (zreal*zreal)-(zimag*zimag);

     zimagans = (zreal * zimag) * 2;

     testreal = zrealans+real;

     testimag = zimagans + imag;


  if (sqrt((testreal*testreal) + (testimag*testimag))>2)
  {   /*Increase the counter and print how many
              iterations were performed before exceeding 2
          */
        count ++;
        printf("Times until 2 is exceeded =%i\n", count);
          return (0);
  }
  else
  {   /*Increase the counter and set the initial variables to the new ones */
        count ++;
        zreal=testreal;
          zimag=testimag;

  }
return (i);
}
0
 
LVL 6

Accepted Solution

by:
GaryFx earned 350 total points
ID: 9631072
Silly me (and silly you):

   float zreal, zimag = 0;

doesn't initialize zreal, only zimag.  Personally, I prefer the style of initialization that puts each variable on its own line, i.e.
    float zreal = 0.0;
    float zimag = 0.0;

Gary
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question