Keep getting error message "Dimensions of arrays being concatenated are not consistent." but not there if I changed the parameters

1 次查看(过去 30 天)
For the following code, i keep getting this error message. BUt it was not there when i had different numbers for temperatures and pressures. I don't understand
R = 8.3144598; %m^3*Pa*K^-1*mol^-1
TC = 421.9; %K
PC = 2177490; %Pa
TT = 148.35; %K
PT = 0.2584; %Pa
TM = 148; %K
PM = 101325; %Pa
omega = 0.4123;
syms T P V
kpeng = 0.37464 + 1.5422.*omega -0.26992.*omega.^2;
sqrtalphapeng(T) = 1+kpeng.*(1-sqrt(T./TC));
alphapeng(T) = (1+kpeng.*(1-sqrt(T./TC))).^2;
apeng(T) = 0.45724.*(R.^2.*TC.^2./PC).*alphapeng(T);
bpeng = 0.07780.*R.*TC./PC;
Ppeng(T,V) = R.*T/(V-bpeng)- apeng(T)./(V.*(V+bpeng)+bpeng.*(V-bpeng));
VC = double(solve(Ppeng(TC,V) == PC,V,'Real',true));
ZC = PC*VC/(R*TC);
VT = double(solve(Ppeng(TT,V) == PT,V,'Real',true));
VM = double(solve(Ppeng(TM,V) == PM,V,'Real',true));
dpdvt = diff(Ppeng(T,V),V);
dpdtv = diff(Ppeng(T,V),T);
alpha(T,V) = 1./V.*(1./(-dpdvt./dpdtv));
kt(T,V) = -1./V.*(1./dpdvt);
DCpv(T,V) = T.*V.*alpha(T,V).^2./kt(T,V);
P1 = linspace(PC,PC,1000);
Tupper = 2.5.*TC;
T1 = linspace(TT,Tupper,1000);
V1 = [];
for n = 1:1000
V2 = double(solve(Ppeng(T1(n),V) == PC,V,'Real',true));
V1 = [V1,V2];
end
DCpv1 = DCpv(T1,V1);
DCpv1 = double(DCpv1);
alpha1 = alpha(T1,V1);
kt1 = kt(T1,V1);
plot(T1,alpha1)
ylim([0 0.06])
grid on
xlabel('Temperature (K)')
ylabel('Energy (J/mol)')
title('\alpha vs. T')
plot(T1,DCpv1)
ylim([0 600])
grid on
xlabel('Temperature (K)')
ylabel('Energy (J/mol)')
title('C_P-C_V')

回答(1 个)

Matt J
Matt J 2019-11-28
These lines will only work if V2 is always a scalar.
V2 = double(solve(Ppeng(T1(n),V) == PC,V,'Real',true));
V1 = [V1,V2];
  3 个评论
Walter Roberson
Walter Roberson 2019-11-28
编辑:Walter Roberson 2019-11-28
V2 = double(solve(Ppeng(T1(n),V) == PC,V,'Real',true));
if isempty(V2); V2 = nan; end
V1 = [V1,V2(end)];
You might want to output something in the case where numel(V2) ~= 1 to warn the user.
Note: at n = 723, you get 3 results. What is happening is that there are normally 3 results, but your 'Real', true is discarding a pair of complex roots up until then, but eventually there are 3 real roots.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by