I have 4 input variables (floats):
- Xmax
- Xmin
- percentage
- mode
and I want to solve the following (rather long) equation for s:
> (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s))) - percentage == 0
I want to use mpmath and sympy to solve the equation, but it gives me the following error message:
TypeError: cannot create mpf from 0.707106781186547*(-s**2 - 0.287682072451781)/s
My code is as follows:
from mpmath import erf, log, sqrt
from sympy import Symbol, solveset, S
percentage = 0.95
mode = 2
Xmin = 1.
Xmax = 1.5
s = Symbol('s')
eqn = (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s))) - percentage)
solveset(eqn, s, domain=S.Reals)
mpf is the float type created by mpmath.
I think I narrowed down the problem to the erf() function, it returns
EmptySet()
when i run
solveset(log(Xmax) - (log(mode) + s ** 2), s, domain=S.Reals)
I cannot figure out what to try next, any help would be appreciated!
I initially thought it was an issue with the math but the equation is solved successfully in matlab, so the problem is probably coming from sympy or mpmath.
Losing the mpmath
import of erf
, and use the sympy version resolves your error.
from sympy import Symbol, solveset, S, erf, log, sqrt
percentage = 0.95
mode = 2
Xmin = 1.
Xmax = 1.5
s = Symbol('s', real=True)
eqn = (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s))) - percentage)
solveset(eqn, s)
Note also:
- you don't have to import
log
andsqrt
frommpmath
. It won't make a difference to your result here to get them fromsympy
- you can specify the Real domain on the variable s, saves you doing so on the
solveset
call(s).
Example of further usage is in the package tests here if you need it.
来源:https://stackoverflow.com/questions/37759214/sympy-and-mpmath-give-typeerror-cannot-create-mpf-when-using-the-erf-functi