fminbnd with function that takes multiple inputs
22 views (last 30 days)
Show older comments
Hi,
my code includes the following
k % vector
kmin % minimum value in the vector
kmax % max value in the vector
there is a function
vf2(k(i),j)
so it takes two inputs. A particular value from the vector k, and another scalar j. What is the correct syntax for fminbnd for this function ?
is this correct?
k1 = fminbnd(@vf2,kmin,j,kmax,j)
in other words I wish to find the minimum value of the function vf2, in the domain k, for some given j that takes as input.
The documentation at least to me, is not that clear so i want to clarify this point.
0 Comments
Answers (2)
Jian Wei
on 29 Jul 2014
Given the function vf2 you defined, you can try the following code to find the minimum value of the function vf2 in the interval [kmin, kmax] with the given value of j.
func = @(k) vf2(k,j); % j is the value you want to pass to the function vf2
k = fminbnd(func, kmin, kmax);
0 Comments
Geoff Hayes
on 29 Jul 2014
x = fminbnd(fun,x1,x2) returns a value x that is a local minimizer of the function that is described in fun in the interval x1 < x < x2. fun is a function_handle.
x = fminbnd(fun,x1,x2,options) minimizes with the optimization parameters specified in the structure options. You can define these parameters using the optimset function. fminbnd uses these options structure fields...
Only three-four inputs are allowed - the function handle, the lower bound on the interval, the upper bound, and the optional options. So your example of Your example of
k1 = fminbnd(@vf2,kmin,j,kmax,j)
would not be correct because the first j would be interpreted as the x2, the kmax as the options and the final j as something else.
That isn't to say that you can't do what you want. In R2014a, at lines 215 and 292 of fminbnd.m, your passed in function is evaluated as either
fx = funfcn(x,varargin{:});
or
fu = funfcn(x,varargin{:});
where varargin{:} is a variable argument input (varargin) array of other parameters to your function.
If your signature for vf2 is something like
function val=vf2(k,j)
then you pass j into your function through fminbnd as follows
k1 = fminbnd(@vf2,kmin,kmax,optimset,j);
Note that optimset is used for the options input to the function, and so all options should default to what they were before. See the above link for details on this parameter.
And that should be sufficient. j will be passed via varargin{:} as the second input into your vf2 function.
Try the above and see what happens! An alternative is to parameterize your function. See Parameterizing Functions for details.
See Also
Categories
Find more on Optimization 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!