Clear Filters
Clear Filters

How to get an additional output from fminsearch

7 views (last 30 days)
Erich
Erich on 29 Sep 2023
Commented: Erich on 30 Sep 2023
I have been trying to implement a nested fminsearch, but I have difficulty to pass the result of the inner fminsearch to the main file. Here is a structure of my code
%%%% main.m file
fun1Ch2 = @(coeff1)outerfunc(coeff,input-parameters);
[coeff,Chi2] = fminsearch(fun1Ch2, guess);
%%%% outerfunc.m file
function [chi2,coeff] = outerfunc(coeff,input-parameters)
%=== some calculations here ====
fun2Ch2 = @(coeff2)innerfunc(coeff2,input-parameters);
[coeff2,Chi2] = fminsearch(fun2Ch2, guess);
Now the question is that how to pass the parameter coeff2 which is the result of the inner fminsearch to the main file.
Thanks in advance!
  2 Comments
Torsten
Torsten on 29 Sep 2023
Edited: Torsten on 29 Sep 2023
function [chi2,coeff] = outerfunc(coeff,input-parameters)
I wonder why your function has two output arguments. Which one do you want to pass to your outer "fminsearch" - chi2 or coeff ? And where do you compute them in "outerfunc" ? I only see Chi2 and coeff2.
To get coeff2, define it as an output argument of "outerfunc" and call "outerfunc" with the solution of the outer "fminsearch".
Erich
Erich on 30 Sep 2023
Edited: Erich on 30 Sep 2023
I have two arguments because I also want to get the value which corresponds to fval. Let's say I want to pass both. There should not be any limitation. Of course, there is another file names as innerfunc.m.
"To get coeff2, define it as an output argument of "outerfunc" and call "outerfunc" with the solution of the outer "fminsearch"."
This does not work in my example.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2023
Which one?
The last function call made by fminsearch() is typically not one that made the final improvement. If it had made an improvement, fminsearch() would generally have continued searching. I say "generally" because there is the situation where fminsearch() will stop if it determines there has not been enough improvement to bother continuing -- but a fair portion of the time fminsearch() stops when it determines that all if its attempts to improve have been useless, that some earlier call gave back the best result.
  5 Comments
Walter Roberson
Walter Roberson on 30 Sep 2023
I have used the outputfcn of fminsearch to apply constraints, by stashing away the current result if the constraint is not violated, and by triggering termination if the constraint was violated. So I know for certain that it is possible to use the technique of having fminsearch outputfcn store current value in some accessible location for retrieval afterwards.
Erich
Erich on 30 Sep 2023
To be more exact;
inside the outerfunc function I do this
if(min(coeff_A(:,end))>Chi2_outer)
dlmwrite(file_name, [coeff_outer coeff_inner Chi2_outer],'-append', 'delimiter',',', 'precision','%10.10f');
end
which means that I store all valuable information to be used at the end. Maybe I couldn't make OutputFcn work.

Sign in to comment.

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!