Skip to content

Commit 1d2ca15

Browse files
Erennn7Copilot
andauthored
Update mathematics/simpson_rule.r
Co-authored-by: Copilot <[email protected]>
1 parent 1817c7e commit 1d2ca15

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

mathematics/simpson_rule.r

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ simpson_rule_vectorized <- function(f, a, b, n) {
9393
x <- seq(a, b, by = h)
9494
y <- f(x)
9595

96-
# Create coefficient vector: [1, 4, 2, 4, 2, ..., 4, 1]
97-
coefficients <- rep(c(4, 2), length.out = n - 1)
98-
coefficients <- c(1, coefficients, 1)
96+
# Create coefficient vector: [1, 4, 2, 4, 2, ..., 2, 4, 1]
97+
coefficients <- rep(2, n + 1)
98+
coefficients[1] <- 1
99+
coefficients[n + 1] <- 1
100+
coefficients[seq(2, n, by = 2)] <- 4
99101

100102
result <- sum(coefficients * y) * h / 3
101103
return(result)

0 commit comments

Comments
 (0)