solve() execution aborted

6.5k views Asked by At

I'm having trouble using Sympy to solve an equation. When I run the code, for example:

print(correction(10))

I expect it to print a number which is f. Instead it gives me ERROR: execution aborted.

def correction(r):

    from sympy import cosh, log, exp, symbols, solve
    f = symbols('f')

    def equation():
        return cosh(((r - 1.0)/(r + 1.0))*(log(2.0)/f)) - 0.5*exp(log(2.0)/f)

    correction = solve(equation(),f)
    return correction

What is the problem?

1

There are 1 answers

4
Stelios On BEST ANSWER

Your equation is highly non-linear and my guess is that a closed-form solution cannot be found. That's why sympy.solve fails. The only option you have is to solve the equation numerically. Sympy offers the nsolve function for this purpose, which, as typical in numerical solvers, requires an estimate of the solution.

import sympy as sp
r, f = sp.symbols('r, f')
expr = sp.cosh(((r - 1)/(r + 1))*(sp.log(2)/f)) - sp.Rational(1,2)*sp.exp(sp.log(2)/f)

sol = sp.nsolve(expr.subs({r:10}), f, 0.5)
print(sol)

0.699259455239414