Pass variables during minimization routine
조회 수: 3 (최근 30일)
이전 댓글 표시
Here's what I'm trying to do
- Create a function that I wish to minimize.
- Set an initial value of fbest=10000000 (or some other very large number).
- Use one of the preset minimization routines to minimize my function.
- During the minimization routine, each time the function is evaluated, if the output is less than "fbest", the parameter being used is displayed, and this output becomes "fbest". This way, I can look at the program as it runs and see how the values of the parameters change.
I created a really simple example to illustrate:
Right now I have:
p=zeros(3,1);
fbest=10000000;
f=@(p) fn_testfbest( p, fbest );
[x,fval,exitflag,output]=fminunc(f, p);
end
With a function to minimize of:
function [ obj ] = fn_testfbest( p, fbest )
obj = (p(1)-1)^2 + (p(2)+1)^2 + (p(3))^2;
if obj < fbest
fbest=obj
end
end
This isn't working, because the value of fbest was "set" when I defined the anonymous function (F=@p...) and any changes to it are lost each time the function runs. How do I allow fbest to change with each iteration and have the new value be saved?
Thanks!
댓글 수: 0
답변 (2개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!