ymin, ymax = 0, 1.5 rmin, rmax = 0, 2 theta_min, theta_max = 0, 2 * np.pi #================================================ # compute integral #================================================ #compute definite integral print "Question 3:" print('exact solution part a: ', round(fct_exact(rmax, theta_max) - fct_exact(rmin, theta_min), 3)) print 'monte Carlo solution part a: ' for n in np.arange(100, 1200, 200): gInt = int_utils.monteCarlo(fct2_xy, fct_gxy, rmin, rmax, theta_min, theta_max, n) #in unt_utils the MonteCarlo method results was supposed to be squared, but they never were. gInt = gInt**2 print 'no. random points', n, 'number integral', round(gInt, 3) print print('exac. sol part b: ', round(fct_Fxy_exact(xmax, ymax) - fct_Fxy_exact(xmin, ymin), 3)) print 'monte Carlo solution part b: ' for n in np.arange(100, 1200, 200): fInt = int_utils.monteCarlo(fct_xy, fct_gxy, xmin + 1, xmax + 1, ymin, ymax, n) #in unt_utils the MonteCarlo method results was supposed to be squared, but they never were. fInt = (fInt**2)
def fct_hxy(r, theta): """ Circular domain """ f_retVal = -1 if r <= 2 and theta >= 0 and theta <= 2 * np.pi: f_retVal = 1 return f_retVal #===================== Parameters============================================== xmin, xmax = 0, 2 ymin, ymax = 0, 1.5 #=======================Calculations=========================================== mc_fxy = int_u.monteCarlo(fxy, fct_hxy, 0, 2, 0, 2 * np.pi, 1000) mc_wxy = int_u.monteCarlo(wxy, fct_gxy, xmin - 1, xmax + 1, ymin - 1, ymax + 1, 1000) #=========================Results============================================== print 'a)' print 'Exact solution: 16.7551608191' #from calculator print 'Montecarlo value:', mc_fxy print 'Difference:', 16.7551608191 - mc_fxy print 'b)' print 'Exact solution:', int_wxy(2, 1.5) print 'Montecarlo value:', mc_wxy print 'Difference:', int_wxy(2, 1.5) - mc_wxy
rho = np.sqrt(x**2 + y**2) phi = np.arctan2(y, x) return (rho, phi) #================================================ # parameters #================================================ xmin, xmax = 0, 2 ymin, ymax = 0, 1.5 a_x = np.linspace(-4, 4, 100) a_y = np.linspace(-4, 4, 100) #================================================ # compute integral #================================================ ##A## for n in np.arange(100, 1200, 200): f_Int = int_utils.monteCarlo(fct_xy, fct_gxy, xmin, xmax, ymin, ymax, n) print('3a: no. of ran points', n, 'num integral', round(f_Int, 4)) ##B## f_2Int_exact = fct_Fxy2_exact(xmax, ymax) - fct_Fxy2_exact(xmin, ymin) for n in np.arange(100, 1200, 200): f_2Int = int_utils.monteCarlo(fct2_xy, fct2_gxy, xmin, xmax, ymin, ymax, n) print('3b: no. of ran points', n, 'num integral', round(f_2Int, 4), 'exact', round(f_2Int_exact, 4))
sol_exact3b = W3(x3[N - 1], y3[N - 1]) - W3(x3[0], y3[0]) # ============================================================================= # Integration # ============================================================================= # For part 1. num_sol_1_midpoint = int_utils.midpoint(f1, t1[0], t1[N - 1], N) num_sol_1_trapezoid = int_utils.trapezoidal(f1, t1[0], t1[N - 1], N) # For part 2. # For part 3. num_sol3a = int_utils.monteCarlo(w3, g3, -2, 2, -2, 2, N2) num_sol3b = int_utils.monteCarlo(f3, g3, -1, 3, -1, 2.5, N2) # ============================================================================= # Comparisons # ============================================================================= # For part 1. print( 'Midpoint Method: %4.4f, Trapezoid Method: %4.4f, Exact Solution: %4.4f' % (num_sol_1_midpoint, num_sol_1_trapezoid, sol_exact1)) # For part 2. #print()
#================================================ # parameters #================================================ xmin, xmax = 0, 2 ymin, ymax = 0, 1.5 a_x = np.linspace(-4, 4, 100) a_y = np.linspace(-4, 4, 100) #================================================ # compute integral #================================================ for n in np.arange(100, 1200, 200): fInt = int_utils.monteCarlo(fct_xy, fct_gxy, xmin - 1, xmax + 1, ymin - 1, ymax + 1, n) print("Num of random points", n, 'Num Integral', round(fInt, 4), 'exact', round(2. / 3, 2)) #================================================ # plotting #================================================ m_X, m_Y = np.meshgrid(a_x, a_y) m_Z = fct_xy(m_X, m_Y) m_Z = fct2_xy(m_X, m_Y) fig1 = plt.figure(1, figsize=(10, 10)) ax = axes3d.Axes3D(fig1) plot1 = ax.plot_surface(m_X, m_Y, m_Z,
# parameters #================================================ xmin, xmax = 0, 2 ymin, ymax = 0, 1.5 a_x = np.linspace( -4, 4, 100) a_y = np.linspace( -4, 4, 100) x2min, x2max = 0, 2 y2min, y2max = 0, 2 #================================================ # compute integral #================================================ for n in np.arange(100, 1200, 200): fInt2 = int_utils.monteCarlo(fct2_xy, fct_gxy, x2min -1, x2max + 1, y2min - 1, y2max + 1, n) print( 'a) integral of (x^2 + y^2)^0.5 = ', fInt2) for n in np.arange(100, 1200, 200): fInt = int_utils.monteCarlo(fct_xy, fct_gxy, xmin - 1, xmax + 1, ymin - 1, ymax + 1, n) print ('b) integral of xy^2 = ', round(fInt,4)) """ The real value of a is ~ 3.5 The real value of b is ~ 2.25 Output console (although it is different every time because of random generation of numbers) a) integral of (x^2 + y^2)^0.5 = 3.5728502443735106
#================================================ # parameters #================================================ xmin, xmax = 0, 2 ymin, ymax = 0, 1.5 a_x = np.linspace(-4, 4, 100) a_y = np.linspace(-4, 4, 100) #================================================ # compute integral #================================================ for n in np.arange(100, 10000, 1000): f_MC1 = inte.monteCarlo(fct_xy, fct_gxy, xmin - 1, xmax + 1, ymin - 1, ymax + 1, n) print('n', n, 'numerical sol:', f_MC1) #================================================ # plotting #================================================ m_X, m_Y = np.meshgrid(a_x, a_y) m_Z = fct_xy(m_X, m_Y) #m_Z = fct2_xy( m_X, m_Y) # fig1 = plt.figure(1, figsize=(10, 10)) ax = axes3d.Axes3D(fig1) plot1 = ax.plot_surface(m_X, m_Y, m_Z, cmap=plt.cm.coolwarm_r,
def int_w(x, y): # exact integrated function for fctw return .5 * (x**2) * 1. / 3 * (y**3) #============================================================================== # parameters #============================================================================== xmin, xmax = 0, 2 ymin, ymax = 0, 1.5 rmin, rmax = 0, 2 # polar boundaries for this function hmin, hmax = 0, 2 * np.pi #============================================================================== # compute integral #============================================================================== # compute definite integral for n in np.arange(100, 1200, 200): fInt = int_utils.monteCarlo(fctf_rh, fct_grh, rmin - 1, rmax + 1, hmin - 1, hmax + 1, n) print('no. ran points', n, 'num integral', round(fInt, 4), 'exact', int_f(rmax, hmax) - int_f(rmin, hmin)) for n in np.arange(100, 1200, 200): fInt = int_utils.monteCarlo(fctw_xy, fct_gxy, xmin - 1, xmax + 1, ymin - 1, ymax + 1, n) print('no. ran points', n, 'num integral', round(fInt, 4), 'exact', int_w(xmax, ymax) - int_w(xmin, ymin))
# parameters #================================================ xmin, xmax = 0, 2 ymin, ymax = 0, 1.5 xmin2, xmax2 = 0, 2 ymin2, ymax2 = 0, 2 a_x = np.linspace(-4, 4, 100) a_y = np.linspace(-4, 4, 100) #================================================ # computations #================================================ fInt = int_utils.monteCarlo(fct_xy, fct_gxy, xmin - 1, xmax + 1, ymin - 1, ymax + 1, 1000) f2Int = int_utils.monteCarlo(fct2_xy, fct_gxy, xmin - 1, xmax + 1, ymin - 1, ymax + 1, 1000) exact_f2 = integral_f2(2 * np.pi) - integral_f2(0) exact_f = integral_f(2, 1.5) - integral_f(0, 0) print('part a.)') print('Monte Carlo Integration:' + str(round(f2Int, 4))) print('exact: ' + str(exact_f2)) print('part b.)') print('Monte Carlo Integration:' + str(round(fInt, 4))) print('exact: ' + str(exact_f)) # I am not sure if my part a.) values are correct, but I think the methodology