Why is my loop not running? (Error using / Matrix dimensions must agree)
1 回表示 (過去 30 日間)
古いコメントを表示
That's my code, supposedly it is about a water particle undergoing both, heat and mass transfer at the same time, and I'm monitoring the rate of change of both, diameter and temperature at the same time with a time step of 0.00001 seconds.
%%% DEFINITIONS %%%
clear;
Tb = 20; % temperature of the bulk air surrounding the particle [C]
Tp(1) = 15; % temperature of the particle's surface [C]
RHo = 0.5; % Relative Humidity air [%]
psat = 2.34; % saturation pressure at room temp [kpa]
pinf = RHo*psat; % partial pressure in bulk [kpa]
M=0.018; % molar mass of water molecule [Kg.mol-1]
Ru = 8.314; % Universal gas constant [Kg.mol.m2/s2.K]
cinf = (pinf*1000)*(M) / ((Ru)*(Tb+273)); % vapor concentration in air at 20 degrees
RHp = 1; %Relative Humidity at surface particle [%]
p = RHp*psat; % partial pressure at the surface of particle [kpa]
st = 0.0727; % surface tension at room temp [N/m]
den = 1000; % density at room temp [kg/m3]
dp(1)=200*10^-9; % initial diameter of the water particle [m]
Kr(1) = exp((4*st*M)/(Ru*den*dp(1)*(Tb+273))); % kelvin effect ratio
pd =Kr*p; % real partial pressure at the surface of the particle [kpa]
cp(1) =(pd*1000)*(M)/((Ru)*(Tp(1)+273)); % water vapor concentration at the particle's surface [Kg/m3]
t = 0.00001; % timestep [s]
D = 2.5*10^-5; % diffusivity coefficient of water at room temp [m2/s]
L = 2.44*10^6; % latent heat of vaporization for water at room temp
cp = 1007; %specific heat at room temp
kair = 0.026; % thermal conductivity for air at room temp [W/mK]
%%% INITIAL CONDITIONS %%%
Tp(1) = 15;
dp(1) = 200*10^-9;
Kr(1) = exp((4*st*M)/(Ru*den*dp(1)*(Tb+273)));
cp(1) =(pd*1000)*(M)/((Ru)*(Tp(1)+273));
mp(1) = (pi/6) *den*(dp(1))^3; %mass particle initially
mpdot(1)=(pi/6)*D*dp(1)*(cp(1)-cinf); %mass rate of the particle initially
t2(1) = 0; %time interval initially
i = 0;
%%% ITERATIONS %%%
while dp(i+1) > 0
i=i+1;
% mass transfer %
mp(i+1) = mp(i) - mpdot(i)*t; %% new mass every iteration
dp(i+1)= ((6*mp(i+1))/(pi*den))^(1/3); %% new diameter every iteration
%heat transfer %
A(i) = -((L*D*(cp(i) - cinf)) + (kair*(Tp(i) - Tb)));
B(i) = ((12*t)/(den*cp*(dp(i))^2));
Tp(i+1) = (B(i)*A(i)) + Tp(i);
Kr(i+1)= exp((4*st*M)/(Ru*den*dp(i+1)*(Tb+273)));
cp(i+1) =(p*Kr(i+1)*1000)*(M)/((Ru)*(Tp(i+1)+273));
t2(i+1) =t2(i) + t;
mpdot(i+1) = (pi/6)*D*dp(i+1)*(cp(i+1)-cinf);
% break if imaginary numbers start appearing %
if imag(dp(i+1)) ~= 0
break;
end
end
%%% PLOTTING %%%
subplot (2,1,1)
plot (t2 ,dp*10^9)
xlabel('time [s]'), ylabel('diameter [nm]')
subplot (2,1,2)
plot (t2,Tp)
xlabel('time [s]'), ylabel('Particle Temperature [C]')
When it comes to the B parameter, it shows this error; ( Error using / Matrix dimensions must agree.)
A and B are parameters to be used for the differential equation that gets me the new temperature at the new timestep.
Why is it not working for B ?
0 件のコメント
採用された回答
David Hill
2022 年 9 月 26 日
Cannot multiply by the entire array (cp).
%heat transfer %
A(i) = -((L*D*(cp(i) - cinf)) + (kair*(Tp(i) - Tb)));
B(i) = ((12*t)/(den*cp(i)*(dp(i))^2));%cp(i)
Tp(i+1) = (B(i)*A(i)) + Tp(i);
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2022 年 9 月 26 日
cp(1) =(pd*1000)*(M)/((Ru)*(Tp(1)+273));
%[...]
cp(i+1) =(p*Kr(i+1)*1000)*(M)/((Ru)*(Tp(i+1)+273));
cp is a matrix.
B(i) = ((12*t)/(den*cp*(dp(i))^2));
You are dividing by something that involves all of cp. After the first iteration, cp would be a vector. Even if the / operation worked properly, the result would surely be a vector that could not be assignd to B(i)
The MATLAB / operator is matrix division. P/Q is similar to P*inv(Q) but with some restrictions on size. P/Q is effectively a "fitting" operation. The element-by-element division operator in MATLAB is ./ like P./Q
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Thermodynamics and Heat Transfer についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!