Can someone please debug my code?
3 views (last 30 days)
Show older comments
I am designing a program to evaluate an iterative calculation of flow rate (Q) within a given network of tubing specified by (loop, line). For some reason my code will not execute the iterative process correctly using the changing values of Q from every iteration. The program without the while loop works to find the refined value of Q. These values need to change to the revised for mom with every iteration until complete. When I run the program it endlessly runs and never hits the value it should be below(0.0001). I am providing the values to input as well as the code in the attached file. The results should reach a final answer after some number of iterations which is shown on the right side of the attached figure. Thank you, I’m so beyond stuck.
(Edit: Somehow after putting all the inputs in without the inputs has caused the code to converge after 27 itterations. I didnt change anything besides eliminating the inputs. How can this be? Please help me find the errors in my solution to further refine my results for the head loss section. Im not sure how I could have accurate values for Q with incorrect head losses when they are calculated as functions of each other.)
When I try to use the HELP2 file with the variables HW it does not stop itterating and im not sure why.
8 Comments
Rik
on 12 Oct 2021
Unfortunately attached files can't be retrieved from the Google cache (nor from the Bing cache), so only the original unformated version can be restored from there.
Answers (1)
David Hill
on 10 Oct 2021
You need to use some cell arrays since the number of lines is different in the loops. Also the calculation for Q has something wrong with it. DELQ is scalar for the loop, but ID is an array for each loop. DQLQ(ID{loop}) makes no sense.
clear;
IW = 1;
NL=input('Please enter the number of loops in the system: ');
while NL>10
fprintf('\nA maximum number of 10 loops is allowed. Try again.\n\n');
NL=input('Please enter the number of loops in the system: ');
end
NJ=zeros(1,NL);
for loop = 1:NL
NJ(loop)=input('\nEnter the number of lines in loop %g: ',loop);
while NJ(loop)>10
fprintf('A maximum number of 10 lines is allowed. Try again.\n');
NJ(loop)=input('\nEnter the number of lines in loop %g: ',loop);
end
end
shared = input('\nHow many lines are shared by two loops? ');
nodes = input('\nPlease enter the number of nodes in the system: ');
asum = ((sum(NJ) - shared) - nodes + 1);
fprintf([num2str(asum),' assumption(s) for flow rate must be made in order to solve for the flow rate distribution.\n']);
ZL=cell(1,NL);
D=cell(1,NL);
Q=cell(1,NL);
Shared1=cell(1,NL);
ID=cell(1,NL);
alpha=cell(1,NL);
HF=cell(1,NL);
DHDQ=cell(1,NL);
DELQ=ones(1,NL);
PH=cell(1,NL);
for loop = 1:NL
zl=zeros(NL,loop);
d=zeros(NL,loop);
q=zeros(NL,loop);
shared1=nan(NL,loop);
id=zeros(NL,loop);
for line = 1:NJ(loop)
zl(loop,line)=input('\nEnter the length (in feet) of line %g in loop %g in feet: ',line, loop);
d(loop,line)=input('\nEnter the diameter (in feet) of line %g in loop %g in feet: ',line, loop);
q(loop,line)=input('\nEnter the flow rate of line %g in loop %g in CFS (if unknown make assumption now): ',line, loop);
shared1(loop,line)=input('\nIs line %g of loop %g a shared line? Enter y of n: ',line, loop,'s');
if lowercase(shared1(loop,line)) == 'y'
id(loop,line)=input('\nEnter the loop that line %g of loop %g is shared with: ',line, loop);
end
end
ZL(loop)=zl;
D(loop)=d;
Q(loop)=q;
Shared1(loop)=shared1;
ID(loop)=id;
end
coef = input('\nPlease enter the given coefficient c: ');
while abs(max(DELQ)) > 0.001
IW = 1 + IW;
for loop = 1:NL
alpha(loop)=39.2740 * ZL{loop}./(pi^1.85*coef^1.85*D{loop}^5.35);
i=Q{loop}<0;
HF{loop}=zeros(size(Q{loop}));
HF{loop}(i)=alpha{loop}(i).*(-abs(Q{loop}(i))^1.85);
HF{loop}(~i)=alpha{loop}(~i).*Q{loop}(~i)^1.85;
DHDQ{loop}=1.85*alpha{loop}.*abs(Q{loop})^1.85;
DELQ(loop)=-sum(HF{loop})/sum(DHDQ{loop});
end
for loop = 1:NL
idx1=abs(DELQ{loop})>0.001;
idx2=Shared1{loop}=='y';
Q{idx1&idx2}= Q{idx1&idx2}+(DELQ{idx1&idx2}-DELQ(ID{idx1&idx2}));%these lines do not make sense to me
Q{idx1&~idx2}= Q{idx1&~idx2}+(DELQ{idx1&~idx2}-DELQ(ID{idx1&~idx2}));
end
end
for loop = 1:NL
PH{loop}= alpha{loop}.*HF{loop};
end
5 Comments
Walter Roberson
on 10 Oct 2021
ZL{loop}=zl;
D{loop}=d;
Q{loop}=q;
Shared1{loop}=shared1;
ID{loop}=id;
However... you are creating things like zl incorrectly.
You have
for loop = 1:NL
zl=zeros(NL,loop);
The assignment to zl is inside for loop so zl is not an overall variable: it is a variable that has to do only with data for this particular value of loop . So why should it gain one extra column for each iteration of loop ?
for line = 1:NJ(loop)
zl(loop,line)=input('\nEnter the length (in feet) of line %g in loop %g in feet: ',line, loop);
You assign into zl columns up to the last value of line, which is NJ(loop) . It makes sense that for this particular value of loop that zl might need one column for each line that is involved . But if so then instead of initializing zl=zeros(NL, loop) you would be closer to initializing zl=zeros(NL,NJ(loop)) .
Now have another look at your code. Suppose you have made it to loop = 3. The you allocate zl=zeros(NL,3), corrected to zl=zeros(NL,NJ(3)) . You then loop over line values, assigning to zl(loop,line) which would in this case be zl(3,line) . Then you store zl away in the cell array. Okay -- so you allocated NL rows to zl, but where did you write into any row other than the one indexed by the current loop number?
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!