user714852
user714852

Reputation: 2154

Matlab integration error: Integrand output size does not match the input size

I am attempting a 4th order nested integration in Matlab. The function is as follows:

second_integral = @(r_dash, phi_dash, r, phi) ( (exp(-1i * k * r_dash * cos(phi_dash) * sin(theta)) .* exp(-1i * k * sqrt(r.^2 + r_dash.^2 - 2*( r * r_dash .* cos (phi_dash - phi))))) / (2 .* pi .* sqrt(r.^2 + r_dash.^2 - 2.*( r .* r_dash .* cos (phi_dash - phi))) ) );        

With the constants in this particular example being:

a_1 = 7e-5;
a_2 = 5e-5;
k = 1;
theta = 0; % can also be 0.5, 1, 1.5

The integration is being done as

integrated_second_integral =integral(@(r_dash)integral3(@(phi_dash,r,phi)second_integral(r_dash,phi_dash,r,phi),0,(2*pi),0,a_2,0,(2*pi)),0,a_1,'ArrayValued',true);

When the integration command is run I get the following errors:

Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  2.725290e-19. 
> In @(r_dash,phi_dash,r,phi)((exp(-1i*k*r_dash*cos(phi_dash)*sin(theta)).*exp(-1i*k*sqrt(r.^2+r_dash.^2-2*(r*r_dash.*cos(phi_dash-phi)))))/(2.*pi.*sqrt(r.^2+r_dash.^2-2.*(r.*r_dash.*cos(phi_dash-phi)))))
  In @(phi_dash,r,phi)second_integral(r_dash,phi_dash,r,phi)
  In integral3>@(y,z)FUN(x(1)*ones(size(z)),y,z) at 139
  In funfun/private/integral2Calc>integral2t/tensor at 229
  In funfun/private/integral2Calc>integral2t at 56
  In funfun/private/integral2Calc at 10
  In integral3>innerintegral at 138
  In funfun/private/integralCalc>iterateScalarValued at 314
  In funfun/private/integralCalc>vadapt at 133
  In funfun/private/integralCalc at 76
  In integral3 at 122
  In @(r_dash)integral3(@(phi_dash,r,phi)second_integral(r_dash,phi_dash,r,phi),0,(2*pi),0,a_2,0,(2*pi))
  In funfun/private/integralCalc>iterateArrayValued at 157
  In funfun/private/integralCalc>vadapt at 131
  In funfun/private/integralCalc at 76
  In integral at 89
  In this_matlab_script at 61 
Error using integral2Calc>integral2t/tensor (line 242)
Integrand output size does not match the input size.

Error in integral2Calc>integral2t (line 56)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);

Error in integral2Calc (line 10)
    [q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);

Error in integral3/innerintegral (line 138)
        Q1 = integral2Calc( ...

Error in integralCalc/iterateScalarValued (line 314)
                fx = FUN(t);

Error in integralCalc/vadapt (line 133)
            [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);

Error in integralCalc (line 76)
        [q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral3 (line 122)
    Q = integralCalc(@innerintegral,xmin,xmax,integralOptions);

Error in
@(r_dash)integral3(@(phi_dash,r,phi)second_integral(r_dash,phi_dash,r,phi),0,(2*pi),0,a_2,0,(2*pi))


Error in integralCalc/iterateArrayValued (line 157)
                fxj = FUN(t(1)).*w(1);

Error in integralCalc/vadapt (line 131)
            [q,errbnd] = iterateArrayValued(u,tinterval,pathlen);

Error in integralCalc (line 76)
        [q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);

Error in this_matlab_script (line 61)
        integrated_second_integral
        =integral(@(r_dash)integral3(@(phi_dash,r,phi)second_integral(r_dash,phi_dash,r,phi),0,(2*pi),0,a_2,0,(2*pi)),0,a_1,'ArrayValued',true); 

The source of this error is a complete mystery to me. Could someone kindly shed some light on what may be happening here and what I can do to remedy it?

Upvotes: 1

Views: 1556

Answers (1)

MeMyselfAndI
MeMyselfAndI

Reputation: 1320

The thrown error is from line 242 in the tensor() function of integral2Calc(). Putting a breakpoint there reveals that there is indeed a mismatch in size between VTSTIDX and Z1. Also, there is the following hint:

% Check that FUN is properly vectorized. This is important here
% because we (otherwise) always pass in square matrices, which
% reduces the probability of the user generating an error by
% using matrix functions instead of elementwise functions.

Looking at your function: in some part of your function you are using r .* r_dash, and somewhere else you use r * r_dash. Also, you use / and * instead of ./ and .* to divide or multiply vectors, while you use .* for multiplication with scalars at the same time. I would advise you to be more consistent in that.

Anyway, changing the function into:

second_integral = @(r_dash, phi_dash, r, phi) ( (exp(-1i * k * r_dash .* cos(phi_dash) * sin(theta)) .* exp(-1i * k * sqrt(r.^2 + r_dash.^2 - 2*( r .* r_dash .* cos (phi_dash - phi))))) ./ (2 * pi * sqrt(r.^2 + r_dash.^2 - 2.*( r .* r_dash .* cos (phi_dash - phi))) ) );

does no longer give errors (but instead many many many warnings), but since I am not into integration I dont know if the above solved your problem.

Upvotes: 2

Related Questions