朱莉娅设置Python

时间:2016-05-04 20:53:12

标签: python nan chaos

我试图在python中设置julia,但我的输出是Nan在一些早期的过程。我不知道是什么原因造成的。 只是为了坦白:我的编程课程不好,我真的不知道我在做什么,这主要来自我从谷歌学到的东西。

以下是代码:

import matplotlib.pyplot as plt

c = complex(1.5,-0.6)
xli = []
yli = []
while True:
    z = c
    for i in range(1,101):
        if abs(z) > 2.0:
            break   
        z = z*z + c

    if i>0 and i <100:
        break

xi  = -1.24
xf = 1.4
yi = -2.9
yf = 2.1

#the loop for the julia set 
for k in range(1,51):
    x = xi + k*(xf-xi)/50   
    for n in range(51):
        y = yi + n*(yf-yi)/50
        z = z+ x + y* 1j 
        print z
        for i in range(51):
            z = z*z + c    #the error is coming from somewhere around here
            if abs(z) > 2:  #not sure if this is correct
                xli.append(x)
                yli.append(y)



plt.plot(xli,yli,'bo')
plt.show()      

print xli
print yli

提前谢谢你:)

1 个答案:

答案 0 :(得分:1)

仅仅为了坦白:我对Julia sets和matplotlib一无所知。

pyplot似乎是一个奇怪的选择,因为它的分辨率低,并且颜色不能被指定为X和X旁边的矢量。是的。如果它按照书面形式工作,'bo'只产生一个蓝色圆圈网格。

当您选择了您认为可行的while True:时,您的第一个c循环是不必要的。

这是我对您的代码的修改:

import matplotlib.pyplot as plt

c = complex(1.5, -0.6)

# image size
img_x = 100
img_y = 100

# drawing area
xi = -1.24
xf = 1.4
yi = -2.9
yf = 2.1

iterations = 8 # maximum iterations allowed (maps to 8 shades of gray)

# the loop for the julia set

results = {}  # pyplot speed optimization to plot all same gray at once

for y in range(img_y):
    zy = y * (yf - yi) / (img_y - 1)  + yi
    for x in range(img_x):
        zx = x * (xf - xi) / (img_x - 1)  + xi
        z = zx + zy * 1j
        for i in range(iterations):
            if abs(z) > 2:
                break
            z = z * z + c
        if i not in results:
            results[i] = [[], []]
        results[i][0].append(x)
        results[i][1].append(y)

for i, (xli, yli) in results.items():
    gray = 1.0 - i / iterations
    plt.plot(xli, yli, '.', color=(gray, gray, gray))

plt.show()      

<强>输出

enter image description here