0

我将代码添加list(Root = xnew, Value = f(xnew), Iterations=i)到以下函数的末尾以将 Root、Value 和 Iterations 全部打印出来,但我只得到一个值,即xnew. 我该如何纠正这个问题?

fixedpoint <- function(fun, x0, tol=1e-08, max.iter=40){
    xold <- x0
    xnew <- fun(xold)
    for (i in 1:max.iter) {
    xold <- xnew
    xnew <- f(xold)
    if ( abs((xnew-xold)) < tol )
        return(xnew)
    }
    stop("max iterations = 20")
}
4

1 回答 1

0

答案转到 N8TRO。

您应该将fixedpoint返回值连接到list. 请看下图:

fixedpoint <- function(fun, x0, tol = 1e-08, max.iter = 40) {
  xold <- x0
  xnew <- fun(xold)
  for (i in 1:max.iter) {
    xold <- xnew
    xnew <- f(xold)
    if (abs(xnew-xold) < tol) {
        return(list(Root = xnew, Value = f(xnew), Iterations = i))
  }
  stop("max iterations = 20")
}
于 2018-09-17T21:05:14.490 回答