# ReleaseHold NIntegrate after Replace variable

I would like to calculate the sum of square of two integral(Ex and Ey). I first hold this two integral because two variables(tx, ty) are not specified.Then, I replace tx,ty and releasehold $Ex^2+Ey^2$. I get the error:

NIntegrate:: inumr: The integrand has evaluated to non-numerical values for all sampling points in the region with boundarie

myPre2[gamma_, lambda_, alpha_, r0_] := Module[{efR = gamma*lambda, k = 2*Pi/gamma, betta = Sqrt[1 - 1/gamma^2], c = 299792458, Ex, ExItgd, EyItgd, Ey, phi, x, y, tx, ty}, phi = k*(r0 - x*Sin[tx] - y*(Cos[ty]*Sin[alpha + ty] - Sin[alpha]/betta) + 1/(2*r0)*(x^2*Cos[tx]^2 + y^2*(1 - Cos[tx]^2*Sin[alpha + ty]^2)) - x*y*Sin[2*tx]*Sin[alpha + ty]); ExItgd = (x/Sqrt[x^2 + y^2*Cos[alpha]^2])* BesselK[1, 2*Pi/(betta*gamma*lambda)*Sqrt[x^2 + y^2*Cos[alpha]^2]]* Exp[I*phi]; EyItgd = (BesselK[1, 2*Pi/(betta*gamma*lambda)*Sqrt[x^2 + y^2*Cos[alpha]^2]]/ Sqrt[x^2 + y^2 Cos[alpha]^2]*(y*Cos[alpha]^2 + x*Tan[tx] Sin[alpha + ty]) - I/gamma*BesselK[0, 2*Pi/(betta*gamma*lambda^2)*Sqrt[x^2 + y^2*Cos[alpha]^2]]* Sin[alpha])*Exp[I*phi]; Ex = Cos[alpha + ty]* Hold@NIntegrate[ExItgd, {x, -efR, efR}, {y, -efR, efR}]; Ey = Cos[tx]*Hold@NIntegrate[EyItgd, {x, -efR, efR}, {y, -efR, efR}]; ReleaseHold[(Abs[Ex]^2 + Abs[Ey]^2) /. {tx -> 0.5, ty -> 0.5}] ]

myPre2[5, 1000*^-8, 0, 1*^-6]


All the variables for the integrand are already specified. Why I still get this error?

Thanks

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Commented Jan 3, 2016 at 6:13
• @MichaelE2 It works, thanks for your help! Commented Jan 3, 2016 at 7:03
• You're welcome. :) Commented Jan 3, 2016 at 7:09

The code

Hold@NIntegrate[ExItgd, {x, -efR, efR}, {y, -efR, efR}]


holds ExItgd literally as a symbol. So the replacement ... /. {tx -> 0.5, ty -> 0.5} has no effect. You need to inject (or write) the code for ExItgd (and EyItgd) into the held integrals (resp.) in some way. This is a typical use of With.

myPre2[gamma_, lambda_, alpha_, r0_] :=
Module[{efR = gamma*lambda, k = 2*Pi/gamma,
betta = Sqrt[1 - 1/gamma^2], c = 299792458, Ex, ExItgd, EyItgd, Ey,
phi, x, y, tx, ty},
phi = k*(r0 - x*Sin[tx] -
y*(Cos[ty]*Sin[alpha + ty] - Sin[alpha]/betta) +
1/(2*r0)*(x^2*Cos[tx]^2 +
y^2*(1 - Cos[tx]^2*Sin[alpha + ty]^2)) -
x*y*Sin[2*tx]*Sin[alpha + ty]);
ExItgd = (x/Sqrt[x^2 + y^2*Cos[alpha]^2])*
BesselK[1,
2*Pi/(betta*gamma*lambda)*Sqrt[x^2 + y^2*Cos[alpha]^2]]*
Exp[I*phi];
EyItgd = (BesselK[1,
2*Pi/(betta*gamma*lambda)*Sqrt[x^2 + y^2*Cos[alpha]^2]]/
Sqrt[x^2 + y^2 Cos[alpha]^2]*(y*Cos[alpha]^2 +
x*Tan[tx] Sin[alpha + ty]) -
I/gamma*BesselK[0,
2*Pi/(betta*gamma*lambda^2)*Sqrt[x^2 + y^2*Cos[alpha]^2]]*
Sin[alpha])*Exp[I*phi];
With[{ex = ExItgd, ey = EyItgd},
Ex = Cos[alpha + ty]*
Hold@NIntegrate[ex, {x, -efR, efR}, {y, -efR, efR}];
Ey = Cos[tx]*Hold@NIntegrate[ey, {x, -efR, efR}, {y, -efR, efR}]
];
ReleaseHold[(Abs[Ex]^2 + Abs[Ey]^2) /. {tx -> 0.5, ty -> 0.5}]]


Then

myPre2[5, 1000*^-8, 0, 1*^-6]
(*
...some convergence warnings...
5.8686*10^-18
*)