user3775495
user3775495

Reputation: 41

Julia while loop going too far and giving error message

I'm new to julia and trying to make a simple script to simulate population growth. So at each time-step the population grows as follows N(t+1)=N(t)(1+beta). So at each time step I sample from a poisson distribution with mean given by N(t+1). I would like to stop when either N reaches a maximum value or reaches 0. I've implemented this in Julia but the population often goes further than the maximum value i define. Additionally any time the N->0 i get an error message : ErrorException("lambda must be positive").

using Distributions
function new_pop(N)
        beta=0.1
        w_fit=1
        rand(Poisson(N*(1+w_fit*beta)))
        end

pop_S=10
pop_Max=100
while (pop_S<pop_Max | pop_S>0)
        pop_S=new_pop(pop_S)
        println(pop_S)
        end

Upvotes: 1

Views: 193

Answers (1)

John Myles White
John Myles White

Reputation: 2929

I think you might want || rather than |. A single bar does bitwise OR, whereas two bars is logical OR.

Upvotes: 1

Related Questions