In [3]: import numpy as np
import [Link] as plt
In [17]: import numpy as np
import [Link] as plt
def f(x):
return x**2 - 1
def dicotomie(f, a, b, e):
if f(a) * f(b) >= 0:
print("La méthode ne fonctionne pas sur cet intervalle.")
return None
a1 = a
b1 = b
while abs(b1 - a1) > e:
m = (a1 + b1) / 2
if f(a1) * f(m) < 0:
b1 = m
else:
a1 = m
return (a1 + b1) / 2
# Paramètres
a = 0.5
b = 1.7
e = 1e-4
# Calcul de la solution
solution = dicotomie(f, a, b, e)
print("Solution approchée :", solution)
Solution approchée : 0.9999877929687501
In [16]: import numpy as np
import [Link] as plt
def f(x):
return x**2 - 1
def dicotomie(f, a, b, e):
if f(a) * f(b) >= 0:
print("La méthode ne fonctionne pas sur cet intervalle.")
return None
a1 = a
b1 = b
while abs(b1 - a1) > e:
m = (a1 + b1) / 2
if f(a1) * f(m) < 0:
b1 = m
else:
a1 = m
return (a1 + b1) / 2
# Paramètres
a = 0.5
b = 1.7
e = 1e-4
# Calcul de la solution
solution = dicotomie(f, a, b, e)
print("Solution approchée :", solution)
# =========================
# Représentation graphique
# =========================
x = [Link](-2, 2, 100)
y = f(x)
[Link]()
[Link](x, y, label="f(x) = x^2 - 1")
[Link](0) # axe des x
# point solution
[Link](solution, f(solution), 'o', label="Solution")
[Link]("Méthode de dichotomie")
[Link]()
[Link]()
[Link]()
Solution approchée : 0.9999877929687501
In [21]: import numpy as np
import [Link] as plt
def f(x):
return x**2 - 1
def dicotomie(f, a, b, e):
if f(a) * f(b) >= 0:
print("La méthode ne fonctionne pas sur cet intervalle.")
return None, 0
a1 = a
b1 = b
n = 0 # compteur d'itérations
while abs(b1 - a1) > e:
m = (a1 + b1) / 2
n += 1
if f(a1) * f(m) < 0:
b1 = m
else:
a1 = m
# (optionnel) afficher les étapes
# print(f"Iteration {n}: a={a1}, b={b1}, m={m}")
return (a1 + b1) / 2, n
# Paramètres
a = 0.5
b = 1.7
e = 10**(-5)
# Calcul
solution, iterations = dicotomie(f, a, b, e)
print("Solution approchée :", solution)
print("Nombre d'itérations :", iterations)
Solution approchée : 1.0000015258789063
Nombre d'itérations : 17
In [24]: import numpy as np
import [Link] as plt
def f(x):
return x**2 - 3
def dicotomie(f, a, b, e):
if f(a) * f(b) >= 0:
print("La méthode ne fonctionne pas sur cet intervalle.")
return None, 0
a1 = a
b1 = b
n = 0 # compteur d'itérations
while abs(b1 - a1) > e:
m = (a1 + b1) / 2
n += 1
if f(a1) * f(m) < 0:
b1 = m
else:
a1 = m
# (optionnel) afficher les étapes
# print(f"Iteration {n}: a={a1}, b={b1}, m={m}")
return (a1 + b1) / 2, n
# Paramètres
a = 0
b = 5
e = 10**-3
# Calcul
solution, iterations = dicotomie(f, a, b, e)
print("Solution approchée :", solution)
print("Nombre d'itérations :", iterations)
# =========================
# Représentation graphique
# =========================
x = [Link](-2, 2, 100)
y = f(x)
[Link]()
[Link](x, y, label="f(x) = x^2 - 3")
[Link](0)
# point solution
[Link](solution, f(solution), 'o', label="Solution")
[Link]("Méthode de dichotomie")
[Link]()
[Link]()
[Link]()
Solution approchée : 1.73187255859375
Nombre d'itérations : 13
In [ ]: