MATLAB: Dear Reserach Team! I need to calculate numerically definite integral of a function g(y), where g(y) is a numerical integral of f(x,y). Please advise how to do it (the details are shown below). Many thanks in advance!.

numerical integration

I made a "toy" problem.
f=@(x,y)x.*sin(y.*x)./y;
g=@(y)integral(@(x)f(x,y),0,1);
s=integral(g,0,2,'ArrayValued',true).
This works, no problem. f(x,y) has a removable singularity at y=0 which is successfully handled by Matlab.
In my real problem f(x,y) also has a removable singularity but f(x,y) contains a combination of Bessel functions so that
I decided to write a case-defined function which (for my "toy" problem) has the form
function fun_f=f
if y ==0
fun_f = @(y,x)x.^2;
else
fun_f = @(y,x)x.*sin(y.*x)./y;
end
end
The problem I have is how to compute s in case f(x,y) is given by the function decsribed above.

Best Answer

function main
g = @(y) integral(@(x)f(x,y),0,1);
s = integral(g,0,2,'ArrayValued',true)
end
function fun_f = f(x,y)
if y==0
fun_f = x.^2;
else
fun_f = x.*sin(x.*y)./y;
end
end