Function value at starting guess must be finite and real

조회 수: 82 (최근 30일)
Dursman Mchabe
Dursman Mchabe 2019년 2월 8일
댓글: Walter Roberson 2022년 7월 27일
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.
  댓글 수: 2
David Goodmanson
David Goodmanson 2019년 2월 8일
Hi Dursman,
The initial guesses may be finite and real, but the error message says that the value of the function at the initial guess is not finite and real. HionpH depends on three global variables (definitely not recommended practice) as well as its input argument, but if you go to the debugger you should be able to track this down.
Dursman Mchabe
Dursman Mchabe 2019년 2월 9일
Thank you somuch. I now understand better.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2019년 2월 8일
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)
  댓글 수: 3
Chen Jinyan
Chen Jinyan 2022년 7월 27일
But how to make sure that the function value is real and finite? Do I have to stop and check, which is not so automatic when I run a cycle.
Walter Roberson
Walter Roberson 2022년 7월 27일
fzero() and some of the other solvers and optimizers will automatically tell you that the value at the initial location is not real and finite; you might want to use try/catch around that.
Or you could invoke the function on your initial guess and check with isfinite() before you run fzero() or the solver. To build a more robust program, it is a good idea to wrap that test in try/catch
For iterations after the first one, fzero() and the solvers by default do not check. You can, however, set the FunValCheck option to tell it to check the calculation every iteration.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by