フィルターのクリア

How to get an additional output from fminsearch

6 ビュー (過去 30 日間)
Erich
Erich 2023 年 9 月 29 日
コメント済み: Erich 2023 年 9 月 30 日
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 件のコメント
Torsten
Torsten 2023 年 9 月 29 日
編集済み: Torsten 2023 年 9 月 29 日
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 2023 年 9 月 30 日
編集済み: Erich 2023 年 9 月 30 日
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.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 9 月 29 日
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 件のコメント
Walter Roberson
Walter Roberson 2023 年 9 月 30 日
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 2023 年 9 月 30 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by