help-octave
[Top][All Lists]
Advanced

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

Re: quadrature of funct. with parameters


From: Guillem Borrell Nogueras
Subject: Re: quadrature of funct. with parameters
Date: Mon, 20 Mar 2006 13:31:10 +0100
User-agent: KMail/1.9.1

Function handles are not 100% compatible in matlab and octave.  The second 
piece of code is valid in Matlab but isn`t in Octave.  Matlab defines two 
variable spaces, base and caller.  The base workspace is in the interpreter 
and the caller is the one in the function.  Global variables live in both 
workspaces.

In Matlab, the anonymous function looks for the arguments in the base 
workspace while Octave`s fh create their own caller workspace.  This is why 
you can pass a scalar to the anonymous function but a variable; you are 
passing a variable that lives in the base workspace.

If you try it in Matlab:
>> nb=2

nb =

     2

>> f2=@(x) foo(x,nb);
>> f2(1)

ans =

     2

One possible workaround would be declare nb as global, but Octave`s fh ignores 
even global variables.

It would be nice to have a better compatiblity but I don`t know if this 
behaviour is intentional.  Any developer can give us some more info?

guillem

On Monday 20 March 2006 11:49, Gorazd Brumen wrote:
> hi,
> i have encountered another problem:
>
> if one writes:
>
> f2=@(x) foo(x,2)
>  f2 =
>
>  @(x) foo (x, 2)
>
>  octave:5> f2(4)
>  ans = 8
>
>
> then it is ok, but if one does
>
> nb = 2
> f2=@(x) foo(x,nb)
>  f2 =
>
>  @(x) foo (x, nb)
>
>  octave:5> f2(1)
>  error: `nb' undefined near line 20 column 18
> error: evaluating argument list element number 2
> error: called from `?unknown?'
>
>
>
> is there a way out without global variables?
>
> thank a lot, g
> .
>
> address@hidden wrote:
> > Hi again.
> >
> > With the anonymous function you are creating a function handle that
> > is assigned to a variable.  Passing a function as an argument
> > with a string is valid only when the function is defined as a
> > *real* function.
> >
> > Look at this piece of code:
> >
> > octave:3> function y=foo(x,a)
> >
> >>y=x*a;
> >>end
> >
> > octave:4> f2=@(x) foo(x,2)
> > f2 =
> >
> > @(x) foo (x, 2)
> >
> > octave:5> f2(4)
> > ans = 8
> > octave:6> quad(f2,0,2) %you must pass the variable that contains
> >                        %the function handle
> > ans = 4
> >
> > On Fri, Mar 17, 2006 at 08:30:43PM +0100, Gorazd Brumen wrote:
> >>Hi Guillem,
> >>
> >>Yes, actually parameteres can be defined, but i cant get the thing
> >>working. If I write
> >>
> >>f2 = @(x) f1(x,2);
> >>f2 (2)
> >>f2 (3)
> >>quad ('f2',0,1)
> >>
> >>where f1 is defined in a separate file, than the first three lines work,
> >>but the 4th doesnt. Can you give me hint what i am doing wrong?
> >>
> >>
> >>thanks,
> >>g.
> >>
> >>Guillem Borrell Nogueras wrote:
> >>>Hi
> >>>
> >>>Yu can use an anonymous function to reduce the variables if some
> >>> parameter is known
> >>>
> >>>f2= @(x) f(x,2)
> >>>
> >>>guillem
> >>>
> >>>On Friday 17 March 2006 14:38, Gorazd Brumen wrote:
> >>>>Hi all,
> >>>>
> >>>>Is there a way to do numeric quadrature of a function over
> >>>>one variables if the function takes as arguments several parameters,
> >>>>i.e. f = f(x,a), and i just want a quadrature over x.
> >>>>Aja, and is there a solution not using global variables?
> >>>>
> >>>>
> >>>>thanks a lot,
> >>>>G.
> >>
> >>--
> >>Gorazd Brumen
> >>Mail: address@hidden
> >>WWW: http://valjhun.fmf.uni-lj.si/~brumen
> >>PGP: Key at http://pgp.mit.edu, ID BCC93240
> >>
> >>
> >>
> >>-------------------------------------------------------------
> >>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
> > -------------------------------------------------------------

-- 
No llores por el dia que has perdido
o las lagrimas te ocultaran la luz de las estrellas.

        R.Tagore

Guillem Borrell Nogueras
WEBSITE
   http://torroja.dmt.upm.es:9673/Guillem_Site/
BLOG
   http://torroja.dmt.upm.es:9673/Guillem_Borrell/
EMAIL
   guillemborrell_at_gmail.com (personal)
   guillem_at_torroja.dmt.upm.es (CFD Lab. ETSIA)



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