help-octave
[Top][All Lists]
Advanced

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

## Re: Passing a function as parameter

 From: Dan Boghiu Subject: Re: Passing a function as parameter Date: Fri, 16 Nov 2001 09:57:53 +0200

```Define your function "mean" in this way:

function y = mean(f, a, b)
y = (feval(f, a) + feval(f, b))/2.0;
endfunction

and then try:
mean('foo', 2, 3)

Dan

----- Original Message -----
From: "Luca Manini" <address@hidden>
To: <address@hidden>
Sent: Friday, November 16, 2001 10:50
Subject: Passing a function as parameter

> Hi,
>
> is it possible to pass functions to other fucntions?
> The simple example is to write a function that calculate the
> mean (or the definite integral) of that function on a given interval.
>
> in Python is like this:
>
> def foo(x):
>     return x * x
>
> def mean(f, a, b):
>     return (f(a) + f(b))/2.0
>
> print mean(foo, 5, 0)
>
> in Octave "should" be something like:
>
> function y = foo(x)
>   y = x * x;
> endfunction
>
> function y = mean(f, a, b)
>   y = (f(a) + f(b))/2.0;
> endfunction
>
> mean (foo, 5, 0)
>
> or
>
> mean ('foo', 5, 0)
>
> but it does not work.
>
>
> Any hint? TIA, Luca.
>         Please answer (also) directly to address@hidden
>
>
>
> -------------------------------------------------------------
> 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
> -------------------------------------------------------------

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