1
$\begingroup$

I would like to carry out a number of numerical integrals in the form of a table:

Iint[\[Theta]_, \[Theta]1_] = 
  Exp[-I n \[Theta]] Exp[
    I n1 \[Theta]] (rx^2 (Cos[\[Theta]] - Cos[\[Theta]1])^2 + 
      ry^2 (Sin[\[Theta]] - Sin[\[Theta]1])^2)^m Sqrt[
    rx^2 Sin[\[Theta]]^2 + ry^2 Cos[\[Theta]]^2] Sqrt[
    rx^2 Sin[\[Theta]1]^2 + 
     ry^2 Cos[\[Theta]1]^2] (rx^2 Sin[\[Theta]] Sin[\[Theta]1] + 
     ry^2 Cos[\[Theta]] Cos[\[Theta]1]);
Table[NIntegrate[
  Iint[\[Theta], \[Theta]1], {\[Theta], 0, 2 \[Pi]}, {\[Theta]1, 0, 
   2 \[Pi]}], {m, 1, 2}, {n, 0, 1}, {n1, 0, 1}]

I know that some of these integrals vanish. When Mathematica does these particular numerical integrals, I get the expected errors regarding convergence being too slow and the code takes a lot longer to run than the non-zero integrals e.g. the following will run very quickly:

m = 1;
n = 0;
n1 = 0;
NIntegrate[
  Iint[\[Theta], \[Theta]1], {\[Theta], 0, 2 \[Pi]}, {\[Theta]1, 0, 
   2 \[Pi]}]

My question is how do I run the table of numerical integrals quickly by telling Mathematica just to display zero if it's encountering convergence problems and the answer is going to be several orders of magnitude smaller than the others? Thanks in advance for any help.

Update (10/07/2020): The best I can come up with for speeding up the calculation when the integral is zero is to add on a bit to the integrand and then subtract off the corresponding amount after the integration:

Table[NIntegrate[
   Iint[\[Theta], \[Theta]1] + 1, {\[Theta], 0, 2 \[Pi]}, {\[Theta]1, 
    0, 2 \[Pi]}] - 4 \[Pi]^2, {m, 1, 2}, {n, 0, 1}, {n1, 0, 1}]

Is there a more elegant way of doing this?

$\endgroup$
2
  • $\begingroup$ Try Quiet@Check[..., 0] ? around your NIntegrate $\endgroup$
    – flinty
    Jul 9, 2020 at 15:39
  • $\begingroup$ Even better, you could pick out the message you don't care about more precisely so other errors aren't suppressed: badmessages = {NIntegrate::inumr, NIntegrate::ncvb}; Quiet[Check[..., 0, badmessages], badmessages] $\endgroup$
    – flinty
    Jul 9, 2020 at 15:45

1 Answer 1

2
$\begingroup$

This is the typical case for AccuracyGoal (absolute error) and PrecisionGoal (relative error).


Define the integrand

Iint[q_, p_] = Exp[-I n q] Exp[I n1 q] 
 (rx^2 (Cos[q] - Cos[p])^2 +  ry^2 (Sin[q] - Sin[p])^2)^m 
 Sqrt[rx^2 Sin[q]^2 + ry^2 Cos[q]^2] 
 Sqrt[rx^2 Sin[p]^2 +ry^2 Cos[p]^2] 
 (rx^2 Sin[q] Sin[p] + ry^2 Cos[q] Cos[p]);

Now do the integrations

Table[NIntegrate[Iint[q, p], {q, 0, 2 Pi}, {p, 0, 2 Pi},
                  PrecisionGoal->Infinity, AccuracyGoal->10], 
        {m, 1, 2}, {n, 0, 1}, {n1, 0, 1}]

  • With PrecisionGoal->p and AccuracyGoal->a, the Wolfram Language attempts to make the numerical error in a result of size $x$ be less than $10^{-a}+|x|10^{-p}$.
  • PrecisionGoal->Infinity specifies that precision should not be used as the criterion for terminating the numerical procedure. AccuracyGoal is typically used in this case. This is what we are doing here.
  • Notice that per default we have AccuracyGoal->Infinity, which means only the relative error is the stopping condition. When the integral $x\rightarrow 0$, this lead to error messages.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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