Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二分法返回“0”;缺少需要true false的值";,can';t复制错误_R - Fatal编程技术网

二分法返回“0”;缺少需要true false的值";,can';t复制错误

二分法返回“0”;缺少需要true false的值";,can';t复制错误,r,R,在我的二等分算法中,它说有一个“需要真/假的缺失值”,它指向这一行: if (sign(f(c)) == sign(f(a)) ) { a <- c } 编辑:完整代码为 Bisection <- function(f, a,b, tol = 0.005, maxiter = 1000) { i <- 1 while (i < maxiter) { c <- (a+b)/2 if (f(c) == 0 | (b-a)/2 < tol) { retur

在我的二等分算法中,它说有一个“需要真/假的缺失值”,它指向这一行:

if (sign(f(c)) == sign(f(a))  ) {
  a <- c

}
编辑:完整代码为

Bisection <- function(f, a,b, tol = 0.005, maxiter = 1000) {
i <- 1
while (i < maxiter) {
c <- (a+b)/2
if (f(c) == 0 | (b-a)/2 < tol) { return(c)}
i <- i + 1
if (sign(f(c)) == sign(f(a))  ) {
  a <- c

}
else {b <- c}}
return(NA)

Bisection一般来说,
缺少值,其中需要TRUE/FALSE
意味着R遇到了NA值-在这种情况下,我希望
a
c
或任何
f()
返回的值中都有NA值


可能是当您手动运行这些行时,存储在全局环境中的
a
c
的值很好(不丢失),但是传递到函数中(或在其中计算)的任何值都包含丢失的值。我建议您检查一下。

在您的问题中添加
f
c
a
。是否将
f
c
a
所有参数都传递到您的函数中?您如何调用该函数?
Bisection <- function(f, a,b, tol = 0.005, maxiter = 1000) {
i <- 1
while (i < maxiter) {
c <- (a+b)/2
if (f(c) == 0 | (b-a)/2 < tol) { return(c)}
i <- i + 1
if (sign(f(c)) == sign(f(a))  ) {
  a <- c

}
else {b <- c}}
return(NA)