function pdf=dferlan(lamda, r, samples) % dferlan(lamda ,r, samples) Generates an Erlang pdf with parameters lamda % and r. samples sets the size of the numerical representation, and is % optional. The default sample size is 1000. if nargin==2 samples=1000; end if lamda<=0 error('invalid value for lamda') end if r<=0 error('invalid value for r') end V=sqrt(r)/lamda; X=5*V; while (lamda^r)*(X^(r-1))*exp(-lamda*X)/fact(r-1) > 1e-6 X=X+V; end x=[0:X/samples:X]; p=(lamda.^r).*(x.^(r-1)).*exp(-lamda.*x)./fact(r-1); pdf=[0 1; x' p']; if ispf(pdf)==0 p=p./ttlprob(pdf); pdf=[0 1; x' p']; end