PREVIOUS: Monte-Carlo Methods | UP: Course Outline | NEXT: Fields from Moving Charges |
def refr(x, y): # return the refractive index at (x, y) if x <= 0.5: return 1.0 else: return 1.5 def transit(y): # return the light transit time for this y array t = 0.0 for i in range(N): x = xmin + (i+0.5)*dx ni = refr(x, 0.5*(y[i]+y[i+1])) dy = y[i+1] - y[i] ds = math.sqrt(dx*dx + dy*dy) t += ni*ds/C return t . . set up the initial y array . . deltay = 0.1 loop: i = random.randint(1, N-1) y0 = y[i] y[i] += random.uniform(-deltay, deltay) t = transit(y)