0

I want to plot Cumulative distribution function (CDF) of a set of values, Z using MATLAB. When I use the following code, the CDF would not be between 0 and 1! I would appreciate if you could help me.

function plot_cdf(Z)    
numberbin=100;
[n,x] =hist(Z,numberbin);
pdf=n/sum(n(:))/diff(x(1:2));
cdf = cumsum(pdf);
plot(cdf)    
end
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Alex
  • 573
  • 1
  • 10
  • 23

1 Answers1

0

You need

cdf = cumsum(pdf) * diff(x([1 2]));

This is because the cdf is the integral of the pdf. To compute the integral, you approximate it as a Riemann sum: sum the function values (cumsum(pdf)) and multiply by their separation along the x axis (diff(x([1 2]))).

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147