MATLAB: Why the cdf does not match cumsum(pdf)

cdfpdfrayleigh

x=1D array of 100 discrete values that I calculate Rayleigh pdf and cdf as below
pdfrayl = 2 * x .* exp(-x .^ 2);
cdfrayl = 1 - exp(-x .^ 2);
Now I plot following two lines:
plot(x, cdfrayl)
hold on;
plot(x, cumsum(pdfrayl )/ sum(pdfrayl))
I expected them to match exactly, but they don't. Can anybody please explain why they don't match?

Best Answer

It does not appear to me that you are approximating the Riemann sum of the integral of the PDF correctly here.
x = 0:0.01:10;
y = x/4.*exp(-x.^2/8);
% \Delta x for the Riemann sum
dx = 10/length(x);
yc = cumsum(y).*dx;
yct = 1-exp(-x.^2/8);
plot(x,yc,'r-.','linewidth',2);
hold on;
plot(x,yct,'b');
legend('Approximation of CDF','True CDF', ...
'Location','SouthEast');
Related Question