pythonsympy

Solve a transcendental equation with sympy


I have trouble solving the following simple equation with sympy. Sympy seems to take way too much time to produce a result.

import sympy as sp
sp.solve(sp.Eq(1000/(sp.exp(log(39) - 1000*k) + 1), 100), k)

The solution is log(39)/1000 - log(3)/500, which can be verified by plugging that into the equation.

What can one do to "help" sympy to solve this equation for k?


Solution

  • I think solve is attempting to solve this as a polynomial equation of degree 1000 in exp(k) which is apparently slow.

    You can use solveset instead:

    In [16]: eq
    Out[16]: 
         1000            
    ─────────────── = 100
            -1000⋅k      
    1 + 39⋅ℯ             
    
    In [17]: solveset(eq, k)
    Out[17]: 
    ⎧n⋅ⅈ⋅π   log(13/3) │      ⎫   ⎧ⅈ⋅(2⋅n⋅π + π)   log(39) │      ⎫
    ⎨───── + ───────── │ n ∊ ℤ⎬ \ ⎨───────────── + ─────── │ n ∊ ℤ⎬
    ⎩ 500      1000    │      ⎭   ⎩    1000         1000   │      ⎭
    
    In [18]: solveset(eq, k, Reals)
    Out[18]: 
    ⎧-log(3/13) ⎫
    ⎨───────────⎬
    ⎩   1000    ⎭