Lec 13
Lec 13
Lec 13
Okay, so let us continue. We are in the first lab of this course and we had already defined
the problem. The problem deals with the analysis of frictional losses when you have pipe
flows. We understood various aspects of the problem, what is given to us, what needs to
be calculated, what needs to be determined, why programming is needed for such a
problem and so on. And we also looked into fairly good detail into the theoretical
background and the associated formulae. So, what we have is now a set of problems.
So, the problem statement which is there is that we have to pump sulfuric acid through a
pipeline network. We have already understood various key aspects of such a problem. And
the first thing which we need to do is determine the head loss due to pipe friction. Now if
you consider this particular sub problem, then you have been very specifically asked the
head loss. So, what kind of an output should be presented? Well, it should be a single
number which is the head loss. Rest everything been given to you, you should be in a
position to use programming, to use the background theory and the formula or set of
formulae to determine the head loss. So, the output from this particular sub problem should
be just a single number. So, let us see the formula which we had with us for determining
the head loss was the Darcy Weisbach equation. Various quantities were specifically given
in this problem. There were two quantities which were not explicitly given which we would
need to calculate.
First the velocity, the flow through the pipe, we can back calculate and the friction factor.
Friction factor in fact, depended upon whether your flow is laminar or turbulent. In case of
lamina, you had straight away formula, which was only in terms of the Reynolds number.
So, Reynolds number also is not explicitly given, but you would need to calculate the
Reynolds number from d, v, ρ and μ. When it comes to flow which is turbulent, we said
that we had the Cole-Brooke correlation and the Cole-Brooke correlation involved both
Reynolds number as well as the roughness ratio, the roughness factor rather. So, once we
have calculated all of this, we can simply substitute all the known quantities of pre-
calculated quantities in the Darcy Weisbach equation and we should get one single answer.
So, look at the typical code which we can develop for such a problem. So, since this is the
first lab for all the sub problems for this lab, I would explicitly give you the complete code
to make the best use of this particular video course. It is highly advisable that you write
this code, maybe also vary this code on MATLAB and run the code. I mean reading this
code will be absolutely of no help or use.
So, for the first lab as it would happen in a typical lab course, you will not be given the
code, you will be expected to develop the code, but since this is the first lab and it is possible
that some of you might be doing MATLAB programming for the first time. In this week
for this particular lab, I would give you all the codes in the final form. So, let us see as we
have been emphasizing, it is a good coding practice to write in the beginning or to provide
in the beginning a comment or a comment block of what this code does. So, we have this
comment where it says that this code calculates the head loss due to pipe friction. Followed
by this we have three commands clc, clear and close all. What is the importance of these
three commands? clc will clear everything from the command window. So, it is possible
that you have been running different commands in your command window and when you
run this code, all of those would be cleared and you will have a clear screen. So, as to know
that you start with a fresh instance of your code running on your computer and if there is
any issue anywhere, you can find out where exactly which instance of the code was running
and from there it would be easier to find the problems out.
Second clear, clear is a command which would clear up your workspace. So, let us remind
ourselves of what the workspace does. Workspace stores all the variables which appear in
your coding. Now, it is possible that you have been using one particular session of
MATLAB for some time and it is possible that you have some of the variables already
stored in the workspace and it is very essential and also a commendable idea to make sure
that you start every you run all the code or run every code as a fresh instance. So, that the
previously stored values do not make a mess anywhere. So, clear, clears your workspace
and close all. Now, close all is a important and helpful command. So, close means closing
the plot. So, all of the windows which show one or the other graph, one or the other plots,
if they are already open, all of them would be closed. Because now this can be seen in an
anticipation that somewhere in solving this problem, it is possible that you might come
across plotting. Perhaps not directly in this particular code, but this problem you will be
making plots and therefore close all will close all the windows which already have the plots
with it and then start everything a fresh.
We said that the beginning of every code would be initialization of the variables and in the
present case, we have the Darcy Weisbach equation which means you will need the
diameter, you will need the value of acceleration due to gravity, you would need velocity,
you need friction factor and so on. We have already analyzed that, analyze what values of
the variables have already been given to us to solve this problem. So, for example, the mass
flow rate G has been given to us, this is in the problem. We are using SI units. So, we have
made conversions here. So, G which is the mass flow rate is here, g the acceleration due to
gravity, the diameter of the pipe, L/d ratio and so on. All of these have been given. In fact,
if you remember our discussion, then you will realize that you will need the value for the
density and viscosity and you will have to go back, look into the data tables, to punch in
those values here. The first calculation that you would need as I made a mention previously
is the velocity of flow. And since we know the mass flow rate which is given to us, we
have fetched the value of the density of sulfuric acid from the data tables. And we know
the diameter of the pipe. So, we would be in a position to calculate the cross-section of the
pipe.
So, with the help of these three values, the velocity in SI units in meter per second would
be calculated. So, you can see here u=G/Aρ. There is another quantity which is not given
to us directly that is the Reynolds number. So, again in the last line of the section where
you initialize and declare various variables is the formula for Re, Re = duρ/μ. So, this
section of your code is pretty straightforward. We realize that we have a formula and there
are various quantities in the formula. Majority of them have been given to us, velocity was
not given, but we could calculate using other quantities which was which were given to us.
And then there is only one quantity which is still not known and that is the friction factor.
So, why do we really need to do programming? This becomes apparent from here onward
that the friction factor depends upon whether your flow is laminar or turbulent. When the
flow is laminar the formula is very simple, simply 64 over Re and we already know the
value of Re.
But when the flow is turbulent we need to solve the Cole-Brooke equation which is the
correlation which is implicit non-linear equation and you not only have the friction factor
of function of Reynolds number, but also the roughness factor. So, therefore, now you have
a condition, right. The only thing which is not known is the friction factor, but which
formula to use depends upon whether your flow is laminar or turbulent. So, therefore, it is
pretty straightforward to imagine that you would need a conditional statement. So, that
conditional statement in the form of if, else if and end is in front of you. So, what are you
going to do? You are going to determine the value of a calculate the value of Reynolds
number and if the Reynolds number is less than 2200 then you would use the formula for
laminar. If it is greater than 2200 then you use the formula for turbulent flow which is the
Cole-Brooke equation.
Now, here we have assumed as I had emphasized previously that we understand that in
the transition region, the value of the friction factor is not very reliable. So, either you can
have a discontinuity say that we have analysis only up to 2200 and then 4000 onwards we
have the analysis or you can continue up to 2200 and then 2200 onwards, but then you
know make a note to yourself or to the user that you know if the Reynolds number in fact,
comes out to be in transition region put a warning that the results might not be very reliable.
So, after calculating everything which is not known, basically the velocity and the
Reynolds number the first thing now you first thing that you now need to do is determine
whether there the flow is laminar or turbulent and that is what you are going to do here
based on the value of Reynolds number less than 2200 more than 2200 and then we in the
previous week saw how we can declare functions right.
So, the way we declare functions is something which you can see within this conditional
statement. So, we have two formulae, one formula for calculation of the friction factor for
the laminar region the other formula for turbulent region and we have therefore, defined
two functions f =laminar(Re). So, the identifier for the function is lamina and then you can
see here in the comment generate f for lamina flow right. So, you see it is just determining
the value single value of the friction factor f and therefore, it is a good idea that you
calculate that value using a function. The second condition where your Reynolds number
is greater than 2200 again you need one value as the output that is the friction factor, but
using a different formula and that output that friction factor comes from the function
turbulent within the brackets Re and L/d ratio.
Again one needs to remember that for lamina flow friction factor is only a function of
Reynolds number and therefore, you would see f =laminar (Re) means that the function
lamina needs only one argument which is the Reynolds number. And whereas, for
Colebrook equation you need two quantities Reynolds number as well as L/d ratio. So, you
will need to this function will need two inputs. Now imagine that your code starts getting
executed firstly it will allocate all these variables and it is their values in the memory it
would calculate the velocity from velocity it will calculate the Reynolds number and after
you get the Reynolds number you will be in a position to decide whether use formula 1 or
formula 2 and once you know, let’s say I need to use formula 2, you will go to that function
and that function will give you the value of friction factor. Now at this stage you have
everything which must be supplied to the Darcy Weisbach equation so as to get the single
value output from your code which is the frictional loss and that you can see at the end,
h_f, hf which you want to calculate and that is the head loss, that is the output and then you
will see that before you come to an end, hf = f*(L/d)*(u2/2g). Rest everything has been
supplied with a semi colon at the end right.
It is only this particular expression at the last in this code which does not have this semi
colon which means that the value of this variable would be printed on the screen. This
makes sense because what we have been asked is to determine the value of the head loss
and therefore, the screen should print only the head loss not unnecessary details unless we
ask for the details it should not print such details. So, now the only thing which is left in
this whole code is the actual definition of laminar (Re) function and turbulent Re/d
function. So, how would you typically do this? So, let us remember what is the way to do
this. We used to define all the functions at the end right. So, our typical code used to have
an initial set of instructions which would declare the variables initialize the variables then
we used to have that main part of the code where variable manipulations would be done
and so on.
And then once this whole business of variable manipulation is over, where we now wanted
to actually do calculations we used to declare functions right. So, looks like, the declaration
of function should follow the hf expression which is the last line right. That is one way of
doing it, that I start declaring my functions lamina and turbulent after the hf expression
which is given to you. However, we would resort to an alternative way of declaring these
functions. So, let me show you what else can be done I make a function file okay. So, what
is the difference that we have done we have not written that function and we have not
declared that function at the end of the code, rather we have made a function file and
because it is a file again it is a good idea to tell what is there in the file. So, therefore, we
have a comment block.
This function file provides f for lamina flow it requires only one input which is the
Reynolds number followed by the general syntax for declaration of function right. So, f =
laminar where the only input argument would be the Reynolds number Re and the formula
for f is simply 64/ Re and you end the function there. So, starting from function and ending
at end these three lines could have been pasted in the main function which is there here at
the end. Instead of doing that what we have done is we have saved this as a file and we
have given the file the name laminar.m. Moreover, this lamina.m means this is again a
MATLAB file is saved in the same directory where you save your native code right. So, if
you are saving this code in directory named say ABC within the same directory you save
this lamina dot m which has nothing, but simply the definition of the function.
So, why you have we done this what is the advantage? We will come to that point a little
later. Now this function file would be invoked only when the code sees that the Reynolds
number is less than 2200. When the Reynolds number is greater than 2200 you need to use
the other formula and therefore, in an analogous manner we can we can write a file called
turbulent.m. Again you will save that in the same file. So, now what does this turbulent dot
m function file do? We have a comment block where it tells that this is this file will give
you the function which you solve for the Cole brook equation it will need two in arguments,
Reynolds number and L/d ratio fine. But then what has been done here and something
which is new and perhaps more important than anything else which you will see here is,
one inbuilt solver which is f solve. So, the first step is that you declare what your function
is right and what your function is basically your Cole brook correlation.
So, f is equal to the entire right hand side. So, you take the right hand side to the left hand
side equate it to 0 now you have a non-linear equation and you want to solve for it right.
So, you are solving for this function basically which means you are determining the zeros
of this non-linear function right. So, what is done in the first step as you would see within
the declaration of function is in fact, you tell. So, if your equation is f(x)= 0 you specify
what f(x) is right and then in the second step you solve for f of x is equal to 0 you want to
you need to determine the zeros of this equation.
So, that is done by an inbuilt solver which is invoked by this command f solve, f s o l v e
right and within this bracket of fsolve, you need to specify two things. One what is the
function or what is the equation that you want to solve for which you can see here and then
this solution technique is going to be an iterative technique which means that l your
computer will need an initial guess right that initial guess obviously, will not be your
solution. So, there would be a metric the distance metric which will be calculated iteratively
after every step when you get, say an improved solution and this distance metric is expected
to reduce or decrease as the iterations progress. And therefore, when you solve for this non-
linear equation, you have at the end of this statement 0.15 right. So, what is that 0.15? That
is the initial guess for your friction factor right.
Now, this is important has this initial guess 0.15 been given just randomly? well no it is
never a good idea to function just random value, but this 0.15 has been given by looking at
your moody chart you know the range of values of the friction factor and therefore, from
those that range you choose one value right fine. So, now, what we would do is we would
save this function file as turbulent.m. When you attempt to run this code on MATLAB I
would encourage you to do this, you will see that you have in the command window. So,
when you run this code in the command window, you will you can see at the bottom hf is
equal to 0.0716. Now, you need to do two things, not only do you need to make this code,
run this code, but also solve this problem analytically by pen and paper,which means that
you will need to use the moody chart to determine the friction factor and then substitute
that value of friction factor in the expression for hf. You will see that you will get value
which is very close to in fact, this value which you get from computations would be very
close to what you would determine by hand analytically. So, this is the first exercise which
I would expect you to do for the sub first subpart of the problem you already have the entire
code with you and you also have the output that you would expect.
Then you can also see that on the right hand side your workspace has been populated by
all the variables you know, area, diameter L/d ratio, friction factor everything has been
populated, it is right there right. So, if you make the code and execute the code successfully
your single output from the code would be just one number for this particular problem for
the given set of variables, you must get the answer as 0.07 meter. So, now what are some
new lessons that we have learnt here? First of all, we learnt to use f solve. So, whenever
you have a non-linear algebraic equation you can use f solve solver, you can invoke f solve
solver from MATLAB to solve your problem ok. How does it work? What is the what are
the underlying algorithms we will go into a little bit more details into when we discuss the
next problem.
We also learnt declaring and calling now it is clear from here, global functions which
means that these functions not very specific are not specific to the problem or the code
which you have open in your right in your terminal. But if as long as you save those in your
system in the same directory those functions can be called. So, what do you need to do save
the functions in as dot m files and then save them in the same directory as the as where you
have the main code fine. So, now the second problem is to examine the effect of Reynolds
number on frictional head loss.
We will take this particular sub problem in the next lecture, but then we can kind of a make
a strategy of solving this. So, we have been given two ranges for Reynolds number 500 to
1500 which means that the flow is laminar, second 5000 to 25000 which means that the
flow is turbulent. Now, in the previous problem your output was supposed to be just one
number right because you were asked to determine head loss. Here in the second sub
problem you are not asked to determine the head loss rather you want to know, that if I
change the Reynolds number how does my frictional head loss change which means you
need to do the calculation. You cannot just do this calculation once, but you will have to
do calculations multiple times over these two separate ranges and that makes a pretty good
case of making a code or solving this problem by the code.
That if you analyze the first sub problem well you do not necessarily need to make a code
for that and I also asked you to rather encourage you to solve the same problem with pen
and paper which means the problem can be fairly trivially solved using a pen and paper.
But for second problem you will in fact, appreciate that coding would help. So, we will
take up this second problem in the next lecture we will develop the strategy for solving this
problem and we will also then write the code, discuss the code, and we will also see what
is the advantage of various strategies that we have been adopting for solving the problems.
Till then good bye.