A fitting algorithm for optimizing ion implantation energies and doses