Pass variables during minimization routine

3 visualizzazioni (ultimi 30 giorni)
Jeff Ack
Jeff Ack il 21 Lug 2016
Commentato: Jeff Ack il 26 Lug 2016
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!

Risposte (2)

Sean de Wolski
Sean de Wolski il 21 Lug 2016

Jeff Ack
Jeff Ack il 26 Lug 2016
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 Commento
Jeff Ack
Jeff Ack il 26 Lug 2016
OK, I found a good example here: Passing Extra Parameters
If I have any additional questions, I will update this post.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by