-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixedPoint.py
More file actions
56 lines (47 loc) · 1.31 KB
/
fixedPoint.py
File metadata and controls
56 lines (47 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from sympy import *
x = symbols("x")
recommencer0 = True
while recommencer0:
try:
f = sympify(input("f(x) = "))
recommencer0 = False
except:
print("Veuillez entrer une fonction valable")
print("******************** Méthode des points fixes ********************")
while True:
try:
x0 = float(input("Entrer la valeur x0 : "))
break
except:
print("Recommencer")
while True:
try:
z = float(input("Critère d'arrêt (epsilon) : "))
assert z > 0
break
except (ValueError, AssertionError):
print("Valeur incorrecte. Recommencer.")
while True:
try:
n = int(input("Nombre maximal d'itérations : "))
assert n > 0
break
except (ValueError, AssertionError):
print("Recommencer.")
while True:
try:
g = sympify(input("Entrer g(x) : "))
break
except:
print("Recommencer.")
x1 = g.subs(x, x0)
i = 0
while abs(x1 - x0) > z and i < n:
x0 = x1
x1 = g.subs(x, x0)
i += 1
pprint(g)
print("La racine approchée est :", x1.evalf(), "\nNombre d'itérations :", i)
choix = input("Voulez-vous visualiser la courbe ? (O pour Oui) : ")
if choix.lower() == 'o':
plot(f, legend=True, line_color="red", show=True)