【问题标题】:Cannot solve a equation with if-else in python error:cannot determine truth value of Relational在 python 错误中无法使用 if-else 求解方程:无法确定关系的真值
【发布时间】:2018-05-15 16:29:35
【问题描述】:

我打算解决一些包含 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()function在确定if es&gt;=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 的真值

【问题讨论】:

  • 在这里一动不动:type(es)type(ey) 是什么?我怀疑type(ey)float,因为它被初始化为float除以int,但是es是用一个涉及c的公式初始化的,除非我看错了,否则它的类型是Symbol,所以结果类型可能不是数字。

标签: python python-3.x sympy equation


【解决方案1】:

在故障点,es 是一个涉及c 和多个操作的 SymPy 对象。在与eyfloat 值进行比较之前,您必须清楚地评估它。

es =  (0.0001*c - 0.0002755907)/c <class 'sympy.core.mul.Mul'> 
ey =  0.0020005206055172414       <class 'float'>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2016-10-12
    • 2021-07-28
    相关资源
    最近更新 更多