Facebook
From Abrupt Leech, 3 Days ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 43
  1. from scipy import random
  2. from scipy.integrate import quad
  3. import numpy as np
  4.  
  5.  
  6. a = 0
  7. b = np.pi
  8. N = 10000000
  9. xrand = random.uniform(a,b,N)
  10.  
  11.  
  12. def f(x):
  13.     return np.sin(x)
  14.  
  15. integral = 0.0
  16.  
  17. for i in range(N):
  18.     integral += f(xrand[i])
  19.  
  20. answer = (b-a)/float(N)*integral
  21.  
  22. print("The integral from 0 to pi of sin(x): ", answer)
  23. print(quad(f,0,np.pi)[0])