I am numerically evaluating the double integral using integral2 matlab command. My code is as follows:
l1=0; m1=0;
funn = @(theta,phi)(Y_hsph_new(l1,m1,theta,phi));
q = integral2(funn,0,pi/2,0,2*pi);
where 'Y_hsph_new' is my own designed MatLab function written as :
function [ YYY ] = Y_hsph_new(l1,m1,theta,phi)
% OUTPUT: (I x (N+1)^2) matrix
I = length(theta);
Y=zeros(I,(l1+1)^2);
for i=1:I
k=1;
for n=0:l1
P=legendre(n,2*cos(theta(i))-1); % (2*x-1) for transformation
for m=-n:n % degree
if m>0
Y(i,k)=(sqrt(((2*n+1)*factorial(n-abs(m)))/((2*pi)*factorial(n+abs(m)))))*P(abs(m)+1)*sqrt(2)*cos(m*phi(i));
elseif m == 0
Y(i,k)=(sqrt(((2*n+1)*factorial(n))/((2*pi)*factorial(n))))*P(1);
else
Y(i,k)=(sqrt(((2*n+1)*factorial(n-abs(m)))/((2*pi)*factorial(n+abs(m)))))*P(abs(m)+1)*sqrt(2)*sin(abs(m)*phi(i));
end
k=k+1;
end
end
end
YYY = Y(:,m1+l1+1+(l1)^2);
I have used Y_hsph_new function in my other usual codes and it is working fine. I believe perhaps the function code is fine but something related to its configuration that is going to be used in integration is causing a problem. The error which I am getting while running it is:-
Error using integral2Calc>integral2t/tensor (line 241)
Integrand output size does not match the input size.
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
I am seriously pulling my hairs after a lot of trying. It would be really helpful if anyone can help me in this regard.
The way funn
is defined is inconsistent with integral2. For example, if I enter an array of theta and phi values to funn
I expect the same array size to be the output, but that's not what funn
produces. for example:
[th ph]=meshgrid(linspace(0,pi/2),linspace(0,2*pi))
>> size(th)
ans =
100 100
whereas:
>> size(funn(th,ph))
ans =
100 1
So funn
doesn't work properly to be used in integral2. I think the first place to start in your function is I = length(theta);
line, as the length of theta is not appropriate if theta is a 2d array.