lpr
lpr

Reputation: 25

I am Writing a program in RStudio to calculate the equilibrium potential and it only outputs Inf

I'm writing a program that automatically calculates the Nernst potential of a given ion in a cell. So, I made the user specify which ion, its charge, and their concentrations inside and outside of the cell. I defined the constants, wrote the equation in R format and all I keep getting as an output is Inf.

Since I'm new to R , I'm pretty sure I have made a mistake somewhere in my code. So, I need your help to revise my code and tell me what I'm doing wrong.

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)

The type of data I'm using as imput is a string for the first question, and integers for the rest. It works really well with other ions like Na+, Cl-, K+, but with Ca2+ it only outputs Inf. The outside concentration of Ca2+ is 0.0001 to 0.0007 or 0.1 uM / 0.7 uM. Is there a way to fix this?

Upvotes: 1

Views: 320

Answers (2)

Haw&#39;n Hillbilly
Haw&#39;n Hillbilly

Reputation: 111

Just for fun, I've adjusted the code a bit, including input for Temperature in C and improved the precision of Faraday's constant.

# 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)

Upvotes: 1

lincolnck
lincolnck

Reputation: 312

The issue in your code is caused by casting your inputs to integers. Using as.numeric instead of as.integer will keep the values after any decimal points. as.integer will cut off decimals, so a Ca2+ concentration of 0.0001 will be interpreted as 0 with as.integer and will lead to a division by 0 error, resulting in an output of Inf.

Try this:

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)

Upvotes: 1

Related Questions