# Optimal approximation of nonlinear probability density function by piecewise constant density

Given a nonlinear probability density function F, the problem is to estimate F using histogram over a partition with N intervals.

I have tried to realise this with MATLAB function fmincon, but it seems to always generate local minimum. The optimisation function is the minimisation of the difference between F and the estimated result. As can be seen from the following figure, the estimation cannot be accepted.

How can the optimal result be obtained?