Vous êtes sur la page 1sur 7

Devoir Libre - Correction

EX 1
In [1]: def dec2bin(n):
if n == 0:
return [0]
elif n == 1:
return [1]
else:
return dec2bin(n // 2) + [n % 2]

EX 2
In [2]: def longueur(s):
if s == '':
return 0
else:
return 1 + longueur(s[1:])

In [3]: def inverse(s):


if s == '':
return s
else:
return s[-1] + inverse(s[:-1])

EX 3
In [4]: def SommeCh1(n):
if n == 0:
return 0
else:
return n % 10 + SommeCh1(n // 10)

In [5]: def SommeCh2(n):


s = str(n)
if len(s) == 1:
return int(s)
else:
return int(s[0]) + SommeCh2(int(s[1:]))

EX 4
In [6]: def EstTriee(L):
if len(L) <= 1:
return True

elif L[0] > L[1]:


return False
else:
return EstTriee(L[1:])
EX 5
In [7]: def Maximum(L):
n = len(L)
if n == 1:
return L[0]
else:
m = n // 2
g = Maximum(L[:m])
d = Maximum(L[m:])
if g > d:
return g
else:
return d

EX 6
In [8]: def Division(a, b):
if a < b:
return 0, a
else:
q, r = Division(a - b, b)
return q + 1, r

EX 7
In [9]: import numpy as np
import matplotlib.pyplot as plt

Q1
In [10]: def f(x):
return x**2 - 2
In [11]: x = np.linspace(1, 4, 100)
y = f(x)
plt.plot(x, y, label='f(x) = x^2 - 2')
plt.title('Tracé de la fonction f(x) = x^2 - 2 sur [1, 4]')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.show()

Q2

2
x − 2 = 0

En ajoutant 2 des deux côtés, on obtient :


2
x = 2

Puis en prenant la racine carrée des deux côtés, on a deux solutions, une positive et une négative :

x = ±√ 2 ​

Cependant, dans l'intervalle [1,4], seule x = √2​est une solution.
Ainsi, la solution de l'équation

f (x) = 0 sur l'intervalle [1,4] est x = √2.

Q3
In [12]: def nvInt(f, a, b):
m = (a + b) / 2
if f(m) * f(a) < 0:
return [a, m]
else:
return [m, b]
Q4
In [13]: def solution(f, a, b, eps):
if f(a) == 0:
return a
elif f(b) == 0:
return b
elif b-a < eps:
return (a + b) / 2
else:
a, b = nvInt(f, a, b)
return solution(f, a, b, eps)

Q5
In [14]: def solution_modifie(f, a, b, eps, itr):
if f(a) == 0:
return a, itr
elif f(b) == 0:
return b, itr
elif b-a < eps:
return (a + b) / 2 , itr
else:
a, b = nvInt(f, a, b)
return solution_modifie(f, a, b, eps, itr+1)

Q6
In [15]: racine2 = np.sqrt(2)
print(racine2)

1.4142135623730951

Q7
In [16]: eps = 1e-11
a = 1
b = 4
sol = solution(f, a, b, eps)
print(sol)

1.4142135623742433

In [17]: ecart = abs(sol - racine2)


print(ecart)

1.1481926520673369e-12

Q8
In [18]: def g(x):
return x**2 - 10
−−
la solution de g(x) = 0 est √10
−−
donc, en utilisant la fonction solution(g, a, b, eps) afin de donner une approximation de √10

In [19]: appr = solution(g, a, b, eps)


print(appr)

3.1622776601698206

In [20]: racine10 = np.sqrt(10)


print(racine10)

3.1622776601683795

EX 8

Q1
In [21]: def f(x):
return x**2

Q2
In [22]: def integration_newton(f, Int, n):
a, b = Int
h = (b - a) / n # Largeur de chaque subdivision
resultat = 0
for i in range(n):
p1 = a + h*i # le premier point de la subdivision
p2 = a + h*(i+1) # le deuxième point de la subdivision
m = (p1 + p2) / 2 # le point médian de la subdivision
resultat += h*f(m) # surface du rectangle = largeur * Hauteur
return resultat

Q3
In [23]: I = integration_newton(f, [0, 1], 100)
print(I)

0.33332500000000004

1
pour calculer la valeur exacte de ∫0 f (x)dx on calcule la primitive de f

In [24]: # la primitive de f
def F(x):
return (1/3)*x**3

In [25]: valeur_exacte = F(1) - F(0)

In [26]: print(valeur_exacte) # à comparer avec I calcuer avant

0.3333333333333333
EX 9
In [27]: U0 = 8000
taux = 3

Q1
In [28]: montant_interet = (U0 * taux) / 100
print(montant_interet)

240.0

Q2

Suite arithmétique de raison 240

Q3
In [29]: # Fonction pour calculer les valeurs du capital au bout de chaque année
def U(U0, n):
capital = [U0]
for a in range(1, n + 1):
nouveau_capital = capital[-1] + montant_interet
capital.append(nouveau_capital)
return capital

Q4
In [30]: nb_annees = 15
annees_liste = list(range(2023, 2023 + nb_annees + 1))
capital_liste = U(U0, nb_annees)
In [31]: plt.plot(annees_liste, capital_liste)
plt.title('Évolution du capital au fil des années')
plt.xlabel('Année')
plt.ylabel('Capital (dh)')
plt.show()

In [ ]:

Vous aimerez peut-être aussi