title: Trouble with creating Mandelbrot Set plot in C
body: <p>I am trying to create a Mandelbrot set in C code; my output will be a data file, one column of the real parts and one column of the imaginary parts to be plotted in the Argand or complex plane. I have all the complex math stuff defined in a header <code>complex.h</code>and am using a structure <code>complex</code> with a <code>double R</code> for the real part and <code>double I</code> for the imaginary. I am trying to loop trough values for dz and update z iter=80 number of times, or until the point lies outside a defined radius. dz is imaginary, essentially on a range [(dzrmin + i*dzimin), (dzrmax +i*dzimax)]. z is the current complex number, updated as z^2 = dz + z. I have a function <code>csum()</code> to sum two complex numbers and a function <code>csquare()</code> to properly square a complex number. Here is my whole code </p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;math.h&gt;
      #include "complex.h"
  
      int main(void)
      {
          double dzrmin, dzrmax, dzimin, dzimax, dzr,dzi, r0,i0, maxrad; 
          int i,j,k,iter, Ndr,Ndi; 
          complex z, dz; 
          dzrmin = -2.1; 
          dzrmax = 0.6; 
          dzimin = -1.2; 
          dzimax = 1.2; 
          Ndr = 200 ; 
          Ndi = 180; 
          dzr = (dzrmax - dzrmin)/Ndr; 
          dzi = (dzimax - dzimin)/Ndi; 
          r0 = 0.0; 
          i0 = 0.0;
          z.R = r0; 
          z.I = i0; 
          dz.R = dzrmin; 
          dz.I = dzimin; 
          maxrad = 2.0; 
          iter = 80 ; 
          for(i = 0; i &lt; Ndr; i++ )
          {
                      for(j = 0; j &lt; Ndi; j++ )
                      {
  
                          for(k = 0; k&lt; iter; k++ )
                          {
                      printf("%.6lf %.6lf\n", z.R, z.I ); 
                      z = csum( csquare( z ), dz ) ; 
  
                      if( cmag(z) &gt; maxrad) k = iter ; 
  
                  }
  
                  dz.I += dzi ; 
              }
              dz.R += dzr; 
              z.R = r0; 
              z.I = i0; 
          }
          return 0; 
      }
  </code></pre>
  
  <p>and my header file <code>complex.h</code></p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  typedef struct complexnumber{ double R; double I ; } complex ;
  double cmag( complex z)                                                                                                                                             
  {
      return pow( z.R*z.R + z.I*z.I, 0.5 ) ; 
  }
  
  complex csquare( complex z )             //returns square of a complex
  {
      complex product ; 
      product.R = z.R*z.R - z.I*z.I ; 
      product.I = 2*z.R*z.I ; 
      return product ; 
  
  }
  
  complex csum( complex z1, complex z2) // sums two complex numbers 
  {
      complex sum ; 
      sum.R = z1.R + z2.R ; 
      sum.I = z1.I + z2.I; 
      return sum ; 
  }
  </code></pre>
  
  <p>I am getting a few real values, they get very big really quickly and lie outside a radius of 2, followed by a lot of real parts <code>-nan</code> and imaginary parts <code>-nan</code>. 
  Any suggestions as to what I am missing? </p>