Matlab iteration issue (I think :P)

  • Context: MATLAB 
  • Thread starter Thread starter roesbak
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB coding issue related to a project on "Multiphase Flow in Pipes." The participant is attempting to simulate pressure changes in a vertical pipe with air injected at the bottom and is encountering problems with the iteration in their code, specifically regarding the pressure drop calculations.

Discussion Character

  • Technical explanation
  • Debugging

Main Points Raised

  • André describes the setup of a simulation involving pressure changes in a pipe, detailing the assumptions made about the fluid dynamics and the parameters used in the code.
  • André expresses concern that the pressure drop calculated in the loop appears constant, regardless of variations in the mass flow rate (m) and liquid flow rate (QL).
  • One participant suggests that a specific question would help in providing assistance and shares the basic syntax for MATLAB for loops.
  • André reiterates the problem, questioning whether the variable "pfrict" updates correctly within the loop and noting that the results do not vary as expected.
  • A later reply recommends saving all variables at each time step for debugging purposes, suggesting that this could help identify where the issue lies in the calculations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specific cause of the issue, and multiple perspectives on debugging strategies are presented. The discussion remains unresolved regarding the exact nature of the problem in the MATLAB code.

Contextual Notes

There is uncertainty regarding the correct updating of variables within the loop, and the impact of the defined parameters on the simulation results is not fully explored. The discussion highlights potential debugging techniques but does not resolve the underlying coding issue.

roesbak
Messages
2
Reaction score
0
Hi there,

I am working on a project in a subject called "Multiphase Flow in Pipes." We are to make a "simulator", describing the pressure (step-wise) in a pipe inserted vertically into water. Air is injected at the bottom of the pipe with a given mass rate, m, and for simplicity, ideal gas, no slip conditions, and a given QL (liquid flow rate) are assumed. The pressure-losses taken into account are hydrostatic and friction only. (I can explain further on request, but my problem is, I hope, code-related, and not physics :P)

I started writing what I thought was a fairly simple piece of "code", and decided to test my program with one given m and QL. However, based on my graph, it looks as though the pressure-drop upwards in the pipe is constant, and does not change, no matter what m and QL I try.

I think I may have done something wrong in my "for" loop, but since I am very inexperienced with Matlab, I thought I would ask if anyone might give me a small pointer..

Code follows:
------------------------------

clf
% First, we set up the main constants
g = 9.81;
h = 50;
D = 0.15;
rhoL = 1000;
P(1) = 1*10^5 + (rhoL*h*g);
Ptop = 1.013*10^5;
Pref = 1.013*10^5;
nu = 1.5*10^-5;
viscL = 0.001;
rhoref = 1.2;


% Then we start entering the equations necessary to iterate.

dH = 0.5;
H = 0:dH:h;
NH = length(H);


m = 1;

ql = 0.001;

for i = 2:NH % Loop, upwards in the well.
% Calculate gas density and friction pressure drop

rhoG = rhoref * (P(i-1)/Pref); % density of gas

qg = (m/rhoG); % volumetric flow rate of gas

EG = (qg)/(qg + ql); % gas fraction

EL = (ql)/(qg + ql); % liquid fraction

rhom = (EL * rhoL) + (EG * rhoG); % density of gas/liquid mixture

viscm = (EL * viscL) + (EG * rhoG * nu); % viscosity of gas/liquid mixture

U = (ql+qg)/((pi/4)*D^2); % flow speed

phyd = rhom * g; % hydrostatic pressure loss

pfrict = (4/D) * 0.046 * ((rhom*U*D)/(viscm))^(-0.2); % frictional pressure loss

ptot = phyd + pfrict; % total pressure loss

P(i) = P(i-1) - (ptot*dH)

end


figure(1)
plot(H,P)
---------------------------------------

Thank you very much for your time.


Yours

André Røsbak
 
Last edited:
Physics news on Phys.org
Can you ask a specific question, I don't really feel like looking through all your code. I'm sure I can help if you ask a question pertaining to MATLAB though.

Maybe someone else will look through it though.

However, since you were talking about for loops. The syntax is

for i=a:h:b
end

a=start value
b=end value
h="step size"

so...

for i=1:1:3
i
end

would print:
1
2
3

and,
for i=1:0.5:2
i
end

would print,
1
1.5
2
 
Thank you for your reply.

I understand that you don't want to look at all of it, but I thought I should include it at least.

My question/problem is: My loop does not seem to be working. I think the problem is that my variable "pfrict" does not update itself for each step. Have I done something wrong when defining "prfict"? (i.e. does it "update" itself for each step, based on what I've written)

The graph I get is linear, i.e. the amount subtracted from the original pressure is the same, or at least veeeery similar, for each time step. This should not be the case. The results should at the very least vary for different m and QL inputs (defined in the lines just above where the for loop starts), but it does not.

Thank you.


- André
 
roesbak said:
My question/problem is: My loop does not seem to be working. I think the problem is that my variable "pfrict" does not update itself for each step. Have I done something wrong when defining "prfict"? (i.e. does it "update" itself for each step, based on what I've written)

The graph I get is linear, i.e. the amount subtracted from the original pressure is the same, or at least veeeery similar, for each time step. This should not be the case. The results should at the very least vary for different m and QL inputs (defined in the lines just above where the for loop starts), but it does not.

For debugging purposes you should save all your variables at each time step. E.g.
pfrict(i) = (4/D) * 0.046 * ((rhom(i)*U(i)*D)/(viscm(i)))^(-0.2);
That makes it much easier to trace backwards and find where the problem is coming from.
 

Similar threads

  • · Replies 45 ·
2
Replies
45
Views
7K
  • · Replies 20 ·
Replies
20
Views
12K
Replies
25
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 27 ·
Replies
27
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K