필터 지우기
필터 지우기

How to add variables to the Plot Fcn?

조회 수: 2 (최근 30일)
Denis Perotto
Denis Perotto 2018년 9월 11일
편집: Adam Danz 2018년 9월 12일
I created a custom Plot Function for 'fminsearch':
opts = optimset('Display','iter','TolFun',1e-5,'MaxIter',100,'PlotFcns',@outf);
[v1, fval] = fminsearch(funtol,v1,opts);
function stop = outf(x, optimValues, state)
...<Plot some stuff>...
end
How can I add some variables to the Plot Function? For example:
function stop = outf(x, optimValues, state, var1, var2)
I don't know how to call it then in 'optimset' properly, because, it seems, there are no additional varibles allowed:
opts = optimset('PlotFcns',@outf<Don't know what to write here>);

채택된 답변

Adam Danz
Adam Danz 2018년 9월 11일
편집: Adam Danz 2018년 9월 12일
One solution is to write an anonymous function and define the extra values prior to creating the anonymous function. Those extra values will then be stored in that function until it's deleted or overwritten. Example:
% Assuming your outf function is within the function or script
var1 = 0;
var2 = 1;
outfAnon = @(x) outf(x, optimValues, state, var1, var1);
opts = optimset('PlotFcns',outfAnon, ...)
For more information on this method or to see other methods including the use of nested functions and global vars (avoid), see this : https://www.mathworks.com/help/optim/ug/passing-extra-parameters.html
[EDIT] Removed a mistake where I included ' @' symbol when entering outfAnon in the opts line.
  댓글 수: 2
Denis Perotto
Denis Perotto 2018년 9월 12일
That didn't help.
This one works:
clc; close all; clear;
f = @(x) (1-x(1)).^2+100.*(x(2)-x(1).^2).^2;
x0 = [2 2];
opts = optimset('Display','iter','TolX',1e-5,'PlotFcn',@outf);
[x1, f1] = fminsearch(f, x0, opts);
function stop = outf(x, optimValues, state)
stop = false;
end
This one, as you suggested, doesn't:
clc; close all; clear;
f = @(x) (1-x(1)).^2+100.*(x(2)-x(1).^2).^2;
x0 = [2 2];
var = 1;
outfanon = @(x, optimValues, state) outf(x, optimValues, state, var);
opts = optimset('Display','iter','TolX',1e-5,'PlotFcn',@outfanon);
[x1, f1] = fminsearch(f, x0, opts);
function stop = outf(x, optimValues, state, var)
stop = false;
end
I get
Error: File: fmintest.m Line: 9 Column: 57
"outfanon" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
Adam Danz
Adam Danz 2018년 9월 12일
Your implementation of my suggestion didn't work because you used '@' when calling outfanon (which I also mistakenly did in my example). The '@' symbol defines an anonymous function. When you use the function handle you shouldn't include that symbol.
If you remove that and use this line instead, the code works.
opts = optimset('Display','iter','TolX',1e-5,'PlotFcn',outfanon);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Weiss
Alan Weiss 2018년 9월 12일
Perhaps you are looking for how to pass extra parameters.
Alan Weiss
MATLAB mathematical toolbox documentation

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by