We have $$ \begin{equation} F = \int_a^b f(x)dx. \end{equation} $$
We can do the integral by
>>> from sympy import *
>>> x = 'symbols('x')
>>> f = x*sin(x)*exp(-x)
>>> integrate(f, x(0, 4))
-2*exp(-4)*sin(4) - 5*exp(-4)*cos(4)/2 + 1/2
Or we can do it numerically via the Trapezoidal rule:
import numpy
def trapezoidal(f, a, b, n=100):
"""Integrate f from a to b with 100 intervals."""
x = numpy.linspace(a, b, n+1)
F = (b-a)/float(n)*(numpy.sum(f(x)) - 0.5*(f(a) + f(b)))
return F
def f(x):
return x*numpy.sin(x)*numpy.exp(-x)
print trapezoidal(f, 0, 4)