見出し画像

การคำนวณค่า Pi และปริมาตรของทรงกลมหลายมิติ โดยวิธีของ MonteCarlo โดยภาษา python

ในยุคที่คอมพิวเตอร์พลังแรงมากมาย เราสามาถใช้พลังการคำนวณนี้ ให้เป็นประโยชน์ ในชีวิตจริงได้หลากหลายมาก ๆ และ หนึ่งในเทคนิคที่ใช้กันบ่อยและสุดยอดคือ เทคนิค MonteCarlo method 
เนื่องจากในเน็ตมีการอธิบายเทคนิคนี้ดีมาก เยอะแยะมาก ผมขอไม่เขียนใหม่ตรงนี้ 
ใครอยากเข้าใจวิธีการคำนวณแบบ MonteCarlo ลองได้ที่นี่ครับ https://en.wikipedia.org/wiki/Monte_Carlo_method

และต่อไปนี้คือ python code สำหรับการคำนวณค่า Pi โดยวิธี MonteCarlo 

import random

def estimate_pi(n):
    num_points_inside_circle = 0
    num_points_total = n

    for i in range(n):
        x = random.uniform(-1, 1)
        y = random.uniform(-1, 1)

        if x**2 + y**2 <= 1:
            num_points_inside_circle += 1

    return 4 * num_points_inside_circle / num_points_total

n = 1000000  # number of points
pi_estimate = estimate_pi(n)

print("Estimated value of pi using Monte Carlo method:", pi_estimate)
Estimated value of pi using Monte Carlo method: 3.140848


ลองอีกแบบ โค๊ด คล้ายๆ กัน

import random
import math

def estimate_pi(n):
    inside = 0
    for i in range(n):
        x = random.random()
        y = random.random()
        if math.sqrt(x**2 + y**2) <= 1:
            inside += 1
    pi = 4 * inside / n
    return pi

print(estimate_pi(1000000))
3.14052

วิธีนี้หาค่า Pi แบบนี้ เล็งดี ๆ  มองอีกมุมก็คือ การหาค่าพื้นที่ของวงกลมรัศมี 1 นั่นเอง

ในชีวิตประจำวัน เรารู้จัก วงกลม(สองมิติ) ทรงกลม(สามมิติ) และเรียนวิธีหาพื้นที่วงกลมและปริมาตรทรงกลม กันมาทุกคน คราวนี้ ว่าแต่ สำหรับรูปทรงกลมมากกว่าสามมิติ มนุษย์ธรรมดา คงนึกภาพไม่ออก แต่ในเชิงคณิตศาสตร์ สามารถคิดได้ และ มีสูตรทั่วไป สำหรับการหา ปริมาตร (volume) ซะด้วย ใครที่สนใจ ลอง ค้นในเว็บดูนะครับ และแนวคิดนี้จะเอาไปใช้ในการคำนวณในฟิสิกส์และคณิตศาสตร์ที่เกี่ยวข้องกับมิติที่มากๆ ฉะนั้น รู้ไว้ น่าจะสนุกและเผลอๆ เรียนไปเรียนมาอาจจะได้ใช้นะครับ https://en.wikipedia.org/wiki/Volume_of_an_n-ball https://ja.wikipedia.org/wiki/%E8%B6%85%E7%90%83%E3%81%AE%E4%BD%93%E7%A9%8D

จากลิงค์ เราจะรู้จักกับ สูตรสำหรับหาค่าปริมาตรของรูปทรงกลมหลายมิติ ได้ และ สามารถเขียนโปรแกรม python สำหรับหาค่าปริมาตรของรูปทรงกลม ที่มีรัศมี 1 จาก 0 จนถึง 20 มิติ ได้ดังนี้
ถ้าต้องการ สามารถ เปลี่ยนค่ามิติสูงสุดที่ต้องการหาได้ โดยการเปลี่ยนค่า dimension ในโปรแกรม และรัศมีเปลี่ยนได้โดยเปลี่ยนค่า radius

import math

def volume_of_sphere(n, radius):
    gamma_value = math.gamma(n/2 + 1)
    volume = math.pow(math.pi, n/2) / gamma_value * math.pow(radius, n)
    return volume

dimension = 20
radius = 1

for i in range(dimension +1):
    radius = 1
    sphere_volume = volume_of_sphere(i, radius)
    print("Volume of a", i, "dimensional sphere with radius", radius, "=", sphere_volume)
Volume of a 0 dimensional sphere with radius 1 = 1.0
Volume of a 1 dimensional sphere with radius 1 = 1.9999999999999998
Volume of a 2 dimensional sphere with radius 1 = 3.141592653589793
Volume of a 3 dimensional sphere with radius 1 = 4.1887902047863905
Volume of a 4 dimensional sphere with radius 1 = 4.934802200544679
Volume of a 5 dimensional sphere with radius 1 = 5.263789013914325
Volume of a 6 dimensional sphere with radius 1 = 5.167712780049969
Volume of a 7 dimensional sphere with radius 1 = 4.7247659703314016
Volume of a 8 dimensional sphere with radius 1 = 4.058712126416768
Volume of a 9 dimensional sphere with radius 1 = 3.2985089027387064
Volume of a 10 dimensional sphere with radius 1 = 2.550164039877345
Volume of a 11 dimensional sphere with radius 1 = 1.8841038793898994
Volume of a 12 dimensional sphere with radius 1 = 1.3352627688545893
Volume of a 13 dimensional sphere with radius 1 = 0.910628754783283
Volume of a 14 dimensional sphere with radius 1 = 0.5992645293207919
Volume of a 15 dimensional sphere with radius 1 = 0.38144328082330436
Volume of a 16 dimensional sphere with radius 1 = 0.23533063035889312
Volume of a 17 dimensional sphere with radius 1 = 0.14098110691713897
Volume of a 18 dimensional sphere with radius 1 = 0.08214588661112819
Volume of a 19 dimensional sphere with radius 1 = 0.046621601030088534
Volume of a 20 dimensional sphere with radius 1 = 0.02580689139001405

จะเห็นความแปลกๆ ก็คือ ปริมาตรของทรงกลมหลายมิติ ไม่ได้ใหญ่ขึ้นๆ ตามขนาดของมิติ แต่ใหญ่ที่สุดที่ 5 มิติ แล้วหลังจากนั้นลดลง จนในที่สุด มีขนาดเป็นศูนย์ มันช่างขัดความรู้สึกพอสมควร หาก ใครเข้าใจตรงนี้ได้ ช่วยขยายความต่อด้วยละกันนะครับ :-)

ทีนี้ หากเราไม่รู้จักสูตรที่ว่า แต่ ต้องหา ปริมาตรของ ทรงกลม ในหลายมิติ เราสามารถใช้ ความรู้จาก การหาในสองมิติ ข้างต้น ขยายแนวคิดให้เป็น การหาปริมาตรของ ทรงกลม ในหลายมิติได้ และเราสามารถเขียน python code ให้คำนวณ ปริมาตรของ ทรงกลม ในหลายมิติ โดยวิธีของ MonteCarlo ได้ดังนั้ครับ

import random
import math

def n_sphere_volume(n, samples):
    count = 0
    for i in range(samples):
        point = []
        for j in range(n):
            point.append(random.uniform(-1, 1))
        distance = math.sqrt(sum([x ** 2 for x in point]))
        if distance <= 1:
            count += 1
    return 2 ** n * count / samples

dimension=20
radius = 1

for j in range(dimension+1):
    print("Volume of a", j, "dimensional sphere with radius", radius, "= by MonteCarloMethod", n_sphere_volume(j,100000))
Volume of a 0 dimensional sphere with radius 1 = by MonteCarloMethod 1.0
Volume of a 1 dimensional sphere with radius 1 = by MonteCarloMethod 2.0
Volume of a 2 dimensional sphere with radius 1 = by MonteCarloMethod 3.13596
Volume of a 3 dimensional sphere with radius 1 = by MonteCarloMethod 4.19064
Volume of a 4 dimensional sphere with radius 1 = by MonteCarloMethod 4.96688
Volume of a 5 dimensional sphere with radius 1 = by MonteCarloMethod 5.27552
Volume of a 6 dimensional sphere with radius 1 = by MonteCarloMethod 5.20128
Volume of a 7 dimensional sphere with radius 1 = by MonteCarloMethod 4.66816
Volume of a 8 dimensional sphere with radius 1 = by MonteCarloMethod 3.96288
Volume of a 9 dimensional sphere with radius 1 = by MonteCarloMethod 3.5072
Volume of a 10 dimensional sphere with radius 1 = by MonteCarloMethod 2.47808
Volume of a 11 dimensional sphere with radius 1 = by MonteCarloMethod 1.69984
Volume of a 12 dimensional sphere with radius 1 = by MonteCarloMethod 1.06496
Volume of a 13 dimensional sphere with radius 1 = by MonteCarloMethod 0.65536
Volume of a 14 dimensional sphere with radius 1 = by MonteCarloMethod 0.16384
Volume of a 15 dimensional sphere with radius 1 = by MonteCarloMethod 0.32768
Volume of a 16 dimensional sphere with radius 1 = by MonteCarloMethod 0.0
Volume of a 17 dimensional sphere with radius 1 = by MonteCarloMethod 0.0
Volume of a 18 dimensional sphere with radius 1 = by MonteCarloMethod 0.0
Volume of a 19 dimensional sphere with radius 1 = by MonteCarloMethod 0.0
Volume of a 20 dimensional sphere with radius 1 = by MonteCarloMethod 0.0

จะเห็นว่า การคำนวณโดย MonterCarlo ซึ่งถือเป็นการคำนวณโดยสถิติ ความแม่นยำเป็นไปตามหลักสถิติ ค่าที่ได้ จะไม่เป๊ะๆ เหมือนกับการใช้สูตร

เอาสองวิธีมาใส่ในโค๊ดเดียวกัน จะได้เปรียบเทียบผลคำนวณได้ง่ายนิด

import math
import random

dimension=20
radius = 1

def volume_of_sphere(n, radius):
    gamma_value = math.gamma(n/2 + 1)
    volume = math.pow(math.pi, n/2) / gamma_value * math.pow(radius, n)
    return volume

def n_sphere_volume(n, samples):
    count = 0
    for i in range(samples):
        point = []
        for j in range(n):
            point.append(random.uniform(-1, 1))
        distance = math.sqrt(sum([x ** 2 for x in point]))
        if distance <= 1:
            count += 1
    return 2 ** n * count / samples

for j in range(dimension+1):
    sphere_volume = volume_of_sphere(j, radius)
    print("Volume of a", j, "dimensional sphere with radius", radius, " by formula   = ", sphere_volume)
    print("Volume of a", j, "dimensional sphere with radius", radius, " by MonteCarlo= ", n_sphere_volume(j,100000))
    print()
Volume of a 0 dimensional sphere with radius 1  by formula   =  1.0
Volume of a 0 dimensional sphere with radius 1  by MonteCarlo=  1.0

Volume of a 1 dimensional sphere with radius 1  by formula   =  1.9999999999999998
Volume of a 1 dimensional sphere with radius 1  by MonteCarlo=  2.0

Volume of a 2 dimensional sphere with radius 1  by formula   =  3.141592653589793
Volume of a 2 dimensional sphere with radius 1  by MonteCarlo=  3.1294

Volume of a 3 dimensional sphere with radius 1  by formula   =  4.1887902047863905
Volume of a 3 dimensional sphere with radius 1  by MonteCarlo=  4.19296

Volume of a 4 dimensional sphere with radius 1  by formula   =  4.934802200544679
Volume of a 4 dimensional sphere with radius 1  by MonteCarlo=  4.98832

Volume of a 5 dimensional sphere with radius 1  by formula   =  5.263789013914325
Volume of a 5 dimensional sphere with radius 1  by MonteCarlo=  5.26496

Volume of a 6 dimensional sphere with radius 1  by formula   =  5.167712780049969
Volume of a 6 dimensional sphere with radius 1  by MonteCarlo=  5.23968

Volume of a 7 dimensional sphere with radius 1  by formula   =  4.7247659703314016
Volume of a 7 dimensional sphere with radius 1  by MonteCarlo=  4.6592

Volume of a 8 dimensional sphere with radius 1  by formula   =  4.058712126416768
Volume of a 8 dimensional sphere with radius 1  by MonteCarlo=  4.16256

Volume of a 9 dimensional sphere with radius 1  by formula   =  3.2985089027387064
Volume of a 9 dimensional sphere with radius 1  by MonteCarlo=  3.4304

Volume of a 10 dimensional sphere with radius 1  by formula   =  2.550164039877345
Volume of a 10 dimensional sphere with radius 1  by MonteCarlo=  2.57024

Volume of a 11 dimensional sphere with radius 1  by formula   =  1.8841038793898994
Volume of a 11 dimensional sphere with radius 1  by MonteCarlo=  1.45408

Volume of a 12 dimensional sphere with radius 1  by formula   =  1.3352627688545893
Volume of a 12 dimensional sphere with radius 1  by MonteCarlo=  1.39264

Volume of a 13 dimensional sphere with radius 1  by formula   =  0.910628754783283
Volume of a 13 dimensional sphere with radius 1  by MonteCarlo=  0.65536

Volume of a 14 dimensional sphere with radius 1  by formula   =  0.5992645293207919
Volume of a 14 dimensional sphere with radius 1  by MonteCarlo=  0.65536

Volume of a 15 dimensional sphere with radius 1  by formula   =  0.38144328082330436
Volume of a 15 dimensional sphere with radius 1  by MonteCarlo=  0.0

Volume of a 16 dimensional sphere with radius 1  by formula   =  0.23533063035889312
Volume of a 16 dimensional sphere with radius 1  by MonteCarlo=  0.0

Volume of a 17 dimensional sphere with radius 1  by formula   =  0.14098110691713897
Volume of a 17 dimensional sphere with radius 1  by MonteCarlo=  0.0

Volume of a 18 dimensional sphere with radius 1  by formula   =  0.08214588661112819
Volume of a 18 dimensional sphere with radius 1  by MonteCarlo=  0.0

Volume of a 19 dimensional sphere with radius 1  by formula   =  0.046621601030088534
Volume of a 19 dimensional sphere with radius 1  by MonteCarlo=  0.0

Volume of a 20 dimensional sphere with radius 1  by formula   =  0.02580689139001405
Volume of a 20 dimensional sphere with radius 1  by MonteCarlo=  0.0

この記事が気に入ったらサポートをしてみませんか?