【问题标题】:Evaluating Temperature using Antoine Eq with Classes使用带类的 Antoine Eq 评估温度
【发布时间】:2019-02-18 21:07:19
【问题描述】:

我需要帮助,通过使用类来使用 Antoine Eq 获取温度。

我的 root 操作失败,我不知道为什么。

我的代码:

from __future__ import division, print_function
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import root

class Chemical(object):

    def __init__(self, name_1, balance_1, name_2, balance_2):
        self.name = name_1 + ' ' + '+' + ' ' + name_2
        self.data_1 = []
        self.data_2 = []
        self.data_1 = balance_1
        self.data_2 = balance_2

    def __str__(self):
        if (self.name):
            return "Mixture: %s" % (self.name)
        else:
            return None

    def bubble_solve(self,a,P = 1.01,T = 300):
        A1,B1,C1 = self.data_1
        A2,B2,C2 = self.data_2
        PA = 10**(A1 - B1/(C1+T)) 
        PB = 10**(A2 - B2/(C2+T)) 
        sol = root(lambda T: P - (a*PA + (1-a)*PB),T)
        return sol.x[0]

    def dew_solve(self, b, P = 1.01, T = 300):
        A1,B1,C1 = self.data_1
        A2,B2,C2 = self.data_2
        PA = 10**(A1 - B1/(C1+T)) 
        PB = 10**(A2 - B2/(C2+T)) 
        sol = root(lambda T: 1 - (b*P/PA + (1-b)*P/PB), T)
        return sol.x[0]


mixture = Chemical('benzene', [ 3.98523 , 1184.24 , -55.578], 'toulene', 
[4.05043 , 1327.62 , -55.528])
print(mixture)
print()

print(mixture.bubble_solve(0.5)) #at a = 0.5
print(mixture.bubble_solve(0.5,2)) #at a = 0.5 and P = 2
print(mixture.dew_solve(0.5)) #at b = 0.5
print(mixture.dew_solve(0.5,2)) #at b = 0.5 and P = 2

这是我的代码正在打印的内容:

Mixture: benzene + toulene

300.0
300.0
300.0
300.0

但是,答案必须是: 365.087、390.14188、371.7743、396.688。

为什么 root 操作会失败?

【问题讨论】:

    标签: python python-3.x numpy jupyter-notebook


    【解决方案1】:

    欢迎来到 StackOverflow,@Nathaniel!

    问题在于bubble_solvedew_solve 中的PAPBsol 行。您似乎将常量与自变量混合在一起!例如,在您的气泡求解中,您为 T 设置了默认值 300。因此在PA 行中,值300 被用于T!解决此问题的一种方法如下:

        ...
        def bubble_solve(self,a,P = 1.01,T = 300):
            A1,B1,C1 = self.data_1
            A2,B2,C2 = self.data_2
            PA = lambda x: 10**(A1 - B1/(C1+x)) 
            PB = lambda x: 10**(A2 - B2/(C2+x)) 
            sol = root(lambda x: P - (a*PA(x) + (1-a)*PB(x)),T)
            return sol.x[0]
    
        def dew_solve(self, b, P = 1.01, T = 300):
            A1,B1,C1 = self.data_1
            A2,B2,C2 = self.data_2
            PA = lambda x: 10**(A1 - B1/(C1+x)) 
            PB = lambda x: 10**(A2 - B2/(C2+x)) 
            sol = root(lambda x: 1 - (b*P/PA(x) + (1-b)*P/PB(x)), T)
            return sol.x[0]
        ...
    

    这将返回您期望的值。请注意,我使用x 作为PAPB 的自变量,但它是任意的。但是,如果您向匿名函数传递值,最好不要为匿名函数重用 T

    【讨论】:

    • 这就像一个魅力!非常感谢你的帮助!研究了几个小时,结果发现只有一个字符是错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2016-02-10
    • 2014-03-24
    • 1970-01-01
    • 2019-02-07
    • 2015-03-02
    • 1970-01-01
    相关资源
    最近更新 更多