# Integral Calculus in Python

This tutorial demonstrates how to leverage popular Python libraries often used in scientific computation to compute the integration of continuous and bounded real functions having real variables in Python. Since this website focuses on computing, the integral calculation methods used here are primarily numerical; specific analytical methods are also given.

The article uses examples; each paragraph includes a sample integral to compute and a snippet of Python code that computes it using the relevant library.

In addition to NumPy and Python version 3, each code snippet discussed in this tutorial needs an extra library like SciPy and SymPy.

## Integration via SciPy

Integration of the function having one variable (Definite Integrals)

A definite integral is a mathematical operator in integral calculus that, if applied on a real-valued function made of real-valued variables and lie in the interval [a,b] (this is a subset of the domain), calculates the value of the area bounded by the graph of the given function in the defined interval [a,b].

The SciPy library offers several numerical methods specifically designed for computing the integrals of such real-valued functions. This section introduces a series of examples to illustrate how to apply these numerical methods. The reader is encouraged to read the official SciPy documentation to get the complete list of these useful methods.

### Calculating the Integral with quad Function

The Python code snippet that computes the integral value using the Scipy quad function is provided below:

Code

Output:

```Integration of a single variable function using the quad function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is
3.1801863337820993 having an error of 3.53071609036315e-14
```

### Calculating the Integral Using the fixed_quad Function

The Python code snippet that computes the integral value using the Scipy fixed_quad function is provided below.

Code

Output:

```Integration of a single variable function using the fixed_quad function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is
3.1801789114559855
```

### Calculating the Integral Using the quadrature Function

The Python code snippet that computes the integral value using the Scipy quadrature function is provided below.

Code

Output:

```Integration of a single variable function using the fixed_quad function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is
3.1801863337773124 having an error of 8.067120305099706e-10
```

### Calculating the Integral Using the Romberg Function

The Python code snippet that computes the integral value using the Scipy romberg function is provided below:

Code

Output:

```Integration of a single variable function using the Romberg function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is
3.1801863337821397
```

### Calculating the Integral Using the trapezoid Function

Since the integration technique for the trapezoid method is a fixed-sample function, the algorithm first uniformly spaces out the integration interval and calculates the appropriate values of the function, denoted as y, for every discrete value of x in the interval before passing a pair of discrete values, x, and y, to the integration algorithm.

The Python code snippet that computes the integral value using the Scipy trapezoid function is provided below:

Code

Output:

```Integration of a single variable function using the trapezoid function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is
3.180182964799505
```

### Calculating the Integration Using the cumulative_trapezoid Function

This function is also a fixed-sample function. Hence, what we applied in the trapezoid function also applies here.

The Python code snippet that computes the integral value using the Scipy cumulative trapezoid function is provided below:

Code

Output:

```Integration of a single variable function using the cumulative_trapezoid function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is
3.180182964799503
```

### Calculating the Integration Using the Simpson Function

This function is also a fixed-sample function. Hence we need to give the x and y data points.

The Python code snippet that computes the integral value using the Scipy Simpson function is provided below.

Code

Output:

```Integration of a single variable function using the Simpson function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is
3.18018296480732
```

### Integration of a Function Using the quad Method (With Extremes = Infinity)

Now we will calculate the integral of a single variable function between infinite extreme values.

The Python code snippet that computes the integral value using the Scipy cumulative trapezoid function is provided below.

Code

Output:

```Finding the integral value of 2x^2e^-x bounded between x = 1 to x = inf
The integration of the given function is
4.203848996896752e-08
```

### Calculation of the Length of a Planar Curve Arc

It is well known that the length of the arc a function makes between defined boundaries, also known as the planar curve, can be calculated using the integrals of the given single-variable function.

Calculation of the length of a planar curve arc expressed in explicit form with the quad.

Here is an example of Python code that calculates the length of a planar curve arc expressed in explicit form using the quad function of the SciPy library.

Code

Output:

```Finding the length of a planar curve arc
Length of the arc of the curve y=e^(-x^3) from x = -2 to x = 2
The arclength of the given function is 2982.8283737550946 with an error equal to 8.847369866821285e-06
```

### Calculating the Length of the Given Parametric Planar Curve's Arc

One way to describe a function having a one-dimensional input that gives multidimensional output is through a planar curve in space. The input given to these functions is a parameter, and the output is the parametric function. It is an essential topic to find the parametric form of a multi-variable function in multi-variable calculus that produces a particular curve.

Here is an instance of code in Python that uses the Scipy quad function to compute the length of a planar curve arc represented in the parametric form:

Code

Output:

```Finding the length of the arc of the parametric curve x(t) = cos^4(t) and y(t) = sin^4(t) from t = 0 to t = 2pi
The length of the arc is 6.492900960560925 with an error of 4.765521304124758e-13
```

## The Double Integral of a Function of Two Variables

Let's are given a real-valued function that has two variables and a set that is a subset of the domain. The definite double integral operator in multivariate integral calculus finds the value of the volume of the solid formed between the surface of the function and the plane or planes that include the given set of pairs.

### Calculating the Integral with dblquad Functon

Below is the instance of the Python code that finds the integral value of a multi-variable function using the Scipy dblquad function.

Code

Output:

```Double integral computation
Finding the integral value of the function 2(x^2)ye^-xy from y = 0 to y = 10 and from x = y - 2 to x = y + 2
The value of the integration is 5.93984306144259 with an error of 7.325997504492067e-11
```

### Calculating the Integral Using the nquad Function

Below is the instance of the Python code that finds the integral value of a multi-variable function using the Scipy nquad function.

Code

Output:

```Double integral using nquad
Finding the value of integration of the function 2(x^2)ye^-xy from y = 0 to y = 10 and from x = y - 2 to x = y + 2
The value of the integration is 5.93984306144259 with an error of 7.325997504492067e-11
```

### The Double Integration of a Function Having Two Variables Using the nquad Function (Infinite Boundaries)

Now we will see how to find the double integration of a two-variable function giving the boundaries are infinite this time.

Below is the instance of the Python code to find the integration using the Scipy nquad function.

Code

Output:

```Double integral using nquad
Finding the value of integration of the function 2(x^2)ye^-xy from y = 0 to y = inf and from x = 1 to x = inf
The value of the integration is 2.2072766470286456 with an error of 3.954831816447722e-08
```

## Triple Integral of a Function of Three Variables

Let us now see the integral calculus involving functions with more than 3 variables. These integrations are called triple integration.

### Calculating the Triple Integration Using tplquad

Below is the instance of the Python code that finds the integration value using the Scipy tplquad function.

Code

Output:

```Triple integration using tplquad
Find the integration value of the function x + y^2z^2 from z = 1 to z = 3, y = z to y = z + 3 and from x = y + z to x = 2(y + z)
R=The value of the integration is 3003.5 with an error of 5.1790448248981665e-11
```

### Calculating the Integral with nquad

Below is the instance of the Python code that finds the integration using the Scipy nquad function.

Code

Output:

```The value of the integration is 3003.5 with an error of 5.1790448248981665e-11
```

## Integration of Single-Variable Function of One Variable using Sympy

A Python library for working with mathematics is called SymPy, which stands for Symbolic Mathematics in Python. The SciPy Ecosystem includes other colossal software packages like NumPy, Pandas, and Matplotlib, as well as this one as a core library. You may work with mathematical expressions with SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while keeping the code as straightforward as possible to make it understandable and simple to extend. All of SymPy's code is written in Python.

SciPy is a library for numerical mathematics, whereas SymPy is for symbolic mathematics. SciPy only functions with numeric values such as floats and does not understand SymPy's symbols.

### Integral of a Function of One Variable with integrate(f, (x, a, b))

Below is the example of Python code that calculates the integral using the integrate(f, (x, a, b)) function of the SymPy library.

Code

Output:

```Finding the integration value of the function 2x^2e^-x from x = 1 to x = 5
The value of integration of our function is
3.18018633378210
```

### Integral of a Function of One Variable with integrate(f, (x, a, b)) Via Indefinite Integral

Below is the example of Python code that calculates the integral using the integrate(f, x) function of the SymPy library.

Code

Output:

```Finding integration of the function 2x^2e^-x from x = 1 to x = 5
The primitive value of integration is
(-2*x**2 - 4*x - 4)*exp(-x)
The value of the integration of our function is
3.180186333782099
```

The program first calculates the indefinite integral and then, applying the fundamental theorem of integral calculus estimates the value of the integral.

### The Double Integral of a Function of Two Variables

Double Integral of a function of two variables with integrate(f, (x, xa, xb), (y, ya, yb))

Below is the example of Python code that calculates the integral using the integrate(f, (x, xa, xb), (y, ya, yb)) of the SymPy library.

Code

Output:

```Finding the integration of the function xy^2e^-xy from y = 0 to y = 5 and from x = y - 2 to x = y + 2
The value of the integration is 0.748661161788309
```

### Feedback   