How to get an additional output from fminsearch
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Torsten
am 29 Sep. 2023
Bearbeitet: Torsten
am 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".
Antworten (1)
Walter Roberson
am 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 Kommentare
Walter Roberson
am 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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!