# Special case of numerical integration over two variables

I'm trying to perform an integral involving the function $$f(r,\theta)$$ where I first compute $$\int_0^{2\pi} f(r,\theta) e^{i m \theta} \, \text{d} \theta$$. Then I square the modulus of this result, multiply by $$r$$, and then integrate over some finite interval in $$r$$. Here, $$m$$ is a constant.

Is there a way to do this with NIntegrate in Mathematica? I do not know how to do numerical integration where one holds the value of $$r$$ as variable in the first integral and then, prior to the second numerical integration, takes the absolute value squared of the integrand.

One way I can imagine doing this is to define

hold[r_, m_] :=  NIntegrate[f[r, theta] Exp[I m theta], {theta, 0, 2 Pi}]


and then calculating

Nintegrate[Abs[hold[r, m]]^2 r, {r, 0, R}],


where I would insert a numerical value for $$m$$ and $$R$$. However, this gives several error messages during evaluation before giving a result. I am unsure if I can trust the result.

• One thing is that you probably want to use ?NumericQ on the arguments of h[]. – Michael E2 Mar 10 '19 at 23:35
• Of course, problems with code usually require the code: What is a typical function for f[]? – Michael E2 Mar 10 '19 at 23:36

Use ? NumericQ on the arguments of h. Then assuming f is analytic and periodic of period 2 Pi in theta, use the "Trapezoidal" method. If f is not periodic, then do not use it. If f is not analytic, then give an example; "unhappy functions are unhappy in their own way," to paraphrase Anna Karenina.

hold[r_?NumericQ, m_?NumericQ] :=
NIntegrate[f[r, theta] Exp[I m theta], {theta, 0, 2 Pi}, Method -> "Trapezoidal"];
Block[{f, R = 2, m = 3},
f[r_, t_] =
TransformedField[ "Cartesian" -> "Polar", x^2 y + y^2 x, {x, y} -> {r, t}];
NIntegrate[Abs[hold[r, m]]^2 r, {r, 0, R}]
]
(*  39.4784  *)

• The function in question is indeed periodic in theta and this solution works very well for it. – K L Mar 11 '19 at 1:13