function pdf=dfgamma(lamda, r, samples) % dfgamma(lamda, r, samples) Generates a gamma pdf with parameter 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 r<=0 error('invalid value for r') end V=sqrt(r)/lamda; X=5*V; while lamda/gamma(r)*((lamda*X)^(r-1))*exp(-lamda*X) > 1e-6 X=X+V; end x=[0:X/samples:X]; p=(lamda/gamma(r)).*((lamda*x).^(r-1)).*exp(-lamda*x); pdf=[0 1; x' p']; if ispf(pdf)==0 p=p./ttlprob(pdf); pdf=[0 1; x' p']; end