我有这个代码需要完美绘制。我是python的新手。错误如上所述。任何形式的帮助将不胜感激。
#Variable Initialization
#%pylab inline
# (a) Curie Temperature for h = 15 W/m^2
# (b) Value of h for cure temp = 50 deg C
import math
import numpy
from numpy import roots
import matplotlib
from matplotlib import pyplot
Tsurr = 30+273; #[K] - Temperature of surrounding
Tf = 20+273; #[K] - Temperature of Fluid Flow
e=.5; # Emissivity of Surface
a = .8; # Absorptivity of Surface
G = 2000; #[W/m^2] - Irradiation falling on surface
h=15; #[W/m^2.k] - Thermal Convectivity from plate to air
stfncnstt=5.67*math.pow(10,(-8)); # [W/m^2.K^4] - Stefan Boltzmann Constant
T=375; #[K] Value initially assumed for trial-error approach
#Using Eq 1.3a & 1.7 and trial-and error approach of Newton Raphson
#calculations and results
while(1>0):
f=((a*G)-(h*(T-Tf)+e*stfncnstt*(T*T*T*T - Tsurr*Tsurr*Tsurr*Tsurr)));
fd=(-h*T-4*e*stfncnstt*T*T*T);
Tn=T-f/fd;
if(((a*G)-(h*(Tn-Tf)+e*stfncnstt*(Tn*Tn*Tn*Tn - Tsurr*Tsurr*Tsurr*Tsurr)))<.01):
break;
T=Tn;
print '%s %.2f %s' %("\n (a) Cure Temperature of Plate =",T-273.,"degC\n");
#solution (b)
Treq=50+273;
#def T(h):
# t=375;
# while(1>0):
# f=((a*G)-(h*(t-Tf)+e*stfncnstt*(t*t*t*t - Tsurr*Tsurr*Tsurr*Tsurr)));
# fd=(-h*t-4*e*stfncnstt*t*t*t);
# Tn=t-f/fd;
# if((a*G)-(h*(Tn-Tf)+e*stfncnstt*(Tn*Tn*Tn*Tn - Tsurr*Tsurr*Tsurr*Tsurr))<.01):
# break;
# tnew=Tn;
# return tnew;
rot=numpy.zeros(4);
rt=0;
def T(h):
coeff = ([-e*stfncnstt, 0,0, -h, a*G+h*Tf+e*stfncnstt*Tsurr*Tsurr*Tsurr*Tsurr]);
rot=numpy.roots(coeff);
for i in range (0,3):
if rot[i]<250 and rot[i]>0:
rt=rot[i];
return rt;
Error at this part. The function is not working.! What can be the error. Please suggest.
h = range(0,100)
tn=range(0,100)
for i in range (0,100):
tn[i] = T(i) -273;
Ti=50+273;
hnew=((a*G)-(e*stfncnstt*(Ti*Ti*Ti*Ti - Tsurr*Tsurr*Tsurr*Tsurr)))/(Ti-Tf);
pyplot.plot(h,tn);
pyplot.xlabel("h (W m^2/K)");
pyplot.ylabel("T (C)");
pyplot.show();
print '%s %.2f %s' %("\n (b) Air flow must provide a convection of =",hnew," W/m^2.K");
#print '%s' %("\n The code for the graph requires more than 10 min to run. ")
#print '%s' %("\n To run it, please remove comments. It is perfectly correct. The reason it takes such a long time")
#print '%s' %("\n is that it needs to calculate using Newton raphson method at 100 points. Each point itself takes a minute.")
#END
答案 0 :(得分:0)
它会说
UnboundLocalError: local variable 'rt' referenced before assignment
由于您已将某些内容分配给rt rt=rot[i]
,因此rt
被识别为局部变量。如果您想将其设为全局,请使用global rt
:
def T(h):
global rt
#do sth....
请注意,即使未执行赋值,解释器仍会将指定的var视为local:
In [136]: var=2
...: def foo():
...: if False:
...: var=4
...: print var
...:
In [137]: foo()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-137-624891b0d01a> in <module>()
----> 1 foo()
<ipython-input-136-ab29dd655074> in foo()
3 if False:
4 var=4
----> 5 print var
6
UnboundLocalError: local variable 'var' referenced before assignment