I am doing something wrong.
This is my algorithm to evaluate the integral $$\int_0^1 \frac{1}{1+x}dx= \log(2).$$ with the Newton Cotes algorithm (Simpson and 3/8). Both give me that for large n (number of subintervals of $[0,1]$ that I take, where I apply each Newton-Cotes algorithm separately) the integral is $\frac{1}{2}$ which is wrong. I don't see what I am doing wrong. Here is my MATLAB code:
function [ sum_simps,sum_da ] = Simpson( n )
x_simps = linspace(0,1,3*n);
t_simps = 1./(1+x_simps);
x_da = linspace(0,1,4*n);
t_da = 1./(1+x_da);
sum_simps = 0;
sum_da = 0;
for i = 1 : n
sum_simps = sum_simps + 1/n*(1/6*t_simps(3*(n-1)+1) + 4/6*t_simps(3*(n-1)+2) + 1/6*t_simps(3*(n-1)+3));
sum_da = sum_da + 1/n*(1/8 * t_da(4*(n-1)+1) + 3/8 * t_da(4*(n-1)+2) + 3/8 * t_da(4*(n-1)+3) + 1/8 * t_da(4*(n-1)+4));
end
end
Your programming error, misusing the limit n instead of the index i, inside of your loop was only part of the problem. Simpson's method, just as Splines, share endpoints between the curve segments, so your use of 3*n x values was entirely off. The looping below will give you n segments with n-1 knots, a total of 2*n + 1 function evaluations are required. Here is a revision of just the Simpson's, if you are satisfied with how it runs, I'm sure that you can build the other variation.