MATLAB: Function value at starting guess must be finite and real

guess finite real

Hi everyone,
I know that the question is self-explaning. I am supriced that I get this error:
Error using fzero (line 328)
Function value at starting guess must be finite and real.
Error in SlurryCase08Feb2019a>SO2_OdeDriver (line 218)
pH = fzero(@(pH)HionpH(pH,b),pH_trial);
Error in SlurryCase08Feb2019a>kinetics (line 164)
y = SO2_OdeDriver(y0,b);
Error in lsqcurvefit (line 213)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in SlurryCase08Feb2019a (line 73)
[b]=lsqcurvefit(@kinetics,b0,tdata,ydata);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Even after ensuring that all all my initial guesses are finite and real. I have checked my code (attached) thoroughly, but I can't find my mistake.
Please help.

Best Answer

As David said, fzero is complaining about the value of your function at the initial guess. A simpler example illustrating the problem:
f = @(x) (1./x)-1;
initialGuess = 0;
fzero(f, initialGuess)
While initialGuess is finite and real, the same cannot be said for the value of f evaluated at initialGuess.
f(initialGuess)
If I change my initial guess to something where f(newInitialGuess) is finite and real, fzero can find a solution.
newInitialGuess = 2;
x = fzero(f, newInitialGuess)
f(x)