<a href="https://colab.research.google.com/github/john-s-butler-dit/CaseStudy_PredatorPrey/blob/master/03_Linear%20Population%20Growth.ipynb" target="_parent"></a>

## Linear Population Differential Equations

In this notebook we will solve a simple population equation [1]


```python
## Library
import numpy as np
import matplotlib.pyplot as plt
```

## Linear Population Equation
The linear population equation is a first order linear differential equation given by, 
\begin{equation}
\frac{d p}{dt}=\alpha_{Birth} p-\alpha_{Death}p=\alpha p,
\end{equation}
where $p$ is the population at time $t$,
for $a\leq t \leq b$ years, where $\alpha$ is the birth rate $\alpha_{Birth}$ minus death rate $\alpha_{Death}$, with the initial condition
$$ p(a)=A. $$
The linear population equation has the exact solution
\begin{equation}
p(t)=Ae^{(\alpha (t-a))}.
\end{equation}
In this simulation we set the parameters of the different equations:
*  as $\alpha=0.3$, with the inital condition $p(0)=3$, over the time period $0\leq t \leq 10$.


```python
alpha=0.3
```

## Discrete Time Domain
To numerically approximate the solution we discretise the continuous domain using a step size, $h=\frac{10-0}{10}=1$, 
which gives
\begin{equation}
t_i=0+i h,
\end{equation}
for $i=0,...10$.

The Figure below illustrate the discrete domain.


```python
h=1
a=0
b=10
t=np.arange(a,b+h/2,h)
fig = plt.figure(1,figsize=(5,4))
ax = fig.add_subplot(1,1,1)
ax.plot(t,0*t,'o:',color='k')
ax.set_xlabel('Time (yrs)')
plt.title('Discrete Time')
plt.show()
```


```python
N=int((b-a)/h) # Number of Steps
P=np.zeros(N+1) # Numerical Solution P
P[0]=3
p=3*np.exp(alpha*(t-a)) # Exact Solution
```

We numerically approximate the differential equation using Eulers method to give the difference equation
\begin{equation}
P_{i}=P_{i-1}+h \big(\alpha P_{i-1} \big),
\end{equation}
where $h$ is the stepsize and where $P_i$ is the numerical approximation of $p(t_i)$ at distance $t_i$, for $i=0,...,10.$



```python
for i in range (1,N+1):
    P[i]=P[i-1]+h*(alpha*P[i-1])


```

## Results
The plot bellow shows the Numerical solution $P_i$ (left), the analytic solution $p(x)$ (middle) and the error $P(x_i)-P_i$ (right) as a function of time $t$.


```python
## Plotting Figure
fig = plt.figure(1,figsize=(12,4))

# --- left hand plot
ax = fig.add_subplot(1,3,1)
ax.plot(t,P,'o:',color='red',label='Population')
ax.legend()

ax.set_xlabel('Time (yrs)')
ax.set_ylabel('Population (billions)')
#ax.legend(loc='best')
ax.set_title('Numerical Solution, P')

# --- middle plot
ax = fig.add_subplot(1,3,2)
ax.plot(t,p,'-',color='red',label='Population')


ax.legend()


ax.set_xlabel('Time (yrs)')
ax.set_ylabel('Population (billions)')

ax.set_title('Analytic (exact) Solution p(t)')

# --- right hand plot
ax = fig.add_subplot(1,3,3)
ax.plot(t,P-p,'o:',color='red',label='ERROR')
ax.legend()


ax.set_title(r'Error $p(x_i)-P_i$')
ax.set_ylabel('Error(Population)')
ax.set_xlabel('Time (yrs)')


# --- title, explanatory text and save
fig.suptitle('Linear Population Equation', fontsize=20)
plt.tight_layout()
plt.subplots_adjust(top=0.85)

```

# References

[1] Stover, Christopher and Weisstein, Eric W. "Population Growth." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/PopulationGrowth.html

03499d0118fdb3dc30e7f4948b94021531145112
ipynb
Jupyter Notebook
03_Linear Population Growth.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
03_Linear Population Growth.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
03_Linear Population Growth.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
Qwen/Qwen-72B
1. YES
2. YES