自己写程序求定积分:
area <- function(f, a, b, eps = 1.0e-06, lim = 10) { #### f是被积函数;a,b是积分端点;eps是积分精度要求;lim见下
fun1 <- function(f, a, b, fa, fb, a0, eps, lim, fun) {
d <- (a + b)/2; h <- (b - a)/4; fd <- f(d)
a1 <- h * (fa + fd); a2 <- h * (fd + fb)
if(abs(a0 - a1 - a2) < eps || lim == 0)
return(a1 + a2)
else {
return(fun(f, a, d, fa, fd, a1, eps, lim - 1, fun)
+ fun(f, d, b, fd, fb, a2, eps, lim - 1, fun))
}
}
fa <- f(a); fb <- f(b); a0 <- ((fa + fb) * (b - a))/2
fun1(f, a, b, fa, fb, a0, eps, lim, fun1)
}
#### 是对分区间的上限,默认值为10,即被积分区间最多被等分为2的10次方个子区间。
使用实例:
f <- function(x) 1/x
quad<-area(f,1,5)
|