I have made the code:
function I = simprule(f, a, b, n)%SIMPRULE Simpsons rule integration.
% I = SIMPRULE(F, A, B, N) returns Simpsons rule approximation
% for the integral of f(x) from x=A to x=B, using N subintervals,
% where F is a function handle.
h = (b-a) / n;x = a:h:b; % an array of length n+1
S = 0;L = 0; for l = 1:2:n %generates the odd number array
S = S + 4*f(x(l));endfor j = 2:2:n % generates the even number array
L = L + 2*f(x(j));end I = (h/3)*(f(a)+ S + L + f(x(n)));
However this doesn't seem to work when estimating f. Any thoughts on what may be the issue?
Best Answer