Attempted to access nv(2.8); index must be a positive integer or logical. Newton-Raphson Flash

조회 수: 2 (최근 30일)
Hi, I'm very new to MatLab and I am trying to write a simple flash calculation code, however I am getting the above error. I was wondering if you could help me. I'm not sure what the error is asking.
if true
% code
Zi = [0.2, 0.1, 0.1, 0.2, 0.2, 0.2];
pvi = [190, 72.2 51.6, 20.44, 15.57, 4.956];
Ki = [3.8, 1.444, 1.032, 0.4088, 0.3114, 0.09912];
A = 0;
B = 0;
for i = 1:length(Zi)
A = A + Zi(i)*(Ki(i)-1);
B = B + Zi(i)*((1/Ki(i))-1);
end
nv0= A/(A+B);
% Display an error message if 0<nv<1
if nv0 > 1 || nv0 < 0 || nv0 ==0
error('nv guess is incorrect')
end
% Step 2 - Solving Equation 5 - 16 for nv Using Newton-Raphson Method
nv = nv0;
nv0 = nv + .01; % Is this for the first gues abs dev?
itermax = 200;
fnkd = 0;
fnk = 0;
while abs(nv0 - nv) > tol & iter < itermax
iter = iter + 1;
nv0 = nv;
for i= 1:length(Zi)
fnk = fnk + ((Zi(i) * (Ki(i)-1))/(nv(Ki(i)-1)+1));
fnkd = fnkd +(-1*(Zi(i)*(Ki(i)-1)^2)/(nv(Ki(i)-1)+1)^2);
end
if fnkd ~= 0
nv = nv0 - fnk/fnkd;
else
nv = nv0 + 0.01;
end
end
nv
This is the error I'm getting: Error in FlashCal (line 74) fnk = fnk + ((Zi(i) * (Ki(i)-1))/(nv(Ki(i)-1)+1)); Whre: fnk = sigma(i) Zi(Ki-1)/nv(Ki-1)+1 fnkd = - sigma(i) Zi(Ki-1)^2/(nv(Ki-1)+1)^2
Many Thanks

채택된 답변

Guillaume
Guillaume 2014년 9월 5일
As the error says an index must be integer and positive (what would be element number 2.8 of a matrix?) and you're using values of Ki as indices into nv when none the Ki values are integer.
Furthermore, even if Ki were integer, you'd get an "index exceeds matrix dimension" error. Since nv is a scalar, there's nothing to index (but element 1).
Change
nv(Ki(i)-1)
to whatever it should be.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by