Skip to content
GitLab
Explorer
Connexion
S'inscrire
Navigation principale
Rechercher ou aller à…
Projet
L
lphys1201
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Extraits de code
Compilation
Pipelines
Jobs
Planifications de pipeline
Artéfacts
Déploiement
Releases
Registre de paquets
Registre de conteneur
Registre de modèles
Opération
Environnements
Modules Terraform
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse CI/CD
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté
Contribuer à GitLab
Donner votre avis
Conditions générales et politique de confidentialité
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
Jérôme de Favereau de Jeneret
lphys1201
Validations
d66d9f9a
Valider
d66d9f9a
rédigé
3 years ago
par
Jérôme de Favereau de Jeneret
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
Upload New File
parent
436e4504
Branches
Project
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
1
Masquer les modifications d'espaces
En ligne
Côte à côte
Affichage de
1 fichier modifié
NM_3_exercice.ipynb
+339
-0
339 ajouts, 0 suppression
NM_3_exercice.ipynb
avec
339 ajouts
et
0 suppression
NM_3_exercice.ipynb
0 → 100644
+
339
−
0
Voir le fichier @
d66d9f9a
{
"cells": [
{
"cell_type": "markdown",
"id": "narrow-newspaper",
"metadata": {},
"source": [
"## Exercice : méthode de Romberg\n",
"\n",
"Estimation de \n",
"\n",
"$$\n",
"\\int_0^{\\frac{3\\pi}{4}} \\sin x\\ dx\n",
"$$\n",
"\n",
"Par la méthode de Romberg. Cette intégrale vaut $1 + 1/\\sqrt{2} \\approx 1.7071067811865475$"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "accomplished-coordinator",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R11=0.8330405509046936\n"
]
}
],
"source": [
"import numpy as np\n",
"import math\n",
"\n",
"def f(x):\n",
" return np.sin(x)\n",
"\n",
"# méthode des trapèzes avec 2 points:\n",
"x_0 = 0\n",
"x_n = 3 * math.pi / 4\n",
"R11 = (f(x_0) + f(x_n))/2 * (x_n - x_0)\n",
"print(f\"{R11=}\")"
]
},
{
"cell_type": "markdown",
"id": "annoying-access",
"metadata": {},
"source": [
"Calcul de R21 par la méthode itérative des trapèzes:"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "fewer-partition",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R21=1.5049402075046334\n"
]
}
],
"source": [
"R21 = R11/2 + f(x_n/2) * (x_n - x_0)/2\n",
"print(f\"{R21=}\")"
]
},
{
"cell_type": "markdown",
"id": "returning-mission",
"metadata": {},
"source": [
"calcul de R22 par Richardson avec $p=2$ puisque l'erreur est dominée par $h^2$:"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "liable-forestry",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R22=1.7289067597046133\n"
]
}
],
"source": [
"R22 = (4*R21 - R11)/3\n",
"print(f\"{R22=}\")"
]
},
{
"cell_type": "markdown",
"id": "streaming-tucson",
"metadata": {},
"source": [
"Calcul de R31 par la méthode itérative des trapèzes:"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "cathedral-investigation",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R31=1.6574582026781939\n"
]
}
],
"source": [
"h = (x_n - x_0)/2 # espace entre les nouveaux points\n",
"x = x_0 + h/2 # position x du premier point\n",
"R31 = R21/2 + (f(x) + f(x+h)) * h/2\n",
"print(f\"{R31=}\")"
]
},
{
"cell_type": "markdown",
"id": "sacred-pension",
"metadata": {},
"source": [
"calcul de R32 par Richardson avec $p=2$ puisque l'erreur sur R21 et R31 est dominée par $h^2$:"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "compliant-zimbabwe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R32=1.708297534402714\n"
]
}
],
"source": [
"R32 = (4*R31 - R21)/3\n",
"print(f\"{R32=}\")"
]
},
{
"cell_type": "markdown",
"id": "elect-desktop",
"metadata": {},
"source": [
"calcul de R33 par Richardson avec $p=4$ puisque l'erreur sur R22 et R32 est dominée par $h^4$:"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "floppy-alignment",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R33=1.706923586049254\n"
]
}
],
"source": [
"R33 = (16*R32 - R22)/15\n",
"print(f\"{R33=}\")"
]
},
{
"cell_type": "markdown",
"id": "surgical-syndrome",
"metadata": {},
"source": [
"Même chose en utilisant les fonctions définies au cours:"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "green-league",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R11=0.8330405509046936\n",
"R21=1.5049402075046334\n",
"R22=1.7289067597046133\n",
"R31=1.6574582026781939\n",
"R32=1.708297534402714\n",
"R33=1.706923586049254\n",
"R41=1.6947487165588795\n",
"R42=1.7071788878524412\n",
"R43=1.7071043114157562\n",
"R44=1.7071071800723674\n",
"\n",
"En utilisant la formule analytique:\n",
"INT=1.7071067811865475\n"
]
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from math import sin, pi, sqrt\n",
"def trapezes_rec(f, x_0, x_n, i_old, k):\n",
" if k == 1: \n",
" return (f(x_0) + f(x_n))*(x_n - x_0)/2\n",
" else:\n",
" n = 2**(k-2)\n",
" h = (x_n - x_0)/n\n",
" x = x_0 + h/2\n",
" sum = 0\n",
" for i in range(n):\n",
" sum += f(x)\n",
" x += h\n",
" return i_old/2 + h*sum/2\n",
"\n",
" \n",
"def richardson(i_1, i_2, k):\n",
" fact = 2**k\n",
" return (fact*i_2 - i_1)/(fact - 1)\n",
"\n",
"# ligne 1\n",
"R11 = trapezes_rec(np.sin, 0, 3*pi/4, 0, 1)\n",
"print(f\"{R11=}\")\n",
"\n",
"# ligne 2\n",
"R21 = trapezes_rec(np.sin, 0, 3*pi/4, R11, 2)\n",
"print(f\"{R21=}\")\n",
"R22 = richardson(R11, R21, 2)\n",
"print(f\"{R22=}\")\n",
"\n",
"# ligne 3\n",
"R31 = trapezes_rec(np.sin, 0, 3*pi/4, R21, 3)\n",
"print(f\"{R31=}\")\n",
"R32 = richardson(R21, R31, 2)\n",
"print(f\"{R32=}\")\n",
"R33 = richardson(R22, R32, 4)\n",
"print(f\"{R33=}\")\n",
"\n",
"# ligne 4\n",
"R41 = trapezes_rec(np.sin, 0, 3*pi/4, R31, 4)\n",
"print(f\"{R41=}\")\n",
"R42 = richardson(R31, R41, 2)\n",
"print(f\"{R42=}\")\n",
"R43 = richardson(R32, R42, 4)\n",
"print(f\"{R43=}\")\n",
"R44 = richardson(R33, R43, 6)\n",
"print(f\"{R44=}\")\n",
"\n",
"print(\"\\nEn utilisant la formule analytique:\")\n",
"print('INT={}'.format(1+1/math.sqrt(2)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "leading-montana",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "printable-neutral",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"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.8.10"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:markdown id:narrow-newspaper tags:
## Exercice : méthode de Romberg
Estimation de
$$
\i
nt_0^{
\f
rac{3
\p
i}{4}}
\s
in x
\
dx
$$
Par la méthode de Romberg. Cette intégrale vaut $1 + 1/
\s
qrt{2}
\a
pprox 1.7071067811865475$
%% Cell type:code id:accomplished-coordinator tags:
```
python
import
numpy
as
np
import
math
def
f
(
x
):
return
np
.
sin
(
x
)
# méthode des trapèzes avec 2 points:
x_0
=
0
x_n
=
3
*
math
.
pi
/
4
R11
=
(
f
(
x_0
)
+
f
(
x_n
))
/
2
*
(
x_n
-
x_0
)
print
(
f
"
{
R11
=
}
"
)
```
%% Output
R11=0.8330405509046936
%% Cell type:markdown id:annoying-access tags:
Calcul de R21 par la méthode itérative des trapèzes:
%% Cell type:code id:fewer-partition tags:
```
python
R21
=
R11
/
2
+
f
(
x_n
/
2
)
*
(
x_n
-
x_0
)
/
2
print
(
f
"
{
R21
=
}
"
)
```
%% Output
R21=1.5049402075046334
%% Cell type:markdown id:returning-mission tags:
calcul de R22 par Richardson avec $p=2$ puisque l'erreur est dominée par $h^2$:
%% Cell type:code id:liable-forestry tags:
```
python
R22
=
(
4
*
R21
-
R11
)
/
3
print
(
f
"
{
R22
=
}
"
)
```
%% Output
R22=1.7289067597046133
%% Cell type:markdown id:streaming-tucson tags:
Calcul de R31 par la méthode itérative des trapèzes:
%% Cell type:code id:cathedral-investigation tags:
```
python
h
=
(
x_n
-
x_0
)
/
2
# espace entre les nouveaux points
x
=
x_0
+
h
/
2
# position x du premier point
R31
=
R21
/
2
+
(
f
(
x
)
+
f
(
x
+
h
))
*
h
/
2
print
(
f
"
{
R31
=
}
"
)
```
%% Output
R31=1.6574582026781939
%% Cell type:markdown id:sacred-pension tags:
calcul de R32 par Richardson avec $p=2$ puisque l'erreur sur R21 et R31 est dominée par $h^2$:
%% Cell type:code id:compliant-zimbabwe tags:
```
python
R32
=
(
4
*
R31
-
R21
)
/
3
print
(
f
"
{
R32
=
}
"
)
```
%% Output
R32=1.708297534402714
%% Cell type:markdown id:elect-desktop tags:
calcul de R33 par Richardson avec $p=4$ puisque l'erreur sur R22 et R32 est dominée par $h^4$:
%% Cell type:code id:floppy-alignment tags:
```
python
R33
=
(
16
*
R32
-
R22
)
/
15
print
(
f
"
{
R33
=
}
"
)
```
%% Output
R33=1.706923586049254
%% Cell type:markdown id:surgical-syndrome tags:
Même chose en utilisant les fonctions définies au cours:
%% Cell type:code id:green-league tags:
```
python
import
matplotlib.pyplot
as
plt
import
numpy
as
np
from
math
import
sin
,
pi
,
sqrt
def
trapezes_rec
(
f
,
x_0
,
x_n
,
i_old
,
k
):
if
k
==
1
:
return
(
f
(
x_0
)
+
f
(
x_n
))
*
(
x_n
-
x_0
)
/
2
else
:
n
=
2
**
(
k
-
2
)
h
=
(
x_n
-
x_0
)
/
n
x
=
x_0
+
h
/
2
sum
=
0
for
i
in
range
(
n
):
sum
+=
f
(
x
)
x
+=
h
return
i_old
/
2
+
h
*
sum
/
2
def
richardson
(
i_1
,
i_2
,
k
):
fact
=
2
**
k
return
(
fact
*
i_2
-
i_1
)
/
(
fact
-
1
)
# ligne 1
R11
=
trapezes_rec
(
np
.
sin
,
0
,
3
*
pi
/
4
,
0
,
1
)
print
(
f
"
{
R11
=
}
"
)
# ligne 2
R21
=
trapezes_rec
(
np
.
sin
,
0
,
3
*
pi
/
4
,
R11
,
2
)
print
(
f
"
{
R21
=
}
"
)
R22
=
richardson
(
R11
,
R21
,
2
)
print
(
f
"
{
R22
=
}
"
)
# ligne 3
R31
=
trapezes_rec
(
np
.
sin
,
0
,
3
*
pi
/
4
,
R21
,
3
)
print
(
f
"
{
R31
=
}
"
)
R32
=
richardson
(
R21
,
R31
,
2
)
print
(
f
"
{
R32
=
}
"
)
R33
=
richardson
(
R22
,
R32
,
4
)
print
(
f
"
{
R33
=
}
"
)
# ligne 4
R41
=
trapezes_rec
(
np
.
sin
,
0
,
3
*
pi
/
4
,
R31
,
4
)
print
(
f
"
{
R41
=
}
"
)
R42
=
richardson
(
R31
,
R41
,
2
)
print
(
f
"
{
R42
=
}
"
)
R43
=
richardson
(
R32
,
R42
,
4
)
print
(
f
"
{
R43
=
}
"
)
R44
=
richardson
(
R33
,
R43
,
6
)
print
(
f
"
{
R44
=
}
"
)
print
(
"
\n
En utilisant la formule analytique:
"
)
print
(
'
INT={}
'
.
format
(
1
+
1
/
math
.
sqrt
(
2
)))
```
%% Output
R11=0.8330405509046936
R21=1.5049402075046334
R22=1.7289067597046133
R31=1.6574582026781939
R32=1.708297534402714
R33=1.706923586049254
R41=1.6947487165588795
R42=1.7071788878524412
R43=1.7071043114157562
R44=1.7071071800723674
En utilisant la formule analytique:
INT=1.7071067811865475
%% Cell type:code id:leading-montana tags:
```
python
``
`
%%
Cell
type
:
code
id
:
printable
-
neutral
tags
:
```
python
```
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter