> For the complete documentation index, see [llms.txt](https://jona-projects.gitbook.io/kiwicalc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jona-projects.gitbook.io/kiwicalc/numerical-methods/finite-integrals.md).

# Finite Integrals

A finite integral of a function is an integral of the form $$\int\_a^b f(x) , dx$$, which evaluates to $$F(b) - F(a)$$ when $$F(x)$$ is the antiderivative of $$f(x)$$ and $$f$$ is a well-behaved function. However, sometimes it may be difficult or impossible to find the antiderivative and thus the finite integral must be computed numerically. We currently support three numerical methods that compute the finite integral of a given function: Reinman's sum, Trapezoid Method, and Simpson's Method.

### Reinman's Sum

Reinman's Sum is the most known method to approximate the finite integral numerically. The method approximates the sum of areas ofthe rectangles that are trapped between the graph and the $$x$$ axis. For example:

```python
import math
reinman(lambda x:sin(x), 0, math.pi, 20)        
```

**Trapezoid Method**

The Trapezoid Rule is a type of Reinman Sum, where you sum the the areas of the trapped trapezoid - namely, it computes the finite integral of a function by dividing the space trapped between the function and the $$x$$ axis, into trapezoids, and summing their areas. Trapezoids under the $$x$$ axis will have a negative "area".

In order to do that, we choose the range of  $$x$$ values: $$a$$ and $$b$$, that represent the boundaries of the integral $$\int\_a^b f(x) , dx$$. We also need to choose an integer $$N$$, that determines the number of intervals and the number of trapezoids. we also compute the length of each interval( denoted as $$\Delta x$$ )  using the following formula: $$\frac{b-a}{N}$$. The formula for the entire thing is:&#x20;

$$
\int\_a^b f(x) , dx \approx \frac{\Delta x}{2} \sum\_{k=1}^{n} \left( f(x\_k) + f(x\_{k-1}) \right)
$$

When $$N \rightarrow \infty$$, the expression tends to the finite integral. However, while rather large $$N$$ values lead to better accuracy, they also result in a slower runtime.

Sources:

* <https://personal.math.ubc.ca/~pwalls/math-python/integration/trapezoid-rule/>
* <https://en.wikipedia.org/wiki/Trapezoidal_rule>

#### Simpson's method

Simpson's method is another approach for computing finite integrals. Here is the signature of the method:

```python
def simpson(f: Callable, a, b, N: int)
```

For instance:

```python
print(simpson(lambda x: sin(x), 0, pi, 11))           
```

Output:

```bash
2.0001095173150043
```
