Search code examples
matlabbessel-functions

Bessel's integral implementation


I'm trying to implement this integral representation of Bessel function of the first kind of order n.

enter image description here

here is what I tried:

t = -pi:0.1:pi;
n = 1;
x = 0:5:20;
A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));
B(t) = integral(A(t),-pi,pi);
plot(A(t),x)

the plot i'm trying to get is as shown in the wikipedia page.enter image description here

it said:

Error using * Inner matrix dimensions must agree.

Error in besselfn (line 8) A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));

so i tried putting x-5;

and the output was:

Subscript indices must either be real positive integers or logicals.

Error in besselfn (line 8) A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));

How to get this correct? what am I missing?


Solution

  • To present an anonymous function in MATLAB you can use (NOT A(t)=...)

    A = @(t) exp(sqrt(-1)*(n*t-x.*sin(t)));
    

    with element-by-element operations (here I used .*).

    Additional comments:

    1. You can use 1i instead of sqrt(-1).

    2. B(t) cannot be the function of the t argument, because t is the internal variable for integration.

    3. There are two independent variables in plot(A(t),x). Thus you can display plot just if t and x have the same size. May be you meant something like this plot(x,A(x)) to display the function A(x) or plot(A(x),x) to display the inverse function of A(x).

    Finally you code can be like this:

    n = 1;
    x = 0:.1:20;
    A = @(x,t) exp(sqrt(-1)*(n*t-x.*sin(t)));
    B = @(x) integral(@(t) A(x,t),-pi,pi);
    for n_x=1:length(x)
        B_x(n_x) = B(x(n_x)); 
    end
    plot(x,real(B_x))