5
\$\begingroup\$

I'm interested in making an animated movie of a zoom in on a part of the Mandelbrot set. My code works well for a few zooms, but upon trying to zoom in quite far, I find that the fractal becomes "smoothed out". Am I missing something that prevents me from seeing the fractal structure at higher zoom levels? Am I hitting machine precision in the computations? Here's what I'm talking about:

Zoom level 3:
zoom level 3
Zoom level 9:
zoom level 9
Zoom level 20:
zoom level 20

The first plot looks good, the second is okay, and the third is not fractal at all.

If there are any other deficiencies or improvements I'd be glad to hear about them as well.

Here's my code:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib import animation
import time

# some interesting places in the set
# http://www.nahee.com/Derbyshire/manguide.html

N = 500
nIts = 25
nZooms = 50
x0=0
y0=-1

movie = np.zeros([N,N,nZooms])

x = np.linspace(-2,1,N)
y = np.linspace(-1,1,N)
X,Y = np.meshgrid(x,y)
c = X + 1j*Y
z = 0*c
for i in range(nIts):
    z = z**2 + c

mask = np.abs(z) < 1
z[z>1]=0
z[np.isnan(z)]=0
movie[:,:,0] = mask


# plotting stuff
for j in range(1,nZooms):
    h=1./(2**j)
    print "Plot number ", j
    x = x0+h*np.linspace(-1,1,N)
    y = y0+h*np.linspace(-1,1,N)
    X,Y = np.meshgrid(x,y)
    c = X + 1j*Y
    z = 0*c
    for i in range(nIts):
        z = z**2 + c

    mask = np.abs(z) < 1
    z[z>1]=0
    z[np.isnan(z)]=0
    movie[:,:,j] = mask

fig = plt.figure()

for j in range(nZooms):
    name = "image%d.png" % j
    plt.imshow(movie[:,:,j], cmap = 'RdBu')
    plt.gray()
    plt.axis('equal')
    plt.axis('off')
    # plt.show()
    fig.savefig(name)
    time.sleep(1)
\$\endgroup\$
  • 1
    \$\begingroup\$ Are you seeking a general review to improve any and all aspects of your code, or just to address that specific concern? \$\endgroup\$ – Phrancis Feb 16 '17 at 23:43
  • 1
    \$\begingroup\$ @Phrancis If there are any other deficiencies, I'd be glad to hear about them. \$\endgroup\$ – Matt Feb 17 '17 at 0:45
  • 2
    \$\begingroup\$ Never seen code that uses x and X. Would vote seriously against doing that. \$\endgroup\$ – MKesper Feb 17 '17 at 9:55
  • 3
    \$\begingroup\$ Are you sure the numeric type you're using has enough precision for the zoom level you want? If not, that seems like a reasonable reason for it not being fractal at all. \$\endgroup\$ – Nic Hartley Mar 29 '17 at 23:18
  • 3
    \$\begingroup\$ In fact, scale the maximum iteration count linearly with the zoom depth. That should help. \$\endgroup\$ – Tamoghna Chowdhury Mar 30 '17 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.