Algorithm To Optimize A Function