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.
2 Answers
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
).
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"}]
NIntegrate[1/Sqrt[x], {x, 0, 1}, Method -> {"NewtonCotesRule", "Points" -> 5}]
$\endgroup$n = 2; NIntegrate[x^7, Evaluate@Flatten@{x, Subdivide[0., 1., n]}, Method -> {"NewtonCotesRule", "Points" -> 5}, MaxRecursion -> 0]
$\endgroup$NIntegrate
with{x, x0, x1,..., xn}
.Subdivide[0., 1., n]
will split [0, 1] inton
equal intervals. Just evaluateEvaluate@Flatten@{x, Subdivide[0., 1., n]}
separately with different values ofn
to see what you get.MaxRecursion -> 0
keepsNIntegrate
from splitting the intervals further. Is that what you want? I can put it in an answer, if so. $\endgroup$