*后面的参数必须是可插入的,而不是浮点

2024-05-20 02:44:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难诊断这个问题。我想这是因为*的位置有问题,但我没有任何运气。然后我认为这是调用函数括号的问题,但也没有运气

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import fsolve

def Psat(T,*args):
    '''
    Calculate the saturation pressure of a component.
    '''
    return 10**(args[0] - args[1]/(T + args[2]))

def dew_pt(T,*args):
    '''
    This will calculate the dew point temperature of a binary system.
    '''
    return (y*P)/Psat(T,*args[0]) + ((1-y)*P)/Psat(T,*args[1]) - 1

def bubble_pt(T,*args):
    '''
    This will calculate the bubble point temperature of a binary system.
    '''
    return (x*Psat(T,*args[0]))/P + (1-x)*Psat(T,*args[1])/P - 1

def plot(dew,bubble):
    '''
    Plotting the dew point and bubble point on a graph.
    '''
    fig = plt.figure(figsize=(60,60))
    plt.plot(x,dew, 'r-',label = 'Dew Point')
    plt.plot(x,bubble, 'b-', label = 'Bubble Point')
    plt.legend(loc = 'best')
    plt.xlabel('Composition')
    plt.ylabel('Temperature $^\circ$C')
    plt.show()
    return fig

#Constants
P = 760 #Units: mmHg
liquid_comp = np.linspace(0,1,101)
vap_comp = np.linspace(0,1,101)

#Antoine Constants A, B, C
Ben_Con = (6.91, 1211, 221)
Tol_Con = (6.95, 1344, 219)

dew = []
for y in vap_comp:
    ans = fsolve(dew_pt,25,*(Ben_Con,Tol_Con))
    dew.append(ans)

错误如下所示


  File "C:\Users\ayubi\Documents\Python Files\Chemical Engineering Files\Txy.py", line 22, in dew_pt
    return (y*P)/Psat(T,*args[0]) + ((1-y)*P)/Psat(T,*args[1]) - 1

TypeError: Psat() argument after * must be an iterable, not float

我确信这是一个简单的解决办法,我就是找不到解决办法

谢谢


Tags: oftheimportptreturnplotdefnp
2条回答

所以我才明白!耶

以@hpaulj为主角的道具

问题本质上是调用fsolve函数

我应该将fsolve函数称为

ans = fsolve(dew_pt, 25, args = (Ben_Con, Tol_Con)

不是

ans = fsolve(dew_pt, 25, (Ben_Con, Tol_Con))

这是因为根据fsolve的文档,有几个参数作为元组传递,我只需要指定要指定的元组

我知道这很容易解决,(:

谢谢你们的帮助

这个错误正好告诉你问题所在。当只能在iterable(如列表或元组)上使用*时,您正在浮点args[0]上使用*

请看Psat()

def Psat(T,*args):
    '''
    Calculate the saturation pressure of a component.
    '''
    return 10**(args[0] - args[1]/(T + args[2]))

这表明我们正在使用args作为列表args被声明为*args,这意味着它正在将函数的其余参数收集到一个列表中

这意味着在调用Psat()时,需要向其传递4个总参数:

Psat(T, x, y, z)

一个常见的快捷方式是“splat”一个iterable作为参数。如果有一个包含参数的列表,则可以执行以下操作:

Psat(T, *args)

注意这里没有索引。您只需将所有值从args传递到Psat

相关问题 更多 >