Code keeps running without any error but no result ,please help!
古いコメントを表示
Dear All,
I have a function named (Untitled) and a script named (Untitled2). I use the function in the script by calling it. Now, I have a problem and the problem is when I run the script it keeps running for a long time without showing any error, warning or result. I think something does not make sence in the code which this causes this problem, I tried to figure it ou but I coud not. It would be appreciated if you guys help me.
%%%% Function
function dz = Untitled (V, z, Ca0, Fa0, Cp0, Cpc, Ua, m, T1, T2, k1, K2, E, R, dH, hx)
% z(1)=Ta, z(2)=X, z(3)=T
k = k1*exp(E*(1/T1-1/z(3))/R);
Kc = K2*exp (dH* (1/T2 - 1/z (3))/R);
ra =-k*Ca0* (1 - (1 + 1/Kc)*z(2));
if hx == 'co', dz (1)= Ua* (z(3) - z(1))/(m*Cpc);
elseif hx =='cn', dz (1)= -Ua* (z (3) - z(1))/(m*Cpc);
else, dz(1) = 0;
end
dz (2) = -ra/Fa0; dz (3)= (ra*dH - Ua* (z (3) -z (1)))/(Fa0*Cp0);
dz = dz';
end
%%%%% Script
clear all;
Ca0= 1.86; Fa0= 14.67; T1 = 360; T2= 333; k1= 31.1; K2= 3.03;
E = 65700; R = 8.314; dH = -34500; Cp0= 141; Cpc= 28; Ua= 5000; m= 500;
hx ='cn'; %data and conditions
crit = 1e-2; Taf = 315; errT = 10; Vi= [0 5];
if hx =='cn' %guess Ta(z1)
Ta01 = 305; Ta02 = 320; Ta0m = (Ta01+Ta02)/2;
while errT >= crit
z01 = [Ta01 0 305];
z02 = [Ta02 0 305];
z0m= [Ta0m 0 305];
[V z1]=ode45 (@Untitled,Vi,z01,[],Ca0, Fa0, Cp0, Cpc, Ua,m, T1, T2, k1, K2, E, R, dH, hx);
[V zm] = ode45 (@Untitled,Vi, z0m,[],Ca0, Fa0, Cp0,Cpc, Ua, m, T1, T2, k1, K2, E, R, dH, hx);
[V z2] = ode45 (@Untitled, Vi, z02,[],Ca0, Fa0, Cp0, Cpc, Ua, m, T1, T2, k1, K2, E, R, dH, hx);
if (z1 (end, 1)-Taf)*(zm (end, 1)-Taf) < 0, Ta02=Ta0m;
Ta0m=(Ta01+Ta02)/2;
else, Ta01 = Ta0m;
Ta0m =(Ta01+Ta02)/2;
end
errt = abs (zm (end, 1)- Taf);
end
else
z0 = [310 0 305]; [V zm] = ode45 (@exbco, Vi, z0, [], Ca0, Fa0, Cp0, Cpc, Ua, m, T1, T2, k1, K2, E, R, dH, hx);
end
Ta =zm(:,1); X = zm(:, 2); T = zm (:, 3); k = k1*exp (E* (1/T1 - 1./T)/R); Kc= K2*exp(dH+ (1/T2 - 1./T)/R);
ra = -k*Ca0.*(1- (1+ 1./Kc).*X);
Xe = Kc./(1+Kc);
subplot (2,2,1), plot (V,T,V, Ta,'--'), xlabel ('V'), ylabel ('T(K)'),
legend ('T', 'T_a')
subplot (2,2,2), plot (V, X, V, Xe,'--'), xlabel('V'), ylabel ('X,X_e'),
legend ('X', 'Xe')
subplot (2,2,3), plot (V, -ra), xlabel ('V'), ylabel('-r_A')
fprintf('Conversion (X) and equilibrium conversion (Xe): Xf = %g, Xef = %g\n',X(end),Xe (end));
fprintf('Final T and Ta: Tf %g, Taf= %g\n', T(end), Ta (end)) ;
fprintf('Final reaction rate: raf = %g\n', -ra (end) );
回答 (1 件)
DGM
2021 年 5 月 5 日
0 投票
If your code runs and doesn't indicate what it's doing, make it tell you what it's doing. For instance, store all the values of errt (the variable on which the exit condition depends). You might see a pattern that will tell you why it never terminates.

It never exits because it's not converging toward zero. You'll have to find out why. Maybe your initial conditions are landing you in some local minimum other than the one you intend -- or something like that. I don't know what problem you're trying to solve.
カテゴリ
ヘルプ センター および File Exchange で Labels and Styling についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!