优先考虑非线性系统中的方程。

huangapple go评论127阅读模式
英文:

Prioritize equations in a nonlinear system

问题

我正在尝试使用最小二乘法解决非线性等式系统(EQS)的问题。我得到了一个结果,但不幸的是它并不非常准确。主要问题是方程29到32必须被满足,但它们并没有被满足。有没有一种方法可以在非线性系统中优先考虑方程?

以下是我的代码:

  1. # 乙醇和1-丙醇与3个塔板
  2. from scipy.optimize import least_squares
  3. import numpy as np
  4. def equations(x):
  5. # 你的方程式在这里
  6. # 界限
  7. # 特定变量的上界
  8. upper_bounds = np.ones(32) * np.inf
  9. # 你的上界在这里
  10. # 特定变量的下界
  11. bottom_bounds = np.zeros(32)
  12. # 你的下界在这里
  13. # 边界数组
  14. bounds = (bottom_bounds, upper_bounds)
  15. # 初始值
  16. x0 = np.ones(32)
  17. # 你的初始值在这里
  18. # 使用变量边界解决方程组
  19. res = least_squares(equations, x0, bounds=bounds, method='trf', ftol=1e-10, xtol=1e-10, gtol=1e-10)
  20. # 打印结果
  21. print(res.x)
  22. # 你的结果打印在这里

e0_x_Vint_tr1_c1e0_x_Vint_tr1_c2 的结果似乎不正确,因为它们的总和不等于1。您可能需要检查方程29和32以确保它们正确地表示了变量之间的关系。如果这些方程正确,但结果仍然不准确,您可能需要进一步调整初始值或尝试不同的优化方法。

英文:

I am trying to solve a nonlinear EQS using the least squares method. I get a result, but unfortunately it is not very accurate. The main problem is that the equations 29 to 32 must be fullfilled, but they are not. Is there a way to maybe prioritize equations in a nonlinear system?

Here is the my code:

  1. # Ethanol and 1_Propanol with 3 trays
  2. from scipy.optimize import least_squares
  3. import numpy as np
  4. def equations(x):
  5. eq1 = x[18] - x[16] * x[19] * x[11] * (x[21] / (x[19] * x[11] * x[13])) ** (2.0 / 3.0)
  6. eq2 = x[19] - ((732.0 * x[10]) + (747.0 * x[9]))
  7. eq3 = x[13] - ((1.25E-5 * x[10]) + (1.6E-6 * x[9]))
  8. eq4 = x[2] - ((((((-6021.54) + (0.055134232180918 * (x[29] - 298.0))) + 2.61275786690816) * x[10])
  9. + ((((-6883.827) + (0.058471271259418 * (x[29] - 298.0))) + 2.38446400137128) * x[9])))
  10. eq5 = x[11] - (((0.055134232180918 * (x[29])) + 2.61275786690816) * x[10] + ((0.058471271259418 * (x[29])) +
  11. 2.38446400137128) * x[9])
  12. eq6 = x[21] - ((((((0.001318 * (x[29])) + 0.16762) * x[10])
  13. + (((-0.001075424413 * (x[29])) + 0.155701011727667) * x[9]))))
  14. eq7 = x[24] - (1.746 * x[22] + 2.76 * x[20])
  15. eq8 = x[15] - (1.0E-5 * x[22] + 6.5E-6 * x[20])
  16. eq9 = x[12] - ((((((((-6021.54) + (0.015220202121093 * (x[26] - 298.0))) + 1.60395800121061) + 922.194)
  17. * x[22]) + ((((((-6883.827) + (0.01847750929725 * (x[26] - 298.0))) + 1.43843773750579)
  18. + 1067.523)) * x[20]))))
  19. eq10 = x[6] - ((((((((0.015220202121093 * (x[26])) + 1.60395800121061)
  20. * x[22]) + (((0.01847750929725 * (x[26])) + 1.43843773750579)
  21. * x[20]))))))
  22. eq11 = x[5] - ((((((((5.07392141746E-4 * (x[29])) + 0.015070468544221)
  23. * x[22]) + (((4.89248E-4 * (x[29])) + 0.014354384)
  24. * x[20]))))))
  25. eq12 = x[3] - (1.0 / (10.0 ** (5.24677 - 1598.673 / (x[28] + 46.424))))
  26. eq13 = x[0] - (1.0 / (10.0 ** (5.31384 - 1690.864 / (x[28] + 51.804))))
  27. eq14 = x[27] - (x[25] * x[24] * x[6] * ((x[5] / (x[24] * x[6] * x[15])) ** (2.0 / 3.0)))
  28. eq15 = 0.0 - ((2.0 * 28000.0) - (x[23] * x[2]) + (x[17] * 0.002))
  29. eq16 = 0.0 - ((0.2 * 30000.0) - (x[14] * x[12]) - (x[17] * 0.002))
  30. eq17 = 0.0 - ((2.0 * 0.5) - (x[23] * x[10]) + (x[8] * 0.002))
  31. eq18 = 0.0 - ((2.0 * 0.5) - (x[23] * x[9]) + (x[7] * 0.002))
  32. eq19 = 0.0 - ((0.2 * 0.6) - (x[14] * x[22]) - (x[8] * 0.002))
  33. eq20 = 0.0 - ((0.2 * 0.4) - (x[14] * x[20]) - (x[7] * 0.002))
  34. eq21 = 0.0 - (x[31] - (x[3] * x[4]))
  35. eq22 = 0.0 - (x[30] - (x[0] * x[1]))
  36. eq23 = x[17] - (x[18] * (x[28] - x[29]) + (x[8] * (((-6021.54 + (0.055134232180918 * (x[29] - 298.0))) +
  37. 2.61275786690816) + (x[7] * (
  38. (-6883.827 + (0.058471271259418 * (x[29] - 298.0))) + 2.38446400137128)))))
  39. eq24 = x[17] - (x[27] * (x[26] - x[28]) + (x[8] * ((((-6021.54 + (0.015220202121093 * (x[26] - 298.0))) +
  40. 1.60395800121061) + 922.194) + (x[7] * (
  41. ((-6883.827 + (0.01847750929725 * (x[26] - 298.0))) + 1.43843773750579) + 1067.523)))))
  42. eq25 = x[8] - (x[19] * x[16] * (x[4] - x[10]) + x[10] * (x[8] + x[7]))
  43. eq26 = x[7] - (x[19] * x[16] * (x[1] - x[9]) + x[9] * (x[8] + x[7]))
  44. eq27 = x[8] - (x[24] * x[25] * (x[22] - x[31]) + x[22] * (x[8] + x[7]))
  45. eq28 = x[7] - (x[24] * x[25] * (x[20] - x[30]) + x[20] * (x[8] + x[7]))
  46. eq29 = 1.0 - (x[10] + x[9])
  47. eq30 = 1.0 - (x[4] + x[1])
  48. eq31 = 1.0 - (x[22] + x[20])
  49. eq32 = 1.0 - (x[31] + x[30])
  50. return [eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10, eq11, eq12, eq13, eq14, eq15, eq16,
  51. eq17, eq18, eq19, eq20, eq21, eq22, eq23, eq24, eq25, eq26, eq27, eq28, eq29, eq30,
  52. eq31, eq32]
  53. # Boundaries
  54. # Upper bounds for specific variables
  55. upper_bounds = np.ones(32) * np.inf
  56. upper_bounds[1] = 1
  57. upper_bounds[4] = 1
  58. upper_bounds[9] = 1
  59. upper_bounds[10] = 1
  60. upper_bounds[20] = 1
  61. upper_bounds[22] = 1
  62. upper_bounds[30] = 1
  63. upper_bounds[31] = 1
  64. # Upper bounds for specific variables
  65. bottom_bounds = np.zeros(32)
  66. bottom_bounds[26] = 340
  67. bottom_bounds[28] = 340
  68. bottom_bounds[29] = 340
  69. # Bounds array
  70. bounds = (bottom_bounds, upper_bounds)
  71. # bounds = (0, np.inf)
  72. # Initial values
  73. x0 = np.ones(32)
  74. x0[1] = 0.5
  75. x0[4] = 0.5
  76. x0[9] = 0.5
  77. x0[10] = 0.5
  78. x0[20] = 0.5
  79. x0[22] = 0.5
  80. x0[30] = 0.5
  81. x0[31] = 0.5
  82. x0[26] = 370
  83. x0[28] = 370
  84. x0[29] = 370
  85. # x0 = np.array([1, 1e-4, 1, 1, 1, 1, 1e-4, 1, 1, 1, 1, 1, 1, 1, 1, 1])
  86. # Lösung des Gleichungssystems mit Variablengrenzen
  87. res = least_squares(equations, x0, bounds=bounds, method='trf', ftol=1e-10, xtol=1e-10, gtol=1e-10)
  88. print(res.x)
  89. print('e0_K_tr1_c2 {0}'.format(res.x[0]))
  90. print('e0_x_Lint_tr1_c2 {0}'.format(res.x[1]))
  91. print('e0_h_L_tr1 {0}'.format(res.x[2]))
  92. print('e0_K_tr1_c1 {0}'.format(res.x[3]))
  93. print('e0_x_Lint_tr1_c1 {0}'.format(res.x[4]))
  94. print('e0_greek_lambda_V_tr1 {0}'.format(res.x[5]))
  95. print('e0_cp_V_tr1 {0}'.format(res.x[6]))
  96. print('e0_f_int_tr1_c2 {0}'.format(res.x[7]))
  97. print('e0_f_int_tr1_c1 {0}'.format(res.x[8]))
  98. print('e0_x_L_tr1_c2 {0}'.format(res.x[9]))
  99. print('e0_x_L_tr1_c1 {0}'.format(res.x[10]))
  100. print('e0_cp_L_tr1 {0}'.format(res.x[11]))
  101. print('e0_h_V_tr1 {0}'.format(res.x[12]))
  102. print('e0_D_L_tr1 {0}'.format(res.x[13]))
  103. print('e0_F_V_tr1 {0}'.format(res.x[14]))
  104. print('e0_D_V_tr1 {0}'.format(res.x[15]))
  105. print('e0_greek_beta_L_tr1 {0}'.format(res.x[16]))
  106. print('e0_e_int_tr1 {0}'.format(res.x[17]))
  107. print('e0_greek_alpha_L_tr1 {0}'.format(res.x[18]))
  108. print('e0_greek_rho_L_tr1 {0}'.format(res.x[19]))
  109. print('e0_x_V_tr1_c2 {0}'.format(res.x[20]))
  110. print('e0_greek_lambda_L_tr1 {0}'.format(res.x[21]))
  111. print('e0_x_V_tr1_c1 {0}'.format(res.x[22]))
  112. print('e0_F_L_tr1 {0}'.format(res.x[23]))
  113. print('e0_greek_rho_V_tr1 {0}'.format(res.x[24]))
  114. print('e0_greek_beta_V_tr1 {0}'.format(res.x[25]))
  115. print('e0_T_V_tr1 {0}'.format(res.x[26]))
  116. print('e0_greek_alpha_V_tr1 {0}'.format(res.x[27]))
  117. print('e0_T_int_tr1 {0}'.format(res.x[28]))
  118. print('e0_T_L_tr1 {0}'.format(res.x[29]))
  119. print('e0_x_Vint_tr1_c2 {0}'.format(res.x[30]))
  120. print('e0_x_Vint_tr1_c1 {0}'.format(res.x[31]))
  121. print('Zusammensetztungen:')
  122. print('e0_x_Vint_tr1_c1 {0}'.format(res.x[31]))
  123. print('e0_x_Vint_tr1_c2 {0}'.format(res.x[30]))
  124. print('e0_x_V_tr1_c1 {0}'.format(res.x[22]))
  125. print('e0_x_V_tr1_c2 {0}'.format(res.x[20]))
  126. print('e0_x_Lint_tr1_c1 {0}'.format(res.x[4]))
  127. print('e0_x_Lint_tr1_c2 {0}'.format(res.x[1]))
  128. print('e0_x_L_tr1_c1 {0}'.format(res.x[10]))
  129. print('e0_x_L_tr1_c2 {0}'.format(res.x[9]))

and the results for x[31], x[30], x[22],... are:

  1. Zusammensetztungen:
  2. e0_x_Vint_tr1_c1 0.5189962572539993
  3. e0_x_Vint_tr1_c2 0.24082903753005624
  4. e0_x_V_tr1_c1 2.3066615630447933e-32
  5. e0_x_V_tr1_c2 1.246796204196686e-33
  6. e0_x_Lint_tr1_c1 0.9999999999999999
  7. e0_x_Lint_tr1_c2 0.01258253962013115
  8. e0_x_L_tr1_c1 7.013309435002394e-33
  9. e0_x_L_tr1_c2 3.0913699644636455e-33

but e0_x_Vint_tr1_c1 + e0_x_Vint_tr1_c2 should be 1, but it's not.

答案1

得分: 1

去掉多余的括号并重新格式化你的方程,以便更容易调试。

用命名变量替换你的索引变量。我通过NamedTuple展示了一种快速的方法。

不幸的是,尽管方法lm似乎可以改善你的结果 - 它声称找到的解决方案的成本比trf低约10倍(30.7比399) - 但它不能使用,因为它不遵守你的边界。没有一个神奇的方法可以提高准确性。了解领域问题是必要的。如果某些方程“比其他方程更重要”,你可以简单地按照它们的权重来缩放这些方程。

添加回归测试。我展示的结果中,方程与你的原始方程等效。

始终检查result.success

第一次尝试可以如下所示:

  1. """乙醇和1-丙醇,3个托盘"""
  2. from pprint import pprint
  3. from typing import NamedTuple
  4. from numpy.random import default_rng
  5. from scipy.optimize import least_squares
  6. import numpy as np
  7. class Vars(NamedTuple):
  8. e0_K_tr1_c2: float # 0
  9. e0_x_Lint_tr1_c2: float # 1
  10. e0_h_L_tr1: float # 2
  11. e0_K_tr1_c1: float # 3
  12. e0_x_Lint_tr1_c1: float # 4
  13. e0_lambda_V_tr1: float # 5
  14. e0_cp_V_tr1: float # 6
  15. e0_f_int_tr1_c2: float # 7
  16. e0_f_int_tr1_c1: float # 8
  17. e0_x_L_tr1_c2: float # 9
  18. e0_x_L_tr1_c1: float # 10
  19. e0_cp_L_tr1: float # 11
  20. e0_h_V_tr1: float # 12
  21. e0_D_L_tr1: float # 13
  22. e0_F_V_tr1: float # 14
  23. e0_D_V_tr1: float # 15
  24. e0_beta_L_tr1: float # 16
  25. e0_e_int_tr1: float # 17
  26. e0_alpha_L_tr1: float # 18
  27. e0_rho_L_tr1: float # 19
  28. e0_x_V_tr1_c2: float # 20
  29. e0_lambda_L_tr1: float # 21
  30. e0_x_V_tr1_c1: float # 22
  31. e0_F_L_tr1: float # 23
  32. e0_rho_V_tr1: float # 24
  33. e0_beta_V_tr1: float # 25
  34. e0_T_V_tr1: float # 26
  35. e0_alpha_V_tr1: float # 27
  36. e0_T_int_tr1: float # 28
  37. e0_T_L_tr1: float # 29
  38. e0_x_Vint_tr1_c2: float # 30
  39. e0_x_Vint_tr1_c1: float # 31
  40. def equations(x: np.ndarray) -> tuple[float, ...]:
  41. v = Vars(*x)
  42. eq1 = v.e0_alpha_L_tr1 - v.e0_beta_L_tr1 * v.e0_rho_L_tr1 * v.e0_cp_L_tr1 * (
  43. v.e0_lambda_L_tr1 / (v.e0_rho_L_tr1 * v.e0_cp_L_tr1 * v.e0_D_L_tr1)
  44. ) ** (2 / 3)
  45. eq2 = v.e0_rho_L_tr1 - (732 * v.e0_x_L_tr1_c1 + 747 * v.e0_x_L_tr1_c2)
  46. eq3 = v.e0_D_L_tr1 - (1.25E-5 * v.e0_x_L_tr1_c1 + 1.6E-6 * v.e0_x_L_tr1_c2)
  47. eq4 = v.e0_h_L_tr1 - (
  48. (
  49. -6021.54
  50. + 0.055134232180918 * (v.e0_T_L_tr1 - 298)
  51. + 2.61275786690816
  52. ) * v.e0_x_L_tr1_c1
  53. + (
  54. -6883.827
  55. + 0.058471271259418 * (v.e0_T_L_tr1 - 298)
  56. + 2.38446400137128
  57. ) * v.e0_x_L_tr1_c2
  58. )
  59. eq5 = v.e0_cp_L_tr1 - (
  60. (
  61. 0.055134232180918 * v.e0_T_L_tr1 + 2.61275786690816
  62. ) * v.e0_x_L_tr1_c1
  63. + (
  64. 0.058471271259418 * v.e0_T_L_tr1 + 2.38446400137128
  65. ) * v.e0_x_L_tr1_c2
  66. )
  67. eq6 = v.e0_lambda_L_tr1 - (
  68. (
  69. 0.001318 * v.e0_T_L_tr1 + 0.16762
  70. ) * v.e0_x_L_tr1_c1
  71. + (
  72. -0.001075424413 * v.e0_T_L_tr1 + 0.155701011727667
  73. ) * v.e0_x_L_tr1_c2
  74. )
  75. eq7 = v.e0_rho_V_tr1 - (1.746 * v.e0_x_V_tr1_c1 + 2.76 * v.e0_x_V_tr1_c2)
  76. eq8 = v.e0_D_V_tr1 - (1E-5 * v.e0_x_V_tr1_c1 + 6.5E-6 * v.e0_x_V_tr1_c2)
  77. eq9 = v.e0_h_V_tr1 - (
  78. (
  79. -6021.54
  80. + 0.015220202121093 * (v.e0_T_V_tr1 - 298)
  81. + 1.60395800121061
  82. + 922.194
  83. ) * v.e0_x_V_tr1_c1
  84. + (
  85. -6883.827
  86. + 0.01847750929725 * (v.e0_T_V_tr1 - 298)
  87. + 1.43843773750579
  88. + 1067.523
  89. ) * v.e0_x_V_tr1_c2
  90. )
  91. eq10 = v.e0_cp
  92. <details>
  93. <summary>英文:</summary>
  94. Remove your redundant parentheses and reformat your equations so that they can be more easily debugged.
  95. Replace your indexed variables with named variables. I show a quick way to do this via `NamedTuple`.
  96. Unfortunately, whereas method `lm` may seem to improve your results - it claims to find a solution with ~ 10 times less cost (30.7) than that from `trf` (399) - it&#39;s not usable because it doesn&#39;t respect your bounds. There is no magic bullet to improve the accuracy. Knowledge of the domain problem is necessary. If some equations &quot;matter more&quot; to the solution than others, you can simply scale those equations by their weights.
  97. Add regression tests. For the results that I show, the equations are equivalent to your original equations.
  98. Always check `result.success`.
  99. A first pass can look like
  100. ```python
  101. &quot;&quot;&quot;Ethanol and 1_Propanol with 3 trays&quot;&quot;&quot;
  102. from pprint import pprint
  103. from typing import NamedTuple
  104. from numpy.random import default_rng
  105. from scipy.optimize import least_squares
  106. import numpy as np
  107. class Vars(NamedTuple):
  108. e0_K_tr1_c2: float # 0
  109. e0_x_Lint_tr1_c2: float # 1
  110. e0_h_L_tr1: float # 2
  111. e0_K_tr1_c1: float # 3
  112. e0_x_Lint_tr1_c1: float # 4
  113. e0_lambda_V_tr1: float # 5
  114. e0_cp_V_tr1: float # 6
  115. e0_f_int_tr1_c2: float # 7
  116. e0_f_int_tr1_c1: float # 8
  117. e0_x_L_tr1_c2: float # 9
  118. e0_x_L_tr1_c1: float # 10
  119. e0_cp_L_tr1: float # 11
  120. e0_h_V_tr1: float # 12
  121. e0_D_L_tr1: float # 13
  122. e0_F_V_tr1: float # 14
  123. e0_D_V_tr1: float # 15
  124. e0_beta_L_tr1: float # 16
  125. e0_e_int_tr1: float # 17
  126. e0_alpha_L_tr1: float # 18
  127. e0_rho_L_tr1: float # 19
  128. e0_x_V_tr1_c2: float # 20
  129. e0_lambda_L_tr1: float # 21
  130. e0_x_V_tr1_c1: float # 22
  131. e0_F_L_tr1: float # 23
  132. e0_rho_V_tr1: float # 24
  133. e0_beta_V_tr1: float # 25
  134. e0_T_V_tr1: float # 26
  135. e0_alpha_V_tr1: float # 27
  136. e0_T_int_tr1: float # 28
  137. e0_T_L_tr1: float # 29
  138. e0_x_Vint_tr1_c2: float # 30
  139. e0_x_Vint_tr1_c1: float # 31
  140. def equations(x: np.ndarray) -&gt; tuple[float, ...]:
  141. v = Vars(*x)
  142. eq1 = v.e0_alpha_L_tr1 - v.e0_beta_L_tr1 * v.e0_rho_L_tr1 * v.e0_cp_L_tr1 * (
  143. v.e0_lambda_L_tr1 / (v.e0_rho_L_tr1 * v.e0_cp_L_tr1 * v.e0_D_L_tr1)
  144. ) ** (2 / 3)
  145. eq2 = v.e0_rho_L_tr1 - (732 * v.e0_x_L_tr1_c1 + 747 * v.e0_x_L_tr1_c2)
  146. eq3 = v.e0_D_L_tr1 - (1.25E-5 * v.e0_x_L_tr1_c1 + 1.6E-6 * v.e0_x_L_tr1_c2)
  147. eq4 = v.e0_h_L_tr1 - (
  148. (
  149. -6021.54
  150. + 0.055134232180918 * (v.e0_T_L_tr1 - 298)
  151. + 2.61275786690816
  152. ) * v.e0_x_L_tr1_c1
  153. + (
  154. -6883.827
  155. + 0.058471271259418 * (v.e0_T_L_tr1 - 298)
  156. + 2.38446400137128
  157. ) * v.e0_x_L_tr1_c2
  158. )
  159. eq5 = v.e0_cp_L_tr1 - (
  160. (
  161. 0.055134232180918 * v.e0_T_L_tr1 + 2.61275786690816
  162. ) * v.e0_x_L_tr1_c1
  163. + (
  164. 0.058471271259418 * v.e0_T_L_tr1 + 2.38446400137128
  165. ) * v.e0_x_L_tr1_c2
  166. )
  167. eq6 = v.e0_lambda_L_tr1 - (
  168. (
  169. 0.001318 * v.e0_T_L_tr1 + 0.16762
  170. ) * v.e0_x_L_tr1_c1
  171. + (
  172. -0.001075424413 * v.e0_T_L_tr1 + 0.155701011727667
  173. ) * v.e0_x_L_tr1_c2
  174. )
  175. eq7 = v.e0_rho_V_tr1 - (1.746 * v.e0_x_V_tr1_c1 + 2.76 * v.e0_x_V_tr1_c2)
  176. eq8 = v.e0_D_V_tr1 - (1E-5 * v.e0_x_V_tr1_c1 + 6.5E-6 * v.e0_x_V_tr1_c2)
  177. eq9 = v.e0_h_V_tr1 - (
  178. (
  179. -6021.54
  180. + 0.015220202121093 * (v.e0_T_V_tr1 - 298)
  181. + 1.60395800121061
  182. + 922.194
  183. ) * v.e0_x_V_tr1_c1
  184. + (
  185. -6883.827
  186. + 0.01847750929725 * (v.e0_T_V_tr1 - 298)
  187. + 1.43843773750579
  188. + 1067.523
  189. ) * v.e0_x_V_tr1_c2
  190. )
  191. eq10 = v.e0_cp_V_tr1 - (
  192. (
  193. 0.015220202121093 * v.e0_T_V_tr1 + 1.60395800121061
  194. ) * v.e0_x_V_tr1_c1
  195. + (
  196. 0.01847750929725 * v.e0_T_V_tr1 + 1.43843773750579
  197. ) * v.e0_x_V_tr1_c2
  198. )
  199. eq11 = v.e0_lambda_V_tr1 - (
  200. (
  201. 5.07392141746E-4 * v.e0_T_L_tr1 + 0.015070468544221
  202. ) * v.e0_x_V_tr1_c1
  203. + (
  204. 4.89248E-4 * v.e0_T_L_tr1 + 0.014354384
  205. ) * v.e0_x_V_tr1_c2
  206. )
  207. eq12 = v.e0_K_tr1_c1 - 1 / 10**(5.24677 - 1598.673 / (v.e0_T_int_tr1 + 46.424))
  208. eq13 = v.e0_K_tr1_c2 - 1 / 10**(5.31384 - 1690.864 / (v.e0_T_int_tr1 + 51.804))
  209. eq14 = v.e0_alpha_V_tr1 - (
  210. v.e0_beta_V_tr1 * v.e0_rho_V_tr1 * v.e0_cp_V_tr1 * (
  211. v.e0_lambda_V_tr1 / (
  212. v.e0_rho_V_tr1 * v.e0_cp_V_tr1 * v.e0_D_V_tr1
  213. )
  214. ) ** (2 / 3)
  215. )
  216. eq15 = -(2 * 28000 - v.e0_F_L_tr1 * v.e0_h_L_tr1 + v.e0_e_int_tr1 * 0.002)
  217. eq16 = -(0.2 * 30000 - v.e0_F_V_tr1 * v.e0_h_V_tr1 - v.e0_e_int_tr1 * 0.002)
  218. eq17 = -(2 * 0.5 - v.e0_F_L_tr1 * v.e0_x_L_tr1_c1 + v.e0_f_int_tr1_c1 * 0.002)
  219. eq18 = -(2 * 0.5 - v.e0_F_L_tr1 * v.e0_x_L_tr1_c2 + v.e0_f_int_tr1_c2 * 0.002)
  220. eq19 = -(0.2 * 0.6 - v.e0_F_V_tr1 * v.e0_x_V_tr1_c1 - v.e0_f_int_tr1_c1 * 0.002)
  221. eq20 = -(0.2 * 0.4 - v.e0_F_V_tr1 * v.e0_x_V_tr1_c2 - v.e0_f_int_tr1_c2 * 0.002)
  222. eq21 = -(v.e0_x_Vint_tr1_c1 - v.e0_K_tr1_c1 * v.e0_x_Lint_tr1_c1)
  223. eq22 = -(v.e0_x_Vint_tr1_c2 - v.e0_K_tr1_c2 * v.e0_x_Lint_tr1_c2)
  224. eq23 = v.e0_e_int_tr1 - (
  225. v.e0_alpha_L_tr1 * (v.e0_T_int_tr1 - v.e0_T_L_tr1)
  226. + v.e0_f_int_tr1_c1 * (
  227. -6021.54
  228. + 0.055134232180918 * (v.e0_T_L_tr1 - 298)
  229. + 2.61275786690816
  230. # really?
  231. # )
  232. + v.e0_f_int_tr1_c2 * (
  233. -6883.827 +
  234. 0.058471271259418 * (v.e0_T_L_tr1 - 298)
  235. + 2.38446400137128
  236. )
  237. )
  238. )
  239. eq24 = v.e0_e_int_tr1 - (
  240. v.e0_alpha_V_tr1 * (v.e0_T_V_tr1 - v.e0_T_int_tr1)
  241. + v.e0_f_int_tr1_c1 * (
  242. -6021.54
  243. + 0.015220202121093 * (v.e0_T_V_tr1 - 298)
  244. + 1.60395800121061
  245. + 922.194
  246. # really?
  247. # )
  248. + v.e0_f_int_tr1_c2 * (
  249. -6883.827
  250. + 0.01847750929725 * (v.e0_T_V_tr1 - 298)
  251. + 1.43843773750579
  252. + 1067.523
  253. )
  254. )
  255. )
  256. eq25 = v.e0_f_int_tr1_c1 - (
  257. v.e0_rho_L_tr1 * v.e0_beta_L_tr1 * (
  258. v.e0_x_Lint_tr1_c1 - v.e0_x_L_tr1_c1
  259. )
  260. + v.e0_x_L_tr1_c1 * (v.e0_f_int_tr1_c1 + v.e0_f_int_tr1_c2)
  261. )
  262. eq26 = v.e0_f_int_tr1_c2 - (
  263. v.e0_rho_L_tr1 * v.e0_beta_L_tr1 * (
  264. v.e0_x_Lint_tr1_c2 - v.e0_x_L_tr1_c2
  265. )
  266. + v.e0_x_L_tr1_c2 * (v.e0_f_int_tr1_c1 + v.e0_f_int_tr1_c2)
  267. )
  268. eq27 = v.e0_f_int_tr1_c1 - (
  269. v.e0_rho_V_tr1 * v.e0_beta_V_tr1 * (
  270. v.e0_x_V_tr1_c1 - v.e0_x_Vint_tr1_c1
  271. )
  272. + v.e0_x_V_tr1_c1 * (v.e0_f_int_tr1_c1 + v.e0_f_int_tr1_c2)
  273. )
  274. eq28 = v.e0_f_int_tr1_c2 - (
  275. v.e0_rho_V_tr1 * v.e0_beta_V_tr1 * (
  276. v.e0_x_V_tr1_c2 - v.e0_x_Vint_tr1_c2
  277. )
  278. + v.e0_x_V_tr1_c2 * (v.e0_f_int_tr1_c1 + v.e0_f_int_tr1_c2)
  279. )
  280. eq29 = 1 - (v.e0_x_L_tr1_c1 + v.e0_x_L_tr1_c2)
  281. eq30 = 1 - (v.e0_x_Lint_tr1_c1 + v.e0_x_Lint_tr1_c2)
  282. eq31 = 1 - (v.e0_x_V_tr1_c1 + v.e0_x_V_tr1_c2)
  283. eq32 = 1 - (v.e0_x_Vint_tr1_c1 + v.e0_x_Vint_tr1_c2)
  284. return (eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10,
  285. eq11, eq12, eq13, eq14, eq15, eq16, eq17, eq18, eq19, eq20,
  286. eq21, eq22, eq23, eq24, eq25, eq26, eq27, eq28, eq29, eq30,
  287. eq31, eq32)
  288. def regression_test(x0: np.ndarray) -&gt; None:
  289. y = equations(x0)
  290. assert np.allclose(
  291. y,
  292. [ 0.00000000e+00, -7.38500000e+02, 9.99992950e-01, 6.44709509e+03,
  293. -2.25156291e+01, 7.93463011e-01, -1.25300000e+00, 9.99991750e-01,
  294. 5.45609068e+03, -6.75527448e+00, 8.00909148e-01, 9.60890608e-01,
  295. 9.50476815e-01, 0.00000000e+00, -5.59990020e+04, -5.99899800e+03,
  296. -5.02000000e-01, -5.02000000e-01, 3.82000000e-01, 4.22000000e-01,
  297. 0.00000000e+00, 0.00000000e+00, 1.28931902e+04, 1.09111814e+04,
  298. 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
  299. 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
  300. ],
  301. rtol=0, atol=1e-4,
  302. )
  303. rand = default_rng(seed=0)
  304. x = rand.uniform(low=0, high=2, size=32)
  305. y = equations(x)
  306. assert np.allclose(
  307. y,
  308. [-8.89664807e-02, -2.59056243e+03, 6.71477620e-02, 2.27496385e+04,
  309. -8.97636119e+00, -3.16304973e-01, -1.26737482e+00, 3.51297461e-01,
  310. 7.17475798e+03, -1.06230892e+00, 1.80356351e+00, -1.59115277e+28,
  311. -3.04937193e+26, -6.69625188e-01, -5.59998961e+04, -5.99749540e+03,
  312. 1.10987295e+00, 1.41775827e+00, 1.83947353e+00, 5.57239541e-03,
  313. -7.24077112e-01, -6.89518259e-01, 1.75063897e+04, 1.47800191e+04,
  314. -3.05993271e+00, -1.36098936e+00, -2.85999752e+00, 2.56167351e+00,
  315. -2.50185196e+00, -1.16611391e+00, -3.97888172e-01, -1.15473631e+00,
  316. ],
  317. rtol=1e-8, atol=0,
  318. )
  319. def make_x0() -&gt; np.ndarray:
  320. x0 = np.ones(32)
  321. x0[[1,4,9,10,20,22,30,31]] = 0.5
  322. x0[[26,28,29]] = 370
  323. return x0
  324. def solve(x0: np.ndarray) -&gt; Vars:
  325. upper_bounds = np.full(shape=32, fill_value=np.inf)
  326. upper_bounds[[1,4,9,10,20,22,30,31]] = 1
  327. bottom_bounds = np.zeros(32)
  328. bottom_bounds[[26,28,29]] = 340
  329. res = least_squares(
  330. fun=equations, x0=x0,
  331. bounds=(bottom_bounds, upper_bounds), # not supported in lm
  332. method=&#39;trf&#39;,
  333. ftol=1e-10, xtol=1e-10, gtol=1e-10,
  334. )
  335. assert res.success
  336. return Vars(*res.x)
  337. def dump(x: Vars) -&gt; None:
  338. pprint(x._asdict())
  339. def main() -&gt; None:
  340. x0 = make_x0()
  341. regression_test(x0)
  342. x = solve(x0)
  343. dump(x)
  344. if __name__ == &#39;__main__&#39;:
  345. main()
  1. {&#39;e0_D_L_tr1&#39;: 0.9984442691049119,
  2. &#39;e0_D_V_tr1&#39;: 0.9986041251126907,
  3. &#39;e0_F_L_tr1&#39;: 128.59184097299763,
  4. &#39;e0_F_V_tr1&#39;: 194.05957359631154,
  5. &#39;e0_K_tr1_c1&#39;: 0.9994676910341592,
  6. &#39;e0_K_tr1_c2&#39;: 0.999729237718534,
  7. &#39;e0_T_L_tr1&#39;: 340.0,
  8. &#39;e0_T_V_tr1&#39;: 559.936902192616,
  9. &#39;e0_T_int_tr1&#39;: 369.2496832286959,
  10. &#39;e0_alpha_L_tr1&#39;: 7.5810026415896,
  11. &#39;e0_alpha_V_tr1&#39;: 30.124493018891847,
  12. &#39;e0_beta_L_tr1&#39;: 0.0,
  13. &#39;e0_beta_V_tr1&#39;: 1.2479650000565898,
  14. &#39;e0_cp_L_tr1&#39;: 5.468269302698259,
  15. &#39;e0_cp_V_tr1&#39;: 1.0196495285046383,
  16. &#39;e0_e_int_tr1&#39;: 1856.5186325404206,
  17. &#39;e0_f_int_tr1_c1&#39;: 0.11670635205679133,
  18. &#39;e0_f_int_tr1_c2&#39;: 0.39727951871442857,
  19. &#39;e0_h_L_tr1&#39;: 190.08447743399068,
  20. &#39;e0_h_V_tr1&#39;: 22.650731758245296,
  21. &#39;e0_lambda_L_tr1&#39;: 1.0083185179925418,
  22. &#39;e0_lambda_V_tr1&#39;: 1.0368032854477796,
  23. &#39;e0_rho_L_tr1&#39;: 1.0430730156966466,
  24. &#39;e0_rho_V_tr1&#39;: 0.9986707757704435,
  25. &#39;e0_x_L_tr1_c1&#39;: 0.0,
  26. &#39;e0_x_L_tr1_c2&#39;: 0.0,
  27. &#39;e0_x_Lint_tr1_c1&#39;: 0.5001045740608061,
  28. &#39;e0_x_Lint_tr1_c2&#39;: 0.49992448934879186,
  29. &#39;e0_x_V_tr1_c1&#39;: 1.4663484981775705e-16,
  30. &#39;e0_x_V_tr1_c2&#39;: 0.00041819098423356144,
  31. &#39;e0_x_Vint_tr1_c1&#39;: 0.4998465855173379,
  32. &#39;e0_x_Vint_tr1_c2&#39;: 0.4995202978525341}

huangapple
  • 本文由 发表于 2023年7月27日 21:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定