# Index exceeds the number of array elements (1).

2 views (last 30 days)
Bruce Lich on 19 May 2021
Edited: Bruce Lich on 19 May 2021
Im struggling with this code, the problem occurs in line 40
lnphi=-log(z-B)+(b(i)*(z-1)/b)-((A/(2*sqrt(2)*B))*((2*sum_A(i)/sum_a)-(b(i)/b))*log((z+(1+sqrt(2)*B))/(z+(1-sqrt(2)*B))));
function[phi]= prsv(T,P,Tc,Pc,w,k1,x)
nrosust=length(Tc);
R=8.314772;
for i=1:nrosust
k0(i)=0.378893+1.4897153*w(i)-0.17131848*w(i).^2+0.0196554*w(i).^3;
k(i)=k0(i)+k1(i)*(1+(T/Tc(i))^0.5)*(0.7-(T/Tc(i)));
alpha(i)=(1+k(i)*(1-((T/Tc(i))^0.5)))^2;
a(i)=(0.457235*(R^2*Tc(i).^2/Pc(i)))*alpha(i);
b(i)=0.07779607*(R*Tc(i)/Pc(i));
end
sum_a=0;
for i=1:nrosust
for j=1:nrosust
aij(i,j)=(a(i)*a(j))^0.5;
sum_a=sum_a+x(i)*x(j)*aij(i,j);
end
end
b=sum(x.*b);
A=sum_a*P/(R*T)^2;
B=b*P/(R*T);
Pol=[1 -1+B A-3*B^2-2*B -A*B+B^2+B^3];
Z=roots(Pol);
z=max(Z);
for i=1:nrosust
sum_A(i)=0;
for j=1:nrosust
sum_A(i)=sum_A(i)+x(j)*aij(i,j);
end
end
for i=1:nrosust
lnphi=-log(z-B)+(b(i)*(z-1)/b)-((A/(2*sqrt(2)*B))*((2*sum_A(i)/sum_a)-(b(i)/b))*log((z+(1+sqrt(2)*B))/(z+(1-sqrt(2)*B))));
phi(i)=exp(lnphi);
end
##### 2 CommentsShowHide 1 older comment
Bruce Lich on 19 May 2021
Hi! When every input is a scalar, it works just fine. The problem is when i try it with vectors as inputs (for mixes).
i.e. this one:
(note that T and P are global parameters, they are always scalars, whilst the rest are not unless its a pure substance)
[phi]= prsv(800,1000*1000,[647.1 512.6],[22055*1000 8097*1000],[0.345 0.564],[-0.06635 -0.16816],[0.3 0.7])

per isakson on 19 May 2021
Here are some links on debugging in Matlab