I've created a code for the Simpson's rule but I think I got the function wrong. I have no other sources to refer to (or they're too difficult to be understood). Here is my code:
function s = simpson(f_str, a, b, h)
f = inline(f_str);
n = (b-a)/h;
x = a + [1:n-1]*h;
xi = a + [1:n]*h;
s = h/3 * (f(a) + f(b) + 2*sum(f(x)) + 4*sum(f(xi)));
end
Can anybody help see where is the wrong part?
Assuming that the
h
in your function is the step size:Source:
https://en.wikipedia.org/wiki/Simpson%27s_rule