8.2 Population Growth: A First Order ODE
221
for n in range(N_t):
u[n+1] = u[n] + dt*f(u[n], t[n])
return u, t
def demo_population_growth():
"""Test case: u’=r*u, u(0)=100."""
def f(u, t):
return 0.1*u
u, t = ode_FE(f=f, U_0=100, dt=0.5, T=20)
plt.plot(t, u, t, 100*np.exp(0.1*t))
plt.show()
if __name__ == ’__main__’:
demo_population_growth()
This program file, called ode_FE.py, is a reusable piece of code with a general
ode_FE function that can solve any differential equation u = f (u, t) and a demo
function for the special case u = 0.1u, u(0) = 100. Observe that the call to the
demo function is placed in a test block. This implies that the call is not active if
ode_FE is imported as a module in another program, but active if ode_FE.py is run
as a program.
The solution should be identical to what the growth1.py program produces with
the same parameter settings (r = 0.1, N 0 = 100). This feature can easily be tested
by inserting a print command, but a much better, automated verification is suggested
in Exercise 8.2. You are strongly encouraged to take a “break” and do that exercise
now.
Remark on the Use of u as Variable
In the ode_FE program, the variable u is used in different contexts. Inside the
ode_FE function, u is an array, but in the f(u,t) function, as exemplified
in the demo_population_growth function, the argument u is a number.
Typically, we call f (in ode_FE) with the u argument as one element of the
array u in the ode_FE function: u[n].
8.2.6 A More Realistic Population Growth Model
Exponential growth of a population according the model N = rN, with exponential
solution N = N 0 e rt , is unrealistic in the long run because the resources needed to
feed the population are finite. At some point there will not be enough resources and
the growth will decline. A common model taking this effect into account assumes
that r depends on the size of the population, N:
N(t + Δt) − N(t) = r(N(t))N(t) .
Précédent

- 241/350

Suivant