我正在写一个程序,自动计算细胞中给定离子的能斯特势。因此,我让用户指定哪种离子,它的电荷,以及它们在细胞内外的浓度。我定义了常量,用R格式写出了方程,我一直得到的输出都是Inf。
由于我是R的新手,我非常确定我在代码中的某个地方犯了一个错误。所以,我需要你的帮助来修改我的代码,并告诉我我做错了什么。
readline("Which ion will you use to measure the potential equilibrium? = ")
z<-as.integer(readline("What is the ion charge? = "))
co<-as.integer(readline("¿What is the extracelular concentration of the ion (mM) = "))
ci<-as.integer(readline("¿What is the intracelular concentration of the ion (mM) = "))
#Defining constants
R<-8.3145 # unit J/K.mol
Tbody<-310.15 # K=Cº+273.15; Cº=37º body temperature
Faraday<-96485.337 # unit C/mol Faraday constant
#Equation
Veq<-((R*Tbody)/(z*Faraday))*log((co/ci), base = exp(1))
Veq_mV<-Veq*1000 # from V to mV
#output
cat("The equilibrium potential for ",ion," is: ",Veq_mV)
对于第一个问题,我使用的数据类型是字符串,对于其余问题,我使用的是整数。它与其他离子如Na+,Cl-,K+一起工作得很好,但对于Ca2+,它只输出Inf。Ca2+的体外浓度为0.0001 ~ 0.0007 uM / 0.7 uM。有没有办法解决这个问题?
发布于 2020-03-27 22:31:21
代码中的问题是由将输入转换为整数引起的。使用as.numeric
而不是as.integer
将保留小数点后的值。as.integer
将截断小数,因此as.integer
将Ca2+浓度0.0001解释为0,并将导致除以0的错误,最终输出为Inf
。
试试这个:
readline("Which ion will you use to measure the potential equilibrium? = ")
z<-as.numeric(readline("What is the ion charge? = "))
co<-as.numeric(readline("¿What is the extracelular concentration of the ion (mM) = "))
ci<-as.numeric(readline("¿What is the intracelular concentration of the ion (mM) = "))
#Defining constants
R<-8.3145 # unit J/K.mol
Tbody<-310.15 # K=Cº+273.15; Cº=37º body temperature
Faraday<-96485.337 # unit C/mol Faraday constant
#Equation
Veq<-((R*Tbody)/(z*Faraday))*log((co/ci), base = exp(1))
Veq_mV<-Veq*1000 # from V to mV
#output
cat("The equilibrium potential for ",ion," is: ",Veq_mV)
发布于 2021-08-20 16:34:51
只是为了好玩,我对代码做了一些调整,包括输入以摄氏度为单位的温度,并提高了法拉第常数的精度。
# Inputs
Ion <- readline("Which ion will you use to measure the potential equilibrium? = ")
Z <- as.numeric(readline("What is the ion charge? = "))
Co <- as.numeric(readline("What is the extracelular concentration of the ion (mM) = "))
Ci <- as.numeric(readline("What is the intracelular concentration of the ion (mM) = "))
TC <- as.numeric(readline("What is the temperature in Centigrade = "))
# Convert C into K
TState <- (TC + 237.15) # body temp = 37º C (310.15º K) K=Cº+273.15
#Defining constants
R<-8.3145 # unit J/K.mol
Faraday<-96485.3321233 # unit C/mol Faraday constant
#Equation
Veq<-((R*TState)/(Z*Faraday))*log((Co/Ci), base = exp(1))
Veq_mV<-Veq*1000 # from V to mV
#output
cat("The equilibrium potential for ",Ion," is: ",Veq_mV)
https://stackoverflow.com/questions/60887788
复制相似问题