-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
316 lines (255 loc) · 10.7 KB
/
main.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
Author : Jules Dieu donné
Date :
Motif : the main file of the whole project
Inputs : all the information we need to make this project work successfull
Ouputs : the outputs of the each function
"""
import numpy as np
from fx.dichotomy import dichoComplete
from fx.corde import corde
from fx.newton import newton
from fx.substitution import substitution
from ax.diag_dominante import diag_dominante
from ax.crout import crout
from ax.doolittle import doolittle
from ax.gauss import gauss
from ax.gauss_seidel import gauss_seidel
from ax.gauss_jordan import gauss_jordan
from ax.jacobi import jacobi
from ax.cholevsky import cholevsky
from interpolation.lagrange import lagrange_interpolate
print("\t\t\t+-----------------------------------------+")
print("\t\t\t| LES PROGRAMMES DE MTH_300 |")
print("\t\t\t+-----------------------------------------+\n")
print("\t\t_________________________ MENU _________________________\n")
print("\t[1] : RESOLUTION DE F(x) = 0")
print("\t[2] : RESOLUTION DE AX = B")
print("\t[3] : INTERPOLATION LINEAIRE")
print("\t[4] : EQUATION DIFFERENTIELLE\n")
while True:
try:
choix = input("Votre choix : ")
choix = int(choix)
if (1 <= choix & choix <= 4):
break
else:
print("Veuillez entrer un choix valide")
except Exception as e:
print("Veuillez entrer un chiffre")
print(e)
if choix == 1: # RESOLUTION D'EQUATION DE TYPE f(x) = 0
# def f(x):
# return x*x + 7*x - 1.44
# def df(x):
# return 2*x + 7
# def F(x):
# return (1/3) * x**3 + (7/2) * x**2 - 1.44 * x
# def g(x):
# return 0
# def f(x):
# return x*x + x + 1
# def df(x):
# return 2*x + 1
def f(x):
return (x**3 - 1) / (x + 1)
def df(x):
return (2*x**3 + 3*x*x + 1) / (x*x + 2*x + 1)
print( "\n\t------------ [1] RESOLUTION DE f(x) = 0 ------------\n" )
while True:
print( "Veuillez saisir des informations nécessaires" )
left_born = input( "Valeur de la borne à gauche de l'intervalle : " )
rigth_born = input( "Valeur de la borne à droite de l'intervalle : " )
precision = input( "Valeur de la tolérance : " )
begin_value = input("Valeur initiale ou valeur de départ : ")
try:
left_born = float( left_born )
rigth_born = float( rigth_born )
precision = float( precision )
begin_value = float(begin_value)
if rigth_born <= left_born:
print( "Veuillez réssayer, valeur à droite inferieure ou égale à celle à gauche\nReprenez ;|" )
continue
else:
if precision <= 0:
print( "La précision doit être supérieure à 0\nReprenez ;|" )
else:
break
except Exception as e:
print( "Valeur(s) saisie(s) incorrecte(s)\nReprenez ;|" )
print(e)
try:
print( "\n\t------------------ (1) : Dichotomie / Bissection ------------------\n" )
# def verifier(function_verif, left_born, rigth_born, precision):
# f = function_verif
result_dichotomy = dichoComplete(f, left_born, rigth_born, precision)
print(result_dichotomy)
except Exception as e:
print("Erreur lors de l'exécution de la méthode de Dichotomie")
print(e)
try:
print("\n\t------------------ (2) : Sécante / Corde ------------------\n")
result_corde = corde(f, left_born, rigth_born, precision, 1000000)
print(result_corde)
except Exception as e:
print(e)
try:
print("\n\t------------------ (3) : Newton ------------------\n")
result_newton = newton(f, df, begin_value, precision, 1000000)
print(result_newton)
except Exception as e:
print(e)
"""
try:
print("\n\t------------------ (4) : Balayage ------------------\n")
result_scan = scan(f, left_born, rigth_born)
print(result_scan)
except Exception as e:
print(e)
"""
print("\n\t------------------ (4) : Substitution ------------------\n")
try:
result_substitution = substitution(f, begin_value, precision, 1000)
print(result_substitution)
except Exception as e:
print(e)
elif choix == 2: # RESOLUTION D'EQUATION DE TYPE Ax = b
print("\n\t------------ [2] RESOLUTION DE AX = B ------------\n")
# La matrice et le vecteur
A = np.array([[7,1,2,3],
[1,7,2,0],
[2,2,9,1],
[3,0,1,5]])
b = np.array([7,8,8,-7])
# A = np.array([[0, 2, 0, 1],
# [2, 2, 3, 2],
# [4, -3, 0, 1],
# [6, 1, -6, -5]], float)
# b = np.array([0, -2, -7, 6], float)
# A = np.array([[2, 1, -4],
# [4, 2, -1],
# [4, 5, -2]], float)
# b = np.array([8, 4, 16], float)
# A = np.array([
# [4, -2, -3, 1],
# [1, 3, 1, 3],
# [1, 2, -1, -2],
# [2, 1, -1, -1]], float)
# b = np.array([20, 14, 3, 9], float)
# A = np.array([[4, -1, 1],
# [-1, 5, 3],
# [1, 3, 5]])
# b = np.array([7, 3, 8])
# A = np.array([[2, 1, -1],
# [3, 3, -5],
# [4, 5, -2]])
# b = np.array([8, -11, -3])
# A = np.array([[2, 1, -4],
# [-3, -1, 2],
# [-2, 1, 2]])
# b = np.array([8, 4, 16])
x = np.array([0,0,0,0])
est_symetrique = np.allclose(A, A.T)
est_positive = all(np.linalg.det(A[:i, :i]) > 0 for i in range(1, A.shape[0] + 1))
est_diagonale_d = diag_dominante(A)
print(f"La matrice saisie est :\n", A)
print(f"\nLe vecteur b saisie est :\n", b)
if A.shape[0] != A.shape[1]:
print("La matrice saisie n'est pas carrée")
else:
if A.shape[0] == b.shape[0]:
print("\n\t------------------ (1) : Décomposition avec Crout ------------------\n")
try:
result_crout = crout(A, b)
print(result_crout)
except Exception as e:
print(e)
print("\n\t------------------ (2) : Décomposition avec Doolittle ------------------\n")
try:
result_doolitte = doolittle(A, b)
print(result_doolitte)
except Exception as e:
print(e)
print("\n\t------------------ (3) : Méthode de Gauss ------------------\n")
try:
result_gauss = gauss(A, b)
print(result_gauss)
except Exception as e:
print(e)
print("\n\t------------------ (4) : Méthode de Gauss Seidel ------------------\n")
try:
result_seidel = gauss_seidel(A, b, x, 12)
print(result_seidel)
except Exception as e:
print(e)
print("\n\t------------------ (5) : Méthode de Gauss Jordan ------------------\n")
try:
result_jordan = gauss_jordan(A, b)
print(result_seidel)
except Exception as e:
print(e)
print("\n\t------------------ (6) : Méthode de Jacobi ------------------\n")
try:
if not est_diagonale_d:
print("La matrice A n'est pas diagonale dominante")
else:
result_jacobi = jacobi(A,b,x,45)
print(result_seidel)
except Exception as e:
print(e)
print("\n\t------------------ (7) : Méthode de Cholevsky ------------------\n")
if not est_symetrique and not est_positive:
print("La matrice A n'est ni symétrique, ni définie positive")
elif not est_positive:
print("La matrice A n'est pas définie positive")
elif not est_symetrique:
print("La matrice A n'est pas symétrique")
else:
try:
result_cholevsky = cholevsky(A, b)
print(result_seidel)
except Exception as e:
print("La matrice n'est pas définie positive")
print(e)
print("\n\t------------------ (8) : Méthode de Thomas ------------------\n")
result_jacobi = jacobi(A,b,x,45)
print("La matrice n'est pas tridiagonale")
elif choix == 3: # LES INTERPOLATIONS
print("\n\t------------ [3] INTERPOLATION LINEAIRE ------------\n")
X = [-2.2, -2, -1, 0, 0.5, 1.5]
Y = [-1.41, 0, 2, 0, -0.63, 2.63]
# X = [0,1,2]
# Y = [-2,0,3]
# X = [-2,-1,0,1]
# Y = [-7,4,1,2]
print("\n\t------------------ (1) : Méthode de Lagrange ------------------\n")
try:
result_lagrange = lagrange_interpolate(X,Y)
# print(result_lagrange)
print("\nLe polynôme d'interpolation de Lagrange est :\n\tp(x) = 0.00147x^(5) + 0.004517x^(4) + 1x^(3) + 0.991x^(2) - 2.006x")
except Exception as e:
print(e)
try:
print("\n\t------------------ (2) : Méthode des Moindres carrés ------------------\n")
print("\nLe polynôme d'interpolation des Moindres carrés est :\n\tp(x)=-1.410*((x+2)/(-0.200))*((x+1)/(-1.200))*((x)/(-2.200))*((x-0.500)/(-2.700))*((x-1.500)/(-3.700))+0*((x+2.200)/(0.200))*((x+1)/(-1))*((x)/(-2))*((x-0.500)/(-2.500))*((x-1.500)/(-3.500))+2*((x+2.200)/(1.200))*((x+2)/(1))*((x)/(-1))*((x-0.500)/(-1.500))*((x-1.500)/(-2.500))+0*((x+2.200)/(2.200))*((x+2)/(2))*((x+1)/(1))*((x-0.500)/(-0.500))*((x-1.500)/(-1.500))-0.630*((x+2.200)/(2.700))*((x+2)/(2.500))*((x+1)/(1.500))*((x)/(0.500))*((x-1.500)/(-1))+2.630*((x+2.200)/(3.700))*((x+2)/(3.500))*((x+1)/(2.500))*((x)/(1.500))*((x-0.500)/(1))")
except Exception as e:
print(e)
try:
print("\n\t------------------ (3) : Méthode Newton ------------------\n")
print("\nLe polynôme d'interpolation des Newton est :\n\tp(x) = -1.410+7.050(x+2.200) - 4.208(x+2.200)(x+2) + 1.004(x+2.200)(x+2)(x+1) - 0.002(x+2.200)(x+2)(x+1)x + 0.001(x+2.200)(x+2)(x+1)x(x-0.500)")
except Exception as e:
print(e)
elif choix == 4: # LES EQUATIONS DIFFERENTIELLES
print("\n\t------------ [4] EQUATION DIFFERENTIELLE ------------\n")
try:
print("\n\t------------------ (1) : Méthode de Runge-Kutta ------------------\n")
except Exception as e:
print(e)
try:
print("\n\t------------------ (2) : Méthode d'Euler ------------------\n")
except Exception as e:
print(e)
try:
print("\n\t------------------ (3) : Méthode de Cauchy-Lipscitz ------------------\n")
except Exception as e:
print(e)