I want to calculate a 2D-integral for a vector of parameters in MATLAB. I know that integral2 has no 'ArrayValued' option. How can I rearrange the function handles to feed the integral a row vector q anyway? The 1D-integration works fine:
clear all
L=1000;
R=80;
formfactor = @(q,alpha) 2*sin(q.*cos(alpha)*L/2)./(q.*cos(alpha)*L/2).*besselj(1,q.*sin(alpha)*R)./(q.*sin(alpha)*R);
result = @(q) integral(@(alpha) formfactor(q,alpha).^2.*sin(alpha),0,pi/2,'ArrayValued',true);
qbins = 100;
q = logspace(-2,0,qbins);
I = result(q);
Up to here the one-dimensional integration along alpha works. Now I multiply the integrand with a term lattice_c which depends additionally on phi and try to integrate again.
a = 24;
b = a*sqrt(3)/2;
x=[-2.5*a, -2*a, -a, -a/2, a/2, a, 2*a, 2.5*a, 2*a, 2.5*a, 2*a, a, a/2, -a/2, -a, -2*a, -2.5*a, -2*a, -a, -a/2, a/2, a, a/2, -a/2 ];
y=[b ,2*b ,2*b ,3*b ,3*b ,2*b ,2*b ,b ,0 ,-b ,-2*b ,-2*b ,-3*b ,-3*b ,-2*b ,-2*b ,-b ,0 ,0 ,b ,b ,0 ,-b ,-b ];
lattice_a = @(q,position,alpha,phi) exp(sqrt(-1)*q*(x(position)*sin(alpha)*cos(phi) + y(position)*sin(alpha)*sin(phi)));
lattice_b = @(q,alpha,phi) sum(lattice_a(q,:,alpha,phi));
lattice_c = @(q,alpha,phi) formfactor(q,alpha).*lattice_b(q,alpha,phi);
lattice_d = @(q) integral2(@(alpha,phi) lattice_c(q,alpha,phi).^2.*sin(alpha),0,pi/2,0,pi/3);
Inew = lattice_d(q);
figure()
loglog(q,I)
The error message is "Error using .* Matrix dimensions must agree." But technically, there are no matrices involved, since none of the parameters is array-valued anymore. What argument do I need to pass on differently?