351 lines
8.3 KiB
Plaintext
351 lines
8.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Listes et fonctions"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Graphique de fonctions\n",
|
|
"\n",
|
|
"Nous allons tracer la représentation graphique de \n",
|
|
"\n",
|
|
"$$ f : x \\mapsto x^2 - 10x + 20 $$\n",
|
|
"\n",
|
|
"**Recopier** le programme suivant, **compléter** les pointillés et calculer les images jusqu'à 8."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"L'image de ... est ...\n",
|
|
"L'image de ... est ...\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def f(x):\n",
|
|
" return x**2 - 10*x + 20\n",
|
|
"\n",
|
|
"# image de 0\n",
|
|
"print(\"L'image de\", \"...\",\" est\", \"...\")\n",
|
|
"# image de 1\n",
|
|
"print(\"L'image de\", \"...\",\" est\", \"...\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Pour tracer le graphique de cette fonction, il faudrait *enregistrer* toutes ces valeurs et donc utiliser des variables. \n",
|
|
"\n",
|
|
"Mettre plusieurs valeurs dans une seule variable c'est possible, on utilise des `listes` (on les a déjà rencontré plusieurs fois).\n",
|
|
"\n",
|
|
"**Recopier et compléter** ce programme pour enregistrer les images et les antécédents jusqu'à 10."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"L'image de ... est ... On l'ajoute à la liste\n",
|
|
"L'image de ... est ... On l'ajoute à la liste\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def f(x):\n",
|
|
" return x**2/10 - 5\n",
|
|
"\n",
|
|
"images = [] # La liste est vide\n",
|
|
"antecedents = []\n",
|
|
"\n",
|
|
"\n",
|
|
"# image de 0\n",
|
|
"print(\"L'image de\", \"...\",\" est\", \"...\", \"On l'ajoute à la liste\")\n",
|
|
"antecedents.append(0) # On ajoute (append) 0 à la liste des antecedents\n",
|
|
"# image de 1\n",
|
|
"print(\"L'image de\", \"...\",\" est\", \"...\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Nous allons maintenant enfin pouvoir tracer le graphique.\n",
|
|
"\n",
|
|
"**Ajouter** la ligne suivante au début de votre programme."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import matplotlib.pyplot as plt"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**Ajouter** cette ligne à la fin."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.plot(antecedants, images)\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Vous avez tracer la courbe représentative de de la fonction $f$.\n",
|
|
"\n",
|
|
"![Représentation graphique de f](./fig/plt_f.png)\n",
|
|
"\n",
|
|
"Dans un nouveau fichier, **tracer** la courbe représentative pour x allant de 0 à 15 de la fonction $g$ tel que\n",
|
|
"$$ g : x \\mapsto -(x - 5)^2 + 5$$\n",
|
|
"\n",
|
|
"![Représentation graphique de f](./fig/plt_g.png)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Précision et controle du tracé\n",
|
|
"\n",
|
|
"Pour le moment, on sait donner un maximum à l'antécédent $x$ mais on ne peut pas contrôler le minimum ni le pas (l'écart en 2 valeurs de $x$). \n",
|
|
"\n",
|
|
"Or pour tracer précisément la représentation graphique d'une fonction, nous avons besoin de contrôler la **fenêtre** (minimum et maximum) et le **pas**."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Boucle `while` (tant que )\n",
|
|
"\n",
|
|
"Une boucle `while` permet de répeter une ou plusieurs actions **tant qu'** une condition est vérifiée.\n",
|
|
"\n",
|
|
"Les 2 programmes ci-dessous font la même chose."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0\n",
|
|
"1\n",
|
|
"2\n",
|
|
"3\n",
|
|
"4\n",
|
|
"5\n",
|
|
"6\n",
|
|
"7\n",
|
|
"8\n",
|
|
"9\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"x = 0\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1\n",
|
|
"\n",
|
|
"print(x)\n",
|
|
"x = x+1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0\n",
|
|
"1\n",
|
|
"2\n",
|
|
"3\n",
|
|
"4\n",
|
|
"5\n",
|
|
"6\n",
|
|
"7\n",
|
|
"8\n",
|
|
"9\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"x = 0\n",
|
|
"while x < 10:\n",
|
|
" print(x)\n",
|
|
" x = x + 1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**Commenter** le programme avec le `while`.\n",
|
|
"\n",
|
|
"Que se passe-t-il si on inverse les 2 dernières lignes?\n",
|
|
"\n",
|
|
"**Réécrire** le programme suivant en utilisant une boucle `while` et en stockant les antécédents."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Les images sont [0.0, 1.0, 1.2246467991473532e-16, -1.0, -2.4492935982947064e-16, 1.0, 3.6739403974420594e-16, -1.0, -4.898587196589413e-16, 1.0, 6.123233995736766e-16, -1.0, -7.347880794884119e-16, 1.0, 8.572527594031472e-16, -1.0, -9.797174393178826e-16, 1.0, 1.102182119232618e-15, -1.0]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from math import sin, pi\n",
|
|
"\n",
|
|
"def h(x):\n",
|
|
" return sin(x*pi/2)\n",
|
|
"\n",
|
|
"images = []\n",
|
|
"antecedants = []\n",
|
|
"\n",
|
|
"# image de 0\n",
|
|
"print(\"L'image de\", \"...\",\" est\", \"...\", \"On les ajoute à la liste\")\n",
|
|
"antecedents.append(0)\n",
|
|
"images.append(h(0))\n",
|
|
"\n",
|
|
"# image de 1\n",
|
|
"print(\"L'image de\", \"...\",\" est\", \"...\", \"On les ajoute à la liste\")\n",
|
|
"antecedents.append(1)\n",
|
|
"images.append(h(1))\n",
|
|
"\n",
|
|
"# ....\n",
|
|
"\n",
|
|
"# image de 20\n",
|
|
"print(\"L'image de\", \"...\",\" est\", \"...\", \"On les ajoute à la liste\")\n",
|
|
"antecedents.append(20)\n",
|
|
"images.append(h(20))\n",
|
|
"\n",
|
|
"print(\"Les images sont \", images)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**Modifier** votre programme pour que les antécédents aillent de -2 à 2.\n",
|
|
"\n",
|
|
"**Modifier** votre programme pour que les antécédents augmentent avec un pas de 0.5 au lieu de 1.\n",
|
|
"\n",
|
|
"**Tracer** sa représentation graphique pour $x$ allant de -2 à 2 avec un pas de 1. \n",
|
|
"\n",
|
|
"**Tracer** sa représentation graphique pour $x$ allant de -2 à 2 avec un pas de 0.5. \n",
|
|
"\n",
|
|
"**Tracer** sa représentation graphique pour $x$ allant de -2 à 2 avec un pas de 0.1.\n",
|
|
"\n",
|
|
"En regroupant les 3 programmes écrit précedement, **tracer** sur le même graphique les 3 représentations pour observer l'amélioration de la précision.\n",
|
|
"\n",
|
|
"*Astuce*: pour tracer 3 graphiques en 1, on fait les `plt.plot(...)` de chaque graphique et uniquement à la fin on lance `plt.show()`.\n",
|
|
"\n",
|
|
"![Comparaison des pas](./fig/comp_sin.png)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"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.7.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|