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
我相信这很容易解决,我只是找不到解决方案。
谢谢!
解决方案
所以我能够弄清楚!耶!
支持@hpaulj 获得领先。
问题本质上是调用 fsolve 函数
我想将 fsolve 函数称为
ans = fsolve(dew_pt, 25, args = (Ben_Con, Tol_Con)
不是
ans = fsolve(dew_pt, 25, (Ben_Con, Tol_Con))
这是因为根据文档,fsolve
有几个参数作为元组传递,我只需要指定哪个元组我指定什么。
我知道这很容易解决,(:
谢谢大家的帮助!
推荐阅读
- yaml - Python:从 .yaml 读取信息
- c# - C# 参数 1:无法从 'string' 转换为 'char'
- ruby-on-rails - 计算rails postgresql查询中的时间差
- python - Django:使用模板中的元素动态设置 url
- java - 一个线程池 VS。java spring boot(web)项目中的许多线程池
- java - 一天前消费rest angular 6和Java Spring的日期
- c# - IOException:Visual Studio 中的“找不到资源”
- php - 为什么 laravel 的门面表现得像一个单例?
- python - CPLEX 错误 1016:促销版,使用学术版 CPLEX
- php - PHP太空飞船排序不适用于变量