| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import numpy as np
- from scipy.optimize import minimize
- x = 3.3
- #fonction python qui trouve les meilleur coef de A et de B dans la fonction x = (0.81 * (A+B)) / B.
- #ou A et proche de 50
- #ou x est un parametre d'entrée de la fonction
- #ou A et B doivent appartenir a une liste
- e192 = np.array([10.0, 10.2, 10.5, 10.7, 11.0, 11.3, 11.5, 11.8, 12.1, 12.4,12.7, 13.0, 13.3, 13.7, 14.0, 14.3, 14.7, 15.0, 15.4, 15.8,16.2, 16.5, 16.9, 17.4, 17.8, 18.2, 18.7, 19.1, 19.6, 20.0,20.5, 21.0, 21.5, 22.1, 22.6, 23.2, 23.7, 24.3, 24.9, 25.5,26.1, 26.7, 27.4, 28.0, 28.7, 29.4, 30.1, 30.9, 31.6, 32.4,33.2, 34.0, 34.8, 35.7, 36.5, 37.4, 38.3, 39.2, 40.2, 41.2,42.2, 43.2, 44.2, 45.3, 46.4, 47.5, 48.7, 49.9, 51.1, 52.3,53.6, 54.9, 56.2, 57.6, 59.0, 60.4, 61.9, 63.4, 64.9, 66.5,68.1, 69.8, 71.5, 73.2, 75.0, 76.8, 78.7, 80.6, 82.5, 84.5,86.6, 88.7, 90.9, 93.1, 95.3, 97.6, 100.0])
-
- def find_coeffs2(X, lst):
- best_a, best_b = None, None
- min_diff = float('inf') # initialisation avec une valeur très grande pour la différence
- for a in lst:
- for b in lst:
- if a != 0 and a != b: # on évite la division par zéro et la redondance des paires A, B
- diff = abs(X - (0.81 * (a + b)) / b)
- if diff < min_diff:
- min_diff = diff
- best_a, best_b = a, b
- return best_a, best_b
-
- def find_coeffs(X, lst):
- best_a, best_b = None, None
- min_diff = float('inf') # initialisation avec une valeur très grande pour la différence
- for a in lst:
- for b in lst:
- if a > b and a != 0: # on vérifie que A est plus grand que B et non nul
- diff = abs(X - (0.81 * (a + b)) / b)
- if diff < min_diff:
- min_diff = diff
- best_a, best_b = a, b
- if a < 50:
- a = 100 - a # on prend la valeur symétrique de A autour de 50
- else:
- a = 50 - (a - 50) # on prend la valeur symétrique de A autour de 50
- for b in lst:
- if a > b and a != 0: # on vérifie que A est plus grand que B et non nul
- diff = abs(X - (0.81 * (a + b)) / b)
- if diff < min_diff:
- min_diff = diff
- best_a, best_b = a, b
- return best_a, best_b
-
- def objective_function(params, x):
- A, B = params
- residuals = x - (0.81 * (A+B)) / B
- penalty = 100 * ((A - 50) ** 2) # pénalité quadratique si A est loin de 50
- return np.sum(residuals**2) + penalty
- def find_AB(x):
- initial_guess = [50, 1] # valeur initiale de A proche de 50, B=1
- result = minimize(objective_function, initial_guess, args=(x,))
- A, B = result.x
- return A, B
- # tension a trouvé
- x = 5
- coeffs = find_coeffs(x,e192)
- A=coeffs[0]
- B=coeffs[1]
- print("A =", A)
- print("B =", B)
- v = (0.81 * (A+B)) / B
- print("V =", v)
|