entrega 1

parent 7d175707
{
"cells": [],
"cells": [
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"# On the computation of $\\pi$"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Asking the maths library"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"My computer tells me that $\\pi$ *is approximatively*"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.141592653589793\n"
]
}
],
"source": [
"from math import *\n",
"print(pi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Buffon’s needle"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Applying the method of [Buffon's needle](https://en.wikipedia.org/wiki/Buffon%27s_needle_problem), we get **the approximation**"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.128911138923655"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"np.random.seed(seed=42)\n",
"N = 10000\n",
"x = np.random.uniform(size=N, low=0, high=1)\n",
"theta = np.random.uniform(size=N, low=0, high=pi/2)\n",
"2/(sum((x+np.sin(theta))>1)/N)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using a surface fraction argument"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A method that is easier to understand and does not make use of the sin function is based on the\n",
"fact that if $$X ∼ \\mathcal{U}(0, 1)$$ and $$\\mathcal{Y} ∼ \\mathcal{U}(0, 1)$$, then $$\\mathcal{P}[X^2 + Y^2 ≤ 1] = π/4$$ (see \"[Monte Carlo method]\"(https://en.wikipedia.org/wiki/Monte_Carlo_method)\n",
"on Wikipedia). The following code uses this approach:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" %matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"np.random.seed(seed=42)\n",
"N = 1000\n",
"x = np.random.uniform(size=N, low=0, high=1)\n",
"y = np.random.uniform(size=N, low=0, high=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_code_all_hidden": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
......@@ -16,10 +142,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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