.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_numerical_methods_general_methods_plot_estimate_integral_iterated_quadrature.py:
Estimate an integral
====================
In this example we are going to evaluate an integral of the form.
.. math::
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.
.. code-block:: default
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
import math as m
ot.Log.Show(ot.Log.NONE)
define the integrand and the bounds
.. code-block:: default
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)'])]
Draw the graph of the integrand and the bounds
.. code-block:: default
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)
view = viewer.View(g)
.. image:: /auto_numerical_methods/general_methods/images/sphx_glr_plot_estimate_integral_iterated_quadrature_001.png
:alt: Integration nodes
:class: sphx-glr-single-img
compute the integral value
.. code-block:: default
I2 = ot.IteratedQuadrature().integrate(f, a, b, l, u)
print(I2)
plt.show()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[-25.1327]
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.133 seconds)
.. _sphx_glr_download_auto_numerical_methods_general_methods_plot_estimate_integral_iterated_quadrature.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_estimate_integral_iterated_quadrature.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_estimate_integral_iterated_quadrature.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_