Implementing on Matlab the Simpson adaptive rule to approximate an integral (the following code), I am struggling with saving nodes correctly. I have tried different solutions, but none of them seems to be the right one. Any suggestions?
[S, nodes] = SimpsonAdapt (f, a, b, tol, hmin)
h = b - a;
c = (a + b) / 2;
d = (a + c) / 2;
e = (c + b) / 2;
fa = feval (f, a);
fb = feval (f, b);
fc = feval (f, c);
fd = feval (f, d);
fe = feval (f, e);
nodes = [a b];
S1 = (h/6)*(fa + 4*fc + fb);
S2 = (h/(12))*(fa + 4*fd + 2*fc + 4*fe + fb);
E2 = (1/(15))*(S2 - S1);
if abs(E2) <= tol && (b - a) < hmin
S = S2 + E2;
nodes =[];
else
[Q1, nodi1] = SimpsonAdapt (f, a, c, tol/2, hmin, nodes);
[Q2, nodi2] = SimpsonAdapt (f, c, b, tol/2, hmin, nodes);
nodes = [nodes, nodi1];
nodes = [nodes, nodi2];
S = Q1 + Q2;
end
nodes = unique(nodes);
end
The nodes list that is returned is the list of points used to evaluate the quadrature formula. You should decide which nodes these are and use that decision uniformly. You first give only the interval end-points. However, in the branching you return the 6th order Richardson extrapolation of the Simpson values, which uses all 5 points
[a,d,c,e,b].In the composition, you simply have to remove the repeated middle point
conce,See also https://stackoverflow.com/questions/58892251/building-a-recursive-function-in-matlab-to-draw-adaptive-trapezoidal-quadrature for a similar question.
Using the adapted python code (which does not care about saving function evaluations)
gives reduced node lists without any clean-up and results in plots like
for the integration task
or more demonstrative
for the task