如何在python中声明变量而不分配值?

时间:2020-06-01 17:44:40

标签: python iteration fractals mandelbrot

我正在尝试创建Mandelbrot集的图形。

我设法通过迭代很多点来做到这一点,但这需要大量的处理能力,因此我现在尝试通过多次迭代f(z) = z**2 + c来生成多项式,然后为z = c找到根,以便生成集合的边界。

但是我似乎无法让Python生成多项式,所以非常感谢任何帮助。

编辑:实施了azro的修复程序,但现在出现错误-TypeError:**或pow()不支持的操作数类型:'NoneType'和'int'

到目前为止的代码:

import numpy as np

c = None

def f(z):
    return z**2 + c

eqn = c

for i in range(100):
    eqn = f(eqn)

np.roots(eqn)

1 个答案:

答案 0 :(得分:0)

这是一个非常困难的问题。搜索文献时,我仅发现this(看起来不太有名)。但是,它似乎确实开始创建您想要的东西。最多只能进行8次迭代。因此,多项式很快变得非常复杂。请参见以下代码:

import matplotlib.pyplot as plt

coeff = [0, 1, 1, 2, 5, 14, 26, 44, 69, 94, 114, 116, 94, 60, 28, 8, 1]
coeff = [0, 1, 1, 2, 5, 14, 42, 132, 429, 1302, 3774, 10652, 29538, 80812, 218324, 582408, 1534301, 3993030, 10269590, 26108844, 65626918, 163107044, 400844588, 974083128, 2340595778, 5560968284, 13062923500, 30336029592, 69640352964, 158015533208, 354347339496, 785248461712, 1719477330477, 3720187393990, 7952125694214, 16792863663700, 35031835376454, 72188854953372, 146932182777116, 295372837865192, 586400982013486, 1149605839249820, 2225301467579844, 4252710138415640, 8022825031835276, 14938862548001560, 27452211062573400, 49778848242964944, 89054473147697354, 157160523515654628, 273551721580800380, 469540646039042536, 794643418760272876, 1325752376790240280, 2180053774442766712, 3532711259225506384, 5640327912922026260, 8870996681171366696, 13741246529612440920, 20959276151880728336, 31472438318100876584, 46514944583399578896, 67649247253332557392, 96791719611591962592, 136210493669590627493, 188481251186354006062, 256386228250001079082, 342743629811082484420, 450159936955994386738, 580706779030058464252, 735537050036491961156, 914470757914434625800, 1115597581733327913554, 1334957092752100409132, 1566365198635995978988, 1801452751402955781592, 2029966595320794439668, 2240353897304462193848, 2420609646335251593480, 2559320275988283588176, 2646791812246207696810, 2676118542978972739644, 2644036970936308845148, 2551425591643957182856, 2403354418943890067404, 2208653487832260558008, 1979045408073272278264, 1727958521630464742736, 1469189341596552030212, 1215604411161527170376, 978057923319151340728, 764655844340519788496, 580430565842543266504, 427417353874088245520, 305060580205223726864, 210835921361505594848, 140960183546144741182, 91071943593142473900, 56796799826096529620, 34150590308701283528, 19772322481956974532, 11008161481780603512, 5884917700519129288, 3016191418506637264, 1479594496462756340, 693434955498545848, 309881648709683160, 131760770157606224, 53181959591958024, 20324543852025936, 7333879739219600, 2490875091238112, 793548088258508, 236221241425176, 65418624260840, 16771945556496, 3958458557608, 854515874096, 167453394320, 29524775520, 4634116312, 639097008, 76185104, 7685024, 637360, 41696, 2016, 64, 1]

r = np.roots(coeff)

plt.plot(r.real, r.imag, '.')

something maybe

我建议使用类似以下内容(从here窃取并修改)。听起来您已经尝试过了。但是,请尝试更改最大迭代次数,以使某些程序可以相对快速地运行(30个速度快并且对我而言具有相对较高的分辨率)。

MAX_ITER = 30

def mandelbrot(c):
    z = 0
    n = 0
    while abs(z) <= 2 and n < MAX_ITER:
        z = z*z + c
        n += 1
    return n
# Image size (pixels)
WIDTH = 600
HEIGHT = 400

# Plot window
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1

img = np.zeros((WIDTH, HEIGHT))
for x in range(0, WIDTH):
    for y in range(0, HEIGHT):
        # Convert pixel coordinate to complex number
        c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
                    IM_START + (y / HEIGHT) * (IM_END - IM_START))
        # Compute the number of iterations
        m = mandelbrot(c)

        if m > MAX_ITER-1:
          img[x, y] = 1

plt.imshow(img.T, cmap='bone')

output picture