kk

parent 42877cc6
{ {
"cells": [ "cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# À propos du calcul de $\\pi$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## En demandant à la lib maths\n",
"Mon ordinateur m'indique que $\\pi$ vaut *approximativement*"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -21,7 +36,9 @@ ...@@ -21,7 +36,9 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": {}, "metadata": {
"scrolled": false
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
...@@ -43,6 +60,14 @@ ...@@ -43,6 +60,14 @@
"2/(sum((x+np.sin(theta))>1)/N)" "2/(sum((x+np.sin(theta))>1)/N)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Avec un argument \"fréquentiel\" de surface\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 :"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 5,
...@@ -79,6 +104,34 @@ ...@@ -79,6 +104,34 @@
"ax.set_aspect('equal')" "ax.set_aspect('equal')"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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 :"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'accept' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-b03d6d9f8ffa>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m4\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maccept\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'accept' is not defined"
]
}
],
"source": [
"4*np.mean(accept)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -92,18 +145,6 @@ ...@@ -92,18 +145,6 @@
"display_name": "Python 3", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
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