help-octave
[Top][All Lists]
Advanced

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

Re: Passing functions as parameters


From: A Scottedward Hodel
Subject: Re: Passing functions as parameters
Date: Wed, 19 Apr 2000 14:48:38 -0500

No, you need to define a value of x in order to evaluate the string.

You could alternatively do something like (try-catch syntax probably
messed up here):

function y = arbfunc(x,f)
## y = arbfunc(x,f)
## evaluate an arbitrary function f at x
##  x: value
##  f: function of x (string)
  try
    y = eval(f);
  catch
    error("Trouble evaluating %s at %g",f,x);
  endtrycatch
endfunction

A Scottedward Hodel wrote:

 Short answer: pass it as a string,
         str = "sin(x)+log(x^2+x) "
 then use
         fx = eval(str);
 to evaluate the function.

 Not pretty, but it works.
Are you sure? I got the following:

octave:5> str = "sin(x)+log(x^2+x)"
str = sin(x)+log(x^2+x)
octave:6> fx = eval(str);
error: `x' undefined near line 5 column 5
error: evaluating expression near line 5, column 5
error: evaluating argument list element number 1
error: evaluating index expression near line 5, column 1
error: evaluating binary operator `+' near line 5, column 7
error: evaluating assignment expression near line 5, column 7
error: evaluating index expression near line 6, column 6
error: evaluating assignment expression near line 6, column 4
I have Octave version 2.0.16. Maybe this feature is working only in a
development version?

        Kind regards
                Dejan

--
A S Hodel Assoc. Prof. Dept Elect and Computer Eng, Auburn Univ,AL 36849-5201
On leave at NASA Marshall Space Flight Center (256) 544-1426
Address until 31 July 2000:Mail Code TD-55, MSFC, Alabama, 35812
http://www.eng.auburn.edu/~scotte



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



reply via email to

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