[R-es] Error in while (diff > 1e-08)

Javier Marcuzzi j@v|er@ruben@m@rcuzz| @end|ng |rom gm@||@com
Dom Feb 2 15:09:03 CET 2025


Estimados

Copio y pego un código, del cuál al ejecutar podrán reproducir el error.

La primer parte es una función, cuándo a mano coloco números próximos al resultado no hay problema, pero claro, ¿si no tengo ni idea del resultado que deseo calcular?

Entonces genero 22 valores, como así también dos versiones para w (donde una de estas es todo 1)

Hasta ahí no hay problema, pueden correr el código que comparto.

El problema es al correr el código, en distinto número de interacciones aparece el error:
#Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed

Pero en la descripción de la función este error no está, y comienza a andar hasta varios ciclos donde aparece y corta.

¿Alguna idea? Gracias 

Javier Marcuzzi

## Given the MME, iteratively solve for the solutions by Jocobi Over Relaxation.

## Arguments
## LHS: left hand side of MME
## RHS: right hand side of MME
## inits: a vecotr of initial values for the solutions
## w: a vector of relaxation factor
## disp: a logical value. If true, show the solutions at each iteration

## Note: When 'w' is all one, usual Jacobi method will be performed.
jor <-
  function(LHS, RHS, inits, w, disp) {
    D <- diag(diag(LHS))
    R <- LHS
    diag(R) <- 0
    w <- diag(w)
    Dinv <- solve(D) %*% w
    x <- matrix(inits)
    I <- diag(1, dim(w)[1])

    diff <- 1
    i <- 0
    while (diff > 10E-9) {
      i <- i + 1
      newx <- (I - w) %*% x + Dinv %*% (RHS - R %*% x)
      if (disp == TRUE) {
        cat("\n")
        cat("iteration ", i, "\n")
        print(x)
      }
      diff <- (sum((newx - x)^2)) / sum(newx^2)
      x <- newx
    }

    cat("\n")
    cat("Final solutions after", i, "th iteration")
    return(x)
  }


# genero 22 números aleatorios
aleatorios <- sample(0:1,
  size = 22,
  replace = TRUE, prob = NULL
)
inits <- aleatorios

w <- c(rep(0.8, 22))
w1 <- c(rep(1, 22))

# -----------------------------------------
# primer prueba - uso w
jor(LHS, RHS, inits, w, disp = TRUE)
#iteration  1010
#Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed


# -----------------------------------------
# segunda prueba - uso w1
# iteration  499
#Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed 
jor(LHS, RHS, inits, w1, disp = TRUE)


	[[alternative HTML version deleted]]



Más información sobre la lista de distribución R-help-es