Functional approach to Trapezoidal rule Integration
Tags: numericalmethod, haskell, functionalprog
Published: 2018-05-21 00:52:19

---
title: Functional approach to Trapezoidal rule Integration
published: true
description: Using haskell to integrate a function with a numerical method.
tags: numerical method, Haskell, functional prog
---

##What are Numerical Methods:
No matter how powerful we make out computers to be, they are limited. Their limits prevent us from directly evaluating mathematical equations, this is where numerical methods come into play. We defined numerical methods as mathematical models which approximate solutions to more complex equations using a computer’s inherent problem-solving methods.


##Integrating using the trapezoidal rule:
Wikipedia describes an integral as “assigning numbers to functions in a way that can describe displacement, area, volume, and other concepts that arise by combining infinitesimal data.”
![Integral](https://wikimedia.org/api/rest_v1/media/math/render/svg/ac02adeed584466d53dee65f3228ad66939eb58b)
They are one of the fundamental concepts introduced in calculus and are used commonly throughout the engineering and scientific world.

The trapezoidal rule is a way of approximating a definite integral by creating trapezoid partitions through the function. Taking the cumulative area of the partitions which give us the approximate integral.

![Trapezoidal rule](https://i.stack.imgur.com/daZ4D.png)

##Functional approach to Numerical Methods:
To compute the following method we are going to use the functional programming language Haskell.
You can find the source to the following code <a href="https://github.com/Juanroalvarado/Integratehaskell">here</a>.


First of all we will define the function we are integrating. 

``` haskell
func x = sin (x)
```
For our example we will be integrating the sin function from 0 to pi.

Now we begin defining our method.

Since Haskell is a lazy language we must first define our function integrate's parameters. 
``` haskell
integrate :: Float -> Float -> Float -> Float -> Float -> Float
```

The function will take :
1. an initial value
2. the number of iterations or partitions needed
3. the lower limit
4. lower limit plus delta
5. the upper limit

Since Haskell is a functional language we will recursively compute each of the trapezoidal partitions.

We must first add a way for the function to know when to compute our final value, this happens by multiplying the complete summation by a delta. Stopping our recursion.

``` haskell
integrate x n lim0 newlim0 lim1 
    | newlim0 >= lim1 = (x * delta)
    where delta = ((lim1-lim0)/n)
```
We check the "newlim0" which will be equal to the initial limit plus the delta, stopping recursion when we get to the upper limit.

``` haskell
integrate x n lim0 newlim0 lim1 
    | newlim0 >= lim1 = (x * delta)
    |  x == 0 = integrate (x +  (func lim0 + func lim1)/2) n lim0 (newlim0+delta) lim1
    where delta = ((lim1-lim0)/n)
```

We follow up with doing the first sum, in which if we look at the formula we are calculating the trapezoid's area. Adding it when our initial value is 0.
Recursively calling integrate with out new value and our new limit.

``` haskell
integrate x n lim0 newlim0 lim1 
    | newlim0 >= lim1 = (x * delta)
    |  x == 0 = integrate (x +  (func lim0 + func lim1)/2) n lim0 (newlim0+delta) lim1
    | otherwise = integrate (x + func newlim0) n lim0 (newlim0+delta) lim1
    where delta = ((lim1-lim0)/n)
```

Finally in any other case we will compute the summation by adding the evaluated function with our newlim0.

Our completed compact code can be found on my GitHub.
https://github.com/Juanroalvarado/Integratehaskell

###Where do we use integrals?

![Integral visual](https://upload.wikimedia.org/wikipedia/commons/9/9f/Integral_example.svg) 

Integrals can be seen everywhere! Specially when dealing with physics!

For example, lets say we have a water tank in our town. We want to know if it will be capable of providing enough water for the town for a week.

Calculating the integral for the tank's area will give us the volume. And now we are able to know how much water it can hold and will be able to provide.


##Conclusion
Usually we won't be able to calculate the integral of an exact function. Or compute a method exactly, using a numerical method to approach a value will be a more efficient and quick way of getting to that value.

A functional programming approach to numerical methods provides an intuitive way to calculate complex equations without the hassle of conventional programming methods.

#### references:
https://en.wikipedia.org/wiki/Trapezoidal_rule
http://learnyouahaskell.com/introduction
https://en.wikipedia.org/wiki/Numerical_method