Skip to content
Advertisement

Why is my trapezoid rule implementation not producing expected results?

I have implemented a function to find the trapezoid rule of a given function, the function produces poor results for

log _{2}(x).

When I try to calculate the trapezoid rule with n < 8 it produces a value much larger than the actual area, which is unexpected, I have graphed f(x) and drawn how I believe the first few numbers of trapezoids would look, and they all should be producing less than the target area.

However, as n increases, the error becomes lower and lower and at n = 10000000 it is within a 0.001 of the solution.

JavaScript

Advertisement

Answer

If you step through trapezoidRule for n = 1 in a debugger, you’ll see that the loop is executed for i=1 and i=2. Since i=2 is treated as a midpoint, it is counted twice.

Why is the loop executed for wrong values of i? The expression i++ uses the post-increment operator, which increments the variable after returning its value. You should be using a pre-increment operator ++i, or a for loop like any sane person:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement