'The variable appears to change size on every loop iteration' Error is shown.

I'm currently trying to create a function which depends on the compressibility factor of species in a mixture. This function uses subprogram of van der waals mixing rules.
The appears to an conistent error there which i can't fix.
Error Message is : The variable appears to change size on every loop iteration
The code used
Subprogram for Mixing rules
function [am, bm, aa] = MIXRULES_VDW(a, b, kappa, eta, x)
%The funtion MIXRULES_VDW calculates the am and bm of a mixture using
%VDW MIXING RULES.
%All input/output data are expressed in SI or are dimensionless.
%giving a firt value for c
c=length(x);
%initialization the values of am and bm
am=0;
bm=0;
%calculation of am, bm and aa
for i=1:c
for j=1:c
aa(i,j)=sqrt(a(i)*a(j))*(1-kappa(i,j));
bb(i,j)=(b(i)+b(j))/2*(1-eta(i,j));
am=am+x(i)*x(j)*aa(i,j);
bm=bm+x(i)*x(j)*bb(i,j);
end
end
Actual program for Calculating compressibilty:
function [Z,V,ZL,ZV,VL,VV] = ZMIX_PRVDW(Pc, Tc, w, kappa, eta, P, T, x)
%The funtion ZMIX_PRVDW calculates the compressibility factor z and the specific
%molar volume V for a mixture at given pressure P, temperature T and concentration
%using PR-CEoS and VDW MIXING RULES. This function requires REALROOTS_CARDANO and
% MIXRULES_VDW funtions to run.
%All input/output data are expressed in SI.
%P[Pa], T[K], w[dimensionless], V[m^3/mol], Z[dimensionless]
%universal gas constant
R=8.314;
%PR attractive constant and covolume at critical point
b=0.07780*(R*Tc)./Pc;
k=0.37464+1.54226*w-0.26992*w.^2;
alpha=(1+k.*(1-sqrt(T./Tc))).^2;
a=0.45724*alpha.*(R^2.*(Tc.^2))./Pc;
%calling MIXRULES_VDW function
[am, bm, aa] = MIXRULES_VDW(a, b, kappa, eta, x);
%calculation of Am and Bm
Am=am*P/(R*T)^2;
Bm=bm*P/(R*T);
%calculation of A and B
a2=-1+Bm;
a1=Am-3*Bm^2-2*Bm;
a0=-Am*Bm+Bm^2+Bm^3;
%calculation of Z and V using REALROOTS_CARDANO function
Z=REALROOTS_CARDANO(a2,a1,a0);
V=Z*R*T/P;
%selection of Z and V values for liquid and vapor
if min(V)<b
ZL=max(Z);
ZV=max(Z);
VL=max(V);
VV=max(V);
else
ZL=min(Z);
ZV=max(Z);
VL=min(V);
VV=max(V);
end
end
Both indicate same problem with the use of 'aa'. Same error is shown.
Any help would be appreciated.

4 commentaires

Bob Thompson
Bob Thompson le 30 Avr 2019
Modifié(e) : Bob Thompson le 30 Avr 2019
Is this an 'error' (red text), or a 'warning' (orange text)? If you aren't using the default colors, then does it actually prevent your code from running, or does it just underline it for you?
I have typically seen this message as a warning, and it can be removed if you preinitialize the variable. Something like the following line can be entered before the for loop to accomplish this.
aa = zeros(c);
Also, if it is just a warning message then it won't have any impact on the effectiveness of your code, it will just run more slowly than is 'optimal.' For small data sets this is of no consequence, though optimization can be important for larger data sets.
It's neither an error or a warning, it's an mlint message that only appears in the editor.
Its an error that comes with a red text. It does prevent it from running my line.
In the command window, this appears in red:
Error in MIXRULES_VDW (line 17)
aa(i,j)=sqrt(a(i)*a(j))*(1-kappa(i,j));
No, The variable appears to change size on every loop iteration is never an error. It won't stop you from running code, the only impact will be slower code.
Pleas give us the full text of the error message. Everything that is red.

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 30 Avr 2019
Modifié(e) : Guillaume le 30 Avr 2019
Documentation on preallocation which explains why you're seeing this.
As Bob Nbob said, to make it go away, preallocate your arrays before the loop:
aa = zeros(c, c); %or zeros(c) it's the same
bb = zeros(c, c); %or zeros(c) it's the same
You may also consider giving more imaginative names to your variables, full words that explain what the variables contain, so any reader of your code can understand it.
Additionally, if you're calculating am and bm in a loop, there's no point of storing aa and bb as an array. So you could just have:
am=0;
bm=0;
%calculation of am, bm and aa
for i=1:c
for j=1:c
aa = sqrt(a(i)*a(j))*(1-kappa(i,j));
bb = (b(i)+b(j))/2*(1-eta(i,j));
am=am+x(i)*x(j)*aa;
bm=bm+x(i)*x(j)*bb;
end
end
However, the loops are completely unnecessary:
aa = sqrt(a .* a.') .* (1-kappa); %calculate all the aa in one go
bb = (b + b.')/2 .* (1-eta); %calculate all the bb in one go
am = sum(x .* x.' .* aa, 'all'); %requires R2018b or later for the 'all' option
bm = sum(x .* x.' .* bb, 'all');

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by