# Matlab iteration issue (I think :P)

• MATLAB
• roesbak
In summary, the conversation is about a project involving a "simulator" for Multiphase Flow in Pipes. The person is having trouble with their code and is specifically asking for help with their for loop. They provide their code and explain the problem they are facing. They also ask for a small pointer and in the end, thank the person for their time.
roesbak
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:
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

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.

## 1. What is an iteration in Matlab?

An iteration in Matlab refers to the process of repeating a set of instructions or calculations multiple times in order to achieve a desired result. It is a fundamental concept in programming and is often used in various algorithms and simulations.

## 2. How do I perform iterations in Matlab?

To perform iterations in Matlab, you can use a variety of control flow statements such as "for" loops, "while" loops, and "do-while" loops. These statements allow you to repeat a block of code for a specified number of times or until a certain condition is met.

## 3. What is the difference between a "for" loop and a "while" loop in Matlab?

A "for" loop in Matlab is used when you know how many times you want to repeat a set of instructions, whereas a "while" loop is used when you want to repeat a set of instructions until a certain condition is met. In other words, a "for" loop is used for a predetermined number of iterations, while a "while" loop is used for an uncertain number of iterations.

## 4. How can I troubleshoot iteration issues in Matlab?

If you are experiencing issues with your iterations in Matlab, the first step is to check your loop conditions and make sure they are correct. You can also use the "disp" function to display the values of your variables at each iteration to help identify any potential errors. Additionally, you can use the debugger tool in Matlab to step through your code and see where the issue may be occurring.

## 5. Can I use recursion for iterations in Matlab?

Yes, it is possible to use recursion for iterations in Matlab. Recursion is a technique where a function calls itself repeatedly until a certain condition is met. However, it is important to be cautious when using recursion as it can lead to infinite loops if not implemented correctly.

• General Engineering
Replies
25
Views
3K
• Mechanical Engineering
Replies
20
Views
8K
• Classical Physics
Replies
21
Views
1K
• Mechanical Engineering
Replies
8
Views
967
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
• Introductory Physics Homework Help
Replies
11
Views
1K
• Engineering and Comp Sci Homework Help
Replies
27
Views
4K
• Introductory Physics Homework Help
Replies
6
Views
1K
• Introductory Physics Homework Help
Replies
4
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
3K