Guest User

Untitled

a guest
Jun 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "まずは2次元球(円)のシミュレーションから"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 20,
  13. "metadata": {},
  14. "outputs": [
  15. {
  16. "name": "stdout",
  17. "output_type": "stream",
  18. "text": [
  19. "3.1419336"
  20. ]
  21. }
  22. ],
  23. "source": [
  24. "count = 0\n",
  25. "N = 10000000\n",
  26. "for i in 1:N\n",
  27. " x = rand()\n",
  28. " y = rand()\n",
  29. " if x^2 + y^2 < 1\n",
  30. " count = count + 1\n",
  31. " end\n",
  32. "end\n",
  33. "print((4*count)/N)"
  34. ]
  35. },
  36. {
  37. "cell_type": "code",
  38. "execution_count": 21,
  39. "metadata": {},
  40. "outputs": [
  41. {
  42. "data": {
  43. "text/plain": [
  44. "3.141592653589793"
  45. ]
  46. },
  47. "execution_count": 21,
  48. "metadata": {},
  49. "output_type": "execute_result"
  50. }
  51. ],
  52. "source": [
  53. "pi * 1^2"
  54. ]
  55. },
  56. {
  57. "cell_type": "markdown",
  58. "metadata": {},
  59. "source": [
  60. "3次元球のシミュレーション"
  61. ]
  62. },
  63. {
  64. "cell_type": "code",
  65. "execution_count": 19,
  66. "metadata": {},
  67. "outputs": [
  68. {
  69. "name": "stdout",
  70. "output_type": "stream",
  71. "text": [
  72. "4.1888864"
  73. ]
  74. }
  75. ],
  76. "source": [
  77. "count = 0\n",
  78. "N = 10000000\n",
  79. "for i in 1:N\n",
  80. " x = rand()\n",
  81. " y = rand()\n",
  82. " z = rand()\n",
  83. " if x^2 + y^2 + z^2 < 1\n",
  84. " count = count + 1\n",
  85. " end\n",
  86. "end\n",
  87. "print((8*count)/N)"
  88. ]
  89. },
  90. {
  91. "cell_type": "code",
  92. "execution_count": 14,
  93. "metadata": {},
  94. "outputs": [
  95. {
  96. "data": {
  97. "text/plain": [
  98. "4.1887902047863905"
  99. ]
  100. },
  101. "execution_count": 14,
  102. "metadata": {},
  103. "output_type": "execute_result"
  104. }
  105. ],
  106. "source": [
  107. "(4/3) * pi*1^3"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": 34,
  113. "metadata": {},
  114. "outputs": [
  115. {
  116. "data": {
  117. "text/plain": [
  118. "2.550164039877345"
  119. ]
  120. },
  121. "execution_count": 34,
  122. "metadata": {},
  123. "output_type": "execute_result"
  124. }
  125. ],
  126. "source": [
  127. "n=15\n",
  128. "((pi^(n/2))/gamma((n/2)+1))*1^n"
  129. ]
  130. },
  131. {
  132. "cell_type": "markdown",
  133. "metadata": {},
  134. "source": [
  135. "n次元球のシミュレーション"
  136. ]
  137. },
  138. {
  139. "cell_type": "code",
  140. "execution_count": 49,
  141. "metadata": {},
  142. "outputs": [
  143. {
  144. "name": "stdout",
  145. "output_type": "stream",
  146. "text": [
  147. "2.43712\n",
  148. "2.550164039877345\n"
  149. ]
  150. }
  151. ],
  152. "source": [
  153. "count = 0\n",
  154. "N = 100000 # 繰り返し回数(シミュレーション回数)\n",
  155. "n = 10 # 次元数\n",
  156. "for i in 1:N\n",
  157. " if sum(rand(1,n).^2) < 1\n",
  158. " count = count + 1\n",
  159. " end\n",
  160. "end\n",
  161. "println((2^n*count)/N) # シミュレーションの値\n",
  162. "println(((pi^(n/2))/gamma((n/2)+1))*1^n) #実際の値"
  163. ]
  164. },
  165. {
  166. "cell_type": "code",
  167. "execution_count": null,
  168. "metadata": {},
  169. "outputs": [],
  170. "source": []
  171. }
  172. ],
  173. "metadata": {
  174. "kernelspec": {
  175. "display_name": "Julia 0.6.3",
  176. "language": "julia",
  177. "name": "julia-0.6"
  178. },
  179. "language_info": {
  180. "file_extension": ".jl",
  181. "mimetype": "application/julia",
  182. "name": "julia",
  183. "version": "0.6.3"
  184. }
  185. },
  186. "nbformat": 4,
  187. "nbformat_minor": 2
  188. }
Add Comment
Please, Sign In to add comment