Computational Laboratory 4CCP1300 Assessment
Module coordinator: George Booth ([email protected])
Assessment information
In this jupyter notebook, you will find the guidelines and questions to be completed for the assignment for the computational modeling course. The assignment is due by 26$^{th}$ April 2021 at 12:00 (noon) BST, and must be submitted via Turnitin on KEATS. This means that you must download this completed jupyter notebook from CoCalc, save it locally, and then submit it via KEATS. You can download your completed jupyter notebook from CoCalc once completed via the Files
tab, and then clicking on the 'cloud' download button on the right hand side. Remember that submitting at the last moment might put you at risk of unexpected events (failing internet connection or other problems), so plan your time, and aim to submit well BEFORE the deadline. A section on KEATS will be opened soon to allow for your online submissions there. Please note that the completed jupyter notebooks should be submitted directly (this file ending .ipynb), rather than a pdf or text document, so that the code can be run by the markers and checked for accuracy of the code. Submitted work will be checked for similarity of the submitted notebooks to other students - plagiarism will not be tolerated.
This document will provide a set of three problems that you will need to solve by using the knowledge built up from the notebooks and exercises completed during the course. You can revisit previous notebooks to refresh your knowledge from the course. The various parts of the problems should be submitted in separate code cells under each subsection of the questions. These cells should generate the results, and will build on the work of previous cells. Furthermore, text cells should be included with discussion and more information about each question where required. Note that the code itself (Python cells) will be marked, so please follow good coding practices outlined in the lectures (appropriate comments in the code, and clean, consistent and well structured code, with output appropriately formatted for readability). Note that all cells should execute properly without error messages given, such that when you go in the menu to 'Kernel'
then 'Restart & Run All'
, all the appropriate output from the code cells will be generated for marking each of the questions.
Problem 1: Molecular conformers
a) A molecular conformer is a particular shape or atomic arrangement that a molecule can adopt. Often molecules will flip between these two arrangements of their atoms. The aim in this problem is to model the changes in the numbers of molecules in different shapes (conformers) over a period of time. We assume that there are two conformers of a particular molecule which we want to model, conformer and conformer , and the total number of molecules is fixed, so that remains the same. We assume that every hour, 20% of the current population of conformer will turn into conformer . However, there is a chance of also transitioning in the other direction, and so each hour, 10% of the molecules in conformer turn back into conformer . We assume that initially, there are 150 ' molecules' and 150 ' molecules'. We also assume that the changes in populations take place in discrete time intervals on the hour, each hour.
In the first cell below (text / markdown), write an equation to determine the number of -molecules () and -molecules () after one hour, which should be and . Show that by writing this as a vector, this can be recast as a single matrix equation with a linear system of equations. This will involve a matrix of fixed probabilities acting on this vector of current populations of - and -molecules, to give the populations in the next hour. [3 marks]
b) Write a code cell below, to initialize a numpy array of the appropriate matrix of probabilities assigned to the variable P
, and the vector of current and populations, assigned to the variable pop
, which should be initialized to . Calculate the matrix multiplication of P
and the vector pop
(use the numpy
module to help), and therefore print out what the population of each type is after one hour (which should be and ). Finally, have the code test whether the matrix P
is a hermitian matrix or not, and print out a statement about this. [6 marks]
c) We are particularly interested in what happens after a long time - are there a steady number of - vs. -molecules, or does one conformer win out? If it is a 'steady state' (when the populations settle down to a constant over time), then what are these steady populations? Note that the probabilities remain the same each hour, so we can use the same probability matrix, P
, and use it to update the populations many times.
Set up a loop in the code cell below, to find out what the populations are 72 hours after the initial condition. Each iteration, also check that the total number of molecules () is conserved, and equal to the original number (300, to within numerical precision). Write out the final number of and molecules. Is this a stable number, or does it change after another day? [6 marks]
d) This long-time 'steady-state' behaviour can calculated directly, since we know that any vector where the application of a matrix results only in only a scaling of this vector (leaving the relative magnitudes of the elements of the vector unchanged) must be an eigenvector of the matrix. Specifically, if the populations are steady and not growing overall, the eigenvalue corresponding to this eigenvector must also be equal to 1. (Check the definition of an eigenvector to make sure you are happy with this logic).
In the code cell below, diagonalize the matrix P
, and find the eigenvector corresponding to the eigenvalue equal to 1. Note that this eigenvector is not normalized corresponding to the initial conditions we set (i.e. that the total population is 300 at every time). Find and print out the factor that the eigenvector should be multiplied by, in order to ensure that the long-time steady state populations of - and -molecules matches the values obtained in part c). This number should be printed out to 4 decimal places.
Note that you should use the numpy
or scipy
modules in order to do this diagonalization, though it may be worth checking that your code is doing what it should by comparing to a pen-and-paper diagonalization of the matrix. [6 marks]
Problem 2: The logistic map
In this problem we are considering the logistic map, a very simple sequence with very non-trivial behaviour. It simply updates a (scalar) variable, such that its next value depends solely on its current value. The sequence has the following form:
,
where is the current variable in the range , is the next value in the sequence, and is the growth rate which we will consider to be in the range . This is often used to model systems which grow under a constraint of a finite number of resources, where e.g. represents the fraction of the total possible population size at a particular time. Here, the population will grow according to , but be constrained by the term when gets too large. When , the population is zero, and when , the population is at its maximum possible size. Indeed, since the resources are limited, the expansion is also proportional to and if the civilization reaches the maximum population of it will have exhausted all its resources and then collapses to . Note that compared to the problem above, this sequence has non-linear (quadratic) terms in , which has a profound affect, especially in the long-time limiting behaviour. As a background to this problem, I strongly recommend that you watch the following video here.
a) Write a function called find_fix_point
in the code cell below, which takes as its arguments, the growth rate , and an initial value for . It should return the convergence point of the sequence. We assume in this problem that the sequence reaches a converged state after iterations, so that the convergence point can be approximated by the element of the sequence (). [6 marks]
b) We now want to consider how this 'fix point', , varies for different growth rates, , and different initial conditions, .
In the code cell below, iterate over uniformly spaced growth rates in the interval between a_init
and a_final
. This means that a=a_init
for the first cycle of your for
loop, and a = a_final
for the last cycle. For each value of , we also want to loop over possible values for in the range [0,1]. However rather than these taken to be uniformly spaced, these initial conditions should be chosen at random, using the numpy.random
module. Store all of the values for in an array (found using the function defined in part a), and make a scatter plot of against , for in the range to . Be sure to always label your axes! [9 marks]
c) Repeat the code of b) below, but now make a scatter plot of for equally-spaced values of in the interval [3,4], and a final plot for in the interval [3.5,3.8]. [2 mark]
d) Discuss the results obtained in a Markdown cell. For which values of do you obtain more than one fix point? Discuss the observed trend as is increased, and the consequence of the accumulation of fix points for the stability of a growing population. Why are the long-time trends of this sequence so different to the previous question. [2 marks]
For a>3 there are several fixed points. Between a=3 and a=3.5 there appears to be 2 fixed points and from a=3.5 to a=3.55 this turns into 4. When a>3.55 the pattern appears to chaotically spiral outwards with no visibly fixed trend pattern. However, this returns to a brief period of ordered chaos at around a=3.75 - a=3.8 where there appears to be 2 or 3 fixed points. After this it returns to unorderly chaos and leaves no pattern. The consequences for this is that as a>3 the population number is far less predictable and is far more unstable which varies over time. The long time trends are different due to the non-linear terms in x_n as well as the pervious question had an eventual convergence to a point point.
Problem 3: Projectile motion
One of the big challenges in mathematical physics is the solution to differential equations, such as Newtons equations. In this problem, we aim to simulate the motion of a projectile under the force of gravity. We are going to use the Euler method to integrate these equations in time to follow the trajectory of the projectile.
In order to do this, we note that we can decouple the (horizontal) and (vertical) motion. We therefore aim to end up with two arrays for the -coordinates and -coordinates for each time step that we consider (as well as another set of arrays for the components of the velocity). Finally, to solve differential equations, we will need to specify an initial condition, i.e. the initial and position for the projectile, as well as the initial velocity in each direction. Considering the propagation in the -direction (the direction is simple, as there is no acceleration and the velocity is therefore constant), we can write the expression for the propagation of the coupled -position and -velocity of the particle as
With these two equations, we can predict the position and velocity of the particle at the next time point, from knowledge of the position and velocity at the previous time point.
a) Initialise variables denoting the timestep for the integration, dt
, the acceleration due to gravity, g=9.8
ms, and the maximum time to integrate, t_max
. Calculate the number of timesteps required simulate this maximum time, and initialize numpy arrays to hold the - and - positions and velocities for each time step. It will also be worth initializing variables to store the initial speed, height and angle of the trajectory rather than hard coding them, so that these can be simply changed later on.
Initialize the projectile as starting at ground level, and fired at a speed of 100ms at an angle of 30$^{\circ}$ above ground level, choosing dt=0.01
seconds. Print out the maximum height above ground level that the projectile reaches, the maximum distance the projectile travels before it hits the ground, and the time that the projectile travels. [10 marks]
b) In the code cell below, copy the code from part a) and plot the trajectory as - against -position for the projectile until it reaches the ground. Include axes labels. [3 marks]
c) The total energy of the projectile at each point in time can be calculated as the sum of its kinetic and potential energy. Find the energy of the projectile at each point in time in another array, and create a labelled plot of it as a function of time.
In a text/markdown cell underneath, discuss what this plot is showing, and discuss what it should look like for the exact propagation of the projectile. Describe how the plot changes as the timestep, is changed, and why. What is the fundamental approximation made in the numerical solution we have implemented? [9 marks]