Pass variables during minimization routine
3 views (last 30 days)
Show older comments
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 Comments
Answers (2)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!