# Arguments for a function
args(curve)function (expr, from = NULL, to = NULL, n = 101, add = FALSE,
type = "l", xname = "x", xlab = xname, ylab = NULL, log = NULL,
xlim = NULL, ...)
NULL
\[\begin{equation} pH = pKa + log\frac{[A^-]}{[HA]} \label{hender-hassel} \end{equation}\]
\[\begin{equation} fa+fb=1 \label{frac-tit} \end{equation}\]
\[\begin{equation} pH = pKa + log\frac{fb}{1-fb} \label{HH-frac} \end{equation}\]
From this derivation, one can easily relate that:
\[\begin{equation} fb = \frac{10^{(pH-pKa)}} {1+10^{(pH-pKa)}} \label{HH-fb} \end{equation}\]
And, similarly, one can find fa as
\[\begin{equation} fa = 1- fb \label{HH-fb2} \end{equation}\]
Resulting in
\[\begin{equation} fa = \frac{1}{1+10^{(pH-pKa)}} \label{eq-HH-fa} \end{equation}\]
curve function is used with its arguments (args), as follows:# Arguments for a function
args(curve)function (expr, from = NULL, to = NULL, n = 101, add = FALSE,
type = "l", xname = "x", xlab = xname, ylab = NULL, log = NULL,
xlim = NULL, ...)
NULL
Or, in a simpler way:
# Titration curve for the acetate/acetic acid system
pKa = 4.75
curve((1/(1+10^(x-pKa))),0.14)
# Titration curve for the acetate/acetic acid system
pKa = 4.75
curve(((10^(x-pKa))/(1+10^(x-pKa))),0.14)
\[\begin{equation} fa = \frac{1}{1+10^{(pH-pKa1)}}+ \frac{1}{1+10^{(pH-pKa2)}} \label{HHbic} \end{equation}\]
Therefore,
pKa1 = 6.37
pKa2 = 10.20
curve((1/(1+10^(x-pKa1)))+1/(1+10^(x-pKa2)),0,14)
dev.copy command:dev.copy(pdf,"titBicarb.pdf",width=6, height=3) # alternatively, bmp,
# jpeg, tiff, svg, pngcurve function above, and the flexibility that the internal package Graphics of R allows, one can elaborate a more complex curve, as follows:pKa1 = 6.37
pKa2 = 10.20
curve((1/(1+10^(x-pKa1)))+1/(1+10^(x-pKa2)),0,14,
xlab="pH",ylab="fa",
main="Carbonic Acid Titration, H2CO3/HCO3-",
type="o", n=50,lwd=2,lty="dotted",
pch=3,col="blue",cex=1.2) # titration graph
text(4.7,1.3,"pKa = 6.37") # inserting text into the graph
text(9,0.3,"pKa = 10.20")
abline(0.5,0, lty="dotted") # dotted line at specific intercept
# and slope
abline(1.5,0, lty="dotted")
locator() command. Since it is just one point on the graph, simply type the code locator(1) and left-click on the point on the curve corresponding to the fraction of 0.5 for fa.locator(1) # for more points on the graph, simply increase the value in parentheses\[\begin{equation} fa = \frac{1}{1+10^{(pH-pKa1)}}+ \frac{1}{1+10^{(pH-pK2)}}+\frac{1}{1+10^{(pH-pKa3)}} \label{eq-HHfosf} \end{equation}\]
In R this can be done as follows: \(\eqref{eq-HHfosf}\)
pKa1=2.2
pKa2=7.2
pKa3=12.7
curve((1/(1+10^(x-pKa1)))+
(1/(1+10^(x-pKa2)))+
(1/(1+10^(x-pKa3))),
xlim=c(1,14),
xlab="pH",ylab="fa",
main="Phosphate buffer titration",
sub = " Dotted lines cross pKa values"
)
abline(v=c(2.2,7.2,12.7),col=c("blue","red","green"),lty="dotted") # adding
# vertical lines marking pKa values
text(1.6,2.5,"pKa1")
text(6.5,1.5,"pKa2")
text(11.8,0.5,"pKa3")
function.X <- function( arg1, arg2, arg3 )
{
execution commands
return(function object)
}# Function to convert degrees Celsius to Kelvin
CtoK <- function (tC) {
tK <-tC + 273.15
return(tK)
}# Executing CtoK:
CtoK (37)[1] 310.15
Define a function of R that contains the parameters and the desired operation.
Include a loop structure in the function that allows the operation to be repeated until the compound’s proton count is exhausted.
Define a vector of R containing the compound’s pKa values.
Define the curve expression that enables the simulation.
Below is a code model that allows the simulation for the phosphate buffer.
#Define titration function and plot
fa = function(pH,pKa) {
x=0
for(i in 1:length(pKa)) {
x = x+1/(1 + 10^(pH - pKa[i]))}
return(x)
}
pKa=c(2.2,7.2,12.7)
curve(fa(x,pKa),1,14, xlab="pH", ylab="fa",
col=2)
Note: the pKa value of the bicarbonate system is 6.8 when considering \(CO_2\) as a source of carbonic acid \(H_2CO_3\) in its reaction with \(H_2O\), for example, for determining arterial parameters and from hospital analyzer (\(CO_2\), \(HCO_3^-\), \(O_2\)).↩︎