title: Finding the root of an equation with a constraint
body: <p>In python, I would like to find the roots of equations of the form:</p>
  
  <p>-x*log(x) + (1-x)*log(n) - (1-x)*log(1 - x) - k = 0</p>
  
  <p>where n and k are parameters that will be specified.</p>
  
  <p>An additional constraint on the roots is that x >= (1-x)/n. So just for what it's worth, I'll be filtering out roots that don't satisfy that.</p>
  
  <p>My first attempt was to use scipy.optimize.fsolve (note that I'm just setting k and n to be 0 and 1 respectively):</p>
  
  <pre><code>def f(x):                                                                       
      return -x*log(x) + (1-x)*log(1) - (1-x)*log(1-x)                            
  
  fsolve(f, 1)
  </code></pre>
  
  <p>Using math.log, I got value-errors because I was supplying bad input to log. Using numpy.log gave me some divide by zeros and invalid values in multiply.</p>
  
  <p>I adjusted f as so, just to see what it would do:</p>
  
  <pre><code>def f(x):                                                                       
      if x &lt;= 0:                                                                  
          return 1000                                                             
      if x &gt;= 1:                                                                  
          return 2000                                                             
      return -x*log(x) + (1-x)*log(1) - (1-x)*log(1-x) 
  </code></pre>
  
  <p>Now I get</p>
  
  <pre><code>/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py:221: RuntimeWarning: The iteration is not making good progress, as measured by the 
    improvement from the last ten iterations.
    warnings.warn(msg, RuntimeWarning)     
  </code></pre>
  
  <p>Using python, how can I solve for x for various n and k parameters in the original equation?</p>