How can I integrate a function having another function (in this case - Bessel function) depend on the variable ?
    4 views (last 30 days)
  
       Show older comments
    
My integrand has a bessel function dependent on the same variable. So i would start off by writing int(bessel(0,(2*x)),x..). But the bessel function does not have x defined which needs to be changing from a(lower lim) to b(upper lim) of the integral. It is only later in the command that i define x as the variable for integration. How do I overcome this
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 4 Sep 2017
        
      Edited: Walter Roberson
      
      
 on 4 Sep 2017
  
      syms x
int(besselj(0, 2*x), x, 0, 1)
int(besselj(0, 2*x), x, 0, x)
Note: if you try
int(besselj(0, 2*x), x, x, 1)
then the symbolic engine does not know a closed form solution for that. A closed form solution does exist, but the symbolic engine is not strong enough to find it. However if you manually rewrite besselj(0,2*x) as its hypergeometric equivalent, hypergeom([], [1], -x^2) then the symbolic engine can integrate that.
3 Comments
  Walter Roberson
      
      
 on 4 Sep 2017
				You can also use
integral(@(x) besselj(0, 2*x), 0, 1)
for the case where you have numeric upper and lower bounds.
Your original question asked about int which is symbolic integration. In MATLAB, variables do not exist by default, so if you use
int(besselj(0, 2*x), 0, 1)
without x having been assigned a value, then MATLAB will complain about the variable not being known. The statement
syms x
is an abbreviation for
x = sym('x')
which assigns x a value, with the value being the symbol x . Symbols live inside the symbolic engine, the formal name for which is MuPAD. The name x at the MATLAB level will effectively become a pointer to something inside the symbolic engine. (Note: what I here call "pointer to something inside the symbolic engine" is an implementation detail, and Mathworks does not give a name to these kinds of pointers. Strictly speaking, they are not "pointers" but rather "cursors")
So after you have done
syms x
then when you use
besselj(0, 2*x)
then x has been defined at the MATLAB level, and its value can be looked up (to find the pointer to the symbol). The symbolic engine will be asked to do the multiplication by 2, which will result in a pointer to a symbol inside the symbolic engine. The the MATLAB function resolver will look and see that it is being invoked with a symbolic argument, so it will know to resolve besselj to the symbolic routine for besselj, which will then be invoked with numeric 0 and the pointer to symbolic value. besselj will see if that combination matches any special cases, and will find that it does not, so besselj will construct a symbolic expression inside the symbolic engine to besselj(0, <the pointer to 2*x>) and will return a pointer to that. int() of that expression will see that it has been passed a symbolic pointer and will ask the symbolic engine to resolve the computation.
If, though, you use
integral(@(x) besselj(0, x), 0, 1)
then you have a completely different situation. That is equivalent to
temp = @(x) besselj(0, x);
integral(temp, 0, 1)
The syntax
@(list_of_variables) expression
constructs an "anonymous function". It is quite similar to being as-if you had written
temp = @temporary_function
integral(temp, 0, 1)
function result = temporary_function(x)
  result = besselj(0, x);
end
which says that the function temporary_function is to be located and a pointer to it is to be constructed, and then integral() is to be invoked, passing in the pointer to the function as the first argument. These kinds of pointers-to-functions are used a lot in MATLAB, and have the name "function handle".
When you create an anonymous function, MATLAB does not go to the trouble of creating a formal function and writing the code to disk. In MATLAB, as soon as you first refer to a function that is on disk, the code for the function is retrieved and is parsed into an intermediate "threaded interpreted code" which is what is used for execution, with the code being held in memory. When you construct an anonymous function, MATLAB applies its parser to the body of the anonymous function and constructs threaded interpreted code for it, holding the code for it in memory, without having written anything to disk. (There are some important differences for anonymous functions that I am not describing here.)
integral() is one of the numeric integration routines. It requires a function handle as the first argument, and does adaptive quadrature numeric integration. https://www.mathworks.com/help/matlab/ref/integral.html
More Answers (0)
See Also
Categories
				Find more on Bessel functions in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
