For details, see How to Use.
Numerical Integration Using the Rectangular Method (Rectangular Approximation)
This VCSSL program numerically calculates the value of an integral. As for the algorithm, it uses the simplest method, which approximates small areas using rectangular strips and sums them up.
Note: This program is provided as a sample code for explaining the algorithm, so it doesn't include any fancy GUI input screens. If you'd like to change the integrand function or parameters, please edit the code directly.
The code is written in VCSSL, but feel free to port it to other languages if you wish (C and C++ versions are also included below). This code is copyright-free (public domain / CC0), so you can use it freely at your own risk.
[ Articles in the Numerical Integration Series ]
- Part 1: Numerical Integration Using the Rectangular Method (this article)
- Part 2: Numerical Integration Using the Trapezoidal Method (next article)
- Part 3: Numerical Integration Using Simpson's Rule
- Summary: Programs for Calculating Integral Values (Numerical Integration)
How to Use
Download and Extract
At first, click the "Download" button at the above of the title of this page by your PC (not smartphone). A ZIP file will be downloaded.
Then, please extract the ZIP file. On general environment (Windows®, major Linux distributions, etc.), you can extract the ZIP file by selecting "Extract All" and so on from right-clicking menu.
» If the extraction of the downloaded ZIP file is stopped with security warning messages...
Execute this Program
Next, open the extracted folder and execute this VCSSL program.
For Windows
Double-click the following batch file to execute:
For Linux, etc.
Execute "VCSSL.jar" on the command-line terminal as follows:
java -jar VCSSL.jar
» If the error message about non-availability of "java" command is output...
After Launching
Once launched, the numerical integration process will run, and a graph of the integrated function will be displayed. The calculated integral value will also be shown in the console.

Changing the Integrand Function or Parameters
If you'd like to change the integrand function, integration range, or accuracy, open the program "IntegralRectangular.vcssl" in a text editor and modify the code as needed.
Explanation of the Topic
gIntegrationh in Manual (Analytical) Calculations
When you hear the word integration, you might think it's difficult or feel uncomfortable with it. If we were to ask people on the street, "Do you like integration or dislike it?", the majority would probably answer the latter.
Why is that? Probably because with manual calculations:
- Solving integrals as the inverse of differentiation requires training and familiarity.
- You need to skillfully apply several standard techniques depending on the case.
- The calculations tend to be long and error-prone.
However, in the numerical integration method we're using here, none of that is necessary. So even if you dislike integration for the reasons above, you're safe here.
That said, even though manual integration can be tricky, there are now tools that automatically compute symbolic expressions, capable of solving even extremely complex integrals. However, there are many integrals that cannot be solved manually (analytically) to begin with. In fact, outside of school exams -- which are designed to be solvable -- most real-world integration problems can't be solved by hand.
And that's exactly where numerical integration comes into play.
Numerical Integration: Getting the Value as a Number Instead of a Formula
Numerical integration is a method for calculating an integral value as a number instead of a formula. That might sound vague, so let's look at an example:
\[ \int^{1}_{0} \cos x dx \]
Suppose we want to evaluate this definite integral. In traditional manual (analytical) calculations, we know that integration is the inverse of differentiation, and that differentiating \(\sin\) gives \(\cos\), so:
\[ \int^{1}_{0} \cos x dx \] \[ = [ \sin x ]^{1}_{0} \] \[ = \sin 1 - \sin 0 \] \[ = \sin 1 \] \[ = 0.8414709848... \]In this process, you'd use a calculator at the very end to get the numerical value. That's the typical workflow of integration you might've learned in high school.
Now, here's the main point: in numerical integration, we skip all the above steps. You donft even need to know that "\(\sin\) differentiates to \(\cos\)." You can jump straight to getting the value 0.8414709848..., and quite easily too. In fact, in programming, it can be done with just a single for loop!
The Concept of Numerical Integration: "Finding Area" Rather Than "Undoing Derivatives"
Let's now look at the conceptual image of how numerical integration finds the integral value.
In manual calculations, integration is strongly associated with being the "inverse of differentiation." But in numerical computation, that idea is unnecessary. Instead, what you need is the concept: integration = finding the area.

As the figure shows, integrating f(x) from a to b means finding the area enclosed between f(x) and the x-axis over that interval.
If you use the "Riemann sum" method from high school math and calculate the area for a simple function, you'll see that the result matches the value obtained from analytical integration.
Rectangular Method (Rectangular Approximation): Dividing the Area into Narrow Strips and Summing Up
So how exactly can we compute the area between f(x) and the x-axis in a program?
The method we'll use here is the simplest and most intuitive one, though not the most efficient in terms of accuracy: the Rectangular Method (Rectangular Approximation) for numerical integration.
A "rectangle" in this context refers to long, narrow strips.
The Rectangular Method is based on the idea of dividing the area into long, narrow rectangles (strips), then summing up their areas to approximate the total. Let's denote the number of rectangles (subdivisions) as n.

This gives you a rough approximation of the area. But since the top of each strip approximates the curve with flat edges, there's clearly some overshoot and undershoot, suggesting noticeable error.
The idea is:
It's a rather brute-force kind of algorithm.
Still, it's the most basic method (actually the Riemann sum itself), and very easy to understand. In fact, this kind of idea is probably where the concept of integration started from. It's a great way to intuitively grasp what integration is all about.
Now, let's say we divide the interval from \(a\) to \(b\) into \(n\) strips, with each rectangle having a width of \(\Delta x\), and the \(x\)-value at the lower-left corner of the \(i\)-th rectangle being \(x_i\): \[ \Delta x = (b - a) / n \] \[ x_i = a + i \Delta x \] Then, the total approximate area (i.e., the integral value) is: \[ \textrm{Total area (integral value)} = \sum_{i=0}^{n-1} f (x_i) \Delta x \] Since integration = area, this is the approximate value of the integral.
So, what calculations are needed to get an integral value through numerical integration?
If you want to integrate \(\cos x\), just calculate \(\cos x \times \Delta x\) repeatedly and sum it all up. Simple as that.
As mentioned earlier, you don't need to think about "since the derivative of \(\sin\) is \(\cos\), then...". You can get the integral value without any of that.
Code
Now, let's take a look at the actual code for performing numerical integration. This program is written in VCSSL.
If you're familiar with the C language, you should find VCSSL code fairly easy to read.
In addition, wefll also provide code examples written in C and C++ (see note below).
In the VCSSL version, the graph is automatically plotted after execution. However, the C and C++ versions only output the integral value to standard output.
If you want to visualize the results from the C/C++ code, redirect the standard output to a file when running the program, then open that file in a graphing tool (such as RINEARN Graph 2D) to plot the graph.
Full Code
Let's first look at the complete VCSSL code. It's a short program, less than 50 lines long.
coding UTF-8; // Specify character encoding (to prevent garbled text)
import Math; // Import the math function library
import tool.Graph2D; // Import the graph plotting library
// Parameters for the calculation
const double A = 0.0; // Lower bound of the integration interval
const double B = 1.0; // Upper bound of the integration interval
const int N = 10; // Number of rectangles (more = finer steps = better accuracy)
// Function to integrate: f(x) = cos(x)
double f(double x) {
return cos(x);
}
// ----------------------------------------------------------------------
// Numerical integration using the rectangular method (approximation)
// ----------------------------------------------------------------------
double delta = (B - A) / N; // Width of each rectangle (Delta x)
double value = 0.0; // Calculated integral value (total area of interval)
for(int i=0; i<N; i++) {
// X-value at the lower-left corner of the i-th rectangle
double x = A + i * delta;
// Output the current x and integral value to the console
println(x, value); // (Comment out if N is large, as it can be slow)
// Add the area of the i-th rectangle to the total value
value += f(x) * delta;
}
// Output the final integration result (total area)
println(B, value);
// Plot the contents of the console as a graph
// (Comment out if N is large, as it can be slow)
setGraph2DData(newGraph2D(), load());
IntegralRectangular.vcssl
That's all for the code. Here's the general flow:
Near the beginning, the function to be integrated and the calculation parameters are defined.
In the middle, a for loop performs the numerical integration.
Finally, the progress of the integration is plotted on a graph.
Although the code above uses the "double" type, in VCSSL, "double" and "float" are treated as the same type. In the current runtime system, both are handled as double-precision floating point. So technically, using float would work just as well. However, we used "double" here to keep things consistent with the C and C++ versions.
The C version of the code is as follows:
The C++ version of the code is as follows:
Now that we've seen the full code in both C and C++, let's take a closer look at each part of the implementation.
Since the code structure is nearly identical across C, C++, and VCSSL, the detailed breakdown below will focus only on the VCSSL version for simplicity.
Preparing the Integrand Function and Parameters for the Calculation
At the top of the code (around the "import" or "#include" section), we begin by loading the necessary libraries and headers.
Next, we declare the integrand function f(x) and constants for the integration interval and number of subdivisions (i.e., the number of rectangles). As mentioned in the earlier explanation, the goal is to calculate: \[ \int^{1}_{0} \cos x dx \]
So, we set the integrand function as: \[ f(x) = \cos x \] and the integration interval as: \[ a = 0 \] \[ b = 1 \] The code looks like this:
// Parameters for the calculation
const double A = 0.0; // Lower bound of the integration interval
const double B = 1.0; // Upper bound of the integration interval
const int N = 10; // Number of rectangles (more = finer steps = better accuracy)
// Function to integrate: f(x) = cos(x)
double f(double x) {
return cos(x);
}
code/var.vcssl
This is the VCSSL version of the code. The C++ version is identical in structure, and the C version uses "#define" instead of "const" to declare constants.
This part of the code serves as the initial setup.
The Core: Numerical Integration Calculation
Now we get to the heart of it: the actual numerical integration using the rectangular method (approximation).
As discussed earlier in the conceptual explanation, we're computing the value of: \[ \textrm{Total area (integral value)} = \sum_{i=0}^{n-1} f (x_i) \Delta x \] This means summing up narrow rectangles with height \(f(x_i)\) and width \(\Delta x\). The width of each rectangle is calculated as: \[ \Delta x = (b - a) / n \] And the \(x\)-coordinate at the bottom-left corner of the \(i\)-th rectangle is: \[ x_i = a + i \Delta x \]
Here's the corresponding part of the code:
// ----------------------------------------------------------------------
// Numerical integration using the rectangular method (approximation)
// ----------------------------------------------------------------------
double delta = (B - A) / N; // Width of each rectangle (\Delta x)
double value = 0.0; // Calculated integral value (total area)
for(int i=0; i<N; i++) {
// X-value at the lower-left corner of the i-th rectangle
double x = A + i * delta;
// Output the integral value up to this point
println(x, value); // (Comment this out if N is large, as it may slow things down)
// Add the area of the i-th rectangle to the total value
value += f(x) * delta;
}
// Output the final integration result (total area)
println(B, value);
code/integ.txt
This loop accumulates the total area (i.e., the integral value) into the variable "value." Each rectangle's area is added one by one inside the "for" loop.
In terms of output, just printing the final line would be enough to fulfill the purpose of "calculating the value of the integral from a to b." However, that would look a bit dull. To make it more visually interesting -- and to show the progress of the integration as it accumulates -- we also output the partial result during each step of the loop.
When you run the program, youfll see output like this:
0.1 0.1
0.2 0.1995004165278026
c
0.8 0.7319228590332298
0.9 0.8015935299679464
1.0 0.8637545267950129
In this output:
- The left column shows the x value,
- The right column shows the integral value up to that point.
The final value, 0.8637545267950129, is the result of the numerical integration: \[ \int^{1}_{0} \cos(x) dx \] The exact value is \(\sin 1 \approx 0.8414709848...\), so we can see there's a bit of a difference. That difference is the numerical error.
Here we used \(n=10\), meaning we only used 10 rectangles to estimate the area. So this level of error is to be expected. If you increase \(n\), the result will get closer to the exact value (as we'll discuss later).
Now, let's talk about plotting the results to a graph. This is handled by the final part of the program:
// Plot the contents of the console as a graph
// (Comment this out if N is large, as it may slow things down)
setGraph2DData(newGraph2D(), load());
code/plot.txt
We won't go into the details here, but basically, the load() function retrieves the console output as a string, and setGraph2DData(...) plots it on a graph.
Here's what the resulting graph looks like:

This graph essentially plots the value of the definite integral from 0 up to each x, which is equivalent to plotting: \[ F(x) = \int^{x}_{0} \cos(x') dx' = \sin x\] So, in theory, the shape of the graph should resemble that of \(\sin x\), at least around the origin. And indeed, it does look somewhat similar, though there will be some visible distortion due to numerical errors.
By the way, the current VCSSL system uses RINEARN Graph 2D as its default graph plotting tool, which supports overlaying mathematical expressions on graphs. So let's try overlaying a plot of \(\sin x\) on top of the integration result.
From the menu bar of the graph window, select "Tools > Math." Then, in the input field labeled f(<x>), type:
Then, in the input field labeled f(<x>), type:

As you can see, a blue line is added. The red line shows the numerical integration result, and the blue line is the exact value \(\sin x\). There's a slight gap between them -- thatfs the error of the numerical integration.
Error and Subdivision Count \(n\)
Now, let's talk about the error.
This error basically arises from the fact that, in reality, the top edge of the integration area is curved, but we're approximating it with jagged rectangular strips.

So, if we make the strips thinner and thinner -- in other words, increase the number of rectangles \(n\) -- then the parts that over- or under-estimate the curve will shrink.
As a result, the approximated area will get closer to the true area, and the error will gradually decrease.
Let's try increasing \(n\) by one digit, from 10 to 100, and see what happens. If we overlay the exact function \(sin x\) on top of the numerical result, we get:

You can see that the curves almost overlap. Even at a glance, the error is clearly smaller. Here's the output from the console:
0.01 0.01
0.02 0.019999500004166653
c
1.0 0.8437624610086617
The final value here, 0.8437624610086617, is the result of the integration. The exact value is \(sin 1 \approx 0.8414709848...\).
Previously, with \(n = 10\), the result was around 0.86 -- differing from the exact value from the second decimal digit.
Now, with \(n = 100\), the second digit matches, and the difference starts from the third. So, by increasing \(n\) by one digit (10×), the accuracy improved by roughly one digit.
Let's increase it one more digit: with n = 1000, the result becomes 0.8417007635323798. This time, the result matches the exact value up to the third decimal digit -- the difference begins at the fourth.
So, we can see a trend: each 10× increase in n leads to roughly one digit of increased precision.
The Limits of Accuracy
However, increasing \(n\) also increases the number of iterations in the for loop, which directly increases the computational cost (and time).
In short:
To gain one more digit of reliable accuracy with the rectangular method, you need 10× more calculations.
That's quite inefficient compared to other numerical integration methods.
This method requires a lot of computation to achieve high precision.
Now here's a big question:
Actually, no. There's a limit.
That's because computers can't handle infinitely long decimal numbers. They round values to a fixed number of digits (according to their floating-point format), so rounding errors occur during each calculation. When you're adding small values repeatedly like in this integration method, those rounding errors can accumulate.
And if the number of iterations becomes too large, those accumulated rounding errors can become significant enough to reduce the overall accuracy -- basically the opposite of what you wanted.
Toward More Accurate Algorithms
So what can we do? We can use a more accurate and efficient algorithm.
For example, some methods can give you two or even four digits of accuracy improvement just by increasing the computation 10×. With those, you can achieve higher precision with fewer steps, avoid rounding error accumulation, and significantly improve practical usability.
In the next article, we'll introduce the trapezoidal method, a slightly modified version of the rectangular method that greatly improves accuracy.
[ Articles in the Numerical Integration Series ]
- Part 1: Numerical Integration Using the Rectangular Method (this article)
- Part 2: Numerical Integration Using the Trapezoidal Method (next article)
- Part 3: Numerical Integration Using Simpson's Rule
- Summary: Programs for Calculating Integral Values (Numerical Integration)
License
This VCSSL/Vnano code (files with the ".vcssl" or ".vnano" extensions) is released under the CC0 license, effectively placing it in the public domain. If any sample code in C, C++, or Java is included in this article, it is also released under the same terms. You are free to use, modify, or repurpose it as you wish.
* The distribution folder also includes the VCSSL runtime environment, so you can run the program immediately after downloading.
The license for the runtime is included in the gLicenseh folder.
(In short, it can be used freely for both commercial and non-commercial purposes, but the developers take no responsibility for any consequences arising from its use.)
For details on the files and licenses included in the distribution folder, please refer to "ReadMe.txt".
* The Vnano runtime environment is also available as open-source, so you can embed it in other software if needed. For more information, see here.
Numerical Integration Using Simpson's Rule (Quadratic Approximation) |
|
![]() |
Calculates the value of a definite integral using a method that approximates the curve with parabolas, achieving even higher accuracy than the trapezoidal method. |
Numerical Integration Using the Trapezoidal Method (Trapezoidal Approximation) |
|
![]() |
Calculates the value of a definite integral by summing up small trapezoids that approximate the area under the curve, offering higher accuracy than the rectangular method. |
Numerical Integration Using the Rectangular Method (Rectangular Approximation) |
|
![]() |
Calculates the value of a definite integral using a simple and intuitive method that approximates the area under the curve with rectangular strips. |
Tool For Converting Units of Angles: Degrees and Radians |
|
![]() |
A GUI tool for converting the angle in degrees into radians, or radians into degrees. |
Fizz Buzz Program |
|
![]() |
A program printing the correct result of Fizz Buzz game. |
Vnano | Output Data of Numerical Integration For Plotting Graph |
|
![]() |
Example code computing integrated values numerically, and output data for plotting the integrated functions into graphs. |
Vnano | Compute Integral Value Numerically |
|
![]() |
Example code computing integral values numerically by using rectangular method, trapezoidal method, and Simpson's rule. |