2.4. Exercises#

Exercise 2.14

Use a for loop to produce the following output:

  1. 5, 7, 9, 11, 13, 15

  2. 1, 10, 100, 1000, 10000, 100000

  3. 0, 1, 2, 0, 1, 2, 0, 1, 2 (hint: use the % operator)

Exercise 2.15

Look up the predicted daily maximum and minimum temperatures for the next 10 days according to the BBC weather forecast.

https://www.bbc.co.uk/weather/2643743

Create two arrays containing the maximum and minimum temperatures and plot them as two separate lines on a line graph, including axis labels and title. Look up how to add a legend in the Matplotlib documentation.

../_images/temperatures.png

Exercise 2.16

The Belousov–Zhabotinsky reaction, or BZ reaction, is a chemical reaction which exhibits non-equilbrium dynamics. Under certain conditions, the BZ reaction results in oscillatory behaviour, as demonstrated in this video.

The BZ reaction can be modelled by the following equations, where \(X_i\) and \(Y_i\) are the concentrations of the two reactants X (red) and Y (colourless) at timestep \(i\).

\[\begin{split}\begin{align} X_{i+1} &= X_i + k_1-k_2X_i + k_3X_i^2Y_i\\ Y_{i+1} &= Y_i + k_4X_i - k_3X_i^2Y_i \end{align}\end{split}\]

where \(k_1=0.2\), \(k_2 = 0.4\), \(k_3 = 0.1\) and \(k_4 = 0.3\). The initial concentrations are zero and each timestep has a duration of one second.

  1. Write code to calculate \(X_i\) and \(Y_i\) for the first \(300\) seconds. (Hint: you can use your answer to Exercise 2.8 as a template).

  2. On on set of axes, plot a graph of \(X\) and \(Y\) against time, an in another on another plot \(Y\) against \(X\). Check that your plots look like the examples below. Notice how both \(X\) and \(Y\) reach a steady-state.

  3. Keeping the other parameters fixed, experiment with different values of \(k_1\) between \(0.1\) and \(0.3\). For what range of values of \(k_1\) does the reaction reach a steady-state, and for which values dos it exhibit sustained oscillations?

../_images/27c6321bcb021a0afad4ad9c2ba99c7ad146babb11e4a024815e96151cac1225.png ../_images/8cab4cca2fc32e42a14190fcf275ccd4f93d0292b5d46577db7f437889276701.png

Exercise 2.17

Adapt your solution to the cannonball problem to incorporate air resistance. Assume that at each time step the x- and y-velocities each reduce by a fixed fraction \(d\) (so that the velocity equations become \(u_{i+1} = u_i - du_i\) and \(v_{i+1} = v_i - g\Delta t - dv_i\)).

For a given \(d\), what value of \(\theta\) maximises the distance travelled by the cannonball?