[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Integrating functions with with constant arguments
From: |
Geordie McBain |
Subject: |
Re: Integrating functions with with constant arguments |
Date: |
Tue, 15 Aug 2006 15:57:48 +1000 |
On Tue, 2006-08-15 at 15:29 +1000, John Dalton wrote:
> QUESTION 1:
>
> I'm trying to run the following function under octave:
>
> function z = test_handle(t)
> z = quad(@(x) pow2(x, t), 0.5, 1);
> end
>
> It is an integration with a constant argument in the integrand function.
> It works in Matlab, but octave 2.1.73 fails with the
> following error.
>
> error: `t' undefined
Try this idiom:
octave:5> global t;
octave:8> t = 1;
octave:9> function p = fun2 (x); global t; p = pow2 (x, t); endfunction
octave:10> function z = test_handle (); z = quad (@fun2, 0.5, 1);
endfunction
octave:11> test_handle
ans = 0.75000
The parameter t needs to be declared global inside the integrand
function.
> According to this post:
> http://velveeta.che.wisc.edu/octave/lists/archive//octave-maintainers.2002/msg00110.html
> it should work. I've also tried making 't' global ("global t") and
> also tried using a global intermediate variable between the argument t
> and its use as a parameter to pow2. If pow2 is replaced with a user
> defined function (say fun2) the code fails saying "fun2" is not
> defined. It's as if the evaluation of quad's argument function is
> not seeing the parent's environment.
>
> QUESTION 2:
>
> As an aside, I also did the following test:
>
> octave:5> global a=3;is_global('a')
> ans = 0
>
> I would have though the answer should be '1'.
>
>
> Sorry if these are FAQs. I have spent a day looking for an
> answer but have not found one (apart from the email above).
> Any help will be appreciated very much. (Thanks in advance.)
>
> Regards
> John Dalton
>
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
--
Geordie McBain
www.aeromech.usyd.edu.au/~mcbain