opensw-project / Calculator

GNU General Public License v2.0
3 stars 6 forks source link

적분 그래프 (코드 수정 중) #50

Open choimingyu99 opened 1 year ago

choimingyu99 commented 1 year ago

import math import numpy as np import matplotlib.pyplot as plt from scipy import integrate

iteration = 10000

fx_x = np.arange(0.3 , 1.3 , 1 / iteration) fx_y = np.exp(-np.square(fx_x))

height = input() width = input()

x_rand= np.random.rand(iteration) y_rand = np.random.rand(iteration) fx_rand = np.exp(-np.square(x_rand))

x_in = [] y_in = [] x_out = [] y_out = []

for i in range(iteration): if y_rand[i] <= fx_rand[i]: x_in.append(x_rand[i]) y_in.append(y_rand[i]) else: x_out.append(x_rand[i]) y_out.append(y_rand[i])

ratio = len(y_in) / iteration

area_target = ratio * area_rectangular

random_number = np.random.uniform(low = 0,high = 1, size = iteration)

exp_x_xsquare = np.exp(-np.square(random_number))

scipy_solution = intergrate.quad(lambda x : math.exp(-x**2), 0, 1) print("Conditional : {}, Monre Carlo : {}, scipy : { }.format(area_target, manual_solution,scipy_solution)")

plt.plot(fx_x, fx_y) plt.scatter(x_out, y_out, s=1, color='red') plt.scatter(x_in, y_in, s=1, color='blue') plt.axvline(x=0, color='black') plt.axhline(x=0, color='black') plt.xlim(-0.3, 1.3) plt.ylim(-0.3, 2.3) plt.grid() plt.show()

Asfanchik commented 1 year ago

Cool