How can I prevent an infinite recursion within my program?

조회 수: 1 (최근 30일)
Dursman Mchabe
Dursman Mchabe 2019년 2월 5일
댓글: Dursman Mchabe 2019년 2월 5일
Hi Everyone,
I am trying to run the code below, and I get very long error message stating:
FZERO cannot continue because user-supplied function_handle ==> @(pH)HionpH(pH,b) failed with the error below.
.
.
.
FZERO cannot continue because user-supplied function_handle ==> @(pH)HionpH(pH,b) failed with the error below.
Out of memory. The likely cause is an infinite recursion within the program.
Error in SlurryCase05Feb2019>kinetics (line 127)
pH = fzero(@(pH)HionpH(pH,b),pH1);
Error in lsqcurvefit (line 213)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in SlurryCase05Feb2019 (line 71)
[b]=lsqcurvefit(@kinetics,b0,tdata,ydata);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
How can I prevent an infinite recursion within my program?
I have also attached the code.
  댓글 수: 2
Stephen23
Stephen23 2019년 2월 5일
편집: Stephen23 2019년 2월 5일
"How can I prevent an infinite recursion within my program?"
You really need to simplify your code and learn how to pass parameterize functions:
Currently most of your code repeats exactly the same calculations or defines the same values repeatedly, e.g. the "Parameters" section occurs six times, and the section that defines the "bulkslurry" values occurs four times. Complex code inhibits understanding and debugging, which simply wastes your time. Instead, use functions for repeated code and pass parameters properly.
"and I get very long error message stating:"
For whatever reason you decided to include the entirety of your code (which is better uploaded as a file) and yet decided to edit the error message and remove the bits that actually show us how your function calls are recursive.
Dursman Mchabe
Dursman Mchabe 2019년 2월 5일
The current comparison between calculated results (Results) and experimental results (ydata) can be seen on the figures below:
I do suspect that estimated values of b(1) ... b(15) will improve the fitting. I just don't know how to do it correctly.

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

답변 (1개)

Guillaume
Guillaume 2019년 2월 5일
How can I prevent an infinite recursion within my program?
Completely rethink your function HionpH. I have no idea what that function is doing (there's not a single comment in your code explaining anything). As it is, you have
function ph = HionpH (pH,b)
%... lots of variable declarations that is not relevant to this question
pH = fzero(@(pH)HionpH(pH,b),pH1);
%...
end
So the value of HionPH depends on its own zeros. Problem is, you can't find the zeros if you don't know the function values, and you don't know the values if you don't know the zeros. It's always going to be an infinite recursion.
The function cannot depend on itself. Perhaps, it should be defined as an ODE (maybe? as said there's nothing explaining what the code is trying to do)
  댓글 수: 4
Guillaume
Guillaume 2019년 2월 5일
I'm not sure your initial question has been resolved or not. For your new question, I'm not sure I'm the best person to help. non-linear curve fitting is not my expertise. I would recommend that you ask a new question. Note that as per Stephen's comment, I would start by simplifying the code. At the very least put all your constants into a structure and pass that structure to the functions that need it instead of redefining them in each.
At least you got rid of the global variables, which is a good thing.
Dursman Mchabe
Dursman Mchabe 2019년 2월 5일
Thanks a lot Guillaume. I will put a separate function for parameters and maybe another one for repeated definitions.
If I could prevent the recursion error, the fitting would be perfect.

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

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by