The code for the summed newton cotes formulas in matlab. The functions compute the integral of a function f(x) in the same file from a to b with step size n. Use a sufficient high n for accurate results.
See the example for usage.
x is a vector of equidistant nodes.
function t = trapezoid_rule(a,b,n)
t = 0;
x = linspace(a,b,n);
h = x(2)-x(1);
for i = 1:length(x)-1
t = t + (h/2 * (f(x(i)) + f(x(i+1))));
end
end
function s = simpson_rule(a,b,n)
s = 0;
x = linspace(a,b,n);
h = x(2)-x(1);
for i = 1:length(x)-1
s = s + (f(x(i)) + 4* f((x(i)+x(i+1))/2) + f(x(i+1)));
end
s = s * h/6;
end
function r = rule38(a,b,n)
r = 0;
x = linspace(a,b,n);
h = x(2)-x(1);
for i = 1:length(x)-1
r = r + ( f(x(i)) + 3* f(((x(i) + x(i+1))*(4/3)/3)) + 3* f(((x(i) + x(i+1))*(5/3))/3) + f(x(i+1)));
end
r = r * h/8;
end