这个错误可能的原因是你忘了初始化变量
function EMDGETK%计算Argon热传导系数
%采用快速Fouie变换求解EMD算法得到的热传导系数
%ref: Molecular simulation of liquids.p190
%
%为了节约计算时间和所需内存量,这里将热流文件分开成几个文件,如每1000000个数据放在一个文件中.
%
%计算用到的文件:initpara.dat, datafile.in, flux*.dat,avg.dat;
%flux1.dat, flux2.dat,类似文件的个数
%*********************************************
%依据所拥有的flux*.dat的个数,修改filenum的值*
%*********************************************
filenum=1;
%最大相关长度
maxcor=100000;
%通过一下相关长度计算热传导系数
cor=10000:10000:maxcor;
Kb=1.38E-23;
[filename, filepath] = uigetfile('initpara.dat','载入数据');
if filepath==0
return;
end
inputfile = strcat(filepath, 'DATAFILE.IN');
fid = fopen(inputfile, 'r');
if fid < 0
errordlg({'不能打开初始化文件: ', inputfile});
return;
end
ncx=fscanf(fid, '%d',1);
ncy=fscanf(fid, '%d',1);
ncz=fscanf(fid, '%d',1);
tinit=fscanf(fid, '%f',1);
fclose(fid);
initparas = strcat(filepath, 'initpara.dat');
fid = fopen(initparas, 'r');
if fid < 0
errordlg({'不能打开初始化文件: ', initparas});
return;
end
%sig = fscanf(fid, '%f',1);
%eps = fscanf(fid, '%f',1);
%mass = fscanf(fid, '%f',1);
%A = fscanf(fid, '%f',1);
%dt = fscanf(fid, '%f',1);
%n = fscanf(fid, '%d',1);
%volume = fscanf(fid, '%f',1);
%fclose(fid);
sig = fscanf(fid, '%f',1);
eps = fscanf(fid, '%f',1);
sig2 = fscanf(fid, '%f',1);
eps2 = fscanf(fid, '%f',1);
sigavg = fscanf(fid, '%f',1);
epsavg = fscanf(fid, '%f',1);
mass = fscanf(fid, '%f',1);
mass2 = fscanf(fid, '%f',1);
a = fscanf(fid, '%f',1);
%a2= fscanf(fid, '%f',1);
dt = fscanf(fid, '%f',1);
n = fscanf(fid, '%d',1);
volume = fscanf(fid, '%f',1);
fclose(fid);
tinit=tinit*Kb/eps;
avgsfile = strcat(filepath, 'avgs.dat');
fid = fopen(avgsfile, 'r');
if fid < 0
errordlg({'不能打开初始化文件: ', avgsfile});
return;
end
%这是模拟的真实温度
tinit = fscanf(fid, '%f',1);
fclose(fid);
rtavg=zeros(maxcor+1,1); %+1是因为相关距离从0到maxcor;
%读取有效热流量
for i=1:filenum;
strnum=int2str(i);
fluxfile=strcat(filepath, 'totflux',strnum,'.dat');
totflux=load(fluxfile);
jx=totflux(:,2);
jy=totflux(:,3);
jz=totflux(:,4);
%长度加倍
len=length(jx);
if(maxcor>len/2)
errordlg('maxcor>len/2');
return;
end
jx(len+1:len*2)=0;
jy(len+1:len*2)=0;
jz(len+1:len*2)=0;
%傅氏变换
fjx=fft(jx);
fjy=fft(jy);
fjz=fft(jz);
fjx=fjx;
fjy=fjy;
fjz=fjz;
%能量谱
fjx=abs(fjx).^2;
fjy=abs(fjy).^2;
fjz=abs(fjz).^2;
%逆变换,求相关系数
rtx=ifft(fjx);
rty=ifft(fjy);
rtz=ifft(fjz);
rt=(rtx+rty+rtz);
rt=real(rt);
rt=rt(1:maxcor+1);
norm=len:-1.0:(len-maxcor);
norm=norm';
rt=rt./norm;
rtavg=rtavg+rt;
end
rtavg=rtavg./filenum;
%输入计算的初始参数
outfile = strcat(filepath, '计算结果.DAT');
fid = fopen(outfile,'w');
if fid<0
errordlg('文件:计算结果.DAT 打开失败','错误信息');
return
end
fprintf(fid,'EMD热传导分子动力学模拟结果\n');
fprintf(fid,strcat('\n计算日期:',date,'\n'));
fprintf(fid,'\n初始参数:\n');
fprintf(fid,'sig=%E\n',sig);
fprintf(fid,'eps=%E\n',eps);
%fprintf(fid,'sig2=%E\n',sig2);
%fprintf(fid,'eps2=%E\n',eps2);
%fprintf(fid,'sigavg=%E\n',sigavg);
%fprintf(fid,'epsavg=%E\n',epsavg);
fprintf(fid,'mass=%E\n',mass);
%fprintf(fid,'mass2=%E\n',mass2);
fprintf(fid,'a=%5.3f\n',a);
%fprintf(fid,'a2=%5.3f\n',a2);
fprintf(fid,'dt=%7.5f\n',dt);
fprintf(fid,'计算温度:%5.1f\n',tinit*par_De/Kb);
fprintf(fid,'\n模拟区域大小\n');
fprintf(fid,'ncx=%2d ncy=%2d ncz=%2d\n',ncx,ncy,ncz);
fprintf(fid,'温度:%6.3f\n',tinit);
%计算热传导系数
fprintf(fid, '\n结算结果\n');
fprintf(fid, '相关长度 热传导系数(W/mK)\n');
condavg=0.0;
cornum=length(cor);
cond=zeros(cornum,1);
for i=1:cornum
length=cor(i)+1;
rttemp=rtavg(1:length);
sum=trapz(rttemp);
sum=sum.*dt;
cond(i)=volume*sum/(3.0*tinit*tinit);
cond(i)=cond(i)*Kb*sqrt(eps/mass)/(sig*sig);
condavg=condavg+cond(i);
end
condavg=condavg/cornum;
%计算标准方差
thetasq=0.0;
for(i=1:cornum)
length=cor(i);
thetasq=thetasq+(cond(i)-condavg)*(cond(i)-condavg);
fprintf(fid, '%8d %6.3f\n', length, cond(i));
end
fprintf(fid,'平均热传导系数(W/mK):%6.3f\n',condavg);
thetasq=thetasq/(cornum-1);
thetasq=sqrt(thetasq);
fprintf(fid,'标准方差:%7.4f',thetasq);
fclose(fid);
xx=0:maxcor;
plot(xx,rtavg./rtavg(1));
hold on;
y=[0 0];
%line(x,y,'color','r');
hold off;
msgbox('计算完成!');
错误是
??? Error: File: CANZAEMDGETK.m Line: 162 Column: 5
"length" previously appeared to be used as a function or command, conflicting with its use here as the
name of a variable.
A possible cause of this error is that you forgot to initialize the
variable, or you have initialized it implicitly using load or eval.