OffresSe connecter
Terminale › Continuité › Programme Python

Programme Python : méthode (Terminale spé)

Méthode

Programme Python

def dichotomie(f, a, b, e):
    while b - a > e:
        m = (a + b) / 2
        if f(a) * f(m) <= 0:
            b = m
        else:
            a = m
    return a, b

Exemple d'appel pour $f(x)=x^3+x-1$ sur $[0\,;1]$ à $10^{-3}$ près :

def f(x):
    return x**3 + x - 1

print(dichotomie(f, 0, 1, 0.001))

La fonction renvoie un intervalle $[a\,;b]$ de longueur au plus $e$ qui contient la solution.

Exemple

Déroulé à la main

Avec $f(x)=x^3+x-1$, $a=0$, $b=1$ ($f(0)=-1<0$, $f(1)=1>0$) :

Étape $m$ $f(m)$ Signe Nouvel intervalle
1 $0{,}5$ $-0{,}375$ $-$ $[0{,}5\,;1]$
2 $0{,}75$ $\approx 0{,}172$ $+$ $[0{,}5\,;0{,}75]$
3 $0{,}625$ $\approx -0{,}131$ $-$ $[0{,}625\,;0{,}75]$
4 $0{,}6875$ $\approx 0{,}012$ $+$ $[0{,}625\,;0{,}6875]$
5 $0{,}65625$ $\approx -0{,}061$ $-$ $[0{,}65625\,;0{,}6875]$

Après 5 étapes, la solution $\alpha$ est dans un intervalle de longueur $\dfrac{1}{2^5}=0{,}03125$, ce qui est cohérent avec l'encadrement $0{,}68<\alpha<0{,}69$ obtenu à la section 5.

S’entraîner

Dans le cours

Continuité : Programme Python