2
$\begingroup$

I am trying to learn how to integrate functions using composite newton-cotes. In other words, I want to divide the interval we are integrating over into $n$ equal subintervals, and then apply Newton-Cotes on each. For example if I wanted to integrate $f(x)$ over $[0,1]$ with $n$ intervals with Simpsons rule, I would split $[0,1]$ into $n$ equal intervals and apply Simpsons rule on each interval and the sum is the approximation of the integral. I do not know how to input this into Mathematica. I found an official reference here: https://reference.wolfram.com/language/tutorial/NIntegrateIntegrationRules.html#81663330 However, not only does the "Points" option not work (so I cannot pick which rule I should use, luckily n=3 is default which is the Simpsons rule), I am not sure how to do composite NC. Do I have to manually write the sum of integrals over each interval and then make Mathematica integrate each one using NC? I feel like there must be an easier solution.

$\endgroup$
4
  • $\begingroup$ This works: NIntegrate[1/Sqrt[x], {x, 0, 1}, Method -> {"NewtonCotesRule", "Points" -> 5}] $\endgroup$
    – Michael E2
    Commented Oct 25, 2020 at 20:22
  • $\begingroup$ Maybe you want to do this?: n = 2; NIntegrate[x^7, Evaluate@Flatten@{x, Subdivide[0., 1., n]}, Method -> {"NewtonCotesRule", "Points" -> 5}, MaxRecursion -> 0] $\endgroup$
    – Michael E2
    Commented Oct 25, 2020 at 20:27
  • $\begingroup$ @MichaelE2 ohhh the "points" go into the method! I thought it was an option of NIntegrate. I think the first code you posted just does the interpolation quadrature with polynomial of degree 4 rather than what I want. I think the second code works, could you explain a little bit how it works? $\endgroup$
    – 2132123
    Commented Oct 25, 2020 at 20:50
  • $\begingroup$ In the question, you wrote, "I would split [0,1] into 𝑛 equal intervals...". You can specify intervals in NIntegrate with {x, x0, x1,..., xn}. Subdivide[0., 1., n] will split [0, 1] into n equal intervals. Just evaluate Evaluate@Flatten@{x, Subdivide[0., 1., n]} separately with different values of n to see what you get. MaxRecursion -> 0 keeps NIntegrate from splitting the intervals further. Is that what you want? I can put it in an answer, if so. $\endgroup$
    – Michael E2
    Commented Oct 25, 2020 at 20:56

2 Answers 2

4
$\begingroup$

In the question, the OP writes, "I would split [0,1] into 𝑛 equal intervals...". It seems the OP wants to control precisely how Newton-Cotes is carried out. These things can be done in NIntegrate relatively easily.

You can specify subintervals in NIntegrate with an iterator of the form {x, x0, x1,..., xn}. Subdivide[0., 1., n] will split [0, 1] into n equal intervals. And Flatten@{x, Subdivide[0., 1., n]} will construct an iterator of the desired type.

Normally, NIntegrate will recursively split the intervals further until the error estimate on each interval satisfies the precision and accuracy goals. Setting MaxRecursion -> 0 keeps NIntegrate from splitting the intervals, and NIntegrate will simply apply Newton-Cotes to each subinterval and finish whether or not the goals have been met.

n = 2; 
order = 4;
NIntegrate[x^7,
  Evaluate@Flatten@{x, Subdivide[0., 1., n]},
  Method -> {"NewtonCotesRule", "Points" -> order+1},
  MaxRecursion -> 0]

It will give a warning message that includes the (numerical) error estimate. I like the message, but if you don't, you can use Quiet or set PrecisionGoal and AccuracyGoal extremely low (e.g. -10).

$\endgroup$
3
$\begingroup$

I am trying to learn how to integrate functions using composite newton-cotes. In other words, I want to divide the interval we are integrating over into n equal subintervals, and then apply Newton-Cotes on each.

That describes NIntergrate's (fairly well documented) MultiPanelRule.

Here is an example of computing integral and error estimates directly with it:

Clear[F];
F[x_] := Sqrt[x];

tbl = Flatten[
   Table[
    Block[{absc, weights, errweights},
     {absc, weights, errweights} = NIntegrate`MultiPanelRuleData[{"NewtonCotesRule", "Points" -> npoints}, npanels, MachinePrecision];
     {npanels, npoints, Map[F, absc].weights, Map[F, absc].errweights}
    ], {npanels, 2, 10, 2}, {npoints, {2, 4}}], 1];

ResourceFunction["GridTableForm"][tbl, TableHeadings -> {"Number of panels", "Number of points", "Estimate", "Error"}]

enter image description here

$\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.