This homework is on writing Python scripts using NumPy, SciPy, and Matplotlib.
Note: You must write the code on your own. Do not copy the code from any source. To get full credit,
your script. must be well documented.
1 Mandelbrot Set
(50 points)
Write a Python script. mandelbrot.py to compute the Mandelbrot fractal, with the Mandelbrot iteration:
N_max = 50
some_threshold = 50
c = x + 1j y
z = 0
for j in range(N_max):
z = z 2 + c
Point (x;y) belongs to the Mandelbrot set ifjzj1. Construct a grid of c = x+1j y values in range [ 2;1] [ 1:5;1:5].
2. Do the iteration
3. Form. the 2-d boolean mask indicating which points are in the set
4. Save the result to an image with:
import matplotlib . pyplot as plt
plt .imshow(mask.T, extent=[ 2, 1, 1.5, 1.5])
plt . gray ()
plt . savefig ( ’ mandelbrot .png ’)
1
2 Markov Chain
(50 points)
Markov chain transition matrix P, and probability distribution on the states p:
1. 0 P[i;j] 1: probability to go from state i to state j
2. Transition rule: pnew = PTpold
3. normalization: all(sum(P;axis = 1) == 1), p:sum() == 1
Write a Python script. named markov_chain.py that works with 5 states, and:
Constructs a random matrix, and normalizes each row so that it is a transition matrix.
Starts from a random (normalized) probability distribution p and takes 50 steps to obtain p_50
Computes the stationary distribution: the eigenvector of P.T with eigenvalue 1 (numerically the eigen-
value closest to 1) to obtain p_stationary (remember to normalize the eigenvector).
Check if p_50 and p_stationary are equal to tolerance 1e-5.
The following functions may be useful: np.random.rand, .dot(), np.linalg.eig, etc.