minor changes

parent 13b4177a
...@@ -4,20 +4,10 @@ ...@@ -4,20 +4,10 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# On the computation of $\\pi$" "# On the computation of $\\pi$\n",
] "\n",
}, "## Asking the maths library\n",
{ "\n",
"cell_type": "markdown",
"metadata": {},
"source": [
"## Asking the maths library"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"My computer tells me that $\\pi$ is *approximatively*" "My computer tells me that $\\pi$ is *approximatively*"
] ]
}, },
...@@ -82,13 +72,8 @@ ...@@ -82,13 +72,8 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Using a surface fraction argument" "## Using a surface fraction argument\n",
] "\n",
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A method that is easier to understand and does not make use of thesin function is based on thefact that if $X \\sim U(0,1)$ and $Y \\sim U(0,1)$, then $P[X^2 + Y^2 \\leq 1] = \\frac{\\pi}{4}$ (see [\"Monte Carlo method\" on Wikipedia](https://en.wikipedia.org/wiki/Monte_Carlo_method). The following code uses this approach:" "A method that is easier to understand and does not make use of thesin function is based on thefact that if $X \\sim U(0,1)$ and $Y \\sim U(0,1)$, then $P[X^2 + Y^2 \\leq 1] = \\frac{\\pi}{4}$ (see [\"Monte Carlo method\" on Wikipedia](https://en.wikipedia.org/wiki/Monte_Carlo_method). The following code uses this approach:"
] ]
}, },
...@@ -113,12 +98,14 @@ ...@@ -113,12 +98,14 @@
"source": [ "source": [
"%matplotlib inline\n", "%matplotlib inline\n",
"import matplotlib.pyplot as plt\n", "import matplotlib.pyplot as plt\n",
"\n",
"np.random.seed(seed=42)\n", "np.random.seed(seed=42)\n",
"N=1000\n", "N=1000\n",
"x=np.random.uniform(size=N, low=0, high=1)\n", "x=np.random.uniform(size=N, low=0, high=1)\n",
"y=np.random.uniform(size=N, low=0, high=1)\n", "y=np.random.uniform(size=N, low=0, high=1)\n",
"accept=(x*x+y*y)<=1\n", "accept=(x*x+y*y)<=1\n",
"reject=np.logical_not(accept)\n", "reject=np.logical_not(accept)\n",
"\n",
"fig, ax=plt.subplots(1)\n", "fig, ax=plt.subplots(1)\n",
"ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None)\n", "ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None)\n",
"ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)\n", "ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)\n",
...@@ -151,13 +138,6 @@ ...@@ -151,13 +138,6 @@
"source": [ "source": [
"4*np.mean(accept)" "4*np.mean(accept)"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment