what is wrong with this while loop?

function [Gj,Lj,Xij,Yij,Tj]=SR(N,Tj,Fl,Fg,Zij,Qj,TF,Pj)
%inicio
Fj=Fl+Fg;
[Gj,Lj]=MetodoSR(N,Fg,Fl);
Tj0=0;
k=0;
while (((Tj-Tj0)/Tj)^2)>=1e-10 && k<=100
k=k+1;
for k=1:N
Tj0=Tj(N-1);
Kij=ctteK1(Pj,Tj,N);
%Conversion de C a F
Xij=MatrizxijT(N,Fg,Fl,Gj,Kij,Zij);
[Lj,Gj,Xij,Yij]=Flujos(N,Lj,Xij,Fg,Fl,Kij);
Tj=Tj*1.8+32;
TF=Tj*1.8+32;
%Conversion de kmol a lbmol
[MatrizTH]=TH(N,Tj,Xij,Yij,Lj,Gj);
[HFlj,HFgj]=THFeed(N,TF,Zij);
[Tj]=Balance(N,Tj,Yij,Xij,Lj,Gj,Fj,HFlj,HFgj,Qj,MatrizTH);
end
end

댓글 수: 2

madhan ravi
madhan ravi 2018년 9월 6일
편집: madhan ravi 2018년 9월 6일
provide all the datas for (N,Tj,Fl,Fg,Zij,Qj,TF,Pj) and all the functions and paste it here
There are way too many functions. The code used to run normally until I added the loop, meaning I added this:
Tj0=0;
k=0;
while (((Tj-Tj0)/Tj)^2)>=1e-10 && k<=100
k=k+1;
for k=1:N
Tj0=Tj(N-1);
...
end
end
Also, Tj is a vector.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 9월 6일

0 개 추천

k=k+1;
for k=1:N
The second of those two lines overwrites the variable 'k' referred to in the first of the lines. After the for loop, k will be left at the last value from the for loop, which is to say N . The while loop will be testing that value against 100. If N < 100, then k will never exceed 100.

댓글 수: 6

Giulliana Marin
Giulliana Marin 2018년 9월 6일
What should I do to fix it? delete the for? I just want the loop to run while (((Tj-Tj0)/Tj)^2)>=1e-10 && k<=100
Walter Roberson
Walter Roberson 2018년 9월 6일
We do not know if you want Tj0 to be repeatedly calculated for each k value. But yes, probably you should be deleting the "for k" loop.
Giulliana Marin
Giulliana Marin 2018년 9월 6일
Well, the main variable is Tj, so the loop should continue running until the difference between Tj and the previously calculated Tj (I wrote it as Tj0) is less than 1e-10. Actually, I made a mistake because I had written >= and it's <=. It still doesn't work though
for k = 1 : 100
Kij=ctteK1(Pj,Tj,N);
%Conversion de C a F
Xij=MatrizxijT(N,Fg,Fl,Gj,Kij,Zij);
[Lj,Gj,Xij,Yij]=Flujos(N,Lj,Xij,Fg,Fl,Kij);
Tj=Tj*1.8+32;
TF=Tj*1.8+32;
%Conversion de kmol a lbmol
[MatrizTH]=TH(N,Tj,Xij,Yij,Lj,Gj);
[HFlj,HFgj]=THFeed(N,TF,Zij);
[Tj]=Balance(N, Tj, Yij, Xij, Lj, Gj, Fj, HFlj, HFgj, Qj, MatrizTH);
if abs((Tj-Tj0)/Tj) <= 1e-10; break; end
Tj0 = Tj;
end
Giulliana Marin
Giulliana Marin 2018년 9월 6일
I get this: Undefined function or variable 'Tj0'
That should not be the case, since you had
Tj0=0;
before your loop.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 9월 6일

댓글:

2018년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by