Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "まずは2次元球(円)のシミュレーションから"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "3.1419336"
- ]
- }
- ],
- "source": [
- "count = 0\n",
- "N = 10000000\n",
- "for i in 1:N\n",
- " x = rand()\n",
- " y = rand()\n",
- " if x^2 + y^2 < 1\n",
- " count = count + 1\n",
- " end\n",
- "end\n",
- "print((4*count)/N)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "3.141592653589793"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pi * 1^2"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "3次元球のシミュレーション"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4.1888864"
- ]
- }
- ],
- "source": [
- "count = 0\n",
- "N = 10000000\n",
- "for i in 1:N\n",
- " x = rand()\n",
- " y = rand()\n",
- " z = rand()\n",
- " if x^2 + y^2 + z^2 < 1\n",
- " count = count + 1\n",
- " end\n",
- "end\n",
- "print((8*count)/N)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "4.1887902047863905"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "(4/3) * pi*1^3"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "2.550164039877345"
- ]
- },
- "execution_count": 34,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "n=15\n",
- "((pi^(n/2))/gamma((n/2)+1))*1^n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "n次元球のシミュレーション"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2.43712\n",
- "2.550164039877345\n"
- ]
- }
- ],
- "source": [
- "count = 0\n",
- "N = 100000 # 繰り返し回数(シミュレーション回数)\n",
- "n = 10 # 次元数\n",
- "for i in 1:N\n",
- " if sum(rand(1,n).^2) < 1\n",
- " count = count + 1\n",
- " end\n",
- "end\n",
- "println((2^n*count)/N) # シミュレーションの値\n",
- "println(((pi^(n/2))/gamma((n/2)+1))*1^n) #実際の値"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Julia 0.6.3",
- "language": "julia",
- "name": "julia-0.6"
- },
- "language_info": {
- "file_extension": ".jl",
- "mimetype": "application/julia",
- "name": "julia",
- "version": "0.6.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment