"""" Example code from lecture 27 oct. Use the forward Euler function to solve the model for logistic growth. Since the model includes a couple of parameters, it is convenient to implement it as a class with a call metohod. """ import numpy as np import matplotlib.pyplot as plt def ForwardEuler(f, U0, T, n): """Løs u'=f(t,u), u(0)=U0, med n steg til t=T.""" t = np.zeros(n+1) u = np.zeros(n+1) # u[k] er løsningen ved tid t[k] u[0] = U0 t[0] = 0 dt = T / n for k in range(n): t[k+1] = t[k] + dt u[k+1] = u[k] + dt * f(t[k], u[k]) return t, u class Logistic: def __init__(self, alpha, R): self.alpha = alpha self.R = R def __call__(self, t, u): return self.alpha * u * (1 - u / self.R) problem = Logistic(0.2, 1) U0 = 0.1 t, u = ForwardEuler(problem, U0, 400, 400) plt.plot(t, u,) plt.show()