即使向量中存在元素,is.element()也不会返回true

时间:2019-09-06 15:36:16

标签: r

我遇到一种奇怪的错误,其中向量中存在一个元素,但它是.element()返回false。

在Rstudio版本1.2.1335上运行的简单R代码。

print_t <- c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)

euler <- function(f, y0, a, b, h)
{
  t <- a
  y <- y0

  while(t <= b){
    t <- t + h
    y <- y + h*f(t, y)
    boo = is.element(t, print_t)
    print(boo)
    cat(sprintf("%6.3f\n", t))
    if(boo){
      print(t)
    }
  }
}

exact_value <- function(t)
  return (2*exp(t) - t - 1)

System_model <- function(y, t)
  return (y+t)
euler(System_model, 1, 0, 1, 0.1)

# euler(System_model, 1, 0, 1, 0.01)

实际结果:

[1] TRUE
 0.100
[1] 0.1
[1] TRUE
 0.200
[1] 0.2
[1] FALSE
 0.300
[1] TRUE
 0.400
[1] 0.4
[1] TRUE
 0.500
[1] 0.5
[1] TRUE
 0.600
[1] 0.6
[1] TRUE
 0.700
[1] 0.7
[1] FALSE
 0.800
[1] FALSE
 0.900
[1] FALSE
 1.000
[1] FALSE
 1.100

预期: 一切都必须真实

编辑: 这个问题被标记为this question的重复项,但是即使在没有正确答案的情况下如何找到元素是否在向量中的答案中也没有提及。

0 个答案:

没有答案