Simulating Gamma distribution MATLAB

41 Views Asked by At

I am a beginner in Matlab and I need an explanation for this. I am trying to generate random variables that are gamma distributed and compare them with the output of gamcdf.

% inline function: gamma distributed random variables 
generated by the sum of exponentials
randGamma = inline('-sum(log(rand(1,n)))/a', 'n', 'a');\
for a = [0.2, 1.5]
x = randGamma(n,a);
t = linspace(0, 5/a, 500);
figure
plot(sort(x), (1:n)/n, 'rx'), hold on
plot(t, gamcdf(t,a), 'k')
axis([0, max(t), 0, 1.1])
title(['Gammadistribution a = ', num2str(a)])\
end

in the figures I get x is not showing up. I actually have a doubt in

plot(sort(x), (1:n)/n, 'rx'), hold on.

Can anyone please help me with that. I will have to do such an implementation with other distributions. So understanding this one will help me a lot. Thank you in advance