I want to integrate a function which seems to have no regularity problems. In fact I have defined

K[x_, y_] := 
 Assuming[Element[{x, y}, Reals] && x > 0 && y > 0, 
 NIntegrate[Sqrt[1 - (Cos[x]*Cos[y] + Sin[x]*Sin[y]*Cos[t])^2], {t, 0, 2*Pi}]]
R[x_, 0] := (x - Pi/2)^2
H[x_, 0] := 
 Assuming[Element[{x}, Reals] && x > 0, NIntegrate[Sin[t]*K[x, t]*R[t, 0], {t, 0, Pi}]]

If I plot, the function I obtain a good output

Sin[x]*Exp[-10*H[x, 0]

enter image description here

But if I try to integrate it, even with NIntegrate I receive the following error messages

NIntegrate::inumr: The integrand Sqrt[1-(Cos[t] Cos[x]+Cos[t] Sin[t] Sin[x])^2] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,6.28319}}.
General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation.
NIntegrate::write: Tag Times in -Abs[t] is Protected.
General::stop: Further output of NIntegrate::write will be suppressed during this calculation.
NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near {x} = {0.0217285}. NIntegrate obtained 2.897189900542515`*^-18 and 3.722470516152078`*^-24 for the integral and error estimates.

why is that?

  • 1
    $\begingroup$ But if I try to integrate it you did not show the code for this. $\endgroup$
    – Nasser
    May 31, 2020 at 22:27
  • 1
    $\begingroup$ Sorry I forgot that NIntegrate[Sin[x]*Exp[-10*H[x, 0]], {x, 0, Pi}] $\endgroup$ May 31, 2020 at 22:28

2 Answers 2

  • The main difficulty here is the tiny value of your function and, by consequence, of its integral. Left to its own devices, it is difficult for NIntegrate to decide when it has done a "good enough" job of estimating an integral if its actual value is (close to) zero, since the default PrecisionGoal can never be satisfied (see here). In those cases, you should impose a finite AccuracyGoal instead.

  • I have also removed the assumptions because NIntegrate won't need them; you already take care that they are satisfied when you chose the integration range yourself.

  • In passing, avoid using single-letter uppercase symbols, as most of those have built-in meanings and you could get into conflicts; I have changed those to lower case below.

Here is the modified code:

ClearAll[k, r, h, K, R, H]

k[x_, y_] :=  NIntegrate[Sqrt[1 - (Cos[x]*Cos[y] + Sin[x]*Sin[y]*Cos[t])^2], {t, 0, 2*Pi}]
r[x_, 0] = (x - Pi/2)^2;
h[x_?NumericQ, 0] := NIntegrate[Sin[t]*k[x, t]*r[t, 0], {t, 0, Pi}]

  Sin[x]*Exp[-10*h[x, 0]], {x, 0, Pi},
  AccuracyGoal -> 5

(* Out: 2.8684*10^-18 *)

The calculation is pretty quick (RepeatedTiming returns 0.130 s).

The plot of your function was also a little rough; you can use more plot points to improve its quality. Below is the function plotted together with its integral on a range from 0 to $x$:

  Plot[Sin[x]*Exp[-10*h[x, 0]], {x, 0, Pi}, PlotPoints -> 200],

      Sin[x]*Exp[-10*h[x, 0]], {x, 0, limit},
      AccuracyGoal -> 5
    {limit, 0, Pi}, PlotRange -> All,
    PlotPoints -> 40, MaxRecursion -> 1,
    PlotStyle -> Red

function and its integral



K[x_?NumericQ, y_?NumericQ] := 
 Assuming[Element[{x, y}, Reals] && x > 0 && y > 0, 
   Sqrt[1 - (Cos[x]*Cos[y] + Sin[x]*Sin[y]*Cos[t])^2], {t, 0, 2*Pi}]]
R[x_?NumericQ, 0] := (x - Pi/2)^2
H[x_?NumericQ, 0] := 
 Assuming[Element[{x}, Reals] && x > 0, 
  NIntegrate[Sin[t]*K[x, t]*R[t, 0], {t, 0, Pi}]]

And only now do

NIntegrate[Sin[x]*Exp[-10*H[x, 0]], {x, 0, Pi}]

It is still running, but at least no error now. The above insures the function are called only with numerical arguments.

It finished after about 5 minutes:

enter image description here


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.