Sols TD2
Sols TD2
Sols TD2
1 Exercice 12
[2]: numbers = [0.1] * 1000000
sum_normal = sum(numbers)
# Somme exacte
sum_exact = len(numbers) * 0.1
2 Exercice 11
[3]: dtype = np.float64
a = np.float64(1.000001)
1
b = np.float64(-1000.0)
c = np.float64(1.0)
discriminant = b**2 - 4 * a * c
if discriminant >= 0:
x1 = (-b + np.sqrt(discriminant)) / (2 * a)
x2 = (-b - np.sqrt(discriminant)) / (2 * a)
else:
x1 = None
x2 = None
a = np.float32(1.000001)
b = np.float32(-1000.0)
c = np.float32(1.0)
discriminant = b**2 - 4 * a * c
if discriminant >= 0:
x1 = (-b + np.sqrt(discriminant)) / (2 * a)
x2 = (-b - np.sqrt(discriminant)) / (2 * a)
else:
x1 = None
x2 = None
2
3 Exercice 9
[5]: n = 10000
sum_approx = np.float32(0.0)
for i in range(1, n + 1):
term = 1.0 / np.float32(i * i)
sum_approx += term
4 Exercice 7
[6]: n_terms = 1000000
pi_approx = 0.0
for k in range(n_terms):
denominator = 2 * k + 1
term = 1.0 / denominator if k % 2 == 0 else -1.0 / denominator
pi_approx += term
pi_approx *= 4.0
pi_math = math.pi
3
• Modified Newton-Raphson:
𝑓(𝑥𝑛 )𝑓 ′ (𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
(𝑓 ′ (𝑥𝑛 ))2 − 𝑓(𝑥𝑛 )𝑓 ″ (𝑥𝑛 )
• Halley:
𝑓(𝑥𝑛 )𝑓 ′ (𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
(𝑓 ′ (𝑥𝑛 ))2 − 12 𝑓(𝑥𝑛 )𝑓 ″ (𝑥𝑛 )
Test functions:
• 𝑓1 (𝑥) = 𝑥𝑒𝑥 for which the root is 𝑥 = 0.
• 𝑓2 (𝑥) = (𝑥 − 1)3 (𝑥 + 2) for which the root is 𝑥 = 0 on ℝ+ .
• 𝑓3 (𝑥) = (𝑥 − 2)6 − 3 for which the root is 𝑥 = 2 − 31/6 on the interval [0, 2].
As parameters of convergence, use a tolerance of 1e-6 and a maximum number of iterations of 100.
[7]: - 1.0e308 + 1.0e308
[7]: 0.0
def f(x):
return (x - 2)**6 - 3
def df(x):
return 6 * (x - 2)**5
def d2f(x):
return 30 * (x - 2)**4
4
x -= (f_value * f_derivative) / ((f_derivative**2) - (f_value *␣
↪f_double_derivative))
if abs(f_value) < tol:
return i,x
return None
tolerance = 1e-6
max_iterations = 100
initial_guess = 1.2
5
Méthode de Halley :
Résultat : 0.7990630448239971 avec iteration : 4