HorseGoose
Programmer
I have some code below which calculates the strength of an acid indexed against citric. However, when the code runs it does not give the right answer even though the algoritims are correct. If the same calculations are put into a spread sheet they work.
here is the code and where the error happens
Public Function citriceq(varmw As Double, varpka1 As Double, varpka2 As Double, varpka3 As Double)
' This function converts any acid to a percentage relative to citric acid
' for example if tartaric acid is 75% as strong as citric you need to add target citric value/0.75 to get
' the correct value to add to a beverage which yields the beverage acidity as citric %w/w
Dim varpH, varr1d, varr2d, varr3d, varr4d, varf1d, varf2d, varf3d, varf4d, varfrac, varmM, varmg, varmgcitric83 As Double
varpH = 8.3 ' constant of unconverated alkalinity no pkas used
varmgcitric83 = 0.643039853 ' mg of citric acid at pH 8.3 to index against, this is effectively 100%
If varpka1 = 0 Then varpka1 = 20 ' if an acid has only pka values then third is converted to 20 so the algoritim can work
If varpka2 = 0 Then varpka2 = 20
If varpka3 = 0 Then varpka3 = 20
If varmw = 0 Then GoTo errorproc ' must have molecular weight
varr1d = 10 ^ (varpH - varpka1)
varr2d = 10 ^ (varpH - varpka2)
varr3d = 10 ^ (varpH - varpka3) 'ERROR HAPPENS HERE.....
varf1d = 1 / (1 + varr1d + varr1d * varr2d + varr1d * varr2d * varr3d)
varf2d = varr1d * varf1d
varr3d = varr1d * varr2d * varf1d
varr4d = varr1d * varr2d * varr3d * varf1d
varfrac = varf2d + 2 * varf3d + 3 * varf4d
varmM = 0.01 * varfrac
varmg = varmM * varmw
citriceq = varmgcitric83 / varmg
Exit Function
errorproc:
citriceq = "err"
End Function
========================================================
when the variables are as follows...
varmw = 98.00
varpka1 = 2.12
varpka2 = 7.2
varpka3 = 12.44
The out put of varr3d = 10 ^ (varpH - varpka3) should be 7.2236E-05 but in the function it is 0.96350
It could be that the program needs to finish one calcuation before starting another one but I have no idea how to make it do this. I am happy to sne dthe file to anyone who can help.
Thanks a lot
Horse Goose
here is the code and where the error happens
Public Function citriceq(varmw As Double, varpka1 As Double, varpka2 As Double, varpka3 As Double)
' This function converts any acid to a percentage relative to citric acid
' for example if tartaric acid is 75% as strong as citric you need to add target citric value/0.75 to get
' the correct value to add to a beverage which yields the beverage acidity as citric %w/w
Dim varpH, varr1d, varr2d, varr3d, varr4d, varf1d, varf2d, varf3d, varf4d, varfrac, varmM, varmg, varmgcitric83 As Double
varpH = 8.3 ' constant of unconverated alkalinity no pkas used
varmgcitric83 = 0.643039853 ' mg of citric acid at pH 8.3 to index against, this is effectively 100%
If varpka1 = 0 Then varpka1 = 20 ' if an acid has only pka values then third is converted to 20 so the algoritim can work
If varpka2 = 0 Then varpka2 = 20
If varpka3 = 0 Then varpka3 = 20
If varmw = 0 Then GoTo errorproc ' must have molecular weight
varr1d = 10 ^ (varpH - varpka1)
varr2d = 10 ^ (varpH - varpka2)
varr3d = 10 ^ (varpH - varpka3) 'ERROR HAPPENS HERE.....
varf1d = 1 / (1 + varr1d + varr1d * varr2d + varr1d * varr2d * varr3d)
varf2d = varr1d * varf1d
varr3d = varr1d * varr2d * varf1d
varr4d = varr1d * varr2d * varr3d * varf1d
varfrac = varf2d + 2 * varf3d + 3 * varf4d
varmM = 0.01 * varfrac
varmg = varmM * varmw
citriceq = varmgcitric83 / varmg
Exit Function
errorproc:
citriceq = "err"
End Function
========================================================
when the variables are as follows...
varmw = 98.00
varpka1 = 2.12
varpka2 = 7.2
varpka3 = 12.44
The out put of varr3d = 10 ^ (varpH - varpka3) should be 7.2236E-05 but in the function it is 0.96350
It could be that the program needs to finish one calcuation before starting another one but I have no idea how to make it do this. I am happy to sne dthe file to anyone who can help.
Thanks a lot
Horse Goose