Help, please! Index exceeds array bounds

Hello! I'm trying to simulate the dynamic of some variables (concentration and temperature, basically). However, I keep receiving the same error message over and over again (index exceeds array bounds). I tried to change the order of the code and even erased the line relating to the error (Tr(i+1)). But then, the program displayed the same error for other lines. I would appreciate if someone could give a hand on this. Thank you!!!!
V(1)=Vo; %m³
h(1)=2*(2*Vo/pi)^(1/3); %m
Ca(1)=5; %kmol/m³
Cb(1)=0; %kmol/m³
Cc(1)=0; %kmol/m³
Cd(1)=0; %kmol/m³
Cw(1)=30.7; %kmol/m³
Tr(1)=300; %K
t(1)=0; %s
for i=1:300
V(i+1)=V(i)+dt*(ve);
h(i+1)=h(i)+dt*(ve/A);
kd(i)=ko*exp(E*(1/273-1/Tr(i)));
Kc(i)=10^(3885.44/Tr(i));
wa(i)=Ca(i)/(Ca(i)+Cb(i)+Cc(i)+Cd(i)+Cw(i));
Cpm(i)=wa(i)*Cpa+(1-wa(i))*Cp;
Tcw2=Tr(i)-(Tr(i)-Tcw1)*exp(-UA/(mcw*Cp));
dt1=Tr(i)-Tcw1;
dt2=Tr(i)-Tcw2;
dtml=(dt2-dt1)/log(dt2/dt1);
Tr(i+1)=Tr(i)+dt*(ve*Cpe*(Te-Tref)/(Cpm(i)*V(i))-UA*dtml(i)/(rho*Cpm(i)*V(i))-dHd*kd(i)*Ca(i)*Cb(i)/(rho*Cpm(i))-dHi*kd(i)*Cc(i)*Cd(i)/(rho*Cpm(i)*Kc(i)));
Ca(i+1)=Ca(i)+dt*(kd(i)*(Cc(i)*Cd(i)/Kc(i)-Ca(i)*Cb(i))-ve*Ca(i)/V(i));
Cb(i+1)=Cb(i)+dt*(ve/V(i)*(Cbe-Cb(i))+kd(i)*(Cc(i)*Cd(i)/Kc(i)-Ca(i)*Cb(i)));
Cc(i+1)=Cc(i)+dt*(kd(i)*(Ca(i)*Cb(i)-Cc(i)*Cd(i)/Kc(i))-ve*Cc(i)/V(i));
Cd(i+1)=Cd(i)+dt*(kd(i)*(Ca(i)*Cb(i)-Cc(i)*Cd(i)/Kc(i))-ve*Cd(i)/V(i));
Cw(i+1)=Cw(i)+dt*(ve/V(i)*(Cwe-Cw(i)));
t(i+1)=i;
end

回答(1 个)

Vo = rand * 10;
dt = 1/100;
ve = rand * 50;
A = rand * 100;
ko = rand;
E = 10^3;
Cpa = rand * 10;
Cp = rand * 11;
Tcw1 = rand * 7;
UA = randn();
mcw = rand * 13;
Cpe = rand * 9;
Te = randn();
Tref = sqrt(2);
rho = 0.12345;
dHd = rand;
dHi = rand;
Cbe = rand;
Cwe = rand;
V(1)=Vo; %m³
h(1)=2*(2*Vo/pi)^(1/3); %m
Ca(1)=5; %kmol/m³
Cb(1)=0; %kmol/m³
Cc(1)=0; %kmol/m³
Cd(1)=0; %kmol/m³
Cw(1)=30.7; %kmol/m³
Tr(1)=300; %K
t(1)=0; %s
for i=1:300
V(i+1)=V(i)+dt*(ve);
h(i+1)=h(i)+dt*(ve/A);
kd(i)=ko*exp(E*(1/273-1/Tr(i)));
Kc(i)=10^(3885.44/Tr(i));
wa(i)=Ca(i)/(Ca(i)+Cb(i)+Cc(i)+Cd(i)+Cw(i));
Cpm(i)=wa(i)*Cpa+(1-wa(i))*Cp;
Tcw2=Tr(i)-(Tr(i)-Tcw1)*exp(-UA/(mcw*Cp));
dt1=Tr(i)-Tcw1;
dt2=Tr(i)-Tcw2;
dtml(i)=(dt2-dt1)/log(dt2/dt1); %CHANGED
Tr(i+1)=Tr(i)+dt*(ve*Cpe*(Te-Tref)/(Cpm(i)*V(i))-UA*dtml(i)/(rho*Cpm(i)*V(i))-dHd*kd(i)*Ca(i)*Cb(i)/(rho*Cpm(i))-dHi*kd(i)*Cc(i)*Cd(i)/(rho*Cpm(i)*Kc(i)));
Ca(i+1)=Ca(i)+dt*(kd(i)*(Cc(i)*Cd(i)/Kc(i)-Ca(i)*Cb(i))-ve*Ca(i)/V(i));
Cb(i+1)=Cb(i)+dt*(ve/V(i)*(Cbe-Cb(i))+kd(i)*(Cc(i)*Cd(i)/Kc(i)-Ca(i)*Cb(i)));
Cc(i+1)=Cc(i)+dt*(kd(i)*(Ca(i)*Cb(i)-Cc(i)*Cd(i)/Kc(i))-ve*Cc(i)/V(i));
Cd(i+1)=Cd(i)+dt*(kd(i)*(Ca(i)*Cb(i)-Cc(i)*Cd(i)/Kc(i))-ve*Cd(i)/V(i));
Cw(i+1)=Cw(i)+dt*(ve/V(i)*(Cwe-Cw(i)));
t(i+1)=t(i)+dt; %CHANGED
end

1 个评论

Matteus's "Answer" moved here because it's actually a comment to Walter rather than an answer to the original question. Matteus, if your question is answered by Walter then please click the "Accept this Answer" link to give him credit for it.
Thank you so much, Walter. About the random values, they were already a part of the code that I didn't show. I really just had forgotten about the "(i)" in the dtml line.
But seriously, thank you!!!!!!!!!!!!!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by