Pass variables during minimization routine

3 ビュー (過去 30 日間)
Jeff Ack
Jeff Ack 2016 年 7 月 21 日
コメント済み: Jeff Ack 2016 年 7 月 26 日
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!

回答 (2 件)

Sean de Wolski
Sean de Wolski 2016 年 7 月 21 日

Jeff Ack
Jeff Ack 2016 年 7 月 26 日
How should I go about nesting the functions? Is the outer function the one that gets minimized or the inside one? I'm thinking that comparing the current iteration to "fbest" goes in the outer function, but I'm not certain how.
  1 件のコメント
Jeff Ack
Jeff Ack 2016 年 7 月 26 日
OK, I found a good example here: Passing Extra Parameters
If I have any additional questions, I will update this post.

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

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by