How can I prevent an infinite recursion within my program?

4 ビュー (過去 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 Center および File ExchangeLinear Least Squares についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by