Exercise Numpy/Scipy

1. Basics

  1. generate a 1-vector of size 10
  2. generate a zero-matrix of size 4x3
  3. generate a 3x3 identity matrix
  4. generate 100 evenly spaced values between 7 and 9
  5. Form the 2-D array (without typing it in explicitly):
    [[1, 6, 11],
    [2, 7, 12],
    [3, 8, 13],
    [4, 9, 14],
    [5, 10, 15]]

  6. contruct a vector of length 100 with alternating ones and zeros ( hint: use slice indexing )
  7. construct a 8x8 matrix with a checkboard pattern of zeros and ones
  8. compute the inverse, eigenvectors and eigenvalues of the following matrix: $$\mathbf{A} = \left[\begin{array} {rrr} 6 & 4 & 9 \\ 1 & 0 & 2 \\ 3 & 1 & 1 \end{array}\right] $$

In [2]:
import numpy as np

In [ ]:

2. Mandelbrot

The mandelbrot set is defined as the set of complex numbers $c$ for which the iteration $z_{n+1} = (z_n)^2 + c$, with $z_0=0$ does not diverge. In this exercise we want to compute the Mandelbrot set within a certain range.

  1. implement the function in_mandelbrot(real, comp) which computes whether a complex number (real = real part, comp=complex part) belongs to the mandelbrot set. Hint: the complex number 5+3i can be constructed in python as follows: c = 5 + 1j*3. Iterate N_max times and use the threshold thresh.
  2. generate a 2D array with 1000x1000 zeros called M
  3. compute for for all complex numbers with real part between -2 and 1 and with complex part between -1.5 and 1.5 whether they belong the Mandelbrot set and store the result in M.

In [1]:
N_max = 50 # number of iterations
thresh = 50 # threshold

def in_mandelbrot(real, comp):
    # computes whether a complex number (real = real part, comp=complex part) belongs to the mandelbrot set
    pass

In [ ]:
# plot the mandelbrot fractal
# assusmes M is a 2D array filled with boolean values
import matplotlib.pyplot as plt
plt.imshow(M)
plt.show()

In [ ]:


In [ ]: