## 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)=Aexp(\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=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
t_end=10
t=np.arange(0,t_end+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(t_end/h) # Number of Steps
P=np.zeros(N+1) # Numerical Solution P
P[0]=3
p=3*np.exp(alpha*t) # Exact Solution
```

We numerically approximate the differential equation using Eulers method to give the difference equation
\begin{equation}
P_{i+1}=P_{i}+h \big(\alpha P_{i}k \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='VIS ERROR')
ax.legend()


ax.set_title('Error')
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

030e2ff5231b34f178b2b5762f14e0844bf65a33
ipynb
Jupyter Notebook
.ipynb_checkpoints/NA01_Dodos-checkpoint.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
.ipynb_checkpoints/NA01_Dodos-checkpoint.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
.ipynb_checkpoints/NA01_Dodos-checkpoint.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
Qwen/Qwen-72B
1. YES
2. YES