[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing functions as parameters
From: |
etienne grossmann |
Subject: |
Re: Passing functions as parameters |
Date: |
Wed, 19 Apr 2000 18:30:04 +0100 (WEST) |
From: Dejan Beznec <address@hidden>
# Hello!
# I was wondering whether it is possible to pass a valid octave function
# (e.g. sin(x)+log(x^2+x)) as a parameter to another user-defined
# function.
# For example:
# How to pass f(x)=sin(x)+log(x^2+x) as a parameter to:
#
# function myfunction(f)
# ... (perform some calculations with f(x))
# endfuction
#
# Making a separate m file for each function to be passed is not
# acceptable.
# I am not interested in this particular case, but how to pass a function
# in general.
You can pass a string containing the name of the function, and then
use 'feval', as in :
function myfunction(funcname)
x = 3;
y = feval(funcname,x) ;
printf("y = %f\n",y);
end
function z = f(x)
z=sin(x)+log(x^2+x);
end
myfunction("f")
myfunction("f")
y = 2.626027
myfunction("cos")
y = -0.989992
There is also the 'eval(string)' function, which will evaluate
'string'.
You may even declare new functions within 'string' :
eval(["function y = composed_func(x)\n y = cos( sin( x ) ) ;\nend\n"]);
Hth,
Etienne
-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.che.wisc.edu/octave/octave.html
How to fund new projects: http://www.che.wisc.edu/octave/funding.html
Subscription information: http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------