Attempted to access Tr(4); index out of bounds because numel(Tr)=3

on 21 Jul 2019
Latest activity Answered by Image Analyst

Image Analyst (view profile)

on 21 Jul 2019
I am trying to create a loop to call different variables at each time but I am getting this message "Attempted to access Tr(4); index out of bounds because numel(Tr)=3"
What am I doing wrong?
Tref=273;
Pref=1;
R=8.314;
Tc=647.1;Vc=55.9;Pc=220.55;
iterasimaks = 1000;
toleransi = 0.0000001;
galat = 0.1;
b1s=0.1181193;b2s=0.265728;b3s=0.15479;b4s=0.030323;c1s=0.0236744;c2s=0.0186984;c3s=0;c4s=0.042724;
d1s=1.55488*10^-5;d2s=6.23689*10^-5;beta1=0.65392;gamma1=0.060167;
T=[800 850 900];
P=16;
nT=length(T);
Pout=4;
Pr=Pout/Pc;
ii=1;
while ii<=nT
Tr(ii)=T(ii)/Tc;
Bs(ii) = b1s - (b2s/Tr(ii)) - (b3s/(Tr(ii)^2)) - (b4s/(Tr(ii)^3));
Cs(ii) = c1s - (c2s/Tr(ii)) + (c3s/(Tr(ii)^3));
Ds (ii)= d1s + (d2s/Tr(ii));
ii=ii+1;
end
fungsi=@(V,Tr) ((V^5)*Tr^4)+((V^6)*(-Tr^3)*Pr)+((V^4)*((Tr^4)*Bs))+((V^3)*((Tr^4)*Cs))+((V^3)*c4s*Tr*beta1*exp(-gamma1/(V^2)))+((((V^3)*c4s*Tr*gamma1)/(V^2))*exp(-gamma1/(V^2)))+(Ds*Tr^4);
turunan=@(V,Tr) ((5*V^4)*Tr^4)+((6*V^5)*(-Tr^3)*Pr)+((4*V^3)*((Tr^4)*Bs))+((3*V^2)*((Tr^4)*Cs))+((c4s*Tr*beta1*exp(-gamma1/(V^2)))*(((V^4)/4)+(gamma1*V^4)+(2*gamma1*(V^-2))+2))+((c4s*Tr*gamma1*exp(-gamma1/(V^2))*(((V^2)/2)+(gamma1*(V^-4))+(2*gamma1*(V^-2)))));
jj=1;
while jj<=nT
ii=1;
h(jj)= R*T(jj)/P;
V(ii) = h(jj);
while galat>=toleransi
V(ii+1)=V(ii) - (fungsi(Tr(ii),V(ii))/turunan(Tr(ii),V(ii)));
galat = abs(V(ii+1) - V(ii));
Vrsout= V(ii+1);
iterasi = ii;
ii=ii+1;
end
jj=jj+1;
end
Vrsout
iterasi

Image Analyst (view profile)

on 21 Jul 2019

It's hard to decypher this uncommented alphabet soup (without spending a lot more time), but all we know for sure is that the loop
while galat>=toleransi
occurs more than 3 times. Try to figure out why that is. Maybe you can just avoid the problem by bailing out if it occurs:
while (galat >= toleransi) && (ii <= length(Tr))
but doing that may not get you the right or expected answer.