jeudi 29 novembre 2018

missing value in if condition

I created this function:

quadPol <- function(f,lb,rb,tau) {
  tetalower <- lb
  tetaupper <- rb
  tetabest <- 0.5 * (tetalower + tetaupper)
  tetanew <- 0.5 * (f(tetaupper) * (tetalower^2 - tetabest^2) + f(tetabest) *
                      (tetaupper^2 - tetalower^2) + f(tetalower) * (tetabest^2 -
                       tetaupper^2))/(f(tetaupper) * (tetalower - tetabest) 
                       + f(tetabest) * (tetaupper - tetalower) + f(tetalower) * 
                       (tetabest - tetaupper))
  while (abs(tetaupper - tetalower) >= tau * (abs(tetabest) + abs(tetanew))) {
    if (f(tetanew) < f(tetabest)) {
      z <- tetanew
      tetanew <- tetabest
      tetabest <- z
    }
    if (tetanew < tetabest) {
      tetalower <- tetanew
    }
    else {
      tetaupper <- tetanew
    }
    tetaaponew <- tetanew
    tetanew <- 0.5 * (f(tetaupper) * (tetalower^2 - tetabest^2) + f(tetabest) *
                      (tetaupper^2 - tetalower^2) + f(tetalower) * (tetabest^2 -
                       tetaupper^2))/(f(tetaupper) * (tetalower - tetabest) 
                       + f(tetabest) * (tetaupper - tetalower) + f(tetalower) * 
                      (tetabest - tetaupper))
    if (tetanew == tetaaponew || tetanew <= tetalower || tetanew >= tetaupper) {
      return(tetabest)
    }
  }
  return(tetabest)
}



quadPol(cos,2,4,0.00001)
quadPol(abs,-1,1,0.00001)

the first execution with the cosinus works well, but the second doesn't, and it says:

Error in if (tetanew == tetaaponew || tetanew <= tetalower || tetanew >=  : 
  missing value where TRUE/FALSE needed
enter code here

how is it possible that the value is missing there. Looked over it like ten times. Thank you.

Aucun commentaire:

Enregistrer un commentaire