Numerical methods are an important tool used to solve certain problems in mathematics. These problems tend to be very taxing and are better suited for the processing power of a computer. Integrals are one of the many mathematical problems that can be solved using numerical methods. There are many algorithms to solve integrals numerically, one of these methods is the midpoint method. This method looks to draw several rectangles under a curve and add up their areas to find an estimate of the area under the curve. The purpose of a numerical method is to find a solution to a problem using approximations.
I chose the midpoint method for numerical integration and implemented it in F#, if you’re interested in trying out my code you can find it here. Given a function the midpoint method will create N rectangles to approximate the area under the curve of the function. More rectangles mean a much more accurate approximation.
For this example I'm using the function sin(x).
let f x : float =
The midpoint formula requires a starting point and an ending point. Given these values and the number of rectangles we can calculate the change in x.
let h = (b - a) / (float N)
Once we calculate the change in x we proceed into the loop and calculate the area of each rectangle and once we add them up together we obtain the area under the curve.
for i = 0 to N-1 do
let x = (a+h/2.0)
let funcEval = f(x + float i * h)
midpoint <- midpoint + funcEval * h
For this case we’re calculating the integral of sin(x) going from 0 to pi (3.14159) and we’re dividing it into 100 rectangles, the result for this integral is 2.000082, which is a good approximation considering the real result is 2.
Numerical integrals have many applications in the real world, one of the many applications can be found in the implementation of safety measures in the automobile industry. When a car stops the curve for acceleration over time can be abrupt if the car breaks suddenly or it can be gentle if the car slows down at a slower rate. Regardless of the time it takes to decelerate the area under the curve will always be the same since both cases have the same velocity. Numerical integrals can be used to approximate the area under said curve and find out how much deceleration is experienced during a period. Breaking at a lower rate might take longer but the passengers are not subjected to dangerous peaks, while stopping suddenly is faster but it comes at a higher risk. Doing this without a computer would be harder and would be error prone. Using numerical methods to solve this problem can yield very accurate approximations in a small period of time. This can help car manufacturers perform better and faster tests which can in turn result in new and improved safety measures which will save lives.