help-octave
[Top][All Lists]

## Re: Function as argument of a function.

 From: Michael Creel Subject: Re: Function as argument of a function. Date: Wed, 13 Oct 2004 15:50:49 +0200 User-agent: KMail/1.7

```
On Wednesday 13 October 2004 12:23, Fredrik Bulow wrote:
> Hi all!
>
> Is there any way to send a function as an argument to another function?
>
> I want to do something looking like this:
>
> function minima = find_min(f)
>
> where f is not a matrix, but a function. Calls of this function could
> look like:
>
> find_min(sin)
> find_min(my_function) etc...
>
> /Fredrik

The short answer is yes, e.g., feval(f, args) is using f as an argument of
feval. For a longer answer, that may not be exactly what you want, in the
bfgsmin_example.m file in octave-forge/main/optim, there is the following
snippet:

function obj_value = objective1(x, y)
z = y - ones(size(y));
obj_value = log(1 + x'*x) + log(1 + z'*z);
endfunction

# Check bfgsmin, illustrating variations

x0 = ones(2,1);
y0 = 2*ones(2,1);
control = {10,2,1,1};  # maxiters, verbosity, conv. reg., arg_to_min
[theta, obj_value, convergence] = bfgsmin("objective1", {x0, y0}, control);

So this is using the BFGS algorithm to find a minimum, which in this case is
the single global minimum.

M.

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```