toy-notebook4

parent 73f24198
......@@ -4,38 +4,59 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# toy-notbook-fr\n",
"# ** À propos du calcul de $\\pi$\n",
"\n",
"## March 28, 2019\n",
"\n",
"### **1 À propos du calcul de** $\\pi$\n",
"\n",
"#### **1.1 En demandant à la lib maths**\n",
"## **1.1 En demandant à la lib maths**\n",
"\n",
"Mon ordinateur m'indique que $\\pi$ vaut *approximativement*\n",
"\n",
"In [1]: >from math import *\n",
" >print (pi)\n",
"\n",
"In [1]:\n",
"1 from math import *\n",
"2 print (pi)\n",
" \n",
" 3.141592653589793\n",
"3.141592653589793\n",
" \n",
"\n",
"#### **1.2 En utilisant la méthode des aiguilles de Buffon**\n",
"## **1.2 En utilisant la méthode des aiguilles de Buffon\n",
"\n",
"Mais calculé avec la **méthode** des aiguilles de Buffon, on obtiendrait comme **approximation**\n",
"Mais calculé avec la **méthode** des [aiguilles de Buffon]\n",
"(https://fr.wikipedia.org/wiki/Aiguille_de_Buffon), on \n",
"obtiendrait comme **approximation** :\n",
"\n",
"In [2]: >import numpy as np\n",
" >np.random.seed(seed=42)\n",
" >N = 10000\n",
" >x = np.random.uniform(size=N, low=0, high=pi/2)\n",
" >2/(sum((x+np.sin(theta))>1)/N)\n",
"In [2]:\n",
"1 import numpy as np\n",
"2 np.random.seed(seed=42)\n",
"3 N = 10000\n",
"4 x = np.random.uniform(size=N, low=0, high=1)\n",
"5 theta = np.random.uniform(size=N, low=0, high=pi/2)\n",
"6 2/(sum((x+np.sin(theta))>1)/N)\n",
" \n",
"Out[2]: 3.1289111389236548\n",
"3.1289111389236548\n",
"\n",
"1 ## Avec un argument \"fréquentiel\" de surface\n",
"2 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\n",
"1] = \\pi/4$ (voir [méthode de Monte Carlo sur Wikipedia]\n",
"(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",
"\n",
"#### **1.3 Avec un argument \"fréquentiel\" de surface\n",
"In [3]:\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 si *X* $\\sim$ *U*(0,1) alors *P*[$X^2$ + $Y^2$ ≤ 1] = $^\\pi/_4$ (voir)"
"1%matplotlib inline\n",
"2 iport matplotlib.pyplot as plt\n",
"3\n",
"4 np.random.dees(seed=42)\n",
"5 N = 1000\n",
"6 x = np.random.uniform(size=N, low=0, high=1)\n",
"7 y = np.random.uniform(size=N, low=0, high=1)\n",
"8\n",
"9 accept = (x*x+y*y) <=1\n",
"10 reject = np.logical_not'accept)\n",
"11\n",
"12 fig, ax = plt.subplots(1)\n",
"13 ax.scatter(x[accept], y [accept], c='b', alpha=0.2, edgecolor=None)\n",
"14 ax.scatter(x[reject], y [reject], c='r', alpha=0.2, edgecolor=None)\n",
"15 ax.set_aspect('equal')\n",
"\n"
]
}
],
......
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