# 'Integral2' error in MATLAB for invalid integrand

Here is the code that I am trying to run:

syms z_1 z_2 m_1 m_2 x y Q p s t
m_1=0.01;
m_2=249;
x=5;
y=5;
Q=0.6;
p=0.5;
s=0.04;
t=0.25;
func=(62001*sinh(y/100 - z_2/100))/(100*cosh((249*cos(2*pi*(z_1 - x + 1/4)))/2 +249))-(249*sinh(249*y - 249*z_2))/(10000*(cosh((249*cos(2*pi*(z_1 - x + 1/4)))/2 + 249) - 620009999/10000));
hunc=(m_1*m_2*(Q+p*cos(2*pi*(x-z_1-t))))/(m_2^3*tanh(m_1*(1+p*cos(2*pi*(x-z_1-t))))-m_1^3*tanh(m_2*(1+p*cos(2*pi*(x-z_1-t))))-(1+p*cos(2*pi*(x-z_1-t))*m_1*m_2*(m_2^2-m_1^2)));
lunc=3/(2*pi*s^2*t^3)*exp(-(3*z_1^2/(2*s^2*t^3))+(3*z_2^2/(2*s^2*t^3)));
F=func*hunc*lunc;
integral2(F,-inf,inf,-inf,inf)


I am getting that the error that

Integrand is invalid

in MATLAB console. Though the integrand is long, I hope it has all the correct symbols. As a side, I put the same function and integrated it in SageMath, but I got an answer very close to zero. What exactly is wrong in the integrand or method? Any hints? Thanks beforehand.

• Are you really getting this error in matlab? For me it just complains immediately that F isn't a function handle, which is correct (you don't actually give any hint towards what are the integration variables here). It also expects this to be numerically evaluated, not symbolically. It seems unclear what you actually want here. Commented Jul 3, 2023 at 13:59
• @MikaelÖhman yes, I want to numerically evaluate the function $F$. For simplicity, I split it into a product of three functions. The variables of integrations are $z_1, z_2$. Should I remove syms declaration? Commented Jul 3, 2023 at 14:27
• @MikaelÖhman I tried using @(z_1,z_2) F and deleting syms  and the variables after it. Still getting error. The errors are numerous, ScalarValued, Vadapt errors are just two of the several errors I am getting. Commented Jul 3, 2023 at 14:49

If you wish to evaluate this numerically there is no need to involve symbolic math, and we can just write functions;

func=@(z_1,z_2) (62001*sinh(y/100 - z_2/100))/(100*cosh((249*cos(2*pi*(z_1 - x + 1/4)))/2 +249))-(249*sinh(249*y - 249*z_2))/(10000*(cosh((249*cos(2*pi*(z_1 - x + 1/4)))/2 + 249) - 620009999/10000));
hunc=@(z_1,z_2) (m_1*m_2*(Q+p*cos(2*pi*(x-z_1-t))))/(m_2^3*tanh(m_1*(1+p*cos(2*pi*(x-z_1-t))))-m_1^3*tanh(m_2*(1+p*cos(2*pi*(x-z_1-t))))-(1+p*cos(2*pi*(x-z_1-t))*m_1*m_2*(m_2^2-m_1^2)));
lunc=@(z_1,z_2) 3/(2*pi*s^2*t^3)*exp(-(3*z_1^2/(2*s^2*t^3))+(3*z_2^2/(2*s^2*t^3)));
F=@(z_1,z_2) func(z_1,z_2)*hunc(z_1,z_2)*lunc(z_1,z_2);


However, before integration, I tested just evaluating this complicated function for a few random points

>> F(1,2)
ans =
-Inf
>> F(1,3)
ans =
-Inf
>> F(1,1)
ans =
-Inf
>> F(1,0)
ans =
NaN
>> F(0,0)
ans =
-Inf


I've yet to find any inputs where this is well behaved.

• Thanks, but still I get lot of errors. Pasting the same here: Error in contour>@(z_1,z_2)3/(2*pis^2*t^3)*exp(-(3*z_1^2/(2*s^2*t^3))+(3*z_2^2/(2*s^2*t^3))) (line 11) lunc=@(z_1,z_2) 3/(2*pis^2*t^3)*exp(-(3*z_1^2/(2*s^2*t^3))+(3*z_2^2/(2*s^2*t^3))); Error in contour>@(z_1,z_2)func(z_1,z_2)*hunc(z_1,z_2)*lunc(z_1,z_2) (line 12) F= @(z_1,z_2) func(z_1,z_2)*hunc(z_1,z_2)*lunc(z_1,z_2); Error in integral2Calc>integral2t/tensor (line 237) Z1 = FUN(X(VTSTIDX),Y(VTSTIDX)); NFE = NFE + 1; Contd.... Commented Jul 3, 2023 at 15:33
• Errors contd.... 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 105) Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct); Error in contour (line 13) integral2(F,-100,100,-100,100) Commented Jul 3, 2023 at 15:33
• What do you expect from a function that is -Inf or NaN everywhere? Commented Jul 3, 2023 at 15:48
• exp(1000) becomes Inf with double precision math. I don't know what sagemath does. I suspect this isn't well posed for numerical evaluation. Commented Jul 3, 2023 at 15:52
• I don't know, these expressions are pretty much completely unreadable I'm afraid. Commented Jul 3, 2023 at 16:03