[ Prev | Index | Next ]
Japanese English
Now Loading...
Download
Please download on PC (not smartphone) and extract the ZIP file. Then double-click VCSSL.bat (batch file) to execute for Microsoft® Windows®, or execute VCSSL.jar (JAR file) on the command line for Linux® and other OSes.
For details, see How to Use.

Vnano | Compute Integrated Value Numerically (Numerical Integration)

An example program (script) to perform integration numerically. This script uses simpson's rule by default, but you can use the rectangular method and the trapezoidal method with modifying a line in code.

Code of this script is written in the Vnano. Code written in C, C++, and Java® are also distributed in Code section. In addition, the Vnano is a subset of the VCSSL, so you can execute this script as VCSSL code, with changing the extention of the script file to ".vcssl" (In such case, please enable the "import" statement at the near the top of the code, which is commented-out by default).

Also, if you want to plot the graph of the integrated function, see the next article and code.

Sponsored Link


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.

If you are using Windows, right-click the ZIP file and choose "Properties" from the menu, and enable "Unblock" checkbox at the right-bottom (in the line of "Security") of the properties-window. Otherwise, when you extract the ZIP file or when you execute the software, security warning messages may pop up and the extraction/execution may fail.

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:

VCSSL__Double_Click_This_to_Execute.bat

For Linux, etc.

Execute "VCSSL.jar" on the command-line terminal as follows:

cd <extracted_folder>
java -jar VCSSL.jar

» If the error message about non-availability of "java" command is output...

How to Use After Executing The Program

Output the Integrated Value

When the program has started, a window will be launched. Then, when the calculation has completed, a small window will be launched, and the integrated value will be displayed on it.

The integrated value will be displayed on the window

By default, the calculation will be completed soon. If you make the value of "N" in the script larger, required time of the calculation become longer,

How to Change the Integrant Function, Interval, etc.

For changing the integrant function, modify the content of the function "f(x)" defined in the script. For changing the interval of the integration, modify values of variables "A" and "B" in the script. In addition, if necessary, modify the value of the variable "N" in the script, which is the number of tiny intervals (See: Code section).

By default settings, "cos(x)" will be integrated from x = 0 to x = 1.

How to Change The Algorithm of the Integration

In this script, as algorithms of the numerical integration, you can use the rectangular method, the trapezoidal method, and the Simpson's rule. Each algorithm is implemented as a line in the script (See: Code section), so please add/remove "//" (a line starts with "//" will be ignored) at those lines, for selecting the algorithm you want to use. By default, the Simpson's rule will be used.

Other - How to Plot The Graph of The Integrated Function

If you want to plot the graph of the integrated function, we are destributing the modified version of this script for such purpose:

In the above article, we are explaining how to plot the output data into a graph.

Code and Algorithms

Entier Code

Code of this program is written in the Vnano. The Vnano has a simple C-like syntax, so if you accustomed with C or C-like programming languages, you probably can read/customize the content of code easily.

Let's see the entier code:

The Top of Code

Code starts with the following content:

The first line "coding ..." is for declaring the text-encoding of the script file, for preventing encoding/decoding-related probrems (e.g.: "mojibake").

The next line "import ..." (which is used for loading a library) is necessary if you execute this script as VCSSL code, but it is not necessary when you exeute this script as Vnano code, so it is commented-out by default.

The Vnano is mainly desined for embedded use in an application (software), so available libraries are determined and loaded by the host-application (equipped with the script engine of the Vnano) or its config file. The VCSSL Runtime, which is bundled with the package you downloaded from this page, supports all standard features of the Vnano, so you use them by default.

Definitions of The Integrant Function, The Interval of The Integration, etc.

In the next part, the integrant function, the interval of the integration, and so on are defined:

When you want to customize settings of the integration, modify the above code. Basically, define the integrant function as the function "f(x)" in the above code, and input the value of the lower/upper-limit of the integration to the variable "A" / "B".

"N" represents the number of tiny intervals (for details, see the latter sections). When you make the value of N larger, the precision of the integrated value basically increses, but required time for the calculation become longer. Please note that, if you set the immediately large value to N, the precision might decrease, because the number of total cycles of the integration loop become very large, so tiny numerical errors affect many times to the result.

The Core Part: Performing The Numerical Integration

The next part is, the core part of this script performing the numerical integration:

If you read the above code without any background knowledge, you might not be able to understand meanings of comments in the above code, but don't worry. Let's see keypoints of numerical integration algorithms briefly and graphically.

Calculate The Value of The Integration By Counting-Up Area Numerically

There are many kind of numerical integration algorithms in the world, but three methods used in this script are based on a common concept: calculate the value of the integration by numerically counting-up area between the integrant function f(x) and x-axis:

Area between the integrant function f(x) and x-axis

The area painted by the red color in the above figure equals to the integrated value of  f(x) from a to b.

Why? What condition is assumed? It is difficult to answer those questions strictly. It requires some complicated mathematical discussions about definitions of integral and integrability.

For example, sometimes we can not measure the area in the standard way(definition), e.g.: when f(x) is not continuous at any points, or when f(x) diverges to infinity in somewhere, and so on. See: Lebesgue integration - Wikipedia

We don't want to discuss about it here, so, Let's assume that we choose a standard (not trickey, [} integrable) function as the integrant function f(x), and then the area of the above figure equals to the integrated value.

Rectangular Method

As mentioned above, our goal is very simple: we want to calculate the area painted by the red color in the above figure, as precise as possible. Let's discuss how calculate the area in programs.

Probably the most simple way is as follows: Firstly split the entier area into N-"tiny interval"s, and then approximate the value of area of each tiny interval as a rectangle of which height is the value of  f(x) at the left-bottom of the tiny area, and finally calculate the summation value of area of all rectangles (see the following figure).

Abstract of rectangular rule

This is so-called "the rectangular method".

If you set the height of each rectangle to the value of  f(x) at the center of each tiny interval, the precision of the approximation improves. However, we don't use it here because we will use a more good way later.

Does it make sense? Let's return to code. You can use the rectangular method by removing "//" from the head of the line of the rectangular method, and adding "//" to the head of the line of the Simpson's rule. Then, with removing unnecessary lines, code become as follows:

In the above code, the left-hand-side of the last line "f(x) * delta" represents area of a rectangle of a tiny interval. The above code calculates the summation value of it of all rectangles with moving the value of x in the for-loop.

By the way, by default settings, this script calculated the integrated value of cos(x) from 0 to 1, so the theoretically correct value is sin(1) = 0.8414709848078965... . On the other hand, the numerically integrated value depends on the value of N. The followings are results by using the rectangular method:

Result under N=10:      0.8637545267950129
Result under N=100:     0.8437624610086617
Result under N=1000:    0.8417007635323798
Result under N=10000:   0.8414939689913762
Result under N=100000:  0.8414732832893704
Note: incorrect digits are painted by the red-color.

As the above, with making the value of N larger, we can get more precise result. More strictly, when we make the value of N about 1 digit (10 times) larger, the length of correct digits in the result increases about 1 digit.

Trapezoidal Method

Errors between the correct value and calculated values by using the rectangular method are mainly caused by that: we has approximated the value of area between the x-axis and the "roundy curve" of  f(x) by using "edgy rectangles".

Hence, as the next step, Let's approximate the curve of  f(x) by using a polyline of which nodes are \( (x,y) = (x_i, f(x_i)) \), where \(x_i\) is the x-coordinate value of the left-bottom point of i-th tiny interval (i = 0, 1, 2, ..., N-1) :

`@̊TO}

By the above improvement of the approximation, rectangles of the rectangular method become trapezoides. This is so-called "the trapezoidal method".

You can use the trapezoidal method by removing "//" from the head of the line of the trapezoidal method, and adding "//" to the head of the line of the Simpson's rule. Then, with removing unnecessary lines, code become as follows:

In the above code, the left-hand-side of the last line "( f(x) + f(x+delta) ) * delta / 2.0" represents area of a trapezoid of a tiny interval. The above code calculates the summation value of it of all trapezoides with moving the value of x in the for-loop.

Calculation results by using the trapezoidal method, under the same settings with the rectangular method, are followings:

Result under N=10:      0.8407696420884198
Result under N=100:     0.8414639725380026
Result under N=1000:    0.8414709146853138
Result under N=10000:   0.841470984106668
Result under N=100000:  0.8414709848008824
Note: incorrect digits are painted by the red-color.

As the above, when we make the value of N about 1 digit (10 times) larger, the length of correct digits in the result increases about 2 digits. In the contrast, when we used the rectangular method, we were required to make the value of N about 100 times larger, for improving the precision about 2 digits.

Simpson's Rule

Let's continue to improve the approximation. In the trapezoidal method, we have approximated the curve of  f(x) in a tiny interval by using a line, as depicted in the following figure:

Approximate error of a tiny interval of trapezoidal method

As commented in the above figure, in a tiny interval, if we can approximate f(x) by using a manually integrable curve which fits with f(x) well, its integrated value gives more good approximation of area of a tiny interval, than a trapezoid.

Hence, Let's approximate f(x) in a tiny interval by using a quadratic function throughing same points as f(x), at both-side boundaries and the center of the tiny interval:

Approximate f(x) in tiny interval by using a quadratic function

In general, parameters of a n-dimentional polynomial equation can be determined when values at (n + 1) points are given, so parameters of the above quadratic (2-dimentional) function can be determined, because values at (2 + 1) points are given. Then, we can calculate the integrated value of the determined quadratic function manually.

The result of the above calculation is well-known as the Simpson's rule. By using the Simpson's rule, we can get the approximate value of the area of i-th tiny interval, \(A_i\), as follows:

\[ A_{i} = \frac{ f(x_i) + f(x_i + \Delta x) + 4f(x_i+ \Delta x / 2) }{6} \Delta x \]

As the rectangular method and trapezoidal method, calculate the summation of \(A_i\) for all i ( = 0, 1, 2, ..., N-1).

This script uses the Simpson's rule by default. With removing unnecessary lines, code calculating area by using the Simpson's rule are as follows:

In the above code, the left-hand-side of the last line "( f(x) + f(x+delta) + 4.0 * f(x+delta/2.0) ) * delta / 6.0" represents an approximate value of area of a tiny interval \(A_i\). The above code calculates the summation value of it of all \(A_i\) with moving the value of \(x (= x_i)\) in the for-loop.

Let's check the precision:

Result under N=10:       0.8417720922382719
Result under N=100:      0.8414710140343371
Result under N=1000:     0.8414709848108186
Result under N=10000:    0.8414709848078956 (Near the limit of 64-bit float)
Result under N=100000:   0.8414709848078982 (Near the limit of 64-bit float)
Note: incorrect digits are painted by the red-color.

As the above, when we make the value of N about 1 digit (10 times) larger, the length of correct digits in the result increases about 4 digits. In the contrast, when we used the rectangular method, we were required to make the value of N about 10000 times larger(!!!), for improving the precision about 4 digits. Even when we used the trapezoidal method, it required to make N 100 times larger.

Also, as we can recognize in above results, please note that we can NOT improve the precision of the result beyond the limit of the numerical data type used for calculations (in this script/article, we have used "64-bit binaly floating-point number"). If you forget the above thing, and if you set the value of N to an immediately large number, the precision goes lower than the best, because the number of total cycles of the integration loop become very large, so tiny numerical errors affect many times to the result. The best value of N depends on f(x) and the interval of the integration, so check the behaviour of results with changing the value of N, as we did in this article.

License

The license of this VCSSL / Vnano code (the file with the extension ".vcssl" / ".vnano") is CC0 (Public Domain), so you can customize / divert / redistribute this VCSSL code freely.

Also, if example code written in C/C++/Java are displayed/distributed on Code section of this page, they also are distributed under CC0, unless otherwise noted.



Sponsored Link



Japanese English
[ Prev | Index | Next ]
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.
Index
[ Prev | Index | Next ]
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.
News From RINEARN
* VCSSL is developed by RINEARN.

Released the Latest Versions of RINEARN Graph and VCSSL - Now Supporting Customizable Tick Positions and Labels!
2024-11-24 - Starting with this update, a new "MANUAL" tick mode is now supported, allowing users to freely specify the positions and labels of ticks on the graph. We'll explain the details and how to use it.

Released Exevalator 2.2: Now Compatible with TypeScript and Usable in Web Browsers
2024-10-22 - The open-source expression evaluation library, Exevalator, has been updated to version 2.2. It now supports TypeScript and can be used for evaluating expressions directly in web browsers. Explains the details.

Behind the Scenes of Creating an Assistant AI (Part 1: Fundamental Knowledge)
2024-10-07 - The first part of a series on how to create an Assistant AI. In this article, we introduce the essential knowledge you need to grasp before building an Assistant AI. What exactly is an LLM-based AI? What is RAG? And more.

Launching an Assistant AI to Support Software Usage!
2024-09-20 - We've launched an Assistant AI that answers questions about how to use RINEARN software and helps with certain tasks. Anyone with a ChatGPT account can use it for free. We'll explain how to use it.

Software Updates: Command Expansion in RINEARN Graph, and English Support in VCSSL
2024-02-05 - We updated our apps. This updates include "Enhancing the Command-Line Features of RINEARN Graph" and "Adding English Support to VCSSL." Dives into each of them!

Inside the Repetitive Execution Speedup Impremented in Vnano Ver.1.1
2024-01-17 - Delves into the update in Vnano 1.1 from a developer's viewpoint, providing detailed insights into the specific improvements made to the internal structure of the script engine.

Scripting Engine Vnano Ver.1.1 Released: Dramatic Speed Improvement for Repetitive Executions of the Same Content
2023-12-22 - Released the Vnano script engine Ver.1.1. In this version, we've made significant enhancements in processing speed by reducing the overhead of handling requests. Explains the details.

RINEARN Graph 3D Updated and Next Version (Ver.6) Development Has Begun!
2023-09-04 - We have released RINEARN Graph 3D Ver.5.6.34. In addition, we have initiated the development of the next major version, Ver.6! Details inside.

Updated Contents
Circular Wave Animation

Draws the circular wave as 3D animation, under the specified wave parameters.
2022-12-14
Sine Wave Animation

Draws the sine wave as animation, under the specified wave parameters.
2022-11-26
Tool For Converting Units of Angles: Degrees and Radians

A GUI tool for converting the angle in degrees into radians, or radians into degrees.
2022-11-22
Connector Fatal Exception - Specification
The unchecked exception thrown when errors have occurred, caused by incorrect implementations (might be bugs).
2022-09-26
Connector Exception - Specification
The checked exception thrown when errors have occurred, cause by expected normal problems.
2022-09-26