forked from WilliamMajanja-zz/Partial-Realpart-Analysis-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResursionBlackMagic.R
27 lines (27 loc) · 914 Bytes
/
ResursionBlackMagic.R
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
## Better programming style -- renaming the function will have no effect.
> ## The use of "Recall" as in V+R is VERY black magic. You can get the
> ## same effect transparently by supplying a wrapper function.
> ## This is the approved Abelson+Sussman method.
>
> area <- function(f, a, b, ..., limit=10, eps=1e-5) {
+ area2 <- function(f, a, b, ..., fa = f(a, ...), fb = f(b, ...),
+ limit = limit, eps = eps) {
+ h <- b - a
+ d <- (a + b)/2
+ fd <- f(d, ...)
+ a1 <- ((fa + fb) * h)/2
+ a2 <- ((fa + 4 * fd + fb) * h)/6
+ if(abs(a1 - a2) < eps)
+ return(a2)
+ if(limit == 0) {
+ warning(paste("iteration limit reached near x =", d))
+ return(a2)
+ }
+ area2(f, a, d, ..., fa = fa, fb = fd, limit = limit - 1,
+ eps = eps) + area2(f, d, b, ..., fa = fd, fb =
+ fb, limit = limit - 1, eps = eps)
+ }
+ area2(f, a, b, ..., limit=limit, eps=eps)
+ }
>
>