-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPractice 6.2 2.0.Rmd
92 lines (66 loc) · 2.25 KB
/
Practice 6.2 2.0.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
---
title: "Practice 6.2 2.0"
author: "rpp"
date: "2023-10-26"
output: html_document
---
I believe your problem is in the equation, I read the following in both equations:
$$N1_{t+1}= N1_{t}+\left[growth.rate*N1_{t}*\left(K1-\frac{N1_{t}-\alpha_{12}N2_{t}}{K1}\right)\right]$$
```{r setup}
library(animation)
library(ggplot2)
library (gganimate)
growhw <- function(growth.rateN1,growth.rateN2, plot_N1 = FALSE, plot_N2 = FALSE) {
num_gen <- 30
N1 <- rep(0, num_gen)
N2 <- rep(0, num_gen)
generation <- 1:num_gen
N1[1] <- 1
N2[1] <- 1
K1 <- 100
K2 <- 120
a12 <- 1.5
a21 <- 1.5
for (i in 2:num_gen) {
# N[i] = N[i-1]+ (growth.rate*N[i-1]*((100-N[i-1])/100))
N1[i] = N1[i - 1] + (growth.rateN1 * N1[i - 1] * (100 - (N1[i - 1] - a12 * N2[i - 1]) / 100))
N2[i] = N2[i - 1] + (growth.rateN2 * N2[i - 1] * (120 - (N2[i - 1] - a21 * N2[i - 1]) / 120))
}
# Set an appropriate y-axis limit range, base on the K value
ylim_range <- c(0, 150)
if (plot_N1) {
plot(generation, N1, xlim=c(0,30), ylim=c(0,150), type = 'b',
main = paste("Rate =", growth.rateN1, "- N1"), xlab = "Generation", ylab = "N1")
}
if (plot_N2) {
plot(generation, N2, xlim=c(0,30), ylim=c(0,150), type = 'b',
main = paste("Rate =", growth.rateN2, "- N2"), xlab = "Generation", ylab = "N2")
lines(generation, N2, col = "red")
}
}
#plot Species 1 alone, but the population didn't reach saturation
growhw(0.003,0, plot_N1 = TRUE)
text(8,140,"Species 1 alone")
#plot Species 2 alone, but the population didn't reach saturation
growhw(0,0.003, plot_N2 = TRUE)
text(8,140,"Species 2 alone")
#plot Both Species competing, but the plot only show one line
#and also the population didn't reach saturation,
growhw(0.005,0.005, plot_N1 = TRUE,plot_N2 = TRUE)
text(8,140,"Both Species competing")
saveGIF({
for (i in seq(0.01, 0.5, by = 0.01)){
growhw(i,0, plot_N1 = TRUE) # 生成 N1 的 GIF
}
}, movie.name = "N1.gif")
saveGIF({
for (i in seq(0.1, 0.5, by = 0.01)){
growhw(0,i, plot_N2 = TRUE) # 生成 N2 的 GIF
}
}, movie.name = "N2.gif")
#two lines didn't show at the same time
saveGIF({
for (i in seq(0.1, 0.5, by = 0.01)){
growhw(i,i, plot_N1 = TRUE, plot_N2 = TRUE)
}
}, movie.name = "N1_and_N2.gif")