Chiffchaff
Chiffchaff

Reputation: 31

Mandelbrot Set drawing and ZeroDivisionError in complex numbers

I am making a code that draws versions of the Mandelbrot Set. When it runs, it takes in two inputs, a and b, and converts it to a complex number (e.g. complex(a,b).) Then it draws a Mandelbrot set with z to the power of the complex number, plus c, unlike the normal version which is z**2+c. But some inputs for the complex number, like 5 and 6, sometimes give an error, ZeroDivisionError: complex division by zero, or sometimes, it gives no output whatsoever. Sometimes, when I put in 1 and 1, it works, and puts an output like this:

This is my first question on stackoverflow, so I'm not sure if that picture worked, but anyway, it looks nothing like the normal Mandelbrot set. What is happening? Also, this is my code:

import turtle
s=turtle.Screen()
t=turtle.Turtle()
a=int(input())
b=int(input())
h=complex(a,b)
t.pu()
t.goto(-200, 200)
turtle.tracer(0,0)
t.speed(0)
for y in range(200,-200,-1):
  turtle.update()
  for x in range(-250,200):
    t.goto(x,y)
    r=float(float(x)/170)
    i=float(float(y)/170)
    c=complex(float(r),float(i))
    z=complex(0,1)
    for exptest in range(50):
      z=(z**h)+c
      if abs(z)>2:
        if exptest>0:
          t.color('black')
          if exptest>1:
            t.color('yellow')
            if exptest>2:
              t.color('green')
              if exptest>3:
                t.color('red')
                if exptest>5: 
                  t.color('orange')
                  if exptest>10:
                    t.color('purple')
                    if exptest>20:
                      t.color('blue')
                      if exptest>30:
                        t.color('light blue')
        else:
          t.color('light green')
        t.dot(1)
        break

Upvotes: 3

Views: 49

Answers (1)

Igor Rivin
Igor Rivin

Reputation: 4864

exponentiation is not well-defined for complex numbers. You can define a ** b ax exp(log(a) * b), but log(a) is only defined up to 2pi i, so you have an infinite number of possibilities. for integer powers, you can define z**k as z * z * ... *z k times, but this is almost certainly not what python does for k defined as complex as you do above.

Upvotes: 2

Related Questions