2
$\begingroup$

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.

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

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  *)
$\endgroup$
  • $\begingroup$ The function in question is indeed periodic in theta and this solution works very well for it. $\endgroup$ – K L Mar 11 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.