function I = TrapComp(f,a,b,n) % % TrapComp estimates the value of the integral of f(x) from a to b % by using the composite trapezoidal rule applied to n equal-length % subintervals. % % I = TrapComp(f,a,b,n) where % % f is an inline function representing the integrand, % a and b are the limits of integration, % n is the number of equal-length subintervals in [a,b], % % I is the integral estimate. % h = (b-a)/n; I = 0; x = a:h:b; for i = 2:n, I = I + 2*f(x(i)); end I = I + f(a) + f(b); I = I*h/2;