Numeric Integration

Single Variable Integration

Integration is essentially about calculating areas. Consider the following graph.
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.

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!