1

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.

Louis Maddox
  • 5,226
  • 5
  • 36
  • 66
cachemoi
  • 343
  • 2
  • 13

1 Answers1

7

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 and sqrt from mpmath. It won't make a difference to your result here to get them from sympy
  • 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.

Louis Maddox
  • 5,226
  • 5
  • 36
  • 66
  • unfortunately that does not give me the correct answer. It returns a "Conditionset" object instead of a single float, which is the correct answer I get from matlab – cachemoi Jun 14 '16 at 12:24
  • this matlab code gives the correct answer for one case: function sigma = Test() %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))) syms s eqn =(1/2+1/2*erf((log(4)-(log(2)+s^2))/(sqrt(2)*s))-(1/2+1/2*erf((log(1)-(log(2)+s^2))/(sqrt(2)*s)))) - 0.95 == 0; sigma = solve(eqn,s); %sigma = double(sigma); end – cachemoi Jun 14 '16 at 12:26
  • Sounds like you should open a second question for that since it's unrelated. Title of your question here asks to resolve a `TypeError`, which the above does. Sorry I can't be of more help – Louis Maddox Jun 14 '16 at 16:27
  • 1
    OK, you did solve my initial issue so I will accept your answer. – cachemoi Jun 14 '16 at 17:12