R中的矢量化迭代循环
各位,我想将以下Visual Basic代码翻译成R:R中的矢量化迭代循环,r,loops,vectorization,R,Loops,Vectorization,各位,我想将以下Visual Basic代码翻译成R: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function WetBulb(T As Double, WDes As Double, PAtm As Double) ' Function to calculate wet-bulb temperature from dry-bulb ' and humidity ratio Di
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function WetBulb(T As Double, WDes As Double, PAtm As Double)
' Function to calculate wet-bulb temperature from dry-bulb
' and humidity ratio
Dim Wsat As Double
Dim TWBOld As Double
Dim WOld As Double
Dim TWBNew As Double
Dim TWB As Double
Dim WStar As Double
Dim W As Double
Dim slope As Double
Wsat = HumRatRH(T, RHMax, PAtm)
TWBOld = T
WOld = Wsat
TWBNew = TWBOld - 1
Do
TWB = TWBNew
WStar = HumRatRH(TWB, RHMax, PAtm)
W = ((HfgRef - (CpWat - CpVap) * TWB) * WStar - CpAir * (T - TWB)) / (HfgRef + CpVap * T - CpWat * TWB)
slope = (W - WOld) / (TWB - TWBOld)
TWBNew = TWB - (W - WDes) / slope
If Abs(W - WDes) < Abs(WOld - WDes) Then
WOld = W
TWBOld = TWB
End If
Loop Until Abs((TWBNew - TWB) / TWB) < tolRel
WetBulb = TWB
End Function
<代码>''或''''''''''''''''''''''''''''''''''''''''''''''
功能湿球(T为双功能,WDes为双功能,PAtm为双功能)
'从干球计算湿球温度的函数
温湿度比
将Wsat调暗为双精度
双倍粗体
双倍暗淡的世界
将TWB新建为双精度
双倍调暗TWB
将WStar调暗为双精度
暗W为双色
暗坡为双坡
Wsat=HumRatRH(T,RHMax,PAtm)
TWBOld=T
WOld=Wsat
TWBNew=TWBOld-1
做
TWB=TWBNew
WStar=湿度(TWB、RHMax、PAtm)
W=((HfgRef-(CpWat-CpVap)*TWB)*WStar-CpAir*(T-TWB))/(HfgRef+CpVap*T-CpWat*TWB)
斜率=(W-WOld)/(TWB-TWBOld)
TWBNew=TWB-(W-WDes)/斜率
如果Abs(W-WDes)# Constants independent of unit system
NMol = 0.62198 # ratio of molecular weights, Mvap/MAir
RHMax = 1 # maximum relative humidity, 1 or 100 (if percent)
tolRel = 0.000001 # relative error tolerance for iteration
# Constants for English Units
# Note: constants currently configured for PAtm in atmospheres
HfgRef = 1061 # heat of vaporization at 0C, Btu/hr.lbm.F
CpVap = 0.444 # specific heat of water vapor, Btu/hr.lbm.F
CpWat = 1 # specific heat of liquid water, Btu/hr.lbm.F
CpAir = 0.24 # specific heat of dry air, Btu/hr.lbm.F
RAir = 0.02521 # gas constant for air, (user pressure).ft3/lbm.R
kPaMult = 101.325 # multiplier to get kPascals from user pressure
TAbs = 459.67 # add to user temperature to get absolute temp
TKelMult = 0.555556 # multiplier to get Kelvin from user temp
TAmb = 70 # typical temperature in user units (initial value)
#####################################################################
SatPress <- function(TArg) {
# Define constants for vapor pressure correlations
C1 = -5674.5359
C2 = -0.51523058
C3 = -0.009677843
C4 = 0.00000062215701
C5 = 2.0747825E-09
C6 = -9.484024E-13
C7 = 4.1635019
C8 = -5800.2206
C9 = -5.516256
C10 = -0.048640239
C11 = 0.000041764768
C12 = -0.000000014452093
C13 = 6.5459673
T = (TArg + TAbs) * TKelMult
# Use different correlations for pressure over ice or water
kPa.lo = exp(C1 / T + C2 + T * C3 + T * T * (C4 + T * (C5 + C6 * T)) + C7 * log(T))
kPa.hi = exp(C8 / T + C9 + T * (C10 + T * (C11 + T * C12)) + C13 * log(T))
kPa = ifelse(T < 273.15, kPa.lo, kPa.hi)
SatPress = kPa / kPaMult
return(SatPress)
}
#####################################################################
HumRatRH = function(T,RH,PAtm) {
# function to calculate humidity ratio from temperature
# and relative humidity
pw = SatPress(T) * RH / RHMax
HumRatRH = NMol * pw / (PAtm - pw)
return(HumRatRH)
}
#####################################################################
WetBulb = function(T, WDes,PAtm) {
# Function to calculate wet-bulb temperature from dry-bulb
# and humidity ratio
Wsat = HumRatRH(T, RHMax, PAtm)
TWBOld = T
WOld = Wsat
TWBNew = TWBOld - 1
iterate.TWB = function(x) {
repeat {
TWB = TWBNew
WStar = HumRatRH(TWB, RHMax, PAtm)
W = ((HfgRef - (CpWat - CpVap) * TWB) * WStar - CpAir * (T - TWB)) / (HfgRef + CpVap * T - CpWat * TWB)
slope = (W - WOld) / (TWB - TWBOld)
TWBNew = TWB - (W - x) / slope
TWBOld=ifelse(abs(W - x) < abs(WOld - x),TWB,TWBOld) # update TWBOld first
WOld=ifelse(abs(W - x) < abs(WOld - x),w,WOld) # then update WOld
if (abs((TWBNew - TWB) / TWB) < tolRel) break()
}
return(TWB)
}
WetBulb = sapply(WDes, iterate.TWB)
return(WetBulb)
}
#####################################################################
temp = c(80,55,100)
w = c(0.011,0.009,0.016)
PAtm = 0.8187308
WetBulb(temp,w,PAtm)
# The correct answer:
# 62.95381538 51.3986312 74.02877887
#独立于单位制的常数
NMol=0.62198#分子量比,Mvap/MAir
RHMax=1#最大相对湿度,1或100(如果百分比)
tolRel=0.000001#迭代的相对误差容限
#英制单位常数
#注:当前为大气中的PAtm配置的常数
HfgRef=1061#0℃时的蒸发热,Btu/hr.lbm.F
CpVap=0.444#水蒸汽比热,Btu/hr.lbm.F
CpWat=1#液态水的比热,Btu/hr.lbm.F
CpAir=0.24#干燥空气的比热,Btu/hr.lbm.F
RAir=0.02521#空气的气体常数(用户压力)。ft3/lbm.R
kPaMult=101.325#从用户压力中获取kPascals的乘数
TAbs=459.67#添加到用户温度以获得绝对温度
TKelMult=0.555556#从用户温度获得开尔文的乘数
TAmb=70#以用户单位表示的典型温度(初始值)
#####################################################################
SatPress对函数f
进行矢量化的最简单方法是使用矢量化
。默认情况下,它对其所有参数进行向量化f
。在本例中,您只希望对3个参数中的2个参数进行矢量化,因此可以通过vectorize.args
指定此参数
WetBulb <- Vectorize(WetBulb, vectorize.args=c("T", "WDes"))
wetble我怀疑iterate.TWB
中return
之前的if
语句有问题。尝试:if(all(abs((TWBNew-TWB)/TWB)
,但您可能会进行比某些输入所需更多的计算。您好,我对计算湿球温度的函数很感兴趣。这个版本是最新的吗?我可以这样使用吗?非常感谢。嗨@agenis,是的,这是最新的。请注意,它只在IP单元中工作。