1

I'm currently writing a Mandelbrot program in python. In one implementation, I used python's complex number type. In the other, I implemented complex numbers as two separate numbers (I think I have an error there). I don't understand why the two programs output a different-looking result since they should both do the same thing (unless I have some stupid error in my code). Could someone tell me why this is happening (and possibly what I did wrong), and how I can fix this?

This is my code:

size = 40

for y in range(size):
    for x in range(size*2):
        c = complex(x / (size / 2) - 2, y / (size / 4) - 2)
        z = complex(0, 0)

        i = 0;
        while i < 100:
            z = z**2 + c
            if abs(z) > 2:
                break
            i+=1
        if i == 100:
            print("#", end="")
        elif i > 10:
            print(".", end="")
        else:
            print(" ", end="")
    print()

#########################

size = 40

for y in range(size):
    for x in range(size*2):
        cx = x / (size / 2) - 2
        cy = y / (size / 4) - 2
        zx = 0
        zy = 0

        i = 0
        while i < 100:
            zx = (zx * zx - zy * zy) + cx
            zy = (2 * zx * zy) + cy
            if zx * zx + zy * zy > 4:
                break
            i+=1
        if i == 100:
            print("#", end="")
        elif i > 10:
            print(".", end="")
        else:
            print(" ", end="")
    print()

The upper Implementation prints this:

                                    .
                                        #
                                     ..
                                   .####.
                                   .####.      .
                            ..#..##########......
                           ...#################.
               .           .###################. .
                ......    .#####################.
                .#######..######################.
            ....#########.######################
##############################################.
            ....#########.######################
                .#######..######################.
                ......    .#####################.
               .           .###################. .
                           ...#################.
                            ..#..##########......
                                   .####.      .
                                   .####.
                                     ..
                                        #
                                    .

And the lower one prints this:

                                        # .    .
                                     ..#####.. .   ..
                                 ############# ... .
                  .            #################.
                 .  #.        #################### #
                 .#### .   .#######################
                 #######..###########################.
                ##################################.
              #######################################
             #####################################
##############################################.
             #####################################
              #######################################
                ##################################.
                 #######..###########################.
                 .#### .   .#######################
                 .  #.        #################### #
                  .            #################.
                                 ############# ... .
                                     ..#####.. .   ..
martineau
  • 119,623
  • 25
  • 170
  • 301
Ian Rehwinkel
  • 2,486
  • 5
  • 22
  • 56

2 Answers2

2

You mutated zx and used it in your zy calculation. You need to store your new value for zx elsewhere temporarily so you can properly calculate zy. Something like this:

temp = (zx * zx - zy * zy) + cx                                        
zy = (2 * zx * zy) + cy                                                
zx = temp 

Or more elegantly:

zx, zy = (zx * zx - zy * zy) + cx, (2 * zx * zy) + cy
Chi
  • 322
  • 2
  • 10
-2

This is a guess, but are you using python 2? The values at the bottom plot seem to be making a very approximate graph of mandelbrot, but slightly skewed-looking. I am willing to bet that you are using python 2, where the division operator / only does integer division.

If this is the case, either use python 3 or add this to the beginning of your code:

from __future__ import division
Kaiwen Chen
  • 192
  • 1
  • 1
  • 12