在Python中热交换模拟中产生错误数字的代码。

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

Code producing incorrect numbers in Heat Exchange Simulation in Python

问题

我正在编写一个热交换模拟程序。应该产生的数值为T_hot_out = 65,T_cold_out = 35,A = 22.26。

以下是我一直在使用的代码。

  1. # 问题 4
  2. import numpy as np
  3. # 从文本中获取(原始设计假设)
  4. T_cold_out0 = 35
  5. T_hot_out0 = 65
  6. # 从表 1 中获取
  7. T_hot_in = 160.0
  8. T_cold_in = 20.0
  9. Cp_hot = 5.2
  10. Cp_cold = 2.6
  11. w_hot = 1.0
  12. k = 0.05
  13. l = 0.002
  14. # 从问题 2 中获取
  15. w_cold = 5.555555
  16. A_design = 22.26
  17. def integrate(f, x0, x1, N):
  18. h = (x1 - x0) / (N-1)
  19. xvalues = np.linspace(x0,x1,N)
  20. yvalues = f(xvalues)
  21. F = np.zeros(N)
  22. for i in range (1,N):
  23. F[i] = h/2 * (yvalues[i-1]+yvalues[i])+F[i-1]
  24. return xvalues,F
  25. def bisection(f, y, x0, x1, eps, max_iter=1000):
  26. x1_n = x1
  27. x0_n = x0
  28. T = (x1+x0) / 2.0
  29. f_T = f(T)
  30. for i in range(max_iter):
  31. T_n = (x0_n+x1_n)/2
  32. f_T_n = f(T_n)
  33. error = f_T_n - y
  34. if abs(error) <= eps:
  35. break
  36. elif error > 0:
  37. x0_n = T_n
  38. x1_n = x1_n
  39. elif error < 0:
  40. x0_n = x0_n
  41. x1_n = T_n
  42. return T_n, error, (abs(error)<=eps)
  43. def h_hot(T_n):
  44. return 0.9*(1+0.015*(T_n-70))
  45. def h_cold(T_n):
  46. return 3.0*(1+0.02*(T_n-40))
  47. def h_overall(T_cold, T_hot):
  48. return 1/((h_hot(T_hot))**-1 + (h_cold(T_cold))**-1 +(k/l)**-1)
  49. # 方程(2)使用 h_hot() 和 h_cold()
  50. def T_hot(T_cold, T_hot_out):
  51. return T_hot_out + ((Cp_cold*w_cold)/(Cp_hot*w_hot))*(T_cold - T_cold_in) # 来自问题 3
  52. def T_cold_out(T_hot_out):
  53. return T_cold_in + (Cp_hot*w_hot)/(Cp_cold*w_cold)*(T_hot_in-T_hot_out) # 类似于 T_hot()
  54. def A(T_hot_out):
  55. def f2(T_cold):
  56. return ((Cp_cold*w_cold)/h_overall(T_cold, T_hot(T_cold, T_hot_out)))*(1/(T_hot(T_cold, T_hot_out)-T_cold)) # 来自方程(9)
  57. T_cold, area = integrate(f2,T_cold_in,T_cold_out(T_hot_out), 1000)
  58. return area[-1]
  59. T_hot_out, err, success = bisection(A, A_design, T_cold_in, T_hot_in, 0.001, max_iter=1000)
  60. if not success:
  61. print("二分法失败!")
  62. print("T_hot_out = {:.2f} (而不是{:.2f})".format(T_hot_out, T_hot_out0))
  63. print("T_cold_out = {:.2f} (而不是{:.2f})".format(T_cold_out(T_hot_out), T_cold_out0))
  64. print("A = {:.2f} (误差 = {:.4e})".format(A(T_hot_out), A(T_hot_out) - A_design))

希望这可以帮助你解决代码中的问题。

英文:

I am coding a heat exchange simulation. The values that should be producced are T_hot_out = 65, T_cold_out = 35, and A = 22.26.

The following is the code I've been using. It prints T_hot_out = 36.9 and T_cold_out = 64.32. No error is raised, so I'm confused on where the computational error is coming from and how to fix it.

  1. #problem 4
  2. import numpy as np
  3. # from text (original design assumptions)
  4. T_cold_out0 =35
  5. T_hot_out0 = 65
  6. # from Table 1
  7. T_hot_in = 160.0
  8. T_cold_in = 20.0
  9. Cp_hot = 5.2
  10. Cp_cold = 2.6
  11. w_hot = 1.0
  12. k = 0.05
  13. l = 0.002
  14. # from problem 2
  15. w_cold = 5.555555
  16. A_design = 22.26
  17. def integrate(f, x0, x1, N):
  18. h = (x1 - x0) / (N-1)
  19. xvalues = np.linspace(x0,x1,N)
  20. yvalues = f(xvalues)
  21. F = np.zeros(N)
  22. for i in range (1,N):
  23. F[i] = h/2 * (yvalues[i-1]+yvalues[i])+F[i-1]
  24. return xvalues,F
  25. def bisection(f, y, x0, x1, eps, max_iter=1000):
  26. x1_n = x1
  27. x0_n = x0
  28. T = (x1+x0) / 2.0
  29. f_T = f(T)
  30. for i in range(max_iter):
  31. T_n = (x0_n+x1_n)/2
  32. f_T_n = f(T_n)
  33. error = f_T_n - y
  34. if abs(error) <= eps:
  35. break
  36. elif error > 0:
  37. x0_n = T_n
  38. x1_n = x1_n
  39. elif error < 0:
  40. x0_n = x0_n
  41. x1_n = T_n
  42. return T_n, error, (abs(error)<=eps)
  43. def h_hot(T_n):
  44. return 0.9*(1+0.015*(T_n-70))
  45. def h_cold(T_n):
  46. return 3.0*(1+0.02*(T_n-40))
  47. def h_overall(T_cold, T_hot):
  48. return 1/((h_hot(T_hot))**-1 + (h_cold(T_cold))**-1 +(k/l)**-1)
  49. # equation (2) using h_hot() and h_cold()
  50. def T_hot(T_cold, T_hot_out):
  51. return T_hot_out + ((Cp_cold*w_cold)/(Cp_hot*w_hot))*(T_cold - T_cold_in) # from problem 3
  52. def T_cold_out(T_hot_out):
  53. return T_cold_in + (Cp_hot*w_hot)/(Cp_cold*w_cold)*(T_hot_in-T_hot_out) # similar to T_hot()
  54. def A(T_hot_out):
  55. def f2(T_cold):
  56. return ((Cp_cold*w_cold)/h_overall(T_cold, T_hot(T_cold, T_hot_out)))*(1/(T_hot(T_cold, T_hot_out)-T_cold)) # from equation (9)
  57. T_cold, area = integrate(f2,T_cold_in,T_cold_out(T_hot_out), 1000)
  58. return area[-1]
  59. T_hot_out, err, success = bisection(A, A_design, T_cold_in, T_hot_in, 0.001, max_iter=1000)
  60. if not success:
  61. print("Bisection has failed!")
  62. print("T_hot_out = {:.2f} (instead of {:.2f})".format(T_hot_out, T_hot_out0))
  63. print("T_cold_out = {:.2f} (instead of {:.2f})".format(T_cold_out(T_hot_out), T_cold_out0))
  64. print("A = {:.2f} (error = {:.4e})".format(A(T_hot_out), A(T_hot_out) - A_design))

答案1

得分: 1

你的 T_hot_out 和 T_cold_out 值似乎在某个时候被交换了,而且似乎有一个四舍五入错误。

英文:

It seems like your T_hot_out and T_cold_out values are being switched at some point, and it also seems like you have a rounding error somewhere.

huangapple
  • 本文由 发表于 2023年4月17日 07:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030780.html
匿名

发表评论

匿名网友

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

确定