-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
92 lines (73 loc) · 2.09 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# chaos
Attempting to recreate some of the views I found in this interesting article:
[Geoff Boeing - Chaos Theory and the Logistic Map](https://geoffboeing.com/2015/03/chaos-theory-logistic-map)
## System Behavior and Attractors
As with most things, the tidyverse will make this easier to read and more elegant to code
```{r, warning=FALSE, message=FALSE}
library(tidyverse)
```
Define the logistic map
```{r}
f <- function(x0, r) r*x0*(1-x0)
```
Define a function that iterates the logistic map for a specified number of generations
```{r}
f2 <- function(x0, r, g = 10){
out <- vector(mode="double", length = g)
out[1] <- x0
for(i in 2:g){
o <- f(x0=x0, r=r)
x0 <- o
out[i] <- o
}
out
}
```
Visualise the attractors for different growth rates
```{r}
map2_dfr(seq(0.5, 3.5, by=0.5), 20,
~tibble(rate=as.character(.x),
value=f2(r=.x, x0=0.5, g=.y),
gen=1:.y)) %>%
ggplot(aes(gen, value, col=rate)) +
geom_line()+
labs(title="Logistic model results for different growth rates",
x = "Generation",
y = "Population",
col = "Growth rate")
```
## Bifurcations and the Path to Chaos
Repeat the experiment above but for 10000 growth rates from 0 - 4 with each experiment lasting 200 generations
```{r cache=TRUE}
bi_data <-
map2_dfr(seq(0, 4, length.out = 10000), 200,
~tibble(rate = .x,
value = f2(r=.x, x0=0.5, g=.y),
gen = 1:.y))
```
Throw away the first 100 rows per experiment and visualise the unique attractors
```{r cache=TRUE}
bi_data_attractors <-
bi_data %>%
group_by(rate) %>%
filter(row_number() > 100) %>%
distinct(rate, value)
bi_data_attractors %>%
ggplot(aes(rate, value))+
geom_point(size=0.1)
```
Zoom in
```{r cache=TRUE, message=FALSE}
last_plot() + scale_x_continuous(limits = c(2.8, 4))
last_plot() + scale_x_continuous(limits = c(3.7, 3.9))
```