Reimann Sum Approximation of the Area Under a Curve

by In Linux Find Truth
Reimann Sum Approximation of the Area Under a Curve

There are several methods for approximating the area under the curve of a polynomial function, including Simpson's Rule and Reimann Sum approximation. The former yields a much better approximation in the short term; however, the latter method allows for an approach from the left and the right when dissecting the closed interval [a, b] along the Cartesian Plane's X-axis.

Here is an example of computing the Reimann Sum from the right rather than the left. In this example, I've chosen a trigonometric function that is monotone, continuous, and differentiable over the closed interval [0, pi/6]. I've used a programming language that I'm currently studying called Julia 1.1.0 which lends itself extremely well to areas of Computer Science and especially Mathematics.

The function that I've chosen to work with in this example is a trigonometric function sin(x) and here I have explicitly chosen the function f(x) = sin(3x)^2 which is shown below:

Note that I am dissecting the [ 0, pi/6 ] into four rectangles of equal base size, and summing up the areas of these four rectangles to obtain an approximation of the area under the curve. As you can see, this initial approximation will be larger than we want as it will overstate the exact area under the curve that would be obtained by taking the Integral of sin(3x)^2 from 0 to pi/6. However, as I will show, by increasing the number of these rectangles, we can zero in on a more precise approximation of the area. In fact, I start with four rectangles and increase the number of rectangles to 1,000,000.

I have written a program in Julia that computes this area and which allows me to increase the number of rectangles, n, from the initial four to the 1,000,000 rectangles shown in the diagram.

Here is the Julia program for accomplishing this:

In Julia, I created a function called rrs (which stands for Right Reimann Sum) which takes four arguments: the function f::Function, a (left end-point of the interval), b (right end-point of the interval), and n (the number of rectangles being used to make the computation.

function rrs(f::Function, a, b, n)
δ = (b - a) / n
xs = a .+ (1:n) * δ
fx = map(f, xs)
return sum(fx * δ)

When running the code in the Julia REPL, I can obtain a closer approximation to the actual area under the curve of this trigonometric function as shown in the diagram below:

After interactively loading the Julia program I called rightReimannSum.jl into the Linux Terminal, it brings up Julia's REPL and I'm able to enter the function that I created and work with it. Note, the initial computation of the function rss's first argument of sin(3x)^2 with a = 0, b = pi/6, and n = 4 (number of rectangles) is shown in the graph above. The area computed here was 0.32724923474893675. This is too large and so I increase the number of rectangles from 4 to 1000 in the second example. The area now has been optimized to an approximation of 0.2620611871869485. Finally, I increase the number of rectangles to 1,000,000. Julia has no problem with a number this large in its computation, which takes only milliseconds to run. The result with 1,000,000 rectangles is extremely close to the actual area under the curve and produces the result of 0.2617996495985372.

April 16, 2019
by In Linux Find Truth