问题
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 sympy to solve the equation, but it returns a ConditionSet Object (meaning that it cannot solve the equation)
My code is as follows:
from sympy import *
def CalcScaleParam(mode, CIfact, percentage):
s = Symbol('s', Real=True)
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
sigma = solveset(eqn, s)
print(sigma)
CalcScaleParam(2,2, 0.9545)
My Matlab code is as follows:
function sigma = Test()
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);
end
And returns sigma=0.335
I initially thought it was an issue with the math but since the equation is solved successfully in matlab I suspect that the problem is coming from sympy.
回答1:
The fractions are using Python's integer division, which gives 1/2 == 0. The entire expression reduces to -0.95. Declare it using 1./2 or 0.5, or maybe Rational(1,2). You might also have to express the percentage 0.95 as Rational(19,20).
来源:https://stackoverflow.com/questions/37818218/sympy-returns-a-conditionset-object-when-solving-an-equation-while-matlab-retur