function [ c_x, c_y, c_n ] = histogram_to_cdf ( b_p, b_l, b_r, b_n )

%*****************************************************************************80
%
%% histogram_to_cdf() creates a discrete CDF from a histogram.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 August 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer B_P(B_N), the normalized PDF values associated with the 
%    histogram bins.
%
%    integer B_L(B_N), B_R(B_N), the left and right points associated 
%    with each bin.
%
%    integer B_N, the number of bins.
%
%  Output:
%
%    real C_X(C_N), the points at which the CDF is defined.
%
%    real C_Y(C_N), the values of the CDF.
%
%    integer C_N, the number of points at which the CDF is defined.
%

%
%  Integral PDF = 1, 
%  so we want Sum B_P(I) * ( B_R(I) - B_L(I) ) = 1,
%  but this means we need to compute the current sum and then
%  normalize the B_P values.
%
  s = b_p(1:b_n)' * ( b_r(1:b_n) - b_l(1:b_n) ); 
  b_p(1:b_n) = b_p(1:b_n) / s;

  c_n = b_n + 1;

  c_x = [ b_l ; b_r(b_n) ];

  c_y = zeros ( c_n, 1 );

  c_y(1) = 0.0;
  for i = 2 : c_n
    c_y(i) = c_y(i-1) + b_p(i-1) * ( b_r(i-1) - b_l(i-1) );
  end

  c_y(c_n) = 1.0;

  return
end

