Integrating a function using SciPy's Quad function

share link

by Dejaswarooba dot icon Updated: Mar 21, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Scipy is a Python package designed for scientific and technical computing. It is based on the NumPy library and offers a variety of algorithms and mathematical tools for applications such as optimization, interpolation, integration, linear algebra, signal processing, and others. Scipy also provides statistics and probability modules, as well as Fourier transformations and image processing.

 

Scipy is widely used in many scientific disciplines, including physics, engineering, biology, and finance, alongside machine learning and data science. Its capabilities can be expanded by integrating it with other Python libraries such as Matplotlib for data visualisation, Pandas for data processing, and Scikit-learn for machine learning.

 

The quad function in Scipy is a method for numerically integrating a single-variable function across a given interval. The function accepts two arguments: the first is the function to be integrated, and the second is the integration interval.

 

To quantitatively estimate the integral, the quad function employs a technique known as Gaussian quadrature. This approach use weights and nodes to predict the integrand over the integration interval. The quad function outputs two values: the numerical value of the integral and an estimate of the absolute inaccuracy of the result.

fig-1 Code depicting Scipy quad integration


fig-2 The output obtained

Code

The quad function accepts the following arguments:

 

  • f denotes the function to be integrated. The function in this situation is defined as f = lambda x, a: a**2 * x, which accepts two variables, x and a, and returns a**2 * x.
  • 0 and 5 represent the lower and higher integration limits, respectively.
  • args=(4,): extra arguments for the function f. The value 4 is supplied as the input an in this case.

 

 

The code saves the tuple in variable I before printing it to the console with the print function. The code's output will be (200.0, 2.220446049250313e-12), where the first value is a numerical approximation of the integral and the second is an estimate of the error.

1. Copy and paste the above code from Kandi.

2. Remove the third line F = lambda x, a: quad(f, 0, x, args=(a,))[0] from the code and replace 'F' by 'f' in the next line. (It can be used for double integration).

3. Also ignore the last line. It depicts the output.

4. It is a better option to create a virtual environment while working with python.


I found this code snippet by searching for "Integrate using scipy quad" in kandi. You can try any such use case!

Dependent Libraries


scipyby scipy

Python doticonstar image 11340 doticonVersion:v1.11.0rc1doticon
License: Permissive (BSD-3-Clause)

SciPy library main repository

Support
    Quality
      Security
        License
          Reuse

            scipyby scipy

            Python doticon star image 11340 doticonVersion:v1.11.0rc1doticon License: Permissive (BSD-3-Clause)

            SciPy library main repository
            Support
              Quality
                Security
                  License
                    Reuse

                      If you do not have SciPy that is required to run this code, you can install it by clicking on the above link and copying the pip Install command from the SciPy page in kandi.


                      You can search for any dependent library on kandi like SciPy

                      Environment tested

                      1. This code had been tested using python version 3.8.0
                      2. scipy version 1.10.1 has been used.


                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page.


                      See similar Kits and Libraries