<jupyter_start><jupyter_text>Introduction to Complexity Section 2 Iteration, Population Growth, & Chaos$n_1 = birthrate \times n_0$$n_2 = birthrate \times n_1$Generalizing, we get the model:$n_{t + 1} = birthrate \times n_t$Lecture Question:<jupyter_code>3^4;<jupyter_output><empty_output><jupyter_text>Linear ModelWe'll use Maxima's Lisp support to explore our models.<jupyter_code>to_lisp()$
(defun ->maxima (data)
  (cons '(mlist) 
        (map 'list (lambda (x) `((mlist) ,@x)) data)))

(defun $population (x0 rate n)
  (labels ((pop0 (acc)
    (let ((step (length acc)))
      (if (eql step n) 
        (->maxima acc)
        (let ((x-last (cadar (last acc))))
          (pop0 (append acc `((,step ,(* rate x-last))))))))))
   (pop0 `((0 ,x0)))))
(to-maxima)
pop_0: 1$
birthrate: 2$
steps: 4$
population(pop_0, birthrate, steps);
pop_0: 10$
population(pop_0, birthrate, steps);
xy0: population(1, 2, 11)$
to_lisp()$
(defun maxima-> (data)
  (mapcar (lambda (x) (cdr x)) (cdr data)))
  
(defun $max_x (data)
  (apply #'max (mapcar #'car (maxima-> data))))
  
(defun $max_y (data)
  (apply #'max (mapcar #'cadr (maxima-> data))))
(to-maxima)
set_plot_option([svg_file, "maxplot.svg"])$
data: xy0$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
to_lisp()$
(defun $population_year_over_year (data)
  (labels ((pyoy0 (acc last-year remaining)
    (if (null remaining) 
      (->maxima acc)
      (let ((this-year (cadar remaining)))
        (pyoy0 (append acc `((,last-year ,this-year))) this-year (cdr remaining))))))
    (pyoy0 '() 0 (maxima-> data))))
(to-maxima)
xy1: population_year_over_year(xy0);
data: xy1$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$<jupyter_output><empty_output><jupyter_text>Non-linear ModelAdding deaths due to overcrowding:$n_{t + 1} = birthrate \times (n_t - deaths_{overcrowded})$$deaths_{overcrowded} = \frac {{n_t}^2} {population_{max}}$Adding mortality rate to that:$n_{t + 1} = (birthrate - deathrate) \times (n_t - deaths_{overcrowded})$This is called the "Logistic Model"<jupyter_code>to_lisp()$
(defun R (b-rate d-rate)
  (- b-rate d-rate))

(defun overcrowded-deaths (x-last K)
  (/ (expt x-last 2) K))

(defun growth (b-rate d-rate x-last K)
  (* (R b-rate d-rate)
     (- x-last (overcrowded-deaths x-last K))))
  
(defun $logistic_model (x0 b-rate d-rate K n)
  (labels ((p0 (acc)
    (let ((step (length acc)))
      (if (eql step n) 
        (->maxima acc)
        (let ((x-last (cadar (last acc))))
          (p0 (append acc `((,step ,(growth b-rate d-rate x-last K))))))))))
   (p0 `((0 ,x0)))))
(to-maxima)
pop_0: 1$
birthrate: 2.0$
deathrate: 0.0$
carrying_capacity: 50.0$
steps: 20$
xy2: logistic_model(pop_0, birthrate, deathrate, carrying_capacity, steps);
data: xy2$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
xy3: population_year_over_year(xy2)$
data: xy3$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
pop_0: 1$
birthrate: 2.5$
deathrate: 0.0$
carrying_capacity: 50.0$
steps: 10$
data: logistic_model(pop_0, birthrate, deathrate, carrying_capacity, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
data: population_year_over_year(data)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$<jupyter_output><empty_output><jupyter_text>Logistic Map$x_{t+1} = R [ x_t - x_t^2 ] $<jupyter_code>to_lisp();
(defun l-map (R x-last)
  (* R (- x-last (expt x-last 2))))

(defun $logistic_map (R x0 n)
  (labels ((lm0 (acc)
    (let ((step (length acc)))
      (if (eql step n) 
        (->maxima acc)
        (let ((x-last (cadar (last acc))))
          (lm0 (append acc `((,step ,(l-map R x-last))))))))))
   (lm0 `((0 ,x0)))))
(to-maxima)
R: 2$
x0: 0.2$
steps: 10$
data: logistic_map(R, x0, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
data: population_year_over_year(data)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 2$
x0: 0.8$
steps: 10$
data: logistic_map(R, x0, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 2.5$
x0: 0.2$
steps: 10$
data: logistic_map(R, x0, steps);
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 2.8$
x0: 0.2$
steps: 30$
data: logistic_map(R, x0, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 3.1$
x0: 0.2$
steps: 30$
data: logistic_map(R, x0, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 3.2$
x0: 0.2$
steps: 30$
data: logistic_map(R, x0, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 3.5$
x0: 0.2$
steps: 40$
data: logistic_map(R, x0, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 4$
x0: 0.2$
steps: 80$
data: logistic_map(R, x0, steps)$
plot2d([[discrete,data]], [x, 0, max_x(data)], [y, 0, max_y(data)])$
R: 4$
x01: 0.2$
x02: 0.2000001$
steps: 50$
data1: logistic_map(R, x01, steps)$
data2: logistic_map(R, x02, steps)$
plot2d([[discrete,data1],[discrete,data2]], [x, 0, max_x(data1)], [y, 0, max_y(data1)])$<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2186765857478465, 'fraction_numerical': 0.04306969459671104, 'mean_word_length': 4.885944700460829, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '571467', 'license': 'Apache-2.0', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2254, 'n_words': 556, 'path': 'notebooks/Section 2 - Population Growth.ipynb', 'repo_name': 'oubiwann/intro-to-complexity', 'size': '5108'}