Advertisement
reeps

baluev_3.1-2

Mar 30th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. import sys, math, random, ROOT
  2.  
  3. input_x_a = 0.01
  4. input_x_b = 1
  5. input_y_a = 0
  6. input_y_b = 1
  7.  
  8. def f(x,y):
  9.     #return 1 / ( math.pow(x,2) * ( y + math.sin(x)/2 ) ) - 1/(math.pow(x,2) + y)
  10.     return 1 / ( math.pow(x,2) * ( y + math.sin(x)/2 ) ) - 1/(math.pow(x,2) + y)
  11.  
  12. def single(f, x_limits, y_limits):
  13.     x_a, x_b = x_limits
  14.     y_a, y_b = y_limits
  15.     x = ROOT.gRandom.Uniform(x_a, x_b)
  16.     y = ROOT.gRandom.Uniform(y_a, y_b)
  17.     return (x_b - x_a) * (y_b - y_a) * f(x, y)
  18.  
  19. outerN = 10
  20. innerN = 1000
  21.  
  22. sumtot = 0
  23. sum2tot = 0
  24. ntot = 0
  25.  
  26. for i in range(outerN):
  27.     sumloc, sum2loc, nloc = [0,0,0]
  28.     for j in range(innerN):
  29.         r = (single(f, (input_x_a, input_x_b), (input_y_a, input_y_b)))
  30.         sumloc += r
  31.         sum2loc += r**2
  32.  
  33.     sumtot += sumloc
  34.     sum2tot += sum2loc
  35.     ntot += 1000
  36.  
  37.     run_average = sumloc / innerN
  38.     run_variance = (sum2loc / innerN) - (run_average)**2
  39.     run_sigma = math.sqrt( run_variance / innerN )
  40.  
  41.     average = sumtot / ntot
  42.     variance = (sum2tot / ntot) - (sumtot / ntot)**2
  43.     sigma = math.sqrt( variance / ntot )
  44.  
  45.     print run_average, " +/- ", run_sigma,
  46.     print average, " +/- ", sigma
  47.  
  48. def f_for_drawing(x, p):
  49.     return f(x[0], x[1])
  50.  
  51. tf2 = ROOT.TF2("f", f_for_drawing, input_x_a, input_x_b, input_y_a, input_y_b, 1)
  52. tf2.Draw("surf1z")
  53. sys.stdin.readline()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement