In the composite trapezoidal rule formula, the summation term is multiplied by 2 to account for the fact that the function values at the interior points are counted only once in the formula, whereas the endpoints are counted twice.
In MATLAB, the composite trapezoidal rule can be implemented as follows:
main.m359 chars14 lines
Where:
f
is the function handle representing the integranda
and b
are the lower and upper bounds of integrationn
is the number of intervals (subintervals) used for the approximationNote that in this implementation, the formula is applied to each subinterval separately, and then the results are summed up to obtain the final approximation.
gistlibby LogSnag