In [3]:
import numpy as np
import matplotlib.pyplot as plt
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [4]:
def fun(x):
    return np.sin(x)

In [5]:
def Integra(fun, a, b, NPoints, intentos):
    x = np.random.uniform(a,b,NPoints)
    res = []
    for i in xrange(intentos):
        res.append((b-a)/NPoints*sum(fun(x)))
    return np.mean(res)

In [13]:
a = 0
b = np.pi
N = 10000
intentos = 20
print ('El valor de la integral es {}'.format(Integra(fun,a,b,N,intentos)))


El valor de la integral es 2.00917703987

In [ ]: