Group Abstract Group Abstract

Message Boards Message Boards

0
|
1.7K Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

What does TrapezoidalRule at NIntegrate really do?

Hello everybody,

I have a question as regards the TrapezoidalRule at the implemented NIntegrate function of Mathematica.

If I calculate the numeric(!) integral of f(x)=x^2 from 0 to 3 with 5 trapezoids by hand, I get a result of 9.18. If I use the implemented method "TrapezoidalRule" with the option "Points" -> 5 it gives me exactly 9.0.

So I doubt, that I do not fully understand what Mathematica does when using this Rule. I have tried a lot of (changing the WorkingPrecision, setting the MaxRecursions to 0 etc...), but it always comes up with the "real" (analytical) result.

Can anyone please explain what's going on?

Thanks a lot,

JJJ

f[x_] := x^2;
a = 0; b = 3;
\[CapitalDelta]x[n_] := (b - a)/n;
left[n_] := \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(n - 1\)]\(f[
     a + i*\[CapitalDelta]x[n]]*\[CapitalDelta]x[n]\)\);
right[n_] := \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]\(f[
     a + i*\[CapitalDelta]x[n]]*\[CapitalDelta]x[n]\)\);
trap[n_] := (left[n] + right[n])/2;
trap2[n_] := \[CapitalDelta]x[n]/2 (f[a] + 2 \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n - 1\)]\(f[
        a + i*\[CapitalDelta]x[n]]\)\) + f[b]);
{left[5.], right[5.], trap[5.], trap2[5.]}
NIntegrate[f[x], {x, a, b}, 
 Method -> {"TrapezoidalRule", "Points" -> 3}, WorkingPrecision -> 10,
  MaxRecursion -> 0]
POSTED BY: Joachim Janezic
4 Replies

Thanks a lot.

Two more questions please:

  • Do you also get the warning message which says that the Integrate failed to converge to prescribed accuracy ...?
  • What do you mean by "interior points"? Let's say I have this interval [0,1] and I want to split it in 5 pieces (as it was in my example). Which number do I have to put at the Points-option? 4 (which seems to be obvious for "interior points" gives a bad result (9.125 instead of 9.18). In the documentation it says: "The option "Points"->k can be used how many coarse points are used. The number of points used by "TrapezoidalRule" is 2k-1" In my understanding, This means I cannot have 5 "pieces" (with 4 interior points), because there is no integer k which doubled - 1 = 4. Is this correct?

Thank you,

JJJ

POSTED BY: Joachim Janezic
POSTED BY: Michael Rogers

Wow, great. I will check that tomorrow.

Thank you very much! Your support is very much appreciated!

JJJ

POSTED BY: Joachim Janezic

It's using extrapolation I think. Set the suboption "RombergQuadrature" -> False, and you may as well set "SymbolicProcessing" -> None just in case. It's not needed for x^2, but I'm not sure when it might be necessary. Also, your n determines the number of intervals and has n+1 sample points. The "Points" option in the "TrapezoidalRule" specifies the number of interior points. [Correction: The "Points" -> n option in the "TrapezoidalRule" specifies the number of sample points to be 2n-1.]

{left[4], right[4], trap[4], trap2[4]} // N
NIntegrate[f[x], {x, a, b}, 
  Method -> {"TrapezoidalRule", "Points" -> 3,
   "RombergQuadrature" -> False,
   "SymbolicProcessing" -> None},
  WorkingPrecision -> 10,
   MaxRecursion -> 0]

Both give 9.28125.

Reference: https://reference.wolfram.com/language/tutorial/NIntegrateIntegrationRules.html#618158740

POSTED BY: Michael Rogers
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard