python - 复数中的 Mandelbrot 集绘图和 ZeroDivisionError
问题描述
我正在制作一个绘制 Mandelbrot Set 版本的代码。当它运行时,它接受两个输入,a 和 b,并将其转换为复数(例如 complex(a,b)。)然后它绘制一个 Mandelbrot 集,其中 z 是复数的幂,加上 c,与 z**2+c 的普通版本不同。但是复数的某些输入,例如 5 和 6,有时会给出错误 ZeroDivisionError: complex 除以零,或者有时,它不会给出任何输出。有时,当我输入 1 和 1 时,它会起作用,并输出如下所示:
这是我关于 stackoverflow 的第一个问题,所以我不确定那张图片是否有效,但无论如何,它看起来不像普通的 Mandelbrot 集。怎么了?另外,这是我的代码:
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
解决方案
对于复数,求幂没有明确定义。您可以定义a ** b
ax exp(log(a) * b)
,但log(a)
最多只能定义2pi i
,因此您有无限种可能性。对于整数幂,您可以定义z**k
为z * z * ... *z
k
时间,但这几乎可以肯定不是python 所做的k
定义,complex
就像您在上面所做的那样。
推荐阅读
- java - 构建期间 Maven 中的 OOM
- docusignapi - 在单个集成密钥上使用生产和演示帐户进行身份验证
- c# - Jenkins Windows slave 上的 dotnet restore - 值不能为空
- javascript - wordpress 在插件中调用 javascript 函数
- java-8 - 具有限制+并行的Java8 IntStream
- json - 如何在 Swift 4 中编写 JSON 序列化
- swift - Mac 上的 AVAudioPlayer 无法播放/崩溃
- kotlin - 数组和arrayOf,hashmap和hashmapOf有什么区别?
- python - 如何使用 Python Flask 将文件上传到 Firebase 存储?
- c++ - 转换 STL 列表
成向量 c++11