#A helpful script for Exercise 3.4.8.

#Here is the data for Exercise 3.4.8, in time/population pairs:
data = [[0, 9.6], [1, 18.3], [2, 29], [3, 47.2], [4, 71.1], [5, 119.1], [6, 174.6], [7, 257.3], [8, 350.7], [9, 441],
[10, 513.3], [11, 559.7], [12, 594.8], [13, 629.4], [14, 640.8], [15, 651.1], [16, 655.9], [17, 659.6]]
N = len(data);

#A plot:
plt1 = scatter_plot(data);
show(plt1)

#Given that u(0) = 9.6, the solution to the logistic equation with intrinsic growth rate "r" and carrying capacity "K"
is
var('t K r');
u(t,K,r) = K/(1+exp(-r*t)*(K/9.6-1)); #The model function to fit
SS = function('SS')(K,r);
SS(K,r) = add((u(data[i][0],K,r)-data[i][1])^2 for i in range(N));

#Now minimize SS(K,r) with respect to K and r.