Mandelbrot set: Difference between revisions

1,146 bytes removed ,  3 years ago
→‎Normalized Iteration Count, Distance Estimation and Mercator Maps: The first three examples merged into one program.
(→‎{{header|Python}}: Chapters merged and headline changed)
(→‎Normalized Iteration Count, Distance Estimation and Mercator Maps: The first three examples merged into one program.)
Line 7,206:
 
===Normalized Iteration Count, Distance Estimation and Mercator Maps===
Actually the same, but without optimizations and therefore better suited for teaching. First the simple escape time algorithm using NumPy and complex matrices.
Second,The the''escape time'', ''normalized iteration countingcount'' algorithmand using''exterior distance estimation'' algorithms are used with NumPy and complex matrices (see Wikipedia: [https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set#Continuous_(smooth)_coloring ''Plotting algorithms for the Mandelbrot set: Continuous (smooth) coloring''] and [https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set#Exterior_distance_estimation ''Advanced plotting algorithms: Exterior distance estimation'']).
The Mandelbrot set is also printed using a scatter plot.
<lang python>import numpy as np
import matplotlib.pyplot as plt
Line 7,219 ⟶ 7,221:
C = 2.0 * (A + B * 1j) - 0.5
 
Z, dZ = np.zeros_like(C), np.zeros_like(C)
T, D = np.zeros(C.shape), np.zeros(C.shape)
 
for k in range(n):
M = abs(Z) < r
Z[M], dZ[M] = Z[M] ** 2 + C[M], 2 * Z[M] * dZ[M] + 1
T[M] = T[M] + 1
 
plt.imshow(T ** 0.1, cmap=plt.cm.twilight_shifted)
plt.savefig("Mandelbrot_setMandelbrot_escape_time.png", dpi=250)</lang>
Second, the normalized iteration counting algorithm using NumPy (see Wikipedia: [https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set#Continuous_(smooth)_coloring ''Plotting algorithms for the Mandelbrot set: Continuous (smooth) coloring'']).
<lang python>import numpy as np
import matplotlib.pyplot as plt
 
d, h = 800, 600 # pixel density (= image width) and image height
n, r = 200, 500 # number of iterations and escape radius (r > 2)
 
x = np.linspace(0, 2, d + 1)
y = np.linspace(0, 2 * h / d, h + 1)
 
A, B = np.meshgrid(x - 1, y - h / d)
C = 2.0 * (A + B * 1j) - 0.5
 
Z = np.zeros_like(C)
T = np.zeros(C.shape)
 
for k in range(n):
M = abs(Z) < r
Z[M], T[M] = Z[M] ** 2 + C[M], T[M] + 1
 
N = abs(Z) > r
Line 7,253 ⟶ 7,236:
 
plt.imshow(T ** 0.1, cmap=plt.cm.twilight_shifted)
plt.savefig("Mandelbrot_setMandelbrot_normalized_count.png", dpi=250)</lang>
It is possible to print the Mandelbrot set with a scatter plot. Exterior distance estimation is used, which provides a smooth transition of colors (see Wikipedia: [https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set#Exterior_distance_estimation ''Advanced plotting algorithms: Exterior distance estimation''] and Syntopia: [http://blog.hvidtfeldts.net/index.php/2011/09/distance-estimated-3d-fractals-v-the-mandelbulb-different-de-approximations/ ''Distance Estimated 3D Fractals (V): The Mandelbulb & Different DE Approximations'']).
<lang python>import numpy as np
import matplotlib.pyplot as plt
 
d, h = 80, 60 # pixel density (= image width) and image height
n, r = 20, 50 # number of iterations and escape radius (r > 2)
 
x = np.linspace(0, 2, d + 1)
y = np.linspace(0, 2 * h / d, h + 1)
 
A, B = np.meshgrid(x - 1, y - h / d)
C = 2.0 * (A + B * 1j) - 0.5
 
Z, dZ = np.zeros_like(C), np.zeros_like(C)
D = np.zeros(C.shape)
 
for k in range(n):
M = abs(Z) < r
Z[M], dZ[M] = Z[M] ** 2 + C[M], 2 * Z[M] * dZ[M] + 1
 
N = abs(Z) > 2
Line 7,278 ⟶ 7,242:
 
plt.imshow(D ** 0.1, cmap=plt.cm.twilight_shifted)
plt.savefig("Mandelbrot_setMandelbrot_distance_estimation.png", dpi=250)
 
X, Y = C.real, C.imag
Line 7,285 ⟶ 7,249:
fig, ax = plt.subplots(figsize=(8, 6))
ax.scatter(X, Y, s=S**2, c=D**0.1, cmap=plt.cm.twilight_shifted)
plt.savefig("Mandelbrot_plotMandelbrot_scatter_plot.png", dpi=250)</lang>
A small change in the above code allows Mercator zoomsmaps of the Mandelbrot set (see David Madore: [http://www.madore.org/~david/math/mandelbrot.html ''Mandelbrot set images and videos''] and Anders Sandberg: [https://www.flickr.com/photos/arenamontanus/sets/72157615740829949 ''Mercator Mandelbrot Maps'']).
The maximum magnification is exp(2*pi*h/d) = exp(2*pi*5.5) = 535.5^5.5 = 10^15, which is also the maximum for 64-bit arithmetic.
Note that Anders Sandberg uses a different scaling.
Line 7,314 ⟶ 7,278:
 
plt.imshow(D.T ** 0.1, cmap=plt.cm.nipy_spectral)
plt.savefig("Mercator_mapMandelbrot_Mercator_map.png", dpi=250)
 
X, Y = C.real, C.imag
Line 7,324 ⟶ 7,288:
ax[1, 0].scatter(X[200:500], Y[200:500], s=S[0:300]**2, c=D[200:500]**0.3, cmap=plt.cm.nipy_spectral)
ax[1, 1].scatter(X[300:600], Y[300:600], s=S[0:300]**2, c=D[300:600]**0.2, cmap=plt.cm.nipy_spectral)
plt.savefig("Mercator_zoomMandelbrot_Mercator_zoom.png", dpi=250)</lang>
 
=={{header|R}}==
305

edits