help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function as argument of a function.


From: David Bateman
Subject: Re: Function as argument of a function.
Date: Wed, 13 Oct 2004 16:16:22 +0200
User-agent: Mutt/1.4.1i

In recent versions of octave (2.1.60 for example) you can use
inline functions or function handles.. EG...

function minima = find_min(f)
  if (!isa(f,"inline function") || !isa(f,"function handle"))
    error ("find_min: argument must be inline or function handle");
   else
     t0 = f (0);
     minima = ???
   endif
endfucntion

fcn1 = inline ("abs(1+x)");
fcn2 = @ sin;
fcn3 = @ (x) abs(1+x);
fcn1(1)
ans  = 2
fcn2(1)
ans = 0.84147
fcn3(1)
ans  = 2 
y1 = find_min (fcn1);
y2 = find_min (fcn2);
y3 = find_min (fcn3);

which is perhaps arguably cleaner than using feval...

Regards
D.


According to Michael Creel <address@hidden> (on 10/13/04):
> 
> 
> 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
> 
> printf("\nEXAMPLE 1: Numeric gradient\n");
> 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
> -------------------------------------------------------------

-- 
David Bateman                                address@hidden
Motorola CRM                                 +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax) 
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary



-------------------------------------------------------------
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
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]