Filter löschen
Filter löschen

How can I prevent an infinite recursion within my program?

2 Ansichten (letzte 30 Tage)
Dursman Mchabe
Dursman Mchabe am 5 Feb. 2019
Kommentiert: Dursman Mchabe am 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 Kommentare
Stephen23
Stephen23 am 5 Feb. 2019
Bearbeitet: Stephen23 am 5 Feb. 2019
"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 am 5 Feb. 2019
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 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 Kommentare
Guillaume
Guillaume am 5 Feb. 2019
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 am 5 Feb. 2019
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Least Squares finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by