Well, as said, global variables makes it difficult to debug. I'm certainly not going to bother trying to figure what are the values of the global variables at the point where you're calling your function.
Clearly though, you're not understanding the problem, pH is not an input, it's the output that is non-finite (or non-real). The problem is that fzero is calling HionPH with an initial of pH_trial and b, whatever their actual value is. When called with these values, HionPH either return NaN or +/-Inf.
So either pH_Trial or b are incorrect and outside the domain or the function, or somethin is wrong with the function itself. Maybe CA_total, or C_Total, or S_Total is wrong. You'll have to track where they're created and modified to check they're what you're expect. Since they're global variables they may have been modified inadvertently by any function.
Possibly the best thing to do is to issue dbstop if error at the command line and run the code. It will break into the debugger when it errors. At this point, look at the values of
ph_Trial
b
CA_Total
C_Total
S_Total
and see the result of
and work from there to figure out what went wrong
Best Answer