Debugging 赋值前引用的局部变量python错误
我有这个代码,需要完美地绘制。我是python新手。错误如上所述。任何形式的帮助都将不胜感激Debugging 赋值前引用的局部变量python错误,debugging,variables,Debugging,Variables,我有这个代码,需要完美地绘制。我是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
#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
#变量初始化
#%pylab内联
#(a)h=15 W/m^2时的居里温度
#(b)固化温度为50℃时的h值
输入数学
进口numpy
从numpy导入根
导入matplotlib
从matplotlib导入pyplot
Tsurr=30+273#[K] -周围环境温度
Tf=20+273#[K] -流体流动温度
e=.5;#表面发射率
a=.8;#表面吸收率
G=2000#[W/m^2]-落在表面的辐照
h=15#[W/m^2.k]-从板到空气的热对流
stfncnstt=5.67*数学功率(10,(-8))#[W/m^2.K^4]-Stefan-Boltzmann常数
T=375#[K] 试错法的初始假定值
#使用公式1.3a和1.7以及牛顿-拉斐逊的试错法
#计算和结果
而(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;
如果((a*G)-(h*(Tn-Tf)+e*stfncnst*(Tn*Tn*Tn*Tn-Tsurr*Tsurr*Tsurr)))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;
#如果((a*G)-(h*(Tn-Tf)+e*stfncnst*(Tn*Tn*Tn*Tn-Tsurr*Tsurr*Tsurr*Tsurr))它会说
UnboundLocalError: local variable 'rt' referenced before assignment
由于您已将某些内容分配给rtrt=rot[i]
,rt
被识别为局部变量。如果要使其全局化,请使用global rt
显式将其全局化:
def T(h):
global rt
#do sth....
请注意,即使未执行分配,intepreter仍会将分配的var视为本地:
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
[136]中的:var=2
…:def foo():
…:如果为False:
…:var=4
…:打印变量
...:
在[137]中:foo()
---------------------------------------------------------------------------
取消绑定LocalError回溯(最近一次调用上次)
在()
---->1 foo()
in foo()
3如果为假:
4 var=4
---->5打印变量
6.
UnboundLocalError:赋值前引用的局部变量“var”
谢谢你的回答。这非常有帮助。但是,在同一个问题中,函数给出了4个根,其中一些根很复杂,如何识别其中的真正根。