forked from gbonan/bonanmodeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
268 lines (268 loc) · 7.28 KB
/
.Rhistory
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Parameters for beta distribution
p <- c(2.5, 3.5, 11.5)
q <- c(2.5, 2.0, 3.5)
# Canopy parameters
LAI <- 5
hc <- 10
cat("Leaf area index =", LAI, "\n")
# Create a vector of heights (z) with linearly spaced values
z_min <- 0
z_max <- hc
dz <- 0.1
z <- seq(z_min, z_max, by = dz)
# Initialize lists to store leaf area density profiles
y1 <- numeric(length(z))
y2 <- numeric(length(z))
y3 <- numeric(length(z))
# Calculate the leaf area density profile for each [p,q]
for (i in 1:length(p)) {
sum <- 0
# Loop over each height
for (j in 1:length(z)) {
x <- z[j] / hc
lad <- (LAI / hc) * (x^(p[i]-1) * (1 - x)^(q[i]-1)) / beta(p[i], q[i])
# Numerically sum leaf area for each height
sum <- sum + lad * dz
# Save output for graphing
if (i == 1) {
y1[j] <- lad
} else if (i == 2) {
y2[j] <- lad
} else if (i == 3) {
y3[j] <- lad
}
}
cat("p, q =", p[i], q[i], "\n")
cat("Leaf area index (numerical) =", sum, "\n")
}
# Make a graph for leaf area density in relation to relative height (z/hc)
z_rel <- z / hc
plot(y1, z_rel, type = 'l', col = 'blue', xlab = 'Leaf area density (m^2 m^{-3})',
ylab = 'Height (z/h_c)', main = 'Profiles')
lines(y2, z_rel, col = 'red')
lines(y3, z_rel, col = 'green')
legend('southeast', legend = c('p,q = 2.5,2.5', 'p,q = 3.5,2.0', 'p,q = 11.5,3.5'),
col = c('blue', 'red', 'green'), lty = 1)
z_rel
z
z_min
z_max
# Parameters for beta distribution
p <- c(2.5, 3.5, 11.5)
q <- c(2.5, 2.0, 3.5)
# Canopy parameters
LAI <- 5
hc <- 20
cat("Leaf area index =", LAI, "\n")
# Create a vector of heights (z) with linearly spaced values
z_min <- 0
z_max <- hc
dz <- 0.1
z <- seq(z_min, z_max, by = dz)
# Initialize lists to store leaf area density profiles
y1 <- numeric(length(z))
y2 <- numeric(length(z))
y3 <- numeric(length(z))
# Calculate the leaf area density profile for each [p,q]
for (i in 1:length(p)) {
sum <- 0
# Loop over each height
for (j in 1:length(z)) {
x <- z[j] / hc
lad <- (LAI / hc) * (x^(p[i]-1) * (1 - x)^(q[i]-1)) / beta(p[i], q[i])
# Numerically sum leaf area for each height
sum <- sum + lad * dz
# Save output for graphing
if (i == 1) {
y1[j] <- lad
} else if (i == 2) {
y2[j] <- lad
} else if (i == 3) {
y3[j] <- lad
}
}
cat("p, q =", p[i], q[i], "\n")
cat("Leaf area index (numerical) =", sum, "\n")
}
# Make a graph for leaf area density in relation to relative height (z/hc)
z_rel <- z / hc
plot(y1, z_rel, type = 'l', col = 'blue', xlab = 'Leaf area density (m^2 m^{-3})',
ylab = 'Height (z/h_c)', main = 'Profiles')
lines(y2, z_rel, col = 'red')
lines(y3, z_rel, col = 'green')
legend('southeast', legend = c('p,q = 2.5,2.5', 'p,q = 3.5,2.0', 'p,q = 11.5,3.5'),
col = c('blue', 'red', 'green'), lty = 1)
z_rel
z
hc
plot(y1, z_rel, type = 'l', col = 'blue', xlab = 'Leaf area density (m^2 m^{-3})',
ylab = 'Height (z/h_c)', main = 'Profiles')
lines(y2, z_rel, col = 'red')
lines(y3, z_rel, col = 'green')
legend('southeast', legend = c('p,q = 2.5,2.5', 'p,q = 3.5,2.0', 'p,q = 11.5,3.5'),
col = c('blue', 'red', 'green'), lty = 1)
leaf <- 'Planophile'
switch(leaf,
'Planophile' = {
lad_ave <- 26.76 * (pi/180)
lad_std <- 18.5068 * (pi/180)
},
'Erectophile' = {
lad_ave <- 63.24 * (pi/180)
lad_std <- 18.4960 * (pi/180)
},
'Plagiophile' = {
lad_ave <- 45.00 * (pi/180)
lad_std <- 16.2681 * (pi/180)
},
'Uniform' = {
lad_ave <- 45.00 * (pi/180)
lad_std <- 25.9808 * (pi/180)
},
'Spherical' = {
lad_ave <- 57.30 * (pi/180)
lad_std <- 21.5485 * (pi/180)
}
)
# Convert these to the p,q parameters for the beta distribution
num <- 1 - (lad_std*lad_std + lad_ave*lad_ave) / (lad_ave * pi / 2)
den <- (lad_std*lad_std + lad_ave*lad_ave) / (lad_ave*lad_ave) - 1
p <- num / den
q <- ((pi/2) / lad_ave - 1) * p
# Calculate leaf inclination angle probability density function (PDF) and
# fractional abundance (lad) for 9 10-degree bins
dangle <- 10 * (pi/180) # Leaf inclination angle increment (radians)
angle <- c(5, 15, 25, 35, 45, 55, 65, 75, 85) # Leaf inclination angle (degrees)
angle <- angle * (pi/180) # degrees -> radians
# Initialize vectors to store PDF and lad
beta_pdf <- numeric(length(angle))
beta_lad <- numeric(length(angle))
# Loop through each angle
for (i in 1:length(angle)) {
x <- angle[i] / (pi/2)
fp <- x ^ (p - 1)
fq <- (1 - x) ^ (q - 1)
beta_pdf[i] <- 2 / pi * fp * fq / beta(p, q) # Leaf angle PDF
beta_lad[i] <- beta_pdf[i] * dangle # Fraction of leaves in this angle bin
}
# Calculate the known solution
exact_pdf <- numeric(length(angle))
exact_lad <- numeric(length(angle))
# Loop through each angle
for (i in 1:length(angle)) {
# Exact leaf angle probability density function
switch(leaf,
'Planophile' = {
exact_pdf[i] <- 2 / pi * (1 + cos(2 * angle[i]))
},
'Erectophile' = {
exact_pdf[i] <- 2 / pi * (1 - cos(2 * angle[i]))
},
'Plagiophile' = {
exact_pdf[i] <- 2 / pi * (1 - cos(4 * angle[i]))
},
'Uniform' = {
exact_pdf[i] <- 2 / pi
},
'Spherical' = {
exact_pdf[i] <- sin(angle[i])
}
)
# Exact relative leaf angle distribution (fraction)
exact_lad[i] <- exact_pdf[i] * dangle
}
# Print out fractional abundance and compare with known solution
beta_sum <- sum(beta_lad)
beta_ave <- sum(angle * beta_lad)
exact_sum <- sum(exact_lad)
exact_ave <- sum(angle * exact_lad)
cat("\n")
cat("Leaf type =", leaf, "\n")
cat(" Angle beta exact \n")
for (i in 1:length(angle)) {
cat(sprintf('%10.2f %15.4f %15.4f \n', angle[i]*180/pi, beta_lad[i], exact_lad[i]))
}
cat("\n")
cat("beta distribution \n")
cat("Sum of leaf angle distribution =", beta_sum, "\n")
cat("Mean leaf angle =", beta_ave*180/pi, "\n")
cat("\n")
cat("Exact solution \n")
cat("Sum of leaf angle distribution =", exact_sum, "\n")
cat("Mean leaf angle =", exact_ave*180/pi, "\n")
# Analytical mean leaf angle
fx <- function(x) {
switch(leaf,
'Planophile' = {
x * 2 / pi * (1 + cos(2 * x))
},
'Erectophile' = {
x * 2 / pi * (1 - cos(2 * x))
},
'Plagiophile' = {
x * 2 / pi * (1 - cos(4 * x))
},
'Uniform' = {
x * 2 / pi
},
'Spherical' = {
x * sin(x)
}
)
}
analytical_ave <- integrate(fx, 0, pi/2)$value
cat("\n")
cat("Analytical solution \n")
cat("Mean leaf angle =", analytical_ave*180/pi, "\n")
# Calculate Ross index
F1 <- sum(beta_lad[1:3])
F2 <- sum(beta_lad[4:6])
F3 <- sum(beta_lad[7:9])
beta_xl <- 0.5 * (abs(0.134 - F1) + abs(0.366 - F2) + abs(0.5 - F3))
if ((0.5 - F3) < 0) {
beta_xl <- -beta_xl
}
F1 <- sum(exact_lad[1:3])
F2 <- sum(exact_lad[4:6])
F3 <- sum(exact_lad[7:9])
exact_xl <- 0.5 * (abs(0.134 - F1) + abs(0.366 - F2) + abs(0.5 - F3))
if ((0.5 - F3) < 0) {
exact_xl <- -exact_xl
}
cat("\n")
cat("Ross index \n")
cat("beta distribution =", beta_xl, "\n")
cat("Exact solution =", exact_xl, "\n")
# Graph PDFs
x <- seq(0, pi/2, length.out = 101)
switch(leaf,
'Planophile' = {
y <- 2 / pi * (1 + cos(2 * x))
},
'Erectophile' = {
y <- 2 / pi * (1 - cos(2 * x))
},
'Plagiophile' = {
y <- 2 / pi * (1 - cos(4 * x))
},
'Uniform' = {
y <- 2 / pi
},
'Spherical' = {
y <- sin(x)
}
)
x <- x * (180/pi) # radians -> degrees
angle <- angle * (180/pi) # radians -> degrees
plot(angle, beta_pdf, type = 'l', col = 'blue', xlab = 'Leaf angle (degrees)',
ylab = 'PDF', main = leaf)
lines(angle, exact_pdf, col = 'red', lty = 2)
lines(x, y, col = 'green', lty = 3)
legend('best', legend = c('beta', 'exact', 'analytical'), col = c('blue', 'red', 'green'), lty = c(1, 2, 3))
x
y
angle
x
plot(angle, beta_pdf, type = 'l', col = 'blue', xlab = 'Leaf angle (degrees)',
ylab = 'PDF', main = leaf)
angle <- angle * (180/pi) # radians -> degrees