Can someone please debug my code?

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

No we can't, because we can't get it. Please format it properly (highlight and click the Code icon on the toolbar) so we can copy it easily with a single click. Also attach the m-file with the paperclip icon.
What inputs should we be using?
They are attached in a photo on the left side and the program prompts each value. (To convert ft to inches for the diameters just input 10/12 for example). The final results are on the right after however many iterations are completed.
How many lines are shared by two loops?
Please enter the number of nodes in the system:
Is line 1 of loop 1 a shared line?
... and so on.
It wouldn't hurt you to show us the exact inputs you use for each prompt.
You flagged this as a Duplicate. Which posting does it duplicate?
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 Dev) Restored edit

Sign in to comment.

Answers (1)

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

I recieve an error for displaying the %g in the input command. I recieve error "Error using input, the second argument to INPUT must be 's'."
Additionally, ID refers to the loop that the line is shared with. The formula I have been given only is to be applied when the loops share a common line and is defined to be Qnew = Q(loop) + (DELQ(loop) - DELQ(shared loop)). I called it Q however so that when the while loop runs, the new value of Q after it has been corrected is inputted again.
DELQ is found for each loop and is used for the correction factor according to the corresponding loop.
I hope this makes sense, its so hard to understands someone else thought process but I am impressed with the amount of revisions you did in such a short time.
NJ(loop)=input('\nEnter the number of lines in loop %g: ',loop);
should be
NJ(loop) = input(sprintf('\nEnter the number of lines in loop %g: ',loop));
That helped Walter, thank you.
Now I get the error "Conversion to cell from double is not possible. Error in Answer1 (line 47) ZL(loop)=zl;"
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?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 10 Oct 2021

Commented:

on 18 Oct 2021

Community Treasure Hunt

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

Start Hunting!