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.

from davinci.python import _from import math mesh = _from(0,1,100); f = math.sin integral = 0 a = 0 b = 2*math.pi for left in _from(a,b, 100): right = left + b/100 integral += (right - left) * f(left + (right-left)/2)


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

Python Numeric Implementation

from scipy.integrate import quad # Define the function you want to integrate: f(x) = x^2 def integrand(x): return x**2 # Define the integration bounds [a, b] a = 0 b = 2 # quad returns a tuple: (estimated_integral_value, absolute_error) result, error = quad(integrand, a, b) print(f"Approximated Integral: {result}") print(f"Estimated Error: {error}")