1

如何为以下二分法代码设置最大迭代 = 20?我应该只做 feval 还是其他什么?

rf.bisection <- function(f, lower, upper, tol=1e-4) { 
flow <- f(lower) 
fupper <- f(upper) 
feval <- 2 

if (flow * fupper > 0) stop("The given interval does not contain the root!
\n") 
diff <- upper - lower 

while (abs(diff) > tol) { 
    newpoint <- (lower + upper) / 2 
    newf <- f(newpoint) 
    if (abs(newf) <= tol) break 
    if (flow * newf < 0) upper <- newpoint 
    if (fupper * newf < 0) lower <- newpoint 
    diff <- upper - lower 
    feval <- feval + 1 
} 
list(x = newx, value = newf, fevals=feval) 
} 
4

1 回答 1

2

将您的条件更改whilewhile (abs(diff) > tol & feval<22)。22 因为在第一次迭代feval中已经是 2。

于 2013-03-09T19:17:30.280 回答