vendredi 27 mars 2020

How to stop the counting process for the if loop in my code?

I want to stop the counting process for y after it first time meet the condition of y < 0 for each i in the for loop. Which means, the counting process for x will still continue as long as x > 0 (condition stated in the while loop). I had tried to do something which is if (y < 0 & (y - 120*(16/81)*time + (z-2)) > 0 & z > 2), it make sense for me but the result doesn't make sense because after I run the code, count_x is around 100 (make sense) but count_y is more than 1000 which doesn't make sense since the for loop is only from 1:1000. Any help will be greatly appreciated!

count_x <- 0
count_y <- 0

for (i in 1:1000){
  x <- 25
  y <- 25
  t <- 0
  while ((x > 0 | y > 0) & t < 100){
    time <- rexp(1,100)
    u <- runif(1,0,1)
    z <- 4/((1-u)^0.2) - 4
    if (z < 2){
      x <- x + 110*(65/81)*time - z
    } else {
      y <- y + 120*(16/81)*time - (z-2)
      x <- x + 110*(65/81)*time - 2
    }
    t <- t + time
    if (x < 0){
      count_x <- count_x + 1
    }
    if (y < 0 & (y - 120*(16/81)*time + (z-2)) > 0 & z > 2){
      count_y <- count_y + 1
    }    
  }
}

Aucun commentaire:

Enregistrer un commentaire