Pythonです。
昨日まる一日調べましたが、やはり無い頭で考えても、さっぱり何をどうすれば良いのか分からないので教えてください。
やりたいことは、モンテカルロ法で円周率を求める際の横軸を試行回数、縦軸を円内に点を打った回数です。
なお、写真はこのプログラムを実行したときに出たメッセージです。
import math
import numpy as np
import matplotlib.pyplot as plt
n = 100000
x = np.zeros([n])
y = np.zeros([n])
xin = np.zeros([n])
yin = np.zeros([n])
xout = np.zeros([n])
yout = np.zeros([n])
ins = 0
out = 0
def monte(n,ins):
result_x = []
result_y = []
for i in range(n):
x[i] = 1-2*np.random.random()
y[i] = 1-2*np.random.random()
if x[i]**2 + y[i]**2 <= 1:
xin[ins] = x[i]
yin[ins] = y[i]
ins += 1
result_x.append(i)
result_y.append(ins)
return result_x, result_y
result = monte(n,ins)
result_x = result[0]
result_y = result[1]
plt.plot(result_x, result_y)
plt.axhline(math.pi, color='r')
plt.xlim([0,result_x])
plt.ylim([0,result_y])
plt.show()
回答
まだ回答がありません。
この質問を見ている人は
こちらの質問も見ています😉