- Écrire un script (programme) Python pour obtenir le graphique
suivant :
-
Écrire une fonction Python nommée
f()
permettant d'obtenir un tel graphique sur l'intervalle [m
;M
], oùm
etM
sont des entiers relatifs passés en paramètres. - Écrire une fonction Python nommée
g()
qui renvoie l'image du réel $x$ où $x \in [m ; M]$, $m$ et $M$ sont des entiers relatifs, et dont le graphe est celui de la question précédente.
- Question 1
- Question 2
- Question 3
from matplotlib.pyplot import *
X = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Y = [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
# construction du graphique :
plot(X, Y, 'g-', linewidth=4)
# pour imposer repère orthonormé :
axis('equal')
# tracé d'une grille en fond :
grid()
# pour voir le résultat :
show()
from matplotlib.pyplot import *
def f(m, M) :
X = list(range(m, M+1)) # équivalent de X = [ k for k in range(m,M+1) ]
Y = [ n%2 for n in X ]
# construction du graphique :
plot(X, Y, 'g-', linewidth=4)
# pour imposer repère orthonormé :
axis('equal')
# tracé d'une grille en fond :
grid()
# pour voir le résultat :
show()
##----- Appel de la fonction sur l'intervalle [-5 ; 6] -----##
f(-5, 6)
Un code possible :
from matplotlib.pyplot import *
from math import floor # floor : fonction partie entière
# from numpy import arange
def f(x) :
if floor(x)%2 == 0 :
return x-floor(x)
else :
return -x+ floor(x)+1
###############################################
# Graphique pour vérification de la formule :
###############################################
m = -5
M = 6
X = [m+i/10 for i in range(10*(M-m)+1)] # liste [m, m+0.1, m+0.2, m+0.3, m+0.4, ..., M]
# arange(m, M+0.1, 0.1) en important le module numpy
Y = [ f(x) for x in X ]
# construction du graphique :
plot(X, Y, 'g-', linewidth=4)
# pour imposer repère orthonormé :
axis('equal')
# tracé d'une grille en fond :
grid()
# pour voir le résultat :
show()