no commit message

parent e391755a
{
"cells": [],
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# toy_notebook_fr\n",
"\n",
"## March 28, 2019\n",
"\n",
"## 1 À propos du calcul de p\n",
"\n",
"## 1.1 En demandant à la lib maths\n",
"\n",
"Mon ordinateur m’indique que _p_ vautapproximativement\n",
"\n",
"In [ 1 ]: frommathimport *\n",
"print(pi)\n",
"\n",
"3.\n",
"\n",
"## 1.2 En utilisant la méthode des aiguilles de Buffon\n",
"\n",
"Mais calculé avec la **méthode** desaiguilles de Buffon, on obtiendrait comme **approximation** :\n",
"\n",
"In [ 2 ]: import numpyas 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)\n",
"\n",
"Out[ 2 ]: 3.\n",
"\n",
"## 1.3 Avec un argument \"fréquentiel\" de surface\n",
"\n",
"Sinon, une méthode plus simple à comprendre et ne faisant pas intervenir d’appel à la fonction\n",
"sinus se base sur le fait que siX \u0018U(0, 1)etY \u0018U(0, 1)alorsP[X^2 +Y^2 \u0014 1 ]= _p_ /4 (voir\n",
"méthode de Monte Carlo sur Wikipedia). Le code suivant illustre ce fait :\n",
"\n",
"In [ 3 ]: %matplotlib inline\n",
"import matplotlib.pyplotas plt\n",
"\n",
"```\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 )\n",
"```\n",
"### 1\n",
"\n",
"\n",
"```\n",
"accept =(x*x+y*y) <= 1\n",
"reject =np.logical_not(accept)\n",
"```\n",
"```\n",
"fig, ax =plt.subplots( 1 )\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.set_aspect('equal')\n",
"```\n",
"Il est alors aisé d’obtenir une approximation (pas terrible) de _p_ en comptant combien de fois,\n",
"en moyenne,X^2 +Y^2 est inférieur à 1 :\n",
"\n",
"In [ 4 ]: 4 *np.mean(accept)\n",
"\n",
"Out[ 4 ]: 3.\n",
"\n",
"### 2\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
......@@ -16,10 +96,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [],
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1\n",
"# À propos du calcul de $\\pi$\n",
"\n",
"1\n",
"## En demandant à la lib maths\n",
"2\n",
"Mon ordinateur m'indique que $\\pi$ vaut *approximativement*\n",
"In [1]:\n",
"\n",
"1\n",
"from math import *\n",
"2\n",
"print(pi)\n",
"3.141592653589793\n",
"\n",
"1\n",
"## En utilisant la méthode des aiguilles de Buffon\n",
"2\n",
"Mais calculé avec la __méthode__ des [aiguilles de Buffon](https://fr.wikipedia.org/wiki/Aiguille_de_Buffon), on obtiendrait comme __approximation__ :\n",
"3\n",
"​\n",
"In [2]:\n",
"\n",
"1\n",
"import numpy as np\n",
"2\n",
"np.random.seed(seed=42)\n",
"3\n",
"N = 10000\n",
"4\n",
"x = np.random.uniform(size=N, low=0, high=1)\n",
"5\n",
"theta = np.random.uniform(size=N, low=0, high=pi/2)\n",
"6\n",
"2/(sum((x+np.sin(theta))>1)/N)\n",
"3.1289111389236548\n",
"\n",
"1\n",
"## Avec un argument \"fréquentiel\" de surface\n",
"2\n",
"Sinon, une méthode plus simple à comprendre et ne faisant pas intervenir d'appel à la fonction sinus se base sur le fait que si $X\\sim U(0,1)$ et $Y\\sim U(0,1)$ alors $P[X^2+Y^2\\leq 1] = \\pi/4$ (voir [méthode de Monte Carlo sur Wikipedia](https://fr.wikipedia.org/wiki/M%C3%A9thode_de_Monte-Carlo#D%C3%A9termination_de_la_valeur_de_%CF%80)). Le code suivant illustre ce fait :\n",
"In [3]:\n",
"\n",
"1\n",
"%matplotlib inline \n",
"2\n",
"import matplotlib.pyplot as plt\n",
"3\n",
"​\n",
"4\n",
"np.random.seed(seed=42)\n",
"5\n",
"N = 1000\n",
"6\n",
"x = np.random.uniform(size=N, low=0, high=1)\n",
"7\n",
"y = np.random.uniform(size=N, low=0, high=1)\n",
"8\n",
"​\n",
"9\n",
"accept = (x*x+y*y) <= 1\n",
"10\n",
"reject = np.logical_not(accept)\n",
"11\n",
"​\n",
"12\n",
"fig, ax = plt.subplots(1)\n",
"13\n",
"ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None)\n",
"14\n",
"ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)\n",
"15\n",
"ax.set_aspect('equal')\n",
"\n",
"\n",
"1\n",
"Il est alors aisé d'obtenir une approximation (pas terrible) de $\\pi$ en comptant combien de fois, en moyenne, $X^2 + Y^2$ est inférieur à 1 :\n",
"In [4]:\n",
"\n",
"1\n",
"4*np.mean(accept)\n",
"3.1120000000000001\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
......@@ -16,10 +107,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