Thread Subject:
Functions as parameters to functions (higher-order functions)

Subject: Functions as parameters to functions (higher-order functions)

From: Andy Moran

Date: 28 Jun, 2000 11:58:38

Message: 1 of 11


Is it possible to pass in functions as parameters in Matlab? Is it possible
to return functions as results?

Cheers,

Andy

--
Andy Moran
PacSoft x7557
Computer Science & Engineering +1 503 748 7557
Oregon Graduate Institute http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: branch@mathworks.com (Mary Ann Branch)

Date: 28 Jun, 2000 15:27:15

Message: 2 of 11

You can pass a string, the function name, to another function.
Then use FEVAL with that string to evaluate the function.
This is how FZERO, FMINBND, etc all work.



HTH.
-- Mary Ann





In article <395A4ADE.16FEC0D8@cse.ogi.edu>, moran@cse.ogi.edu says...
>
> Is it possible to pass in functions as parameters in Matlab? Is it possible
> to return functions as results?
>
> Cheers,
>
> Andy
>
> --
> Andy Moran
> PacSoft x7557
> Computer Science & Engineering +1 503 748 7557
> Oregon Graduate Institute http://www.cse.ogi.edu/~moran
>

Subject: Functions as parameters to functions (higher-order functions)

From: Andy Moran

Date: 28 Jun, 2000 16:48:37

Message: 3 of 11

Mary Ann Branch wrote:

> You can pass a string, the function name, to another function.
> Then use FEVAL with that string to evaluate the function.
> This is how FZERO, FMINBND, etc all work.

Thanks, that's partly what I want. But, really, I want to be able
to partially apply functions and return functions as results. Any such
mechanisms in Matlab?

Cheers,

Andy

--
Andy Moran
PacSoft x7557
Computer Science & Engineering +1 503 748 7557
Oregon Graduate Institute http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: Nabeel

Date: 29 Jun, 2000 11:00:54

Message: 4 of 11

Hi Andy,

> Thanks, that's partly what I want. But, really, I want to be able
> to partially apply functions and return functions as results. Any such
> mechanisms in Matlab?

What do you mean by "partially apply functions and return functions as
results"? Can you give us an example of what you'd like to be able to
do?

-- Nabeel

Subject: Functions as parameters to functions (higher-order functions)

From: michaels@ece.ece.arizona.edu (Michael Schweisguth)

Date: 29 Jun, 2000 16:53:08

Message: 5 of 11

you coudl pass in inline functions and pass out inline functions
if performance isnt important.

or, if the output functions are pretty much fixed, you can just pass
out a function name, or even generate an m-file in real time.

Andy Moran (moran@cse.ogi.edu) wrote:

: Is it possible to pass in functions as parameters in Matlab? Is it possible
: to return functions as results?

: Cheers,

: Andy

: --
: Andy Moran
: PacSoft x7557
: Computer Science & Engineering +1 503 748 7557
: Oregon Graduate Institute http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: Andy Moran

Date: 29 Jun, 2000 16:17:16

Message: 6 of 11

Nabeel wrote:

> What do you mean by "partially apply functions and return functions as
> results"? Can you give us an example of what you'd like to be able to
> do?

Well, imagine you have a number of different control algorithms that you want
to run in a generic loop, assigning the name of the control function to the
variable "control_law":

   for i: 1 to interval
      e = command (x, e);
      u = feval(control_law, x, e, u);
      x = dynamics (x, u);
   end

(Here x and e are state vectors, and u is a control vector.) The problem is
that each algorithm has quite different local state that needs to be
initialised, so the different control algorithm functions, while they all take
two state vectors and a control vector and return a control vectors, all have
many more additional (and different) parameters. If I could write an
initialisation function that looked like:

   control = initialise(control_law);

where "control" is now a function that takes two state vectors and a control
vector and returns a control vector, and "control_law" is as before. Then I
can just say:

   for i: 1 to interval
      e = command (x, e);
      u = control(x, e, u);
      x = dynamics (x, u);
   end

which is much more elegant. In the former case, I still need to do some
initialisation, but I need to then pack it up in a record and pass that in as
parameter in the feval line. (Or produce m-files on the fly, which I am
loathe to do.)

Cheers,

Andy

--
Andy Moran
PacSoft x7557
Computer Science & Engineering +1 503 748 7557
Oregon Graduate Institute http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: Jordan Rosenthal

Date: 30 Jun, 2000 10:26:43

Message: 7 of 11

Andy,

This sounds like a perfect place to use object-oriented techniques.

Jordan

"Andy Moran" <moran@cse.ogi.edu> wrote in message
news:395BD8FC.77060157@cse.ogi.edu...
> Nabeel wrote:
>
> > What do you mean by "partially apply functions and return functions as
> > results"? Can you give us an example of what you'd like to be able to
> > do?
>
> Well, imagine you have a number of different control algorithms that you
want
> to run in a generic loop, assigning the name of the control function to
the
> variable "control_law":
>
> for i: 1 to interval
> e = command (x, e);
> u = feval(control_law, x, e, u);
> x = dynamics (x, u);
> end
>
> (Here x and e are state vectors, and u is a control vector.) The problem
is
> that each algorithm has quite different local state that needs to be
> initialised, so the different control algorithm functions, while they all
take
> two state vectors and a control vector and return a control vectors, all
have
> many more additional (and different) parameters. If I could write an
> initialisation function that looked like:
>
> control = initialise(control_law);
>
> where "control" is now a function that takes two state vectors and a
control
> vector and returns a control vector, and "control_law" is as before. Then
I
> can just say:
>
> for i: 1 to interval
> e = command (x, e);
> u = control(x, e, u);
> x = dynamics (x, u);
> end
>
> which is much more elegant. In the former case, I still need to do some
> initialisation, but I need to then pack it up in a record and pass that in
as
> parameter in the feval line. (Or produce m-files on the fly, which I am
> loathe to do.)
>
> Cheers,
>
> Andy
>
> --
> Andy Moran
> PacSoft
x7557
> Computer Science & Engineering +1 503 748
7557
> Oregon Graduate Institute
http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: Andy Moran

Date: 30 Jun, 2000 09:25:51

Message: 8 of 11

Jordan Rosenthal wrote:

> Andy,
>
> This sounds like a perfect place to use object-oriented techniques.

Not really; OO would be just as clumsy as the solution I use now ...

Cheers,

Andy

--
Andy Moran
PacSoft x7557
Computer Science & Engineering +1 503 748 7557
Oregon Graduate Institute http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: Jordan Rosenthal

Date: 30 Jun, 2000 13:13:22

Message: 9 of 11

Andy,

Hmmm. I guess I must be misunderstanding your problem because, to me, it
seems really well-suited to object oriented techniques. :)

Well, good luck with whatever you wind up doing.

Jordan

"Andy Moran" <moran@cse.ogi.edu> wrote in message
news:395CCA0F.E96C3A79@cse.ogi.edu...
> Jordan Rosenthal wrote:
>
> > Andy,
> >
> > This sounds like a perfect place to use object-oriented techniques.
>
> Not really; OO would be just as clumsy as the solution I use now ...
>
> Cheers,
>
> Andy
>
> --
> Andy Moran
> PacSoft
x7557
> Computer Science & Engineering +1 503 748
7557
> Oregon Graduate Institute
http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: michaels@ece.ece.arizona.edu (Michael Schweisguth)

Date: 1 Jul, 2000 02:24:32

Message: 10 of 11

i got the same idea, sort of...

basically, if you have an object with method "munch data" than
the function can be pretty optimized to invoke the proper method.


Jordan Rosenthal (jr@ece.gatech.edu) wrote:
: Andy,

: Hmmm. I guess I must be misunderstanding your problem because, to me, it
: seems really well-suited to object oriented techniques. :)

: Well, good luck with whatever you wind up doing.

: Jordan

: "Andy Moran" <moran@cse.ogi.edu> wrote in message
: news:395CCA0F.E96C3A79@cse.ogi.edu...
: > Jordan Rosenthal wrote:
: >
: > > Andy,
: > >
: > > This sounds like a perfect place to use object-oriented techniques.
: >
: > Not really; OO would be just as clumsy as the solution I use now ...
: >
: > Cheers,
: >
: > Andy
: >
: > --
: > Andy Moran
: > PacSoft
: x7557
: > Computer Science & Engineering +1 503 748
: 7557
: > Oregon Graduate Institute
: http://www.cse.ogi.edu/~moran

Subject: Functions as parameters to functions (higher-order functions)

From: Gregory Johnston

Date: 21 Jun, 2012 20:49:07

Message: 11 of 11

Actually, it's a perfect place to use functional programming techniques (http://en.wikipedia.org/wiki/Functional_programming). In general, procedural programming and functional programmings are two distinct sets, and OO programming is a subset of procedural.

Also note that Matlab does have functional features. Consider arrayfun (http://www.mathworks.com/help/techdoc/ref/arrayfun.html) and cellfun (http://www.mathworks.com/help/techdoc/ref/cellfun.html) which take a function handle as their first parameter. Underlying these two functions' implementations may be a call to feval, I don't know. But given that Matlab is loosely typed, I'd assume that you could write a function to take *a* parameter, and then use it as a function if it's a function...



"Jordan Rosenthal" <jr@ece.gatech.edu> wrote in message <8jia66$e8g$1@news-int.gatech.edu>...
> Andy,
>
> This sounds like a perfect place to use object-oriented techniques.
>
> Jordan
>

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us