필터 지우기
필터 지우기

How to get an additional output from fminsearch

조회 수: 7 (최근 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 CenterFile Exchange에서 Solver 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