MATLAB: Hİ every body the question iis that when ı try to run the code it says that (Inner matrix dimensions must be agree) for frho = a * rho .^ 6 + b * rho .^ 3 + c * rho .^ 2 + d * rho + e * rho .^ 3 * (1 + f * rho .^ 2) * exp(-f * rho .^ 2) – g;

inner

yH2S=0;
yCO2 =0;
yN2=0;
N2=input(' N2 enter:');
CO2=input('CO2 enter:');
H2S=input('H2S enter:');
Gg=input('Gg enter:');
T=input('T enter:');
P=zeros(1,100);
for i=1:100
P(1)=0;
P(i+1)=P(i)+100;
yN2 = N2 / 100;
yCO2 = CO2 / 100;
yH2S = H2S / 100;
cGg = (Gg - 0.9672 * yN2 - 1.5197 * yCO2 - 1.1768 * yH2S) / (1 - yN2 - yCO2 - yH2S);
Tpc = 168 + 325 * cGg - 12.5 * cGg ^ 2;
CorrTpc = Tpc - 80 * yCO2 + 130 * yH2S - 250 * yN2;
Ppc = 677 + 15 * cGg - 37.5 * cGg ^ 2;
CorrPpc = Pc + 440 * yCO2 + 600 * yH2S - 170 * yN2;
Tr = (T + 460) / CorrTpc;
Pr = P / CorrPpc;
a = 0.064225133;
b = 0.53530771 * Tr - 0.61232032;
c = 0.31506237 * Tr - 1.0467099 - 0.57832729 / Tr ^ 2;
d = Tr;
e = 0.68157001 / Tr ^ 2;
f = 0.68446549;
g = 0.27 * Pr;
rho = 0.27 * Pr / Tr ;
rhoold = rho;
for i = 1:100
frho = a * rho .^ 6 + b * rho .^ 3 + c * rho .^ 2 + d * rho + e * rho .^ 3 * (1 + f * rho .^ 2) * exp(-f * rho .^ 2) - g;
dfrho = 6 * a * rho .^ 5 + 3 * b * rho .^ 2 + 2 * c * rho + d + e * rho .^ 2 * (3 + f * rho .^ 2 * (3 - 2 * f * rho .^ 2)) * exp(-f * rho .^ 2);
rho = rho - frho / dfrho;
test = abs((rho - rhoold) / rho);
if test < 0.00001
rhoold = rho;
ZFactor = 0.27 * Pr / rho / Tr;
end
end
Zm=ZFactor;
MW = 28.97 * Gg;
a = (9.4 + 0.02 * MW) * (T + 460) ^ 1.5 / (209 + 19 * MW + (T + 460)) / 10000;
b = 3.5 + 986 / (T + 460) + 0.01 * MW;
c = 2.4 - 0.2 * b;
rho = P * MW / Zm / 10.73 / (T + 460);
Ug = a * exp(b * (rho / 62.4) ^ c);
mP = 0;
Pold = 0;
Xold = 0;
Pstep = P / 20;
for j=0:20
Pnew = Pold + Pstep;
Pr = Pnew / Pc;
Xnew = 2 * Pnew / Zm / Ug;
mP = mP + (Xold + Xnew) / 2 * Pstep;
Pold = Pnew;
Xold = Xnew;
end
end
plot(P,mP)

Best Answer

I can’t run your code, but when in doubt, vectorise everything:
frho = a .* rho .^ 6 + b .* rho .^ 3 + c .* rho .^ 2 + d .* rho + e .* rho .^ 3 .* (1 + f .* rho .^ 2) * exp(-f .* rho .^ 2) - g;
dfrho = 6 * a .* rho .^ 5 + 3 * b .* rho .^ 2 + 2 * c .* rho + d + e .* rho .^ 2 .* (3 + f .* rho .^ 2 .* (3 - 2 * f .* rho .^ 2)) .* exp(-f .* rho .^ 2);
See if these vectorised lines solve your problem.