简体   繁体   中英

How to declare a variable in python without assigning a value?

I'm trying to create graphs of the Mandelbrot set.

I have managed to do this by iterating over a lot of points but this takes a lot of processing power, so I'm now trying to generate a polynomial by iterating f(z) = z**2 + c many times and then finding the roots for z = c , in order to generate a boundary of the set.

However I can't seem to get Python to generate the polynomial, any help would be much apprecaited.

Edit:Implemented azro's fix but now I get the error - TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'

Code so far:

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)

This is a very hard problem. Searching through literature, I only found this (which doesn't seem very reputable). However, it does seem to begin to create what you want. This is only up to 8 iterations. So the polynomial gets very complicated very fast. See the following code:

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, '.')

某事也许

I would suggest something more like the following (stolen and modified from here ). This sounds like something you've already tried. But try changing the max iterations to get something that can run relatively fast (30 was fast and had relatively high resolution for me).

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')

输出图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM