Created
March 19, 2015 00:41
-
-
Save lettergram/da70ba74dc8542a3268d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def func1(x, y): | |
return x*x + x*y + y*y | |
def func2(y, x): | |
return x**3 + y**3 + y*x**2 + y**2 + 2 | |
def monte(func, n, ymin, ymax, xmin, xmax): | |
sum = 0 | |
x = np.random.uniform(xmin, xmax, n) | |
y = np.random.uniform(ymin, ymax, n) | |
for i in range(n): | |
sum += func(x[i], y[i]) | |
print(i, sum / (i+1)) | |
area = abs(xmax - xmin) * abs(ymax - ymin) | |
return area * (sum / n) | |
total1 = monte(func1, 10000, 0, 1, 2, 4) | |
total2 = monte(func2, 10000, 1, 3, 1, 2) | |
print(total1, total2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment