% plot gamma pdfs:
xx = 0.0:0.1:10.0;
shapes = [ 1.0, 2.0, 3.0, 5.0];
cc = jet(length(shapes) );
for i=1:length(shapes)
    yy = gampdf(xx, shapes(i), 1.0);
    plot(xx, yy, '-', 'linewidth', 3, 'color', cc(i,:), ...
        'DisplayName', sprintf('s=%.0f', shapes(i)) );
    hold on;
end

% generate gamma distributed random numbers:
n = 50;
x = gamrnd(3.0, 1.0, n, 1);

% histogram:
[h,b] = hist(x, 15);
h = h/sum(h)/(b(2)-b(1));
bar(b, h, 1.0, 'DisplayName', 'data');

% maximum likelihood estimate:
p = mle(x, 'distribution', 'gamma');
yy = gampdf(xx, p(1), p(2));
plot(xx, yy, '-k', 'linewidth', 5, 'DisplayName', 'mle' );

hold off;
xlabel('x');
ylabel('pdf');
legend('show');
savefigpdf(gcf, 'mlepdffit.pdf', 12, 8)