Python

SciPy Romberg

Python is the most used computer language which is used to create different programming software and tasks. Python not only provides its features for a limited amount of tasks but it is a general-purpose programming language in the easiest script as compared to all other existing languages. With Python, we can create different software for the autonomous system, and we can train the neural networks to make artificially intelligent programs. SciPy is designed on top of Python’s most famous “NumPy” library as NumPy deals with the function related to the matrics and the matrices themselves. Similarly, Scipy is a library that performs the interpolation, integration, independence test, and mathematical computations on this nd-array.

The Romberg() method is offered by Python’s SciPy library that uses the “integration” module and computes the Romberg integration of a function within the upper limit value and the lower limit value. Integration evaluates the volume and the areas that are under the curve. Integration adds up the quantities or the value and makes them function as a single or alone system.

Procedure:

We will apply the Romberg integration to the different mathematical equations and will find out the results of this integration. This article covers the methodology to implement the SciPy Romberg function on the differential equations. It will also describe the syntax and the parameters for the SciPy Romberg integration.

Syntax:

The “Romberg” function from SciPy can be written in the Python script as :

$ scipy. integrate.Romberg(func, a, b, show = False)

Let us now discuss the parameters for this function. The Romberg() function has four parameters in the list. The “func” is defined as the function which is a mathematical equation which can be presented in an array-like shape or method. The “a” represents the upper limit of the integration. And the “b” marks to be the second limit for the integration.

Both these limits are the interval under which we want to integrate the given function. The last parameter in the list is the “show” which is an optional kind of parameter. The function for this parameter is that if the “func” parameter or the array-like sample, which we want to integrate, exists in one dimension and we set this value to Boolean “True”, it displays the table in the output that represents the Richardson extrapolation. Otherwise, the default value is “False”.

Return Value:

The function returns the integration results for the sample that we have given to the function as the parameter under the upper and lower bounds.

Example 1:

In this first example, we will implement to show the demonstration for the application of the Romberg integration function on the array sample. For writing the program to implement the example, we use the “Google Collab”. This collab provides Python with the latest version that has all the packages downloaded and installed beforehand. It further gives the extra memory for saving the programs and for their execution. Once the memory space is allocated by Google Collab, we can now create the notebooks to write the program and use the Python compiler. So, we create a new notebook and give it a unique title.

Now, we need to bring the important backend information to the program so that we can run the Romberg integration function. The Romberg integration function uses an “integration” attribute of Python’s SciPy library. So, we integrate the “integrate” module from the scipt and use this integration module. We then call the SciPy Romberg integration function. The other library that also needs to be integrated into the program is Python’s Numpy library. This library imports the array function to the project. For this, we import the NumPy with the unique prefix, “np”. Use the NumPy’s np and declare a NumPy array in the variables as “func”.This array is has only one dimension. The values or the elements that it hold are decided by the “arrange()” function since we only ask the np to use its arrange() module to create an array that starts from “4” and ends at “13”.

We can do this by simply writing the arrange function with np as “np. arange(5, 14)”. This way, we create an array with the 10 elements which are distributed equally. Moving further, we apply the Romberg integration on this array. For that, we call the “integrate.Romberg()”. We pass on the array with the name “func” and the “show” equal to Boolean “True” in the parameter list of this function so that the Richards exploration table is displayed with the integration result in the output. The code to write this program in Python is shown as follows:

import numpy as np

from scipy import integrate

x = np.arange(514)

integrate.romb(x)

Example 2:

This example performs the Romberg integration on the exponential array or the equation. We already imported the NumPy and the SciPy “integrate” modules in the program. We utilize the NumPy with the name, “np”, and declare a “lambda” function which has the equation in the exponential form as “np. exp(-x**3)”. Then, we save the function in “func” variable and pass this func to the “Romberg integration()” function with the upper and the lower limits of the “x” that we used in the equation.

The upper limit has the value “1”. The lower limit has the value “4”. And the “show” parameter is Boolean “True” in the list. The results for this program are displayed using the “print()” function. We write the whole program in Python which can be copied from the following figure and run on the Python compilers to check the output of the function:

import numpy as np

from scipy import integrate

func = lambda x: np.exp(-x**3)

result = integrate.romberg(func, 14, show = True)

 

print(result)

Conclusion

The working methodology of the “SciPy integration Romberg” is discussed in detail in this article. We did the two examples for this function and these two examples used two different methods to declare the “func” parameter for this function – one as an exponential equation and the second one as a normal one-dimensional array.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content