help-octave
[Top][All Lists]
Advanced

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

Re: bessel functions with octave


From: Bård Skaflestad
Subject: Re: bessel functions with octave
Date: Wed, 17 Aug 2011 20:24:59 +0200

On Wed, 2011-08-17 at 19:51 +0200, john wrote:
> Hi,
> 
> concerning my question from 16/08/2011

> [i.e., "How to compute integral approximation of J0(x) by means of QUADL"]

>  I did the following:
> 
> function y=bintegra(q);
> global x
> y=cos(x*(sin(q)));
> endfunction;
> when I run the program I got an error  * no conform operator opt1 1x1 opt 2
> 13x1.
> I changed the x with 3 and then it works.

Without a bit more context (i.e., what, precisely, did you type into
Octave), it's a bit difficult to say what's happening here.  First of
all, what is the (type and) value of the GLOBAL 'x'?  If the 'x' truly
is a (numeric) scalar then the above *should* work, albeit a little
circuitous compared to what I'll demonstrate below.  In my opinion, of
course...

> 
> How can I import the x in this function from the begin input x=3 I used
>  GLOBAL

Is there a reason you can't simply use anonymous functions?  The above
could be (simpler, in my opinion) be implemented as

    function y = bintegra(q, x)
      y = cos(x .* sin(q));
    endfunction

and then you could evaluate the integral as

    x  = 3;                      # or something
    F  = @(q) bintegra(q, x)     # integrand, "captures" above 'x'
    J0 = quadl(F, 0, pi) / pi

On the other hand, in this case you don't really *need* the 'bintegra'
function and can say something akin to

    x  = 3;                      # or something
    F  = @(q) cos(x .* sin(q));  # integrand, "captures" above 'x'
    J0 = quadl(F, 0, pi) / pi;

or, in two lines if you want to,

    x  = 3; # or something
    J0 = quadl(@(q) cos(x .* sin(q)), 0, pi) / pi;

I personally try to avoid GLOBAL as much as possible.  With the
introduction of function handles and (occasionally creative) use of
anonymous functions (and closures derived from these), avoiding GLOBAL
is usually feasible -- or even easy.


Sincerely,
-- 
Bård Skaflestad <address@hidden>
SINTEF ICT, Applied Mathematics



reply via email to

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