minimization with a negative cost function: works in MATLAB, not in Python
The problem is with division on python 2.x.  In python 2.x, division involving two integers produces an integer result.  So 1/2==0.  Python 3.x does not have this problem, 1/2==.5.

There are two ways to avoid this.  First, you can always convert one value you a float.  So 1./2==0.5 and 1/2.==0.5.  However, you have to remember to do this everywhere, and if you forget it can lead to hard-to-find errors.

The more reliable method is to always put this at the top of your code: from __future__ import division.  This will switch python 2.x to the python 3 behavior, so 1/2==.5.  In python 3.x it does nothing, so it also makes your code python 3.x compatible in this regard.