MATLAB Answers

How can I prevent an infinite recursion within my program?

Dursman Mchabe 님이 질문을 제출함. 5 Feb 2019
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

"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.
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.

로그인 to comment.

태그

답변 수: 1

Guillaume 님의 답변 5 Feb 2019

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

표시 이전 댓글 수: 1
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.
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.
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.

로그인 to comment.



Translated by