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

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

Code producing incorrect numbers in Heat Exchange Simulation in Python

问题

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

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

# 问题 4
import numpy as np

# 从文本中获取(原始设计假设)
T_cold_out0 = 35
T_hot_out0 = 65

# 从表 1 中获取
T_hot_in = 160.0
T_cold_in = 20.0
Cp_hot = 5.2
Cp_cold = 2.6
w_hot = 1.0
k = 0.05
l = 0.002

# 从问题 2 中获取
w_cold = 5.555555
A_design = 22.26

def integrate(f, x0, x1, N):
  h = (x1 - x0) / (N-1)
  xvalues = np.linspace(x0,x1,N)
  yvalues = f(xvalues)
  F = np.zeros(N)
  for i in range (1,N):
      F[i] = h/2 * (yvalues[i-1]+yvalues[i])+F[i-1]
  return xvalues,F
  

def bisection(f, y, x0, x1, eps, max_iter=1000):
  x1_n = x1
  x0_n = x0
  T = (x1+x0) / 2.0
  f_T = f(T)
  for i in range(max_iter):
    T_n = (x0_n+x1_n)/2
    f_T_n = f(T_n)
    error = f_T_n - y
    if abs(error) <= eps:
      break
    elif  error > 0:
      x0_n = T_n
      x1_n = x1_n
    elif error < 0:
      x0_n = x0_n
      x1_n = T_n
  return T_n, error, (abs(error)<=eps)

def h_hot(T_n):
    return 0.9*(1+0.015*(T_n-70))
    
def h_cold(T_n):
    return 3.0*(1+0.02*(T_n-40))
    
def h_overall(T_cold, T_hot):
    return 1/((h_hot(T_hot))**-1 + (h_cold(T_cold))**-1 +(k/l)**-1)
    # 方程(2)使用 h_hot() 和 h_cold()
    

def T_hot(T_cold, T_hot_out):
    return  T_hot_out + ((Cp_cold*w_cold)/(Cp_hot*w_hot))*(T_cold - T_cold_in) # 来自问题 3
    
def T_cold_out(T_hot_out):
    return  T_cold_in + (Cp_hot*w_hot)/(Cp_cold*w_cold)*(T_hot_in-T_hot_out) # 类似于 T_hot()
  
def A(T_hot_out):
    def f2(T_cold):
      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)
    T_cold, area = integrate(f2,T_cold_in,T_cold_out(T_hot_out), 1000)
    return area[-1]
  
T_hot_out, err, success = bisection(A, A_design, T_cold_in, T_hot_in, 0.001, max_iter=1000)
if not success:
    print("二分法失败!")
    
print("T_hot_out = {:.2f} (而不是{:.2f})".format(T_hot_out, T_hot_out0))
print("T_cold_out = {:.2f} (而不是{:.2f})".format(T_cold_out(T_hot_out), T_cold_out0))
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.

#problem 4
import numpy as np
# from text (original design assumptions)
T_cold_out0 =35
T_hot_out0 = 65
# from Table 1
T_hot_in = 160.0
T_cold_in = 20.0
Cp_hot = 5.2
Cp_cold = 2.6
w_hot = 1.0
k = 0.05
l = 0.002
# from problem 2
w_cold = 5.555555
A_design = 22.26
def integrate(f, x0, x1, N):
h = (x1 - x0) / (N-1)
xvalues = np.linspace(x0,x1,N)
yvalues = f(xvalues)
F = np.zeros(N)
for i in range (1,N):
F[i] = h/2 * (yvalues[i-1]+yvalues[i])+F[i-1]
return xvalues,F
def bisection(f, y, x0, x1, eps, max_iter=1000):
x1_n = x1
x0_n = x0
T = (x1+x0) / 2.0
f_T = f(T)
for i in range(max_iter):
T_n = (x0_n+x1_n)/2
f_T_n = f(T_n)
error = f_T_n - y
if abs(error) <= eps:
break
elif  error > 0:
x0_n = T_n
x1_n = x1_n
elif error < 0:
x0_n = x0_n
x1_n = T_n
return T_n, error, (abs(error)<=eps)
def h_hot(T_n):
return 0.9*(1+0.015*(T_n-70))
def h_cold(T_n):
return 3.0*(1+0.02*(T_n-40))
def h_overall(T_cold, T_hot):
return 1/((h_hot(T_hot))**-1 + (h_cold(T_cold))**-1 +(k/l)**-1)
# equation (2) using h_hot() and h_cold()
def T_hot(T_cold, T_hot_out):
return  T_hot_out + ((Cp_cold*w_cold)/(Cp_hot*w_hot))*(T_cold - T_cold_in) # from problem 3
def T_cold_out(T_hot_out):
return  T_cold_in + (Cp_hot*w_hot)/(Cp_cold*w_cold)*(T_hot_in-T_hot_out) # similar to T_hot()
def A(T_hot_out):
def f2(T_cold):
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)
T_cold, area = integrate(f2,T_cold_in,T_cold_out(T_hot_out), 1000)
return area[-1]
T_hot_out, err, success = bisection(A, A_design, T_cold_in, T_hot_in, 0.001, max_iter=1000)
if not success:
print("Bisection has failed!")
print("T_hot_out = {:.2f} (instead of {:.2f})".format(T_hot_out, T_hot_out0))
print("T_cold_out = {:.2f} (instead of {:.2f})".format(T_cold_out(T_hot_out), T_cold_out0))
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:

确定