Numeric Integration

Single Variable Integration


Integration is essentially about calculating areas. Consider the following graph.
copy
We would like to calculate the area underneath the chart, in this case, it is the blue area. The theory of integration essentially proceeds by sectioning the area underneath the chart into recangular areas, each area we know how to calculate.
copy

Simple Implementation


We can use the davinci api to approximate the integral. Consider the following code, which utilizes the $from api.


let mesh = $from(0,1,100);

let f = Math.sin;
let integral = 0;
$from(0,2*Math.PI, 100).forEach(left=>{
    let right = left + 2*Math.PI/100;
	integral += (right - left) * f(left + (right-left)/2);					
})
					
Try it!

The davinci library also hosts an library for integration, which lets you numerically calculate these integrals without having to understand the internals.


let it = await import('/lib/numeric/integration/v1.0.0/integration.mjs');
let f = Math.sin;
let integral1 = it.leftPoint(f,{
  lower:0,
  upper: 2*Math.PI,
  numberMeshPoints:1000
});
						 
Try it!

Implementations


  • Point Approximations
    • Left Point Approximation
    • Right Point Approximation
    • Mid-Point Approximation
  • Monte Carlo

Topics


  • Error Analysis