无法用python错误中的if-else求解方程式:无法确定Relational

时间:2018-05-15 16:29:36

标签: python python-3.x sympy equation

我打算解决一些包含if-else比较的方程式,但我得到错误

    raise TypeError("cannot determine truth value of Relational")
    TypeError: cannot determine truth value of Relational

我的代码如下:

from sympy import *
import math


b=400*0.0393701;h=600*0.0393701;d=530*0.0393701;
d1=70*0.0393701;fc=24*145.0377439;fy=400*145.0377439;Es = 29000000

# Stage1
Ag=b*h;As=math.pi*((32*0.0393701)**2)/4
ey=fy/Es
peta=0.85

e=[0,200,400,600]

def init(ec,f1, c):
    es = ec*(c-d1)/c
    if es>=ey:
        t=3*As*f1/1000
    else:
        t=3*es*Es/1000
    cc=0.85*fc*peta*c*b/1000
    es1=ec*(d-c)/c
    if es1>=ey:
        cs=3*As*(f1-0.85*fc)/1000
    else:
        cs = 3*As*(Es*es1-0.85*fc)/1000
    res = [cc, cs, t]
    return res

def com(ec, c,k):

    if k==1:   # choose Hognestad modal
        e0=2*0.9*fc/(57000*(fc**(0.5)))  #0.00186
        if ec<=e0:
            fc_1 = 0.9*fc*((2*ec/e0) - ((ec/e0)**2))
        else:
            fc_1 = 0.9*fc*(1-0.15*((ec-e0)/(0.0038-e0)))
        re = init(ec, fc_1, c)

    if k==2:  # choose Collins and Mitchell Model
        k3=0.6+10/24; Ec_rev = 4730*(24**(0.5)); n =0.8 + 24/17
        ec_rev = (24/Ec_rev)*(n/(n-1))
        if ec/ec_rev <=1:
            k_rev = 1
        else:
            k_rev = 0.67+24/62
        fc_2=k3*fc*(ec/ec_rev)*(n/(n-1+((ec/ec_rev)**(n*k_rev))))
        re = init(ec, fc_2, c)

    return re

ec_real=[];
for i in range(0,39):
    ec_real.append(i/10000)

c= Symbol('c')
peta = 0.85
a=peta*c
### Final Solution
M=[]
F=[]
final1={}

for k_final in [1, 2]:
    for x1 in e:
        for x2 in ec_real:
            c_final = solve(com(x2, c, k_final)[0] * (h / 2 - a / 2) + com(x2, c, k_final)[1] * (h / 2 - d1) + com(x2, c, k_final)[2] * (d - h / 2), c)[1]
            Pn = com(x2, c_final, k_final)[0] + com(x2, c_final, k_final)[1] - com(x2, c_final, k_final)[2]
            Mn = Pn * x1
            fai = x2 / c_final
            M.append(Mn);
            F.append(fai)
        ss = {str(x1):{}}
        ss[str(x1)]["M"]=M
        ss[str(x1)]["Fai"]=F
final1 = {str(k_final):{}}
final1[str(k_final)]=ss

print(final1)  

我运行此代码并且无法解决显示init()函数在确定if es>=ey:时遇到一些问题,详细错误显示如下:

    if es>=ey:
  File "E:\anaconda\lib\site-packages\sympy\core\relational.py", line 195, in __nonzero__
    raise TypeError("cannot determine truth value of Relational")

TypeError:无法确定Relational的真值

1 个答案:

答案 0 :(得分:1)

在失败时,es是一个SymPy对象,涉及c和几个操作。您需要仔细评估此问题,然后才能与float的{​​{1}}值进行比较。

ey