Sympy在求解方程式时返回一个ConditionSet对象,而在求解相同方程式时,matlab返回一个浮点数 [英] Sympy returns a ConditionSet object when solving an equation, while matlab returns a single float when the same equation is solved
问题描述
我有4个输入变量(浮点数):
I have 4 input variables (floats):
最大 Xmin 百分比 模式 我想为s求解以下(相当长的)方程:
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
我想用sympy来求解方程,但是它返回一个ConditionSet对象(这意味着它不能求解方程)
I want to use sympy to solve the equation, but it returns a ConditionSet Object (meaning that it cannot solve the equation)
我的代码如下:
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)
我的Matlab代码如下:
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
并返回sigma = 0.335
And returns sigma=0.335
起初我以为这是数学问题,但是由于方程式已在Matlab中成功解决,所以我怀疑问题出在sympy.
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.
推荐答案
分数使用Python的整数除法,得到1/2 == 0
.整个表达式简化为-0.95
.使用1./2
或0.5
或Rational(1,2)声明它.您可能还必须将百分比0.95表示为Rational(19,20)
.
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)
.
这篇关于Sympy在求解方程式时返回一个ConditionSet对象,而在求解相同方程式时,matlab返回一个浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!