FICHE NUMPY / MATPLOTLIB — L'ESSENTIEL
IMPORTS
import numpy as np
import [Link] as plt
CRÉER DES TABLEAUX
Fonction Exemple
[Link](a, b, n) [Link](-2, 2, 400) → 400 pts de -2 à
2
[Link](a, b, pas) [Link](0, 5, 1.2) → [0, 1.2, 2.4, 3.6,
4.8]
[Link]([...]) [Link]([1, 2, 3])
[Link]((n, p)) [Link]((3, 3)) → matrice de zéros
[Link]((n, p)) [Link]((2, 2)) → matrice de uns
[Link](n) [Link](3) → matrice identité 3×3
[Link](n, p) [Link](80, 80) → valeurs entre 0
et 1
INFOS SUR UN TABLEAU
Attribut Ce que ça donne
[Link] nombre total d'éléments
[Link] dimensions ex: (3,) ou (2,3)
[Link] type des éléments ex: float64
OPÉRATIONS SUR TABLEAUX
Opération Code
Calcul terme à terme f = x * [Link](-x) ou f = x**2
Addition scalaire x + 5 → ajoute 5 à chaque élément
Multiplication scalaire 2 * x → multiplie chaque élément par 2
Produit terme à terme x * y (pas produit matriciel)
Produit matriciel A @ B ou [Link](A, B)
Transposée A.T
Somme tous éléments [Link](A)
Trace [Link](A)
Copie B = [Link]()
MAX / MIN
Ce qu'on cherche Code
Valeur max [Link](f)
Indice du max i = [Link](f)
Position x du max x[i]
Valeur min [Link](f)
Indice du min i = [Link](f)
LECTURE / SLICING
Syntaxe Signification
v[k] élément d'indice k
v[1:] tout sauf le premier
v[:-1] tout sauf le dernier
v[i:j] de l'indice i à j-1
M[i, j] élément ligne i, colonne j
M[i, :] ligne i entière
M[:, j] colonne j entière
TESTS SUR MATRICES
Test Code
Vérifier symétrie [Link](B, B.T) → True/False
Tous éléments > 0 ? (A > 0).all()
Au moins un > 0 ? (A > 0).any()
Deux tableaux égaux ? (A == B).all()
DÉRIVÉE NUMÉRIQUE
Formule : f'(xi) ≈ (f(xi+1) - f(xi)) / (xi+1 - xi)
fp = (f[1:] - f[:-1]) / (x[1:] - x[:-1])
⚠ fp a un élément de moins que f → tracer avec x[:-1]
MATPLOTLIB — TRACER
Action Code
Courbe simple [Link](x, f)
Courbe avec label [Link](x, f, label='f(x)')
Point rouge (max) [Link](x_max, f_max, 'ro')
Titre [Link]('mon titre')
Label axes [Link]('x') / [Link]('y')
Légende [Link]()
Grille [Link]()
Afficher [Link]()
Plusieurs courbes plusieurs [Link]() avant [Link]()
SUITES
Suite simple Un(n) :
def Un(n):
return (-1)**n / (n + 1)
n = [Link](0, 101)
[Link](n, Un(n))
[Link]()
Suite récurrente u_{n+1} = f(u_n) — jusqu'à convergence :
def f(x):
return (3*x + 1) / (2*x + 2)
u = [0]
while abs(u[-1] - f(u[-1])) > 1e-6:
[Link](f(u[-1]))
print('limite =', u[-1])
EXEMPLE COMPLET — f(x) = x·e^(-x) (type DS)
import numpy as np
import [Link] as plt
# Q1 - créer x
x = [Link](-2, 2, 400)
# Q2 - calculer f
f = x * [Link](-x)
# Q3 - tracer
[Link](x, f)
[Link]()
# Q4 - max et position
i_max = [Link](f)
x_max = x[i_max]
f_max = f[i_max]
print(x_max, f_max)
# Q5 - accentuer le max
[Link](x, f)
[Link](x_max, f_max, 'ro', label='max')
[Link]()
[Link]()
# Q6 - dérivée
fp = (f[1:] - f[:-1]) / (x[1:] - x[:-1])
# Q7 - tracer dérivée
[Link](x[:-1], fp)
[Link]()
# Matrice (exo 2)
A = [Link](80, 80)
trace = [Link](A)
somme = [Link](A)
B = A.T @ A
print([Link](B, B.T)) # vérifie symétrie → True