problem of function handles, thx

2 views (last 30 days)
for instance,
parms=ones(2,1);
myfunc = @(parms) X.*parms(1)+Y.*parms(1);
I can directly indicate 'myfunc' in 'fmincon'. [parms,favl]=fmincon(myfunc,parms,....);
However, if I want to separately write 'myfunc' in a function script and then quote it. [parms,favl]=fmincon(myfunc,parms,....); the 'fmincon' will report 'Not enough input in myfunc'.
X and Y are variables stored in workspace, they are not unknown parameters, only parms(1) and parms(2) required estimation.
anyone can tell me how to solve this problem? many thanks!

Accepted Answer

Sean de Wolski
Sean de Wolski on 14 Jan 2015
Edited: Sean de Wolski on 14 Jan 2015
You need an @ before myfunc, otherwise it tries to evaluate it:
@myfunc % reference to function myfunc; do not evaluate
myfunc % when myfunc is its own file tries to run it
myfunc % when myfunc is a function handle (variable), then myfunc is already a reference to whatever
  4 Comments
Zhixiao
Zhixiao on 15 Jan 2015
How can I call function y =myfunc2(x) in fmincon? e.g. 'myfunc2'?
Sean de Wolski
Sean de Wolski on 15 Jan 2015
fmincon(@myfunc2,etc.)

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!