Exercises

Exercise 7.1: Using arrays

a) Import the NumPy-library by making use of the command

from numpy import *

and use the function add in this library to add the numbers 1 and 2. Print the result to screen, and make sure that the output is 3 as expected. In the same program, create a function which you call “add”. This function should return the sum of two given numbers, plus one. In other words, the function should return the number 4 instead of 3 when you send in the values 1 and 2. If you now add the numbers 1 and 2 by making use of the NumPy-function add, do you get the same result as before?

Import instead the NumPy-library by making use of the line

import numpy as np

Again, use the NumPy-function add to compute the sum of the numbers 1 and 2. Do you get the correct answer now?

b) Create a list of the following elements: 1, 2, 3, a. Add the first element of the list to itself, and print the number to screen. Now use the NumPy-function array to translate the list into an array, and add the first element of the array to itself. Do you get the same result as for the list? What happens if you instead create an array of elements 1, 2, 3, 4 and add the first element to itself?

c) One of the main advantages of using NumPy’s arrays instead of Pyhton’s lists is that operations on arrays, especially large ones, require less computer time. To test this, create a list of 10000 elements. Start a timer, and add the number 3 to all elements in the list. Stop the timer and print the result to screen. Now create an array of the same 10000 elements, start a timer and add the number 3 to all elements in the array. End the timer, and print the result to screen. Do you observe any difference in the time that each process takes? (NB: If you solve this exercise in Jupyter Notebook, make sure to restart the kernel before performing the experiment)

Hint: To take the time of the different processes, you may make use of the function process_time from the Python-library time. The following pseudo-code shows how it can be implemented:

from time import process_time

...create list/array...

start_time = process_time()

...perform operation on list/array...

end_time = process_time()

simulation_time = end_time - start_time
print(simulation_time)

d) In this exercise you will use arrays to decrypt the message hidden in the following set of numbers: 8, 5, 12, 12, 15, 0, 23, 15, 18, 12, 4. Each number represents a letter in the English alphabet. To perform this decryption, create therefore a two-dimensional array of letters and numbers by making use of the NumPy-function zeros. Arrange the array so that the first row consists of the number 0 and an empty space, the second row of the number 1 and the letter a, the third row of the number 2 and the letter b, and so on.

Hint: Instead of writing all the English letters manually, you can create a string of the English alphabet by writing the following code snippet

import string
alphabet_string = string.ascii_lowercase

Also, in order to use the zero-function to create an array of both string characters and integers, you must use the option dtype=object.

Exercise 7.2: Restricted Growth of a Population (Return to this exercise after the last lecture)

a) Use the principles of natural selection to model the growth of a population that takes a maximal number of individuals. Let there be \(n = 5\) people in the population initially. Set the maximum number of people in the population to \(P=100\) and let the initial growth rate be \(\alpha = 0.2\). Simulate over a time period of \(T = 50\) years. Plot the number of people as a function of time. Compare your results to the models introduced in the lectures. How does the growth rate relate to the infection rate?

Hint: If \(n(t)\) is the number of people in the population at a given time \(t\), \(\alpha\) is the initial growth rate and \(P\) is the maximal number of people in the population, then the number of people at the next time step, \(t + \Delta t\), is given by \[ n(t + \Delta t) = n(t) + \alpha n(t)\left(1 - \frac{n(t)}{P}\right)\Delta t. \]

b) Further test your code for \(\alpha = 1\) and \(\alpha = -0.2\). Explain your observations.

Exercise 7.3: Spread of disease (Return to this exercise after the last lecture)

Use the code from the lecture notes modelling the spread of disease, and add a function \(R(t)\) that governs the number of people who have recovered from the disease. The total number of people in the population should therefore be \(P = S + I + R\), and the number of recovered individuals at the beginning of the infection is \(R_0 = 0\). Let the rate of recovered people per day be \(\nu = 0.05\) and simulate the spread of the disease over a period of \(T=50\) days. Assume that every individual that has recovered from the disease is immune to new infections. Plot the number of infected, susceptible and recovered individuals as a function of time. How does the result differ from the scenario of only infected and susceptible individuals?