Lab Sheet 11¶
In [1]:
from header import *
Exercise 1.1
In [8]:
def mandelbrot_test(c, maxiter=100):
"""
Test if a point c is in the Mandelbrot set. Return 1 if it is, 0 otherwise.
The function maps efficiently over numpy arrays.
"""
z = 0 * c # array of zeros of the same shape as c
m = z + 1 # array of ones of the same shape as c
for k in range(maxiter):
# Throughout this loop, m is an array of 1s and 0s and z is an array of complex numbers
# An entry in m is 1 if the iteration starting with the corresponding c has
# not yet escaped from the circle of radius 2, and 0 otherwise.
# The corresponding entry in z is the current value of the iteration if m = 1,
# and 10 if m = 0.
z = m * (z**2 + c) + 10 * (1 - m)
m = (abs(z) <= 2)*1
return m
n = 1000
xs = np.linspace(-2.0, 0.4, n+1) # real parts
ys = np.linspace(-1.2, 1.2, n+1) # imaginary parts
cs = xs.reshape(1, n+1) + 1j*ys.reshape(n+1, 1) # broadcast sum gives a matrix of complex numbers
ms = mandelbrot_test(cs, 100)
In [19]:
img = np.expand_dims(ms, axis=2) * np.array([-255, -180, -255], dtype=int) + np.array([255, 255, 255], dtype=int)
fig = go.Figure(data=[go.Image(x0=xs.min(), y0=ys.min(), dx=xs[1]-xs[0], dy=ys[1]-ys[0], z=img)])
ts = np.linspace(0, 2*np.pi, 200)
fig.add_trace(go.Scatter(x=np.cos(ts)/2-np.cos(2*ts)/4, y=np.sin(ts)/2-np.sin(2*ts)/4, mode='lines', line=dict(color='red')))
fig.add_trace(go.Scatter(x=np.cos(ts)/4-1, y=np.sin(ts)/4, mode='lines', line=dict(color='red')))
fig.add_trace(go.Scatter(x=-0.1225+0.0945*np.cos(ts), y=0.7449+0.0945*np.sin(ts), mode='lines', line=dict(color='red')))
fig.add_trace(go.Scatter(x=-0.1225+0.0945*np.cos(ts), y=-0.7449+0.0945*np.sin(ts), mode='lines', line=dict(color='red')))
fig.update_layout(width=600, height=600, showlegend=False, xaxis_visible=False, yaxis_visible=False)
fig.show()