Showing posts with label Laugh. Show all posts
Showing posts with label Laugh. Show all posts

Saturday, 19 March 2016

Diffusion of the dead - Epilogue. Part 2, The results.

Last time, I presented a question that I had been given by PrettyGreen, a PR company that asked me to get involved with their Halloween publicity campaign for the mobile network operator GiffGaff. We also saw how I modelled the problem and this week I present the results and also the Matlab codes that I used, should you want to recreate, or play around, with the solutions.

The advert presenting the characters involved can be found below.

________________________________________________________________
________________________________________

Results
The survival time of each character in ascending order can be found in the table below. This is further complemented by Figure 1, in which we illustrate the health of each combatant over a 60 minute time span.

The first two characters to die are the Dead Girl and Skin Face. Although these two characters are relatively strong their low initial healths mean that the other combatants are able to dispatch them with relative ease. Thus, neither of them last longer than a minute demonstrating that neither of them are able to use their aggression to its fullest. Similarly, Baby Man is next to die, because although he is able to defend himself better, lasting just over a minute, the combatants once again go after the weakest.

Name Survival time
Dead Girl 3 secs
Skin Face 13 secs
Baby Man 1 mins 44 secs
Pumpkin Head 7 mins 9 secs
Tall Guy 10 mins 7 secs
Hero Girl 55 mins 37 secs
Evil Clown Survivor
The next two to die are quite interesting, these are Pumpkin Head and Tall Guy, lasting approximately 7 and 10 minutes respectively. Up until this point the group has simply been killing the current weakest. At the five minute marker this would be Tall Guy, however, as seen in Figure 1, he is able to outlast Pumpkin Head because Tall guys higher agility allows him to defend better.
Eventually, only Evil Clown and Hero Girl are the surviving combatants. This is interesting as it is often the plot of a horror film to have the heroine survive up until the last few moments, when the final kill is unleashed during extended scenes of heightening tensions. This is exactly what is seen in Figure 1. Initially, things are looking good for Hero Girl as she begins the final battle with much more health than Evil Clown. Furthermore, the agility of the Hero Girl and Evil Clown are equal and the highest out of the group. This has kept them safe up until this point because they were able to run away from danger. Similarly, for the 30 minutes after Tall Guy’s death the two characters are able to capably defend themselves and escape each others attacks.

These tropes are used in many horror films: although the heroine is no physical match for the villain, she is able to make up for the lack of strength in terms of speed. It is also a standard horror trend that it should look like the heroine will survive, but then she is subjected to escalating bouts of terror. Although she is able to put up a valiant fight Hero Girl’s life is slowly drained by Evil Clown’s higher strength, ultimately leading to his victory after 55 minutes and with 45 health remaining.

Who'd have thought that the Evil Clown was so dangerous?! And who could have foreseen where such a simple piece of mathematics, based on the modelling of zombies, would take us?

This brings us to the end of this current set of posts. Next time: something completely different.

________________________________________________________________
________________________________________
function Battle_Royale
%Clear computer memory and plots.
clear
close all
clc

options = odeset('RelTol',1e-4,'AbsTol',1e-5); %Options set for the ODE solver.

S=[9  7  6  8  3  8  9]; %Vector of strength values.
A=[7  8  4  3  8  6  3]; %Vector of agility values.
H=[45 65 60 40 90 10 20];%Vector of initial health.
C=S.*A; %The interaction function is scaled by the strength multiplied by the agility.

[T,Y] = ode45(@(t,y) ODES(t,y,C),[0 7000],H,options); % Solve the ODE.

cc=lines(7); % Set the legend colours.
for i=1:7
    %Find the last moment alive.
    YY=Y(:,i);
    YY(YY<0.1)=0;
    index=find(YY,1,'last');
    YY(YY==0)=nan;
    YY(index)=0;

    plot(T/60,YY,'linewidth',3,'color',cc(i,:)); %Plot results.
    t(i)=T(index)/60;
    hold on
end
t %Print out the times of death.
axis([0 60 0 90]) %Set axes.

%Make the plot look nice.
legend('Tall guy','Evil Clown','Pumpkin Head','Baby Man','Hero Girl','Dead Girl','Skin Face','location','northoutside','orientation','horizontal')
xlabel('Time in minutes')
ylabel('Combatant Health')

function dy = ODES(t,y,C)
Terms=y.*C'; %The value of the each combatant's attack.
dy=zeros(7,1); %Preallocation of the variables.
for i=1:7
    indices=[1:i-1,i+1:7]; %Indices to be summed over.
    dy(i)=-heaviside(y(i))/((1+y(i)^2)*2*C(i))*sum(Terms(indices)); %Differential equation.
end

Saturday, 5 March 2016

Diffusion of the dead - Epilogue. Part 1, Horror Battle Royale.


Having written and published the article on zombie invasion I thought the story would end there. Of course the media picked it up and I had my 15 minutes of fame on the radio and TV. However, it appears I was very wrong. A PR company, PrettyGreen, contacted me about getting involved with their Halloween publicity campaign for GiffGaff, a mobile network operator. 

Their question was: suppose seven stereotypical characters from horror movies are released in an arena leading to a battle royale, who would survive? The characters are:
  • Tall Guy – A very tall (6,9 ft) escaped convict;
  • Evil Clown – An evil, twisted clown;
  • Pumpkin Head – A man with a pumpkin as a head;
  • Baby Man – A somewhat mental man who has spent is life trapped in a basement;
  • Hero Girl – A normal young girl;
  • Dead Girl – A dead bride;
  • Skin Face – A man with excess skin stapled over his face.
My first thought was how was I going to do this? As mentioned when we first started this set of posts there are many different ways mathematics could answer this question. I decided to use differential equations to model the health of each character. As the characters interact their health would reduce depending on certain factors. Thus, I asked PrettyGreen to provide me with a strength, agility and initial health for each combatant and I used these to simulate who will be the last person standing. They were even kind enough to send me a picture to use with this post.
Figure 1. Hero Girl, Evil Clown, Dead Girl, Tall Guy and Pumpkin Head all having a nice jog in London. Image courtesy of PrettyGreen.
This post is a little more mathematical then normal as I present all the gory equation details. However, do not let this scare you. Hopefully, I provide all the intuitive details you should need in the text. If you just want the brief that appeared in the news, then please look here.
________________________________________________________________
________________________________________
Method
The battle simulation is based on the reduction of each combatant’s initial health at a rate proportional to their meeting with an opponent. Once a combatant’s health has dropped to zero, they are immediately removed from the conflict, whilst the survivors fight on.

The interaction outcome is based on a scaled form of the empirical rule known as the “Law of Mass Action”. Explicitly, the rate of reduction in health is proportional to the attacker’s health, scaled by an inverse square of the defender’s health. Intuitively, this simply means that a reduction in health can only occur when the attacker and defender meet. Moreover as the attacker gets weaker, their attacks do less damage. Equally, as the defender becomes weaker their defence is less effective. The precise form of the interaction term is,
\begin{equation}
\frac{H_a}{1+H_d^2},
\end{equation}
where $H_a$ is the health of the attacker and $H_d$ is the health of the defender. It should be understood that this interaction term is known as a "constitutive equation". This means that it is not based on any fundamental law, but it is postulated because it produces the right kind of dynamics that we would expect. There are many other functions I could have used in its place, however, in using this equation, I am hoping that it is simple enough to capture the general outcomes of the system and, thus, the results will be robust to any small changes that might occur.

From these assumptions we can construct a coupled set of ordinary differential equations that will evolve the battle and predict who will win. The exact form of the equation for combatant $i=1,2,…,7$ is
\begin{equation}
\tau\frac{dH_i}{dt}=\frac{-1}{1+H_i^2}\frac{1}{S_iA_i}\sum_{j\neq i}S_jA_jH_j.
\end{equation}
Note that the equation is only active while $H_i>0$. Also notice that we have scaled the terms with the strength parameter, $S_i$, and agility parameter, $A_i$. Thus, speed is, potentially, just as important as strength. Parameters can be found in the Table 1, below. Finally, we define the initial condition as:
\begin{equation}
H_i{0}=H_{i0}.
\end{equation}

Name and variable Strength, $S_i$ Agility, $A_i$ Initial health, $H_{i0}$
Tall Guy, $H_1$ 9 7 45
Evil Clown, $H_2$ 7 8 65
Pumpkin Head, $H_3$ 6 4 60
Baby Man, $H_4$ 8 3 40
Hero Girl, $H_5$ 3 8 90
Dead Girl, $H_6$ 8 6 10
Skin Face, $H_7$ 9 3 20
The parameters were chosen such that strength and agility were on a scale from 1 to 10, such that higher numbers represent higher strengths and speeds, respectively. The health parameter is on a scale of 0 to 100, with the relative size determining how healthy each character is. It should be noted that in the original data Dead Girl’s health was 0. Understandably, this would make her “dead”, however, it would also suggest that she was unkillable, resulting in her inevitable win. To ensure such a case does not occur I changed her initial health to 10.

Next time I will present the results of the above simulations and demonstrate that although this is only a simple model it produces an outcome that you would expect to see in any good (or bad) horror film. In the mean time, have a go at simulating the system yourself and perhaps vary the interaction rule to see how the results are influenced by this equation.

Saturday, 20 February 2016

Diffusion of the dead - The maths of zombie invasions. Part 9, The complete strategy.


Over the past 8 posts we have investigated the mathematical implications of a zombie invasion. If you really can't be bothered to read all of the details, please see part 1, where you'll find some links to presentations that I have done and I'll give you all the details inside of one hour!

For the more patient reader, we began in parts 2, 3 and 4 where we discussed the philosophy of modelling, the assumptions that would make up the zombie invasion model and some basic mathematical simulation techniques. Although important for the recreational mathematician if you're really in a pinch you'll want to skip right to the results.

In particular, in parts 5 and 6 we showed that running away from zombies increases the initial interaction time far more than trying to slow the zombies down. Thus, fleeing for your life should be the first action of any human wishing to survive. However, we cannot run forever; interaction with zombies is inevitable.

In part 8 we showed that the best long-term strategy is to create a fortified society that can sustain the population, whilst allowing us to remove the zombies as they approach. This, in effect, reduces the risk to zero and we will survive.

In the event of the apocalypse, it is unlikely that we would be able to support such a commune without raiding parties scavenging for medicine, food and fuel. Thus, in this case, we fall back on the maxim of being more deadly than the zombies which allows us to survive as shown in part 7.

Over all, it is difficult for us to survive simply because zombies do not have a natural death rate, and by biting us, they are able to increase their own ranks whilst reducing ours. Thus reinforcing our greatest fear that human allies could quickly become our biggest nightmares.

Our conclusion is grim, not because we want it to be so but because it is so. It had always been the authors' intention to try and save the human race. So to you, the reader, who may be the last survivor of the human race, we say: run. Run as far away as you can get; an island would be a great choice. Only take the chance to fight if you are sure you can win and seek out survivors who will help you stay alive.
Good luck.
You are going to need it.

Saturday, 6 February 2016

Diffusion of the dead - The maths of zombie invasions. Part 8, Surfing the infection wave.


Last time we considered the interaction rules
  1. humans kill zombies;
  2. zombies kill humans; and
  3. zombies can transform humans into zombie,
and produced the following system of equations,
\begin{align}\frac{\partial H}{\partial t}&=D_H\frac{\partial^2 H}{\partial x^2}-\alpha HZ\label{Human_PDE}\\\frac{\partial Z}{\partial t}&=D_Z\frac{\partial^2 H}{\partial x^2}+\beta HZ\label{Zombie_PDE},\end{align}
which predicts the evolution of the human population, $H$, and the zombie population, $Z$.

To see how quickly the infection moves through the human population, we look for a particular type of solution, known as a "Fisher wave". This type of wave travels at a specific speed and does not change its shape as its travels. Such a solution can be seen below.
 Movie: The black line represents the human population. The red dashed line represents the zombie population. Initially, there are only a small number of zombies, but over time the infected population spreads out and transforms the susceptible population.

By manipulating the equations we can show that the wave speed, $v$, has a value of
\begin{equation}
v^2=4D_Z\beta H_0,
\end{equation}
where $D_Z$ is diffusion rate of the zombies, $\beta$ is the net-rate of zombification (a.k.a how quickly the zombie population grows) and $H_0$ is the initial human population. Critically, note that the left-hand side of the equation is positive, because it is a squared value.

In order to slow the infection, we should try to reduce the right-hand side of the above equation.
  • Reducing $D_Z$ amounts to slowing the zombies down; thus, an effective fortification should have plenty of obstructions that a human could navigate but a decaying zombie would find challenging.
  • Reducing $\beta$ occurs through killing zombies quicker than they can infect humans. If we are really effective in our zombie destroying ways then we can make $\beta$ negative. This would make the right-hand side of the equation negative, but the left-hand side of the equation is positive. Since we get a contradiction no wave can exist and the infection wave is stopped.
  • Reducing $H_0$ involves reducing the human population. This could mean geographically isolating the population on an island because if the zombies are unable to get to you then they can't infect you.
The last tactic of reducing the human populations could also lead to a rather controversial tactic. Suppose you don't live near a deserted fortified island. Rather, you work in an office surrounded by people who would not be able to protect themselves from the oncoming hordes. Then you may consider removing the humans around you in a more drastic and permanent way. This is because everyone that surrounds you is simply a potential infection!

I do not recommend this cause of action, as reducing the human population also reduces the number of people able to fight the zombies and humans sacrificing other humans would only speed the extinction of their own species. The human population will have enough trouble trying to survive the hordes of undead, without worrying about an attack from their own kind!

This, pretty much, brings us to the end of the mathematical zombie story. It's been a long journey, thus, to ensure that you haven't missed any of the critical strategies along the way the next post simply focuses on summarising and concluding what we're been discussing since Halloween. See you then.

Saturday, 23 January 2016

Diffusion of the dead - The maths of zombie invasions. Part 7, Face to face with a zombie.

So far we have only considered zombie motion. It has been an incredibly simply model, but it has been able to furnish us with a wealth of information. In particular, we have been able to predict how long it will take the zombies to get to us and we have shown that the best strategy is to run away.

Unfortunately, you can only run so far. At some point you are no longer running away from the living dead, but actually running towards a different mob of zombies. So what should you do when you finally end up having to go hand to hand with a zombie?

To model human-zombie interactions, we suppose that a meeting between the
two populations can have three possible outcomes. Either
  1. the human kills the zombie;
  2. the zombie kills the human; or
  3. the zombie infects the human and so the human becomes a zombie.
These outcomes are illustrated in Figure 1.
    Allowing $H$ to stand for the human population and $Z$ to stand for the zombie population, these three rules can be written as though they were chemical reactions:
    \begin{align}
    H+Z&\stackrel{a}{\rightarrow}H \text{ (human kills zombie)}\\
    H+Z&\stackrel{b}{\rightarrow}Z \text{ (zombie kills human)}\\
    H+Z&\stackrel{c}{\rightarrow}Z+Z \text{ (human becomes zombie).}
    \end{align}
    The letters above the arrows indicate the rate at which the transformation
    happens and are always positive. If one of the rates is much larger than the
    other two, then this "reaction" would most likely happen.
    Figure 1. The possible outcomes of a human-zombie interaction. Either (a)
    humans kill zombies, (b) zombies kill humans, or (c) zombies convert humans.
    To transform these reactions into a mathematical equation, we use the "Law of Mass Action". This law states that the rate of reaction is proportional to the product of the active populations. Simply put, this means that the above reactions are more likely to occur if we increase the number of humans and/or zombies. Thus, we can produce the following equations which govern the population dynamics
    \begin{align}\frac{\partial H}{\partial t}&=D_H\frac{\partial^2 H}{\partial x^2}-\alpha HZ\\\frac{\partial Z}{\partial t}&=D_Z\frac{\partial^2 H}{\partial x^2}+\beta HZ.\end{align}
    where $b+c=\alpha$ is the net death rate of humans and $c-a=\beta$ is the net creation rate of zombies.

    If we ignore the reactions for a second, we have seen the first part of the equations before. Explicitly we are assuming that both the zombies and humans randomly diffuse throughout their domain. Now we have previously justified the zombies' diffusive motion as they are mindless monsters. However, humans are not usually known for their random movement. Here, we use the fact that if the dead should start to rise from their graves, then panic would set in and humans would start to run away and spread out randomly from location of high population density. Thus, human movement could also be described by diffusion, although their diffusion rate is likely to be much larger than the zombies'.

    If we now include the interaction formulation once again then the equations immediately highlight some important components of this problem. Firstly, because $b$, $c>0$ and $H$, $Z\geq 0$ then the human interaction term, $-\alpha HZ$, is always negative. Thus, the  human population will only ever decrease over time.

    We could add a birth term into this equation, which would allow the population to also increase in the absence of zombies but, as we have seen previously, the time scale on which we are working on is extremely short, much shorter than the 9 months it takes for humans to reproduce! Thus we ignore the births since they are not likely to alter the populations a great deal during this period.

    Interpreting the zombie equation is not so easy. The term $c-a=\beta$ may either be positive or negative. If $(c-a)>0$ then the creation rate of zombies, $c$, must be greater than the rate which we can destroy them, $a$. In this case the humans will be wiped out as our model predicts that the zombie population will grow and the human population will die out. However, there is a small hope for us. If the rate at which humans can kill zombies is greater than the rate at which zombies can infect humans then $(c-a) < 0$. In this case both populations are decreasing, thus our survival will come down to a race of which species becomes extinct first.

    Next week we will delve into the equations more and consider the spread of infection. We will then be able to derive expressions that really tell us how to survive, or at least delay, the zombie uprising.

    Saturday, 9 January 2016

    Diffusion of the dead - The maths of zombie invasions. Part 6, Run, don't fight.

    Last time I demonstrated how to approximately find the time of your first interaction with a zombie using the diffusion equation and the bisection method. The Time-Distance-Diffusion graph is illustrated below.
    Figure 1. Time in minutes until the density of zombies reaches one for various rates of diffusion and distances.
    When the apocalypse does happen, we have to ask ourselves the question: do we want to waste time computing solutions when we could be out scavenging? In order to speed up the computational process we consider the diffusive time scale:
    \begin{equation}t=\frac{L^2}{\pi^2 D}.\label{Time_scale}\end{equation}
    You may recognise this group of parameter, as we saw it back in Part 4. In particular, in the solution to the diffusion equation, we can see that this is the time it takes for the first term of the infinite sum to fall to $\exp(-1)$ of its original value. The factor of $\exp(-1)$ is used due to its convenience.

    Only the first term of the expansion is considered because as $n$ increases, the contribution from the term
    \begin{equation}\exp\left(-\left( \frac{n\pi}{L} \right)^2Dt\right)\end{equation}
    rapidly decreases. Thus, the first term gives an approximation to the total solution and, so, equation \eqref{Time_scale} gives a rough estimate of how quickly the zombies will reach us.

    For example, being 90 metres away and with zombies who have a diffusion rate of 100m$^2$/min, $t\approx 26$ minutes, comparable to the solution in Figure 1. We have had to use no more computing power than you would find on a standard pocket calculator. More importantly this parameter grouping also implies a very important result about delaying the human-zombie interaction.There are two possible ways we could increase the time taken for the zombies to reach us. We could:  
    1. run away, thereby increasing $L$; or
    2. slow the zombies down, thereby decreasing $D$.
    Since the time taken is proportional to the length squared, $L^2$ and inversely proportional to the diffusion speed, $D$. This means that if we were to double the distance between ourselves and the zombies, then the time for the zombies to reach us would approximately quadruple. However, if we were to slow the zombies down by half, then the time taken would only double.

    Since we want to delay interaction with the zombies for as long as possible then, from the above reasoning, we see that it is much better to expend energy running away from the zombies than it is to try and slow them down. Note that we are assuming that zombies are hard to kill without some form of weaponry. If they weren't difficult to destroy then we need not worry about running away.

    These conclusions are confirmed in Figure 1. Slowing a zombie down from 150 m/min to 100 m/min only gains you a couple of minutes when you are 50 metres away. However, running from 50 m to 90 m increases the time by over 10 minutes, even in the scenario of relatively fast zombies.

    It should be noted that the time derived here is a lower bound. In reality, the zombies would be spreading out in two dimensions and would be distracted by obstacles and victims along the way, so the time taken for the zombies to reach us may be longer. The fact that this is a conservative estimate though will keep us safe, since the authors would prefer to be long gone from a potential threat rather than chance a few more minutes of scavenging!

    Of course, we can't run forever. Next week we will begin to ask what happens when we finally meet this horrific horde!

    Saturday, 26 December 2015

    Diffusion of the dead - The maths of zombie invasions. Part 5, Time of first interaction.

    Previously, we saw how to simulate the diffusion equation, which we're using to model zombie motion. Critically, the diffusion equation allows us to predict the density of zombies at all places and for all time. 

    There are many questions we could answer with this equation; however, the most pressing question to any survivors is, "How long do we have before the first zombie arrives?". The mathematical formulation of this question is, "For what time, $t_z$, does $Z(L, t_z) = 1$?". Unfortunately, this does not have a nice solution that can be evaluated easily. However, we can calculate $t_z$ numerically to any degree of accuracy we choose by using a simple solution searching technique known as the "Bisection Search Algorithm"

    The first thing to notice about $Z(L,t)$ is that it is monotonically increasing in time, thus, if $t_1>t_2$ then  $Z(L,t_1)>Z(L,t_2)$. This can be seen in a number of ways. For example, by watching the diffusion simulation from last time we see that as time increases the zombie population on the boundary only ever increases. This makes intuitive sense, because diffusion is causing the zombie population to spread out, so that it becomes uniform everywhere.

    Using this knowledge then a simple method of solving $Z(L, t_z) = 1$ would be to substitute in a value of $t$. If $Z(L, t) < 1$ then we double $t$ and consider $Z(L, 2t)$, which will be greater than $Z(L,t)$, because of the monotonic property we discussed above. We keep doubling $t$ until we reach a value such that $Z(L, 2^nt) > 1$. Defining $t_0 = 2^{n-1}t$ and $t_1 = 2^nt$, then we know that there exists some $t_z$ between $t_0$ and $t_1$ such that $Z(L, t_z) = 1$.

    To gain better approximations to the value of $t_z$, we start halving this domain and only keep the half that contains the solution. However, how do we know which half contains the solution? We once again rely on the monotonic property and evaluate the function at the midpoint of the domain. Explicitly, if  $Z(L,(t_0+t_1)/2) < 1$ then the solution is in the right half. Alternatively, if $Z(L,(t_0+t_1)/2) > 1$ then the solution is in the left half.

    An example illustrating this concept is shown in Figure 1. In the initial setup, $Z(L,(t_0+t_1)/2)>1$, thus, we redefine $t_1 = (t_0 + t_1)/2$ and repeat the process.

    Figure 1. Bisection technique. After each iteration, the domain, shown by the dashed lines,
    becomes half as long as it was before.
    After each iteration, we halve the size of the interval $[t_0, t_1]$, making it smaller and smaller. Thus, by design, since we always have $t_z$ within $[t_0, t_1]$ and by repeating the halving process, we can estimate $t_z$ to any accuracy we like.

    The benefit of this method is its simplicity and reliability; it will always work. However, the cost of this reliability comes at the price of speed. If the initial searching region is very big, it may take a large number of iterations before the process produces an answer to an accuracy with which we are happy. There are quicker methods, but these are usually more complex and sometimes they may fail to find a solution altogether.

    If the zombies are closing in on you and you need to compute their interaction times more quickly, we direct you to consider Newton-Raphson techniques rather than the bisection method. Although if the zombies really are that close may I suggest you focus on fighting them off before you read any further?

    No matter what solution method you use you should end up with a graph like Figure 2, which illustrates the time in minutes we have before we meet a zombie depending on how far away we are and how fast the zombies are diffusing.
    Figure 2. Time in minutes until the first zombie arrives for various rates of diffusion and distances.
    At this point we can consider two general strategies. Either we run away, or we try and slow the zombie down. Both of these approaches will indeed increase the time it takes for the zombies to get to you. However, Figure 2 clearly shows that we gain much more time by running away compared to the strategy of slowing the zombies down. Next time I will expand on this point and show how to estimate the zombie interaction time much quicker.
    ________________________________________________________________
    ________________________________________
    My amazing wife on her zombie themed hen do.
    Note that the theme had nothing to do with me.
    You may be wondering how I chose the variable ranges for Figure 2. Well, the Oxford maths department is approximately 90m away from a graveyard so the distances simply came from a matter of self preservation. The diffusion speed on the other hand was generated with the aid of my loving wife. I got her to wander randomly around, staggering like a zombie. I timed her and measured how far she had moved and, thus, calculated that her zombie diffusion rate was approximately 115m$^2$/minute. Don't worry we did the experiment three times and took an average, which ensures that this value is accurate.

    Saturday, 12 December 2015

    Diffusion of the dead - The maths of zombie invasions. Part 4, Simulating zombie movement.


    Last time we presented the diffusion equation
    \begin{equation}
    \frac{\partial Z}{\partial t}(x,t)=D\frac{\partial^2 Z}{\partial x^2}(x,t).
    \end{equation}
    and demonstrated that it had the right properties to model zombie motion. However, stating the equation is not enough. We must add additional information to the system before we can solve the problem uniquely. Specifically, we need to define: the initial state of the system, where the boundaries of the system are, and, finally, what happens to the zombies at the boundaries.

    For the boundary condition we assume that the zombies cannot move out of the region $0\leq x\leq L$. This creates theoretical boundaries which the population cannot cross; the zombies will simply bounce off these boundaries and be reflected back into the domain. Since no zombies can cross the boundaries at $x=0$ and $x=L$, the `flux of zombies' across these points is zero,
    \begin{equation}
    \frac{\partial Z}{\partial x}(0,t)=0=\frac{\partial Z}{\partial x}(L,t) \text{ (the zero flux boundary conditions).}
    \end{equation}

    For the initial condition, we assume that the zombies are all localised in one place, a graveyard for example. Thus the zombies have a density of $Z_0$ zombies/metre in the region $0\leq x \leq 1$,
    \begin{equation}
    Z(x,0)=\left\{\begin{array}{cc}
    Z_0&\text{for }0\leq x\leq 1,\\
    0&\text{for } x>1,
    \end{array}
    \right. \text{ (the initial condition).}
    \end{equation}

    The diffusion with the given initial and boundary conditions can be solved exactly and has the form,
    \begin{equation}
    Z(x,t)=\frac{Z_0}{L}+\sum^\infty_{n=1}\frac{2Z_0}{n\pi}\sin\left(\frac{n\pi}{L}\right) \cos\left(\frac{n\pi}{L}x\right)\exp\left({-\left( \frac{n\pi}{L} \right)^2Dt}\right).\label{Solution}
    \end{equation}
    Although I have made this solution magically appear from nowhere, the solution can be rigorously produced using methods called "separation of variables" and "Fourier series".

    Separating the variables means that we assume space and time components of the solution are not coupled together in a complicated manner. Namely, the solution can be written as a spatial component, multiplied by a time component, which can be seen above because the space variable, $x$, only appears in the cosine function, whilst the time variable, $t$, only appears in the exponential function.

    Fourier series allows us to write a function as an infinite summation of sines and cosines. Although this may seem cumbersome the Fourier series usually have very nice properties, which allow them to be manipulated with ease.

    The `$\sin$' and `$\cos$' functions are those that the reader may remember from their trigonometry courses. The exponential function, `$\exp$', is one of the fundamental operators of mathematics, but for now the only property that we are going to make use of is that if $a>0$ then $\exp(-at)\rightarrow 0$ as $t\rightarrow \infty$. Using this fact we can see that as $t$ becomes large most of the right-hand side of the solution becomes very small, approximately zero. Hence, for large values of $t$, we can approximate
    \begin{equation}
    Z(x,t)\approx\frac{Z_0}{L}.\label{Longtime_approximation}
    \end{equation}
    This means that as time increases, zombies spread out evenly across the available space, with average density $Z_0/L$ everywhere.



    The simulation illustrates two solutions to the diffusion equation for the zombie density. Namely, the video above compares the  analytical solution, given in equation \eqref{Solution}, with a direct numerical solution of the diffusion equation. If you stop the video right at the start, the initial condition can be seen and it shows that there is a large density of zombies between $0\leq x \leq 1$. The movie then illustrates how diffusion causes the initial peak to spread out filling the whole domain. After 500 time units the density of zombies has become uniform throughout the domain. The Matlab codes for plotting this solutions can be found below.

    There are a couple of things to note in this simulation. Firstly, we say that the analytical solution is only a approximation because we can not simulate an infinite number of cosine terms. The movie shows only the first 1000 terms and, as you can see, the comparison between the two results is pretty good. Secondly we have not given units for the density, space or time, thus, are we using seconds and metres or minutes and miles? Well, the answer is that in some sense it doesn't matter, whilst in other cases it matters a great deal. Since we are only interested in visualising the dynamics of diffusion, we can keep the units arbitrary. However, if we had a specific application, with data, then we would have to ensure that all our units were consistent.

    This finishes our look at solving the diffusion equation. Next time we actually use these solutions to provide us with the first of ours answers. These answers will then provide us with a strategy to deal with the eventual zombie apocalypse.
    ________________________________________________________________
    ________________________________________
    Below you will find the Matlab code used to generate the above movie. If you have Matlab then you can simply copy and paste the text into a script and run it immediately.

    function Diffusion_solution
    clear
    close all
    clc

    Z0=100; %Initial density.
    L=10; %Length of domain.
    D=0.1; %Diffusion coefficient.
    dx=0.1; %Space step.
    x=[0:dx:L]; %Spatial discretisation.
    dt=1; %Time step.
    final_time=500; %Final time.
    times=0:dt:final_time; %Time discretisation.
    iter=1; %Parameter initialisation.

    %% Analytical solution.
    Z=ones(1+final_time/dt,length(x))*Z0/L;
    for t=0:dt:final_time
    for n=1:1000
    Z(iter,:)=Z(iter,:)+2*Z0/(n*pi)*sin(n*pi/L)*cos(x*n*pi/L)*...
    exp(-(n*pi/L)^2*t*D);
    end
    iter=iter+1;
    end

    %% Direct numerical solution.
    sol = pdepe(0,@(x,t,u,DuDx)Zombies(x,t,u,DuDx,D),@(x)ICs(x,Z0),@BCs,x,times);

    %% Plotting
    for i=1:final_time+1
    plot(x,Z(i,:),'b','linewidth',3)
    hold on
    plot(x,sol(i,:),'g--','linewidth',3)
    set(gca,'yTick',[0:20:100])
    xlabel('Distance, x','fontsize',20);
    ylabel('Density, Z','fontsize',20);
    set(gca,'fontsize',20)
    grid off
    axis([0 10 0 100])
    title(['Time=',num2str((i-1)*dt)])
    legend('Approximate analytic solution','Approximate numerical solution','location','northoutside')
    drawnow
    hold off
    end

    function value = ICs(x,Z0)
    % Setting the initial condition, which is a step function. Namely, the
    % density of zombies is Z0 when x is less than 1 and 0 everywhere else.
    if x < 1
    value=Z0;
    else
    value=0;
    end
    function [c,b,s] = Zombies(~,~,~,DuDx,D)
    % Matlab syntax for the diffusion equation.
    c = 1;
    b = D*DuDx(1);
    s = 0;
    function [pl,ql,pr,qr] = BCs(~,~,~,~,~)
    % Matlab syntax for the zero flux boundary conditions.
    pl = 0;
    ql = 1;
    pr = 0;
    qr = 1;

    Saturday, 28 November 2015

    Diffusion of the dead - The maths of zombie invasions. Part 3, Diffusive motion.


    As discussed previously, we are going to model the zombie motion using the diffusion equation. In this post we introduce the gritty details. I've interpreted the mathematical symbols intuitively, so, if you stick with it, you should find yourself understanding more than you ever thought you could.

    It is impossible to overstate the importance of the diffusion equation. Wherever the movement of a modelled species can be considered random and directionless, the diffusion equation will be found. This means that by understanding the diffusion equation we are able to describe a host of different systems such as heat conduction through solids, gases (e.g. smells) spreading out through a room, proteins moving round the body, molecule transportation in chemical reactions and rainwater seeping through soil, to name but a few of the great numbers of applications.

    If you've never come across diffusion before, or want to know more about it's basic properties the video below is a very good primer, although feels very much like a "Look around you" episode.

    The mathematical treatment of diffusion begins by defining the variables that we will need. Let the density of zombies at a point $x$ and at a time $t$ be $Z(x,t)$ then the density has to satisfy the diffusion equation,
    \begin{equation}
    \frac{\partial Z}{\partial t}(x,t)=D\frac{\partial^2 Z}{\partial x^2}(x,t).
    \end{equation}
    To some an equation can be scarier than any zombie, but fear not. I am going to break this equation down into bits so that you are able to see the reality behind the mathematics.

    Notice that the equation is made up of two terms, the left-hand side and the right-hand side, which are defined to be equal. Explicitly, the left-hand side is known as the time derivative and it simply tell us how the zombie density is changing over time,
    \begin{equation}
    \frac{\partial Z}{\partial t}(x,t)=\text{rate of change of $Z$ over time at a point $x$}.
    \end{equation}
    Although the numerical value of this term is important, what is more important is if the term is positive or negative. Specifically, if $\partial Z/\partial t$ is positive then $Z$ is increasing at that point in time, and, vice-versa, if $\partial Z/\partial t$ is negative then $Z$ is decreasing. Thus, we use this term to tell us how the zombie population is changing over time.

    The term on the right-hand side is known as the second spatial derivative and it is a little more complicated than the time derivative. Essentially it encapsulates the idea that the zombies move from areas of high density to areas of low density (i.e. they spread out). To aid your intuitive understanding of this term see Figure 1.
    Figure 1. A typical initial zombie density graph. There are regions of high zombie activity, e.g. a graveyard, and there are regions of low zombie density, e.g. your local library.
    In the figure, there are initially more zombies on the left of the space than the right. Just before the peak in density the arrow (which is the tangent to the curve known as the spatial derivative, or $\partial Z/\partial x$ at this point) is pointing upwards. This means that as $x$ increases, so does the zombie density, $Z$. At this point
    \begin{equation}
    \frac{\partial Z}{\partial x}=\textrm{rate of change of $Z$ as $x$ increases} > 0.
    \end{equation}
    Just after the peak the arrow is pointing down thus, at this point,
    \begin{equation}
    \frac{\partial Z}{\partial x}=\textrm{rate of change of $Z$ as $x$ increases} < 0.
    \end{equation}
    Thus, at the peak, the spatial derivative is decreasing, because it goes from positive to negative. This, in turn, means that the second derivative is negative at the peak, because a negative second derivative means the first derivative is decreasing. This is analogous to statements made above about the sign of the time derivative and the growth, or decay, of the zombie population.

    In summary, our hand wavy argument tells us that at local maximum $\partial^2 Z/\partial x^2<0$. Using the equality of the diffusion equation, this means that at a local maximum the time derivative is negative and, thus, the density of zombies is decreasing. A similar argument shows that the population of zombies at a local minimum increases. In summary, we see that diffusion causes zombies to move from regions of high density to low density.

    Finally, we mention the factor $D$, which is called the diffusion coefficient. $D$ is a positive constant that controls the rate of movement. Specifically, the larger $D$ is the faster the zombies spread out.

    And with that you now understand one of the most important partial differential equations in all of mathematics. That wasn't too hard was it? Next time we discuss the solution of the diffusion equation including some simulations and Matlab code for you to try yourself.

    Saturday, 14 November 2015

    Diffusion of the dead - The maths of zombie invasions. Part 2, Important questions you need to ask in a zombie outbreak.


    We begin modelling a zombie population in the same way that a mathematician would approach the modelling of any subject. We, first, consider what questions we want to ask, as the questions will direct which techniques we use to solve the problem. Secondly, we consider what has been done before and what factors were missing in order to achieve the answers we desire. This set of blog posts will consider three questions:
    1. How long will it take for the zombies to reach us?
    2. Can we stop the infection?
    3. Can we survive?
    In order to answer these questions I, Ruth Baker, Eamonn Gaffney and Philip Maini focused on the motion of the zombies as their speed and directionality would have huge effects on these three questions. Explicitly, we used a mathematical description of diffusion as a way to model the zombies motion. This was discussed in a previous post, but I recap the main points here.
    • The original zombie infection article by Robert Smith? did not include zombie, or human movement.
    • Zombies are well known for is their slow, shuffling, random motion. The end of Dawn of the Dead (shown in the YouTube clip below) gives some great footage of zombies just going about their daily business.
    • This random motion is perfectly captured through the mathematics of diffusion.


      Of course, there is plenty of evidence to suggest that zombies are attracted to human beings, as they are the predator to our prey. However, as we will see, we are going to be over run on a time scale of minutes! Thus, although mathematicians can model directed motion, and chasing, these additional components complicate matters. Further, random motion leads to some nice simple scaling formulas that can be used to quickly calculate how long you approximately have left before you meet a zombie.

      Another simplifying assumption that we make is that we can model the zombie (and human) populations as continuous quantities. Again, this is incorrect as zombies are discrete units (even if they are missing body parts). Since we are making an assumption we will create an error in our solution. But how big is this error? In particular, if the error in the assumption is smaller than the errors in our observable data set then we do not have to worry too much. The error introduced by this assumption is actually dependent on the size of the population we are considering. The more individuals you have, you more the population will act like a continuous quantity. Since there are a lot of corpses out there, we do not think this assumption is too bad.

      Note that we could model the motion of each zombie individually, however,  the computing power needed by such a simulation is much larger than the continuum description, which can be solved completely analytically. This is particularly important in the case of the zombie apocalypse, where time spent coding a simulation, may be better spent scavenging.

      These are the basic assumptions we made when modelling a zombie population. Although I have tried to justify them you may have reservations about their validity. That is the very nature of mathematical modelling; try the simplest thing, first, and compare it to data. If you reproduce the phenomena that you are interested in then you have done your job well. However, if there is a discrepancy between the data and your maths then you have to revisit your assumptions and adapt them to make them more realistic.

      Next time we contend with the equations and model the motion of the zombie as a random walker.

      Saturday, 31 October 2015

      Diffusion of the dead - The maths of zombie invasions. Part 1, For those who can't wait.

      Many years ago I posted a blog post about an academic article I had written about zombies. Finally, the article was published in Mathematical Modelling of Zombies. The book is designed to be readable by anyone with an interest in mathematics. However, those with an numerical background will find that it pushes them further as it does not shy away from clearly displaying the mathematics, whilst explaining the methods behind the madness.

      As always, thank you to Martin Berube
      for the use of his zombie image.
      The articles range over a number of fields and are simply a means to dress up our everyday techniques in a way that is more palatable for a non-mathematical audience. Of course not all of you will want to shell out for the book. Thus, I've decided to essentially serialize the chapter in the next few posts, thus, we will be looking at all of the results of our paper and, hopefully, I'll be explaining the mathematics more clearly, so that any one is able to follow it. I may even throw in a matlab code or two, so that anyone is able to reproduce the results.

      For those of you who are just interested in a quick review, you can read The Times article, or my brief version on the University of Oxford's Mathematical Institute website. Alternatively, if you are too tired to read you can always watch, or listen to a recorded version from the Athens Science Festival, Cambridge Science Festival, or on The Science of Fiction radio show. Finally, you could always see me live, when I'm giving one of my talks.

      Next time, we will start at the beginning by modelling the zombie motion.




      Monday, 4 February 2013

      The egg of Columbus

      Figure 1. An egg standing to attention.
      Last time I demonstrated that if a weeble of uniform density existed, then it could not be effectively 2D. This then led to considerations of the stability of 3D shapes in order to try and find the elusive shape with exactly one stable equilibrium point and one unstable equilibrium point. Since we can construct shapes with lots of stable points what we would really like to do is find a find of reducing this number. However, today, we take a sleight detour and consider the opposite problem of making an unstable point stable!

      We start with a challenge (supposedly) set by Christopher Columbus. Whilst dining one night, a nobleman approached him and suggested that finding the Americas was not that impressive because anyone sailing out that way could not have missed them. In reply Columbus challenged the nobleman to take a normal egg and place it so it stood upright. Now, there are many ways of doing this, but most use some other piece of apparatus (such as alternating current), but Columbus used only the egg.

      When the nobleman gave up Columbus simply took the egg and tapped it gently breaking the top of the shell, making it flatter. The moral of the story is normally anyone can solve a problem once they have seen a solution. However, for us, the story shows how to make unstable points stable.
      Figure 2. Stabilising the unstable. By flattening the top the egg becomes stable upside down.
      What is more amazing is that you can actually rigorously construct a mathematical algorithm that does exactly this flattening operation. Thus, if a uniform density weeble does exist then following this algorithm will allow us to construct a solid with any number of stable equilbria.

      In two weeks we return to the trail of finding this illusive shape.

      ________________________________________________________________
      ________________________________________

      Monday, 6 August 2012

      Maths laugh 8. Careful when out in the sun.

      Not quite the sun tan most people would have in mind.
      As usual, tweet me any maths jokes to illustrate @ThomasEWoolley.

      Maths laugh 7. Bend in the road.

      This humorous piece of mathematical graffiti got me thinking. What other road signs can be given a mathematical nature?

      Normally they only give the gradients of steep hills. Here, we can work out the gradient of the bend!

      As usual, tweet me any maths jokes to illustrate @ThomasEWoolley.

      Monday, 15 August 2011

      Maths laugh 6. Up to date satire.

      Ali G. was actually an excellent mathematical philosopher!

      As usual, tweet me any maths jokes to illustrate @ThomasEWoolley.

      Monday, 1 August 2011

      Maths laugh 5. More food based maths.

      Anyone else hungry?


      For some more maths based food I recommend the following links from the Evil Mad Scientist Laboratories:

      Here is a demonstration of how to cut a bagel into two connected pieces and, finally, turn your fruit into platonic a solid.

      Monday, 18 July 2011

      Arts vs Science

      A couple of years ago a debate took place in Oxford, the title of which was "Poetry is beautiful, but science is what matters". An evocative title I'm sure you'll agree. However, what I personally liked about the debate was how it was advertised, as shown in the above picture.

      Upon a background of equations you have a smooth, regal, Einstein with an angelic glow to show support for science. On the left, in support of the "beauty" of poetry, is an elderly, wrinkled gentleman, smoking a cigarette with unkempt hair.

      Now, this may have been done completely by accident, but I just love how biased this picture comes across in its advertising of the event.

      [Edit based on the excellent suggestion of Christian Perfect.] Interestingly it was an internet debate so you can read the whole set of arguments here:


      In summary, the motion was defeated 62% to 38%. As you might expect, the debate never really got around to stating which field matters more. Instead, the battle was over semantics. What does "matter" mean? What does science mean? Etc.

      Personally, I don't subscribe to the whole arts vs science utility debate. As Richard Feynman once said,
      "Physics is like sex. Sure, it may give some practical results, but that's not why we do it."


      P.S. If you know who the poet is in the figure please say so in a comment below. I would love to know who it is.

      P.P.S. I've just been informed that it is W. H. Auden. Isn't the internet wonderful?



      Monday, 9 May 2011

      Maths laugh 3. Roller coasters.

      Quite a simple one today and, although I like it, it irritates me as I know that the graph is not well defined.

      Ah the trials and tribulations of a mathematician.

      As ever, if you have any maths jokes that you think would benefit from my illustrative skills then simply tweet them to me @ThomasEWoolley

      Monday, 2 May 2011

      Maths laugh 2. Do you suffer from Triskaidekaphobia?

      OK, so it is more of a pun than a mathematical joke but hey ho I like it anyway.

       

      Thanks to @reflectivemaths and @SallyRobin for suggesting maths jokes that I can illustrate. Keep'em coming.