TrouveResistance.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import numpy as np
  2. from scipy.optimize import minimize
  3. x = 3.3
  4. #fonction python qui trouve les meilleur coef de A et de B dans la fonction x = (0.81 * (A+B)) / B.
  5. #ou A et proche de 50
  6. #ou x est un parametre d'entrée de la fonction
  7. #ou A et B doivent appartenir a une liste
  8. 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])
  9. def find_coeffs2(X, lst):
  10. best_a, best_b = None, None
  11. min_diff = float('inf') # initialisation avec une valeur très grande pour la différence
  12. for a in lst:
  13. for b in lst:
  14. if a != 0 and a != b: # on évite la division par zéro et la redondance des paires A, B
  15. diff = abs(X - (0.81 * (a + b)) / b)
  16. if diff < min_diff:
  17. min_diff = diff
  18. best_a, best_b = a, b
  19. return best_a, best_b
  20. def find_coeffs(X, lst):
  21. best_a, best_b = None, None
  22. min_diff = float('inf') # initialisation avec une valeur très grande pour la différence
  23. for a in lst:
  24. for b in lst:
  25. if a > b and a != 0: # on vérifie que A est plus grand que B et non nul
  26. diff = abs(X - (0.81 * (a + b)) / b)
  27. if diff < min_diff:
  28. min_diff = diff
  29. best_a, best_b = a, b
  30. if a < 50:
  31. a = 100 - a # on prend la valeur symétrique de A autour de 50
  32. else:
  33. a = 50 - (a - 50) # on prend la valeur symétrique de A autour de 50
  34. for b in lst:
  35. if a > b and a != 0: # on vérifie que A est plus grand que B et non nul
  36. diff = abs(X - (0.81 * (a + b)) / b)
  37. if diff < min_diff:
  38. min_diff = diff
  39. best_a, best_b = a, b
  40. return best_a, best_b
  41. def objective_function(params, x):
  42. A, B = params
  43. residuals = x - (0.81 * (A+B)) / B
  44. penalty = 100 * ((A - 50) ** 2) # pénalité quadratique si A est loin de 50
  45. return np.sum(residuals**2) + penalty
  46. def find_AB(x):
  47. initial_guess = [50, 1] # valeur initiale de A proche de 50, B=1
  48. result = minimize(objective_function, initial_guess, args=(x,))
  49. A, B = result.x
  50. return A, B
  51. # tension a trouvé
  52. x = 5
  53. coeffs = find_coeffs(x,e192)
  54. A=coeffs[0]
  55. B=coeffs[1]
  56. print("A =", A)
  57. print("B =", B)
  58. v = (0.81 * (A+B)) / B
  59. print("V =", v)