Estimate an integralΒΆ

In this example we are going to evaluate an integral of the form.

I_f = \int_{a}^{b}\, \int_{l_1(x_0)}^{u_1(x_0)}\, \int_{l_2(x_0, x_1)}^{u_2(x_0,x_1)}\, \int_{l_{n-1}(x_0, \dots, x_{n-2})}^{u_{n-1}(x_0, \dots, x_{n-2})} \, f(x_0, \dots, x_{n-1})\mathrm{d}{x_{n-1}}\dots\mathrm{d}{x_0}

with the iterated quadrature algorithm.

[2]:
from __future__ import print_function
import openturns as ot
import math as m
[3]:
# define the integrand and the bounds
a = -m.pi
b = m.pi
f = ot.SymbolicFunction(['x', 'y'], ['1+cos(x)*sin(y)'])
l = [ot.SymbolicFunction(['x'], [' 2+cos(x)'])]
u = [ot.SymbolicFunction(['x'], ['-2-cos(x)'])]
[5]:
# Draw the graph of the integrand and the bounds
g = ot.Graph('Integration nodes', 'x', 'y', True, 'topright')
g.add(f.draw([a,a],[b,b]))
curve = l[0].draw(a, b).getDrawable(0)
curve.setLineWidth(2)
curve.setColor('red')
g.add(curve)
curve = u[0].draw(a, b).getDrawable(0)
curve.setLineWidth(2)
curve.setColor('red')
g.add(curve)
g
[5]:
../../_images/examples_numerical_methods_estimate_integral_iterated_quadrature_4_0.svg
[6]:
# compute the integral value
I2 = ot.IteratedQuadrature().integrate(f, a, b, l, u)
print(I2)
[-25.1327]