octave-maintainers
[Top][All Lists]
Advanced

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

Re: Bi variate to mono variate functions


From: John W. Eaton
Subject: Re: Bi variate to mono variate functions
Date: Thu, 30 Mar 2006 10:52:49 -0500

On 30-Mar-2006, David Bateman wrote:

| I defined the function
| 
| function y = myfun2(x,c)
|   global p;
|   p = 10;
|   y = 1./(x.^3-2*x-c);
| end
| 
| and then did the following in matlab
| 
| >> global p
| >> p = 5;
| >>  Q = quad(@(x)myfun2(x,p),0,2)
| 
| Q =
| 
|    -0.4605
| 
| >> p = 10;
| >>  Q = quad(@(x)myfun2(x,p),0,2)
| 
| Q =
| 
|    -0.2043
| 
| >>
| 
| I think this makes it pretty clear that the anonymous variable is
| evaluated when the anonymous function is declared...

I think your test is not quite right.  You are not using P in the
expression inside of myfun2, and you are redefining the anonymous
function in each case (so how can you tell whether the parameter is
evaluated when the function is defined or evaluated?).

Maybe a better test would be

  >> type myfun2

  function y = myfun2(x,c)
         y = 1./(x.^3-2*x-c);

  >> p = 5;
  >> f = @(x) myfun2 (x, p);
  >> quad (f, 0, 2)

  ans =

     -0.4605

  >> p = 10;
  >> quad (f, 0, 2)

  ans =

     -0.4605

so the function is defined just once, and it keeps the value
available at the time of the definition instead of picking up the
modified value that is defined later, or

  >> clear all
  >> f = @(x) myfun2 (x, p);
  >> quad (f, 0, 2)
  ??? Undefined function or variable 'p'.

  Error in ==> @(x) myfun2 (x, p)


  Error in ==> quad at 63
  y = f(x, varargin{:});

  >> p = 10

  p =

      10

  >> quad (f, 0, 2)
  ??? Undefined function or variable 'p'.

  Error in ==> @(x) myfun2 (x, p)


  Error in ==> quad at 63
  y = f(x, varargin{:});


I suppose the way to do this is to modify the function
make_anon_fcn_handle in parse.y to perform some magic necessary to
assign initial values to any symbols that are present in the anonymous
function symbol table and are defined as variables in the current
symbol table, skipping those symbols that appear in the argument list
of the anonymous function.  Would that work?  Would it cause trobule
for inline functions (which are implemented using an anonymous
function).

jwe



reply via email to

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