implementing Nelder Mead method