Effacer les filtres
Effacer les filtres

How to get an additional output from fminsearch

6 vues (au cours des 30 derniers jours)
Erich
Erich le 29 Sep 2023
Commenté : Erich le 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 commentaires
Torsten
Torsten le 29 Sep 2023
Modifié(e) : Torsten le 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 le 30 Sep 2023
Modifié(e) : Erich le 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.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 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 le 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.

Connectez-vous pour commenter.

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by