Compares the results of the trapezoid method with a simple summation.
For N+1 evenly spaced data points (i.e., N equal segments), the definite integral can be approximated by:
Here (b-a)/N
is the spacing between data points.
Note that this equates to a simple summation multiplied by the data point spacing, minus a correction term:
( f(1) + f(end) )/2
If the spacing between the data points is not constant, the formula generalizes to:
Sample output:
Test code:
for N = logspace(1, 4, 4)
x = pi*linspace(0, 1, N+1);
y = sin(x);
fprintf('N = %-6d \tTrapz = %.6f\n', N, trapz(x, y))
end
N = 10 Trapz = 1.983524
N = 100 Trapz = 1.999836
N = 1000 Trapz = 1.999998
N = 10000 Trapz = 2.000000
Resources:
https://www.mathworks.com/help/matlab/ref/trapz.html
https://web.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section%208%20Integration.pdf