Professional Documents
Culture Documents
Peter P. Valko
Contents
I Introduction
4.1.1 Bisection .........................................................................................................
0. 2 Resources 4
29
6
4.1.2 False position ..................................................................................................
0. 3 Course structure 29
7
II Tools 8
1 Introduction to Excel and VBA 9
1.1 Starting Excel, saving a file ......................................................................................... 9
1.2 Workbook, worksheet, views, printing ........................................................................ 9
1.2.1 Review ............................................................................................................ 9
1.3 Spreadsheet basics ....................................................................................................... 9
1.4 Graphing with Excel .................................................................................................... 10
1.5 Numerical methods inside Excel .................................................................................. 10
1.6 Database Management with Excel .............................................................................. 11
1.7 Excel and the WWW .................................................................................................... 11
1.8 Keyboard macros and what they cant do ..................................................................... 11
1.9 VBA, variable, sub, function, module ........................................................................... 11
1.10 Structured Programming: Subs, Decisions and Loops ................................................. 17
2 Mathematica 21
1
Appendix 64
7 Study4.1.3
guide Newtons method ............................................................................................ 65 30
7.1 Be able to ..................................................................................................................... 65
4.1.4 Optimization: Golden ratio search for a mnimum ........................................ 32
8 Assignments, testiteration
4.2 Fixed-point problems as a general framework for iterative processes . . . . 68 32
4.3 Representing, manipulating
8.1 Error propagation: borehole volume functions .........................................................................
............................................................................ 33
68
4.3.1 propagation:
8.2 Error Numerical one-gallon
Differentiation cube of functions that can be evaluated every- where ..
............................................................................. 33
68
4.3.2 Numerical Integration ....................................................................................
8.3 Error propagation: barrel ............................................................................................ 34
68
4.3.3 propagation:
8.4 Error Simpsons 1/3 rule ...........................................................................................
hydrocarbon volume ..................................................................... 35
68
4.4
8.5 Interpolation, Smoothing,
Root finding; Newton Curvez fit,
iteration: Least
factor squares ....................................................
....................................................................... 36
69
8.6 4.4.1 Leastsolution
Root finding: Squaresofand theitsPRNumerical
equation of Aspects ......................................................
state ....................................................... 37
69
4.5 Root
8.7 Numerical solution
finding: of ordinary
heat balance differential equations ................................................
........................................................................................... 39
70
8.8 4.5.1 Basicfriction
Root finding: methods for solving
factor ODE .......................................................................
......................................................................................... 39
70
8.9 Root finding: Well deliverability ................................................................................. 71
5 Methods related to multi-variable problems 41
8.10 Root finding: isoterm flash .......................................................................................... 71
5.1 Matrices, vectors .......................................................................................................... 41
8.11 Optimization: Maximizing Net Present Value ............................................................. 72
5.2 System of linear equations .......................................................................................... 43
8.12 Integration of a function: PV ........................................................................................ 73
5.3 LU Decomposition and Backsubstitution ..................................................................... 46
8.13 Numerical integration of discrete data: pore volume .................................................. 73
5.4 System of nonlinear equations, Newton-Raphson method .......................................... 50
5.5 Multivariable extrema ................................................................................................ 53
5.6 Solution of system of ordinary differential equations: RK4 ......................................... 54
5.7 Partial differential equations and reservoir simulation ............................................... 55
5.7.1 Reservoir Simulation ..................................................................................... 59
2
Tables 81
3
Part I
Introduction
4
0.1 Course Objectives
Learn the underlying principles behind some commonly used numerical methods.
Recognize the main features of a mathematical problem arising in a technical con- text.
Single- or multi-variable
Linear or non-linear
Involves algebraic, differential or integral equations
Direct or iterative technique should be applied
Explicit or implicit scheme is appropriate
Develop skills to formulate a problem starting from physical principles.
5
0.2 Resources
The concepts and methods of technical computing are summarized in the excellent book of Chapra
and Canale: Numerical Methods for Engineers (CC). In order to make efficient use of our time, we
request you to read the material before the lecture. Any similar book can be used, however.
The Visual Basic for Applications (Excel) program development environment will be available in
the lab. Whenever you are asked to solve a problem by computer, we request an Excel Workbook with
Visual Basic source code and results. Excel and VBA for Excel has a vast literature.
We will give you the opportunity to experiment with many of the numerical methods using a
Living Textbook, developed particularly for this course. To make full use of the Living Textbook you
will need a software system called Mathematica. This course, however, does not require you to learn
how to program in Mathematica. In fact, to run any portion of the Living Textbook you will need
minimum knowledge which can be acquired in 5 minutes.
Many of the specific petroleum engineering problems also will be provided in the form of Living
Textbook. You may experiment with them and may use the results for checking your Visual Basic for
Applications (VBA) program results.
Basic References
1) S.C. Chapra and R. P. Canale : Numerical Methods for Engineers, McGraw Hill, any edition,
any year (or any similar title)
6
0.3 Course structure
1. Engineering problem solving tools and programming
(a) Engineering problem solving approaches, software tools
(b) Excel
(c) VBA
(d) Structured and Modular Programming
2. Basics of numerical methods
(a) Taylor polynomial, Numerical errors, Error propagation
(b) Iteration, Convergence, Order, Stability, Sensitivity
(c) Classification of problems and methods
3. Methods related to single variable problems
(a) Roots of equations and extrema of functions Bisection; False position; Newton; golden
ratio search
(b) Numerical differentiation and integration Finite difference; Trapezoid rule; Simpsons
rule
(c) Interpolation, Smoothing, Curve fit, Least squares
7
Part II
Tools
8
Chapter 1
To save a workbook:
On the File menu, click Save As.
In the File name box, type a new name for the workbook.
In the Save as type list, click a file format (if you want other than .xls)
(In general, it is useful to click the right mouse button. Most of the time it will offer a menu item you
really want.)
9
Relative references A relative cell reference in a formula, such as A1, is based on the relative
position of the cell that contains the formula and the cell the reference refers to. If the position of the
cell that contains the formula changes, the reference is changed. If you copy the formula across rows
or down columns, the reference automatically adjusts. By default, new formulas use relative
references. For example, if you copy a relative reference in cell B2 to cell B3, it automa tically adjusts
from =A1 to =A2.
Absolute references An absolute cell reference in a formula, such as $A$1, always refer to a cell in
a specific location. If the position of the cell that contains the formula changes, the absolute reference
remains the same. If you copy the formula across rows or down columns, the absolute reference does
not adjust. By default, new formulas use relative references, and you need to switch them to absolute
references. For example, if you copy a absolute reference in cell B2 to cell B3, it stays the same in
both cells $A$1.
Import a text file Click the cell where you want to put the data from the text file. To ensure that
the external data does not replace existing data, make sure that the worksheet has no data below or
to the right of the cell you click. On the Data menu, point to Import External Data, and then click
Import Data. In the Files of type box, click Text Files. Check out also: On the Data menu: Text to
Columns.
Creating an XY (Scatter) Plot You can create a chart on its own sheet or as an embedded object on
a worksheet. You can also publish a chart on a Web page. To create a chart, you must first enter the
data for the chart on the worksheet.
Arrange your data in columns, with x values in the first column and corresponding y values
and/or bubble size values in adjacent columns.
Then select that data and use the Chart Wizard to step through the process of choosing the chart
type XY and the various chart options, or use the Chart toolbar to create a basic chart that you can
format later.
Best-fit equations may be calculated for chart data via the Add Trendline dialog. Trendline
equations are displayed by checking the correct box under the Options tab Errors bars can be added
to a chart by left clicking on a data series, and accessing the Format Data Series dialog box
Goal Seek The Goal Seek dialog box can be used to solve for roots of a single equation. An initial
guess is needed. Goal Seek performs calculations which improve the solution accuracy.
10
Solver The Solver can be used to solve both linear and nonlinear optimization problems,
i. e. maximize or minimize an objective function subject to known constraints.
The Solver Parameters dialog box refers to the following: Cell for the objective function value
(which is to be maximized or minimized) Cells for the the values of the indepen- dent (decision)
variables If the values of the independent variables are subject to the constraints, then cells
constrained.
(a) Mathematics
(b) Data type conversion
11
(c) String manipulation
(d) File system
2. Ability to control all of Excels features through the Microsoft Excel Objects, in- cluding
3. Ability to add custom functions which can be used like built-in Excel functions
4. Capabilities allowing the development of event driven, user friendly programs through a
variety of interface tools
5. The ability to extend the language through object oriented programming with the use of Class
Modules
Example: VBA011 program (After Hahn): If a stone is thrown vertically upward with an initial
speed u, its vertical displacement s after a time t has elapsed is given by a simple formula. In this
formula, g is the acceleration due to gravity. Air resistance has been ignored. We would like to
compute the value of s, given u and t. Note that we are not concerned here with how to derive the
formula, rather we want to compute its value. The logical preparation of this program is as follows:
0. Place the values of g, u and t into variables which can be used for calculations
3. Stop
This plan may seem trivial to you, and a waste of time writing down. Yet you would be surprised
how many beginners, preferring to rush straight to the computer, try to program step 2 before step
1.
It is well worth developing the mental discipline of planning your program first-if pen and paper
turns you off why not use your word processor? You could even enter the plan as comment lines in
the program.
Option Explicit
Const g As Double = 9.81 gravitational acceleration, m/s~2
Sub VBA011() vertical motion under gravity Dim s As Double
vertical displacement, m Dim t As Double time, s Dim u
As Double initial velocity, m/s
12
u = 30
t=6
s=u * t - ( g / 2 ) * t ~ 2
MsgBox "s = " & CStr(s) &" meters." s is converted to a string End Sub
1. From a new Excel workbook, use the Tools, Macros, Visual Basic Editor menu choice to start
the VBA integrated development environment (includes editor, compiler, help and debugger).
2. In Project window of VBA, right click on your new Excel workbook (usually VBAProject
(Book1)) and insert a new module.
3. Cut and paste the text of VBA011, shown in the above text box into the newly created module.
Note that the Option Explicit is not part of the program, but is a module option. Note the
colors of comments (green), language elements (blue), and names (black).
4. Left click to place the cursor anywhere inside the subroutine, and use the menu to choose Run,
Run Sub/User Form.
5. If you have questions on the language elements, shown in blue, place the cursor on the word
and hit the F1 key.
Some remarks:
4. The strange way of declaring g makes it a named constant, (its value cannot be changed). The
fact that it is placed outside the subroutine makes it valid in the whole module (scope:
module).
5. There is no input. All needed data are hard wired into this simple subroutine. (Later we will
learn how to read from the worksheets.)
6. Output is done using a message box. Later we learn how to put results into the worksheets.
7. This module contains only one subroutine (macro). In fact many of them can be placed into one
module.
13
Example: Sin function
Returns a Double specifying the sine of an angle. Syntax: Sin(number)
The required number argument is a Double or any valid numeric expression that expresses an angle
in radians.
Remarks: The Sin function takes an angle and returns the ratio of two sides of a right triangle. The
ratio is the length of the side opposite the angle divided by the length of the hypotenuse. The result
lies in the range -1 to 1. To convert degrees to radians, multiply degrees by pi/180. To convert
radians to degrees, multiply radians by 180/pi.
Those functions are useful, but they are not customizable. Even if you find a Visual Basic function
that is this close to what you need, you cant worm your way into the innards of Visual Basic to
change the way it works. You can, however, create a function of your own.
A great number of other functions are also available. For instance, both Excel and Visual Basic have
functions that return a random number between 0 and 1. The Excel function is named Rand(), and
the Visual Basic function is named Rnd(). You can use the Excel function in a worksheet cell and
(with some tricks) also in your VBA macro, but you can use the Visual Basic function only in a
macro.
Data Types The following table shows the supported data types
Arrays of any data type require 20 bytes of memory plus 4 bytes for each array dimension plus the
number of bytes occupied by the data itself.
Note:
will declare b as an Integer, but a will be the default (Variant). You have to write
14
if you wish to declare both as integer.
Numerics VBA makes a clear distinction between integer and real. Integers are used for counting
things. Real numbers include a fractional part. Real numbers stored as integers are rounded to the
nearest integer.
z = p*v/(R*T)
Strings To convert any other value to a string, use the Cstr intrinsic function.
This will output date and time into cells A4 and A5 of Sheet2:
Sub VBA014()
Dim d As String, t As String d = Date() Intrinsic function t = Time() Intrinsic function Display the result
With ThisWorkbook.Worksheets("Sheet2")
.Cells(1, 4)= d
.Cells(1, 5)= t End With
End Sub
(Objects have properties and methods associated with them. For instance, when you are
formatting a cell, you change some of its properties using some methods available.)
Advanced topics: Variable scope, lifetime, visibility The following key words are important:
Static, Public, Private.
The scope of a variable is from where it can be reached/altered. Variables declared in a
procedure can be used only within the procedure (local variable for the procedure).
15
Variables declared on the module level can be used in the entire modul in any of the
procedures within the module (global variable for the module).
Static variables keep their value between two occasions of entering into their scope. In
other words, their lifetime is not limited to one call. In general, a procedure-level variable
looses its value between two calls, unless it is declared Static.
Both module-level variables and all procedures are Public, by default, meaning that
they are visible even from other modules (or even other applications.) You can make
them Private, and then they are visible only within the module.
We will often use module level variables (globals) to simplify engineering problem
solving, when some data are needed in various parts of the program.
Errors, Debugging and Testing Compilation errors are errors in syntax and construc-
tion, like spelling mistakes, that are picked up by the compiler during compilation, the
process whereby your program is translated into machine code. They are the most fre-
quent type of error. The compiler prints messages, which may or may not be helpful,
when it encounters such an error. Generally, there are three sorts of compiler errors:
1. Ordinary errors-the compiler will attempt to continue compilation after one or more
of these errors has occurred, e.g. missing End If statement
2. Fatal errors-the compiler will not attempt further compilation after detecting a fatal
error, e.g. Program too complicated-too many strings
3. Warnings-these are not strictly errors, but are intended to inform you that you
have done something unusual which might cause problems later, e.g. Expression
in If construct is constant or variable has not been assigned a value but used, or
variable has not been declared (if implicit typing is used)
Some typical errors:
g = 9,81
x = 1 + (2 * 3
.Cell(2,1)=5
If (a=b) comma instead of decimal point
x=3 unpaired parentheses
Else Wrong function name: Cell instead of Cells Missing
x=4 then
End If
If a program compiles successfully, it will run. Errors occurring at this stage are called run -time
errors, and are invariably fatal, i.e. the program crashes. An error message, such as
16
2. Overflow
generated.
Overflow is quite common. It occurs, for example, when an attempt is made to compute a real
expression which is too large, or when you divide by a previously not assigned variable, which might
be almost zero, because it contains some trash.
Negative argument to Sqr or non-positive argument to Log is often the result of a previous logical
error.
At times a program will give numerical answers to a problem which appear inexplicably different
from what we know to be the correct mathematical solution. This can be due to rounding error, which
results from the finite precision available on the computer. , e.g. two or four bytes per variable,
instead of an infinite number.
Watch the Immediate Window! You will find that you need to crash the program to stop, e.g. with
ctrl-break. The reason is: x never has the value 0.2 exactly, because of rounding error. In fact, x
misses the value of 0.2 by about 10 -9.
Now try this:
If (abs(sumx - 0.2) < 1e-6) Then Exit Do
Programming is not about avoiding errors from the beginning. In fact that would be a lmost
impossible. Real programming is about localizing your errors by debugging and correcting them.
Check your work 100+1 times. Use test problems. Print out intermedate results. Investigate
limiting cases.
Using a Subroutine Subroutines are an important component of top down program design.
Subroutines allow the programmer to build and test program procedures that perform a single, well
defined task. This can make complex programs easier to design, test, and debug.
All executable code must be contained in a procedure. Procedures cant be nested within other
procedures.
17
Example Let in your Excel workbook, Sheet3 contain the following: Cells A1 and B1 should
contain the X and Y coordinates of a point. Cells A2 and B2 should contain the X and Y coordinates
of a second point.
This program will read the coordinates of the two points and calculate the slope and intercept of the
unique straight line defined by the two points, and output the results in a message box. These two
subroutines should be contained in a brand new module within your VBA project. Because the
My_Line subroutine has arguments, it cannot be executed directly, like the previously discussed
subroutines, by using the VBA Run menu.
Option Explicit
Private Intercept As Double
Sub VBA015()
Dim X1 As Double, X2 As Double, Y1 As Double, Y2 As Double Dim Slope As
Double Get the two points from the worksheet, "Sheet3":
With ThisWorkbook.Worksheets("Sheet3")
X1 = .Cells(1, 1)
Y1 = .Cells(1, 2)
X2 = .Cells(2, 1)
Y2 = .Cells(2, 2)
End With
Note: Watch for scope of the various variables! Intercept is a module level variable, but Slope is not.
Intercept cannot be seen from other modules.
18
Loops: the heart of programming math
Countable: Do the block for i = 1, then i = 2 , etc.:
For i = 1 To 10
"block... "
Next i
Select Case structure makes your code readable Use the Select Case statement as an
alternative to using ElseIf in If...Then...Else statements when comparing one expres- sion to several
different values. While If...Then...Else statements can evaluate a different expression for each ElseIf
statement, the Select Case statement evaluates an expression only once, at the top of the control
structure.
Select Case performance Case 1
bonus = salary * 0.1 Case
2, 3
bonus = salary * 0.09 Case
Is > 3
bonus = 100 Case Else
bonus = 0
End Select
Arrays Arrays are declared the same way as other variables, using the Dim, Static, Private, or
Public statements. The difference between scalar variables (those that arent arrays) and array
variables is that you generally must specify the size of the array. An array whose size is specified is a
fixed-size array. An array whose size can be changed while a program is running is a dynamic array.
Whether an array is indexed from 0 or 1 depends on the setting of the Option Base statement. If
Option Base 1 is not specified, all array indexes begin at zero.
Declaring a fixed array In the following line of code, a fixed-size array is declared as an Integer
array having 10 rows and 10 columns:
19
Option Base 1
Dim MyArray(10, 10) As Integer
The first argument represents the rows; the second argument represents the columns. As with any
other variable declaration, unless you specify a data type for the array, the data type of the elements
in a declared array is Variant. To write code according to PETE301 conventions, explicitly declare
your arrays to be of a data type other than Variant.
Arrays can also be dimensioned on-the fly, using the ReDim command. It is used for dynamic
allocation of arrays on the procedure level.
A useful related function is UBound(). It returns the largest available subscript for the indicated
dimension of an array.
20
Chapter 2
Mathematica
Many of the concepts, methods and applications of this course are now programmed as Living
Textbook. Using the software Mathematica any section (notebook) of the Living Textbook can be
read on the screen and/or run as a program. The particular examples can be modified and rerun
providing a unique possibility for experimenting with numerical methods and petroleum engineering
applications.
Mathematica is a complete environment for calculating and communicating in science and
technology, delivering computing power to over one million people around the world. Breakthrough
new features such as an innovative typesetting system that can do math now make Mathematica
even easier to use. Known for delivering quick, accurate numeric and symbolic solutions,
Mathematica is ideal for creating interactive scientific papers, technical reports, presentations, and
courseware that include text, active formulas, two- and three-dimensional graphics, and
customizable buttons and palettes. A growing library of application packages provides specialized
capabilities in areas such as engineering, fi- nance, statistics, data analysis, optics, astronomy, and
fuzzy logic. The associated web site is at http://www.wolfram.com.
While it is very useful and therefore strongly recommended that you should become familiar with
the basics of the fully integrated environment for technical computing, Math- ematica, this course is
not intended to teach programming in Mathematica. In fact, to make use of the Living Textbook you
need to know only a very few things:
The basic concept is the notebook. A Mathematica notebook is something between a word
processor-type document and a Fortran-type program. When a notebook is running the results of the
computations are appearing immediately after the input lines.
To start a Mathematica notebook in Windows: Double click on the notebook.
To see details: A Mathematica notebook consists of a hierarchical structure of cells. You can
think of the main cells as Chapters. Every Chapter contains sections, every section contains
subsections, etc. At start usually you will see only the main cell names, that is the Chapter titles.
Then you can open or close any Chapter by clicking the little triangle at the left.
To run the whole notebook: Select Kernel, Evaluate notebook from the menu. If the system asks
a question about evaluating initialization cells, respond clicking yes. To see what is happening use
the mouse and the scroll bar or the PgUp and PgDn keys.
To make a small change and rerun an example: Go to the item you want to change with the
mouse cursor. Use the Del or Backspace keys and type what you want. Once
21
you are ready, select Kernel, Evaluate Cell . (For those interested in shortcuts: you can use the
Enter key on the numeric keypad, as well.)
Mathematica notebooks may contain a large amount of graphics. Since printing graph- ics may
overload the system and especially the printer, we ask you not to print the note- books, unless it is
necessary. Before printing make sure you deleted unnecessary parts.
22
Part III
23
Chapter 3
Several terms on the right hand side will enter the formula of the method. The first term left out is
the leading error term. The additional terms are usually neglected in the analysis.
Truncation error: difference between true result (for actual input) and the approximate result
produced by given algorithm using exact arithmetic. in general, it is estimated by the leading error
term.
Rounding error: difference between result produced by given algorithm using exact arith- metic and result
produced by same algorithm using limited precision arithmetic.
Computational error is sum of truncation error and rounding error, but one of these usu- ally dominates.
Since usually input data are in error and we do a lot of steps, the most important issue is: How does the
error propagate?
Measures of the error in approximating a number with true value x by an approxima- tion x:
24
True Error, Et = x x Relative Error, xx
'X
Of course, in general, the true value will not be known so these error measurements cannot be
carried out rigorously. However, in many cases upper bounds for the values of these errors can be
found which give us enough information to put the result into perspective.
df d f
f (x + Ax, y + Ay) = f (x, y) + Ax + Ay + ...
Rearranging and introducing Af = f (x + Ax,y + Ay) f (x, y) we obtain the basic relatiuon of error
calculus:
In this error calculus Ax means always a positive quantity, it represents the maximum possible
value of the actual error.
f=x+yf
= x y f
=xy
f = x/y
From there we obtain, that for summation/substraction the absolute errors are added. For
multiplication and division the relative errors are added.
Example: Find the error in a laboratory determination of the gas z-factor, given that:
p=3 2 4 5 A p = 3 ( m e a n i n g : 3ps)
v = 1.977 ft3 Av = 0.003 ft3
n =1 mole An = 0.001 mole
T = 739.67 oR AT = 0.03 oR
25
Az = y TRT yAp + y T R T yAv + y n,fT yAn + y nRT 2 iiat
A simpler form is
Az = yzy ( jp + jr + jn + jr j =2.8 10-3
(Does the result have any dimension? Why did we not use more decimal figures? Why do we use
absolute value? Why do we not use absolute value in the p term? Does it matter that T is in the
denominator?
(Note: lb-mole is a rather obsolete unit: it is 453.592 mol, or sometimes the mass or weight of it;
R = 10.73 psi ft 3/(lb mole oR ) is the universal gas constant in the English or field system of
units)
Error propagation in a numerical method What happens with the error inherited? The stability
of a computational algorithm : errors (originated from truncation and round- off) are not amplified
from step to step but are rather kept under control
Ill-conditioned problem A problem is ill-conditioned if results are very sensitive to input data
(regardless of the method used)
xo, xi, x2 , x3 , x4 ,
If they appear to be converging, then a measure something like the percentage relative error is
used:
x i -i-i xi
a = -+ ----------- i X 100
x i+1
(If, however, the x can be near-zero, an absolute error criterion is better. Why?)
If this error approximation a drops below some user-specified tolerance, then the process is
stopped. (The results should be checked if possible, in some other independent way for the required
accuracy.)
26
3.5 Order of the method, rate of convergence, stabil- ity,
sensitivity
Since most methods are derived from some Taylor expansion considerations, the theoret- ical order of
a method is related to the truncation of the Taylor series. Examples will be given in the numerical
differentiation section. Broadly speaking: the order of the method is the exponent of the variable
step-size in the leading error term.
Practical rate of convergence is often determined by numerical experimentation. For this purpose
we need simple examples with known exact solutions. If we plot estimates of errors on a log-log plot,
we will see that the error is decreasing with step-size, and the relation appears to be a straight line.
If, for instance, the for every log cycle of the step-size the error changes two log cycles, the rate
is quadratic.
Often instead of the effect of step-size we are more interested in the effect of number of
iterations.
Trade-Off Regarding step-size The point is, that smaller step-size provides larger accuracy, but
after a while the increased number of calculations bring more round-off errors. Therefore there exists
always an optimum.
Trade-Off regarding complexity is very similar. It is better to use a higher order method, then a
lower order method, up to a certain point. Too much complexity, however, may backfire the same way
as to many small steps.
An algorithm is stable if errors (originated from truncation and round-off) are not amplified from
step to step but are rather kept under control. The opposite is unstable.
A problem is well-conditioned, if results are not very sensitive to input data (at least if an
appropriate method is used). The opposite is called ill-conditioned.
Single variable methods: Root of a single equation Minimum of function Manipula- tion of
functions (differentiation, integration) functions given in form of expression or algorithm
discrete data point (smoothing, curve fitting + diff and int) Ordinary Differential Equation
(ODE)
27
Multi variable problems: Linear Algebra, Matrices, vectors Systems of linear equa- tions
direct, special, iterative Nonlinear System of nonlinear equations Minimum of multi variable
function (general and least squares) System of ODE Partial Differen- tial Equations Reservoir
simulation
28
Chapter 4
4.1.1 Bisection
In the bisection method first the user estimates, approximately, the position of the root providing a
lower- and upper-bound bracketing the root. If the interval [a, b] is a bracket, then f (a)f (b) < 0 .
A first estimate of the root is then computed as the mid-point between the two bounds.
x = (a + b)/2
We can improve on this estimate by determining which side of x the root lies and then moving the
bracket accordingly. To do this, we have to overwrite either a or b by the current mid-point, x,
depending on which previous function value has the same sign as f (x). At this point we have a new
bracket and can continue with the next iteration.
Each iteration halves (bisects) the bracket (and therefore halves the maximum possible error)
hence the term bisection. We can calculate a-priori the necessary number of steps to reach a
certain fraction of the initial bracket.
29
and requiring that y should be zero, we obtain for the next approximation of the root:
c = b ---------------
C b f (b)-f (a) b-a
It is easily seen, that c is inside the interval [a; b]. Now we calculate f (c) and overwrite either a or b
with c, depending on which function value has the same sign as f (c). After one step, again we have a
bracket, but it is smaller than the initial one.
(Note:
= f (a)
c a f (a)-f (b) a b
is the same.)
During the process the size of the bracket does not tend to zero. The criterion to stop the iteration
is the following: the deviation of the new location c from any of the old locations a or b should be less
than a specified tolerance.
y f (xo) = f '(xo)(xo x)
f(xo)
xi xo
f '(xo)
This is the iteration formula of the Newtons method. The obtained x 1 can be then renamed to xo and
hence we can continue the iterations.
The following program starts the iteration with x = 2. It uses two internal functions: F for f (x)
and DF for its derivative: f '(x). It stops either when the absolute value of the step is less than 1E-6,
or after 20 iterations. Note that there are two conditions that will stop the While loop: either
convergence, or the completion of 20 iterations. Otherwise the program could run indefinitely.
30
Option Explicit
derivative
Function DF(x As Double) As Double DF = 3 * c 3 * x ' " 2 +
2 * c 2 * x + c1 End Function
Main sub
Sub Newton(x As Double, fx As Double, Converged As Boolean)
Dim Its As Integer, d As Double
lnitialize
Its = 0
Converged = False
x = x0
Loop
While (Not Converged And Its < MaxIts) d = -F(x) / DF(x) x = x + d Its = Its
+ 1 Converged = Abs(d) < Eps Wend
Clean-up fx = F(x)
End Sub
To solve another problem you do not have to rewrite the whole program. It is enough to change
the functions F and DF, and some of the input.
31
Failures in the Newton Method: Though it has quadratic convergence, there are likely to be
problems in some cases.
One problem can occur if f '' (xi) is very large in which case the graph y = f (x) is curving very
rapidly.
A second problem occurs if f ' (xi) is very small which can happen if the function is almost parallel
to the x- axis near the root, or if there is a second root very close by (in which case f' (x) =0
somewhere between the roots), or if the graph of y = f (x) just touches the x - axis at the root but does
not cross it. (Multiple root.)
It is often observed, that the method converges from a near enough starting point, but diverges
if the starting point is selected without care.
To repeat the iteration, we need to carry out only textbfone new function evaluation. To reduce
the length of interval by a fixed fraction at each iteration, each new pair of points
must have the same relationship with respect to the new interval that the previous pair had with
respect to previous interval. To accomplish this, choose the relative positions of the two points to
satisfy the golden ratio: T = (v^(5) 1)/2 0.618. Whichever subinterval is retained, its length will
be 0.618 relative to previous interval, and one interior point retained will be at position either at
0.618 or 0.382.
xk +1 g(x k )
where g(x) is constructed from the f (x) function. (If you understand the above formalism, you can
easily create the g-function for example, for the Newton iteration: g(x) =
x f (x)/f '(x).)
The scheme is called fixed-point iteration or direct iteration. There are four basic cases:
monotone convergence
oscillatory convergence
32
monotone divergence
oscillatory divergence
Almost all iterative processes manifest one of the above behavior types, even if the derivation of
the method has nothing to do with direct iteration.
Take the function f (x) = srn(x) Calculate the derivative at x = n /3 using the simplest
forward divided difference Use step size: 1E-5 , 1E-10 , 1E-15 , 1E-20 , ... (until your
calculator stops giving reasonable result )
(Use radian switch if you need.)
Forward Difference Approximation (first derivative)
As a computer cannot divide by zero the computed (finite) version of this expression is:
cu \_ f (x + h) f (x)
f (x)
h
This is the Forward-difference approximation for the numerical derivative of a function, it has the
most basic form for a numerical derivative and is the least accurate.
The derivation through Taylors expansion provides more information on the method:
hh f (x + h) = f (x) + - f'(x) + 2 j
f''(x) + ...
/'(x)= f ( x + f ( x ) + - . f "(z)
We do not know z and hence neglect the term and hence obtain the forward difference approximation
previously derived. With this derivation, however, we see clearly, that the error is approximately
(h/2) f ''(z) , i.e. proportional to h. This method is of first order.
33
A plot of the error on a log-log scale would result in a 45 o straight line. To minimize
the error choose a small value of h, but h should not be too small as round-off errors in
the machine arithmetic increase as h decreases.
Backward difference approximation (of the first derivative) is very similar:
f (x) f (x h)
f'(x)
h
f (x) -----------------------------------
f V 7 2h
Interestingly, it does not make use of the function value at the base point at all. It is
a second order method and the log-log plot of the error (on an example with known exact
result) would give a straight line with slope two.
The central-difference approximation of the second derivative is:
f (x h) 2f (x) + f (x + h)
h2
Trapezoid rule
The aim is to calculate, numerically, the integral of a function f(x) from a to b. The basic idea is to
evaluate the function at specific locations between the limits, summing the function evaluations will
give an approximation to the integral (area under the curve).
There are many formulae for numerical integration (also known as quadrature), with increasing
complexity these formulae provide a greater accuracy. The Trapezoidal Rule is the simplest.
Consider integrating a known function f (x) over the interval [a; b]. The Trapezoidal Rule gives
the following expression for the exact integral, I:
where h is the interval [a; b] , f ''(z) is the second derivative of the function evaluated at some
unknown point z, between a and b. The value of f ''(z) is generally not known (z is unknown though
bounded between a and b) and so this term is omitted from the
34
solution when we perform the numerical calculation. This leads, in general, to an error (a truncation
error) in the numerical evaluation of the integral.
The accuracy of the calculated result can be increased by multiple application. We will cut the
interval [a, b] into n panels:
where
xo = a
x1 = a + h
x2 = a + 2 h
xn 1 = a + (n 1 ) h
x = b
h = (b a)/n
General idea of Gauss quadratures A quadrature rule is weighted sum of finite num- ber of
sample values of the integrand function. (Both the trapezoid and Simpsons rules are in this general
class.) Closed formulas: Containing left and right points Open for-
mulas: NOT containing left and right points. Advantage of open formulas e.g. improper integrals.
The difference between the two results can be used to improve our final approxima- tion; Try
to extrapolate to infinitely small h.
Example: repeated Simpsons rule has a leading error term of 4-th order. Assume we did the
calculations with h and with h/2. Therefore we have two computed values: 1 1
35
and I2. However, what we really want to know is I, that is still unknown.
11 = I + Ch4
4
12 = I + C 7 ^
In the above equations C is also unknown. To eliminate it, multiply by 2 4 and 1:
24 I2 = 24I + Ch4
(Ii = I + Ch4)
Then subtract
24 I2 Ii = (24 1)I
Finally rearrange and obtain estimate of the unknown I:
16 I2 Ii ~ 15
Note that we do not really need C and we have not calculated it.
Polynomials
Exponential functions
Rational functions
An interpolating polynomial is one which goes exactly through a set of data points. In general a
set of n +1 data points can be interpolated by a polynomial of degree n. Such interpolation leaves no
degree of freedom, the interpolating polynomial is unique. Unfortunately, it is only useful for fairly
small sets of data points. An interpolating polynomial of large degree is required for a large set of
data points, and such high degree polynomials tend to have many oscillations and will approximate
the data set very badly.
Cubic spline is piecewise 3rd order polynomial that is twice differentiable. It is used extensively to
interpolate a large number of data points. Rational function is a fraction:
36
Differentiation and integration of noise-corrupted data
Differentiation is more sensitive to error in input data: You might want to use quite large step
size (why?)
Integration is smoothing out: Step-size is the smaller the better in almost all cases.
Smoothness,
Straight line fit - linear regression Suppose that some the data points should fit a straight line, y
= mx + b, if it were not for experimental and measurement errors. The problem is to find the straight
line characterized by r n and b which best fits the data points in the least squares sense.
Model:
y = mx + b
37
Observations:
(xi, yi) (x2 ,y2 ) . . . (x,y)
The derivation of formulae for the slope and intercept should be familiar by now. The final formulae
are
m = n (EHi xiyi) (EHi xi)(E"=i yi) m n
(E=i x,2) (E=i xi) 2
Function scalprod(x() As Double, y() As Double) As Double Dim n As Integer, i As Integer n = UBound(x) scalprod=0 For
i=1 to n scalprod=scalprod + x(i)*y(i)
Next End Function
Transformations to straight line form Models of physical phenomena are in few cases in straight-
line form. Many 2-parameter nonlinear models can be, however, easily transformed into straight-line
form. In the log-log paper does the same for a power-law model.
For other nonlinear models you should be able to figure out the transformation and make the
correspondence between the straight line parameters (slope and intercept) and the real model
parameters. We will see many examples.
Excel services: trendline option on graphs (with the Show Trendline Option); slope and intercept
spreadsheet functions; linear regression in data analysis package.
38
4.5 Numerical solution of ordinary differential equa- tions
Meaning Ordinary differential equation (ODE): all derivatives are with respect to single
independent variable, often representing time.
Equations with higher derivatives can be transformed into a system of first order equations. The
ODE f' = f (t, y) is non-autonomous. The ODE f' = f (y) is autonomous.
Solution embeds into the directional field. The ODE f' = f (t, y) does not by itself
determine a unique solution. To single out a particular solution, one must specify a value y o of the
solution function at some point t o. This is called initial value. Order of ODE
Euler Eulers method uses the forward difference approximation for gf-and can be sum- marized by
the marching formula:
x = x1 + h
y = yi1 + h f [x1 ,y1 ]
RK-4 This method for solving y' = f ( x , ) , starting at (x o, yo) is one of the most popular ones because
it is quite accurate and stable. The formulae are:
x = x1 + h
y i = y1 + h ( k 1 + 2k2 + 2k3 + k4 )
where
k1 = f [xi1 ,yi1 ]
k2 = f [xi1 + I, yi1 + I 1 ]
k3= f [xi1 + 1, yi 1 + 1 k2 ]
4 = f [xi1 + h, yi1 + h 3 ]
Note that the k i values are various approximations to the slope of the unknown function.
Heun Heuns method improves on Eulers method by a better choice of the slope of the line followed
from the point (x n,yn) to the next point (xn+1 ,yn+1). Instead of choosing this slope as dy dXn) = f (xn,yn) as
in the Euler method, choose it as the mean between this slope and the slope at the destination point
reached by the Euler method.
Simple Heun
xi = xi1 + h
yo = yi1 + h /[xi1 , yi1 ]
y/ = yi1 + I (f [xi1 ,yi1 ] + f [x i, y])
39
Iterated Heun (simplest predictor-corrector)
In choosing step size for advancing numerical solution of ODE, we want to take large steps to
reduce computational cost, but must also take into account both stability and accuracy.
A method is called implicit, if some kind of iteration is involved. Predictor-corrector methods are
implicit.
A stiff ODE corresponds to physical process whose components have disparate time scales or
whose time scale is small compared to interval over the interval which it is studied on.
40
Chapter 5
41
A subroutine to multiply two (compatible) matrices
Sub matmult(a() As Double, b() As Double, c() As Double)
multiplies matrices a anb b to get c
Dim row As Integer, cola As Integer, rowb As Integer, col As Integer Dim i As Integer, j As
Integer, k As Integer Dim s As Double row = UBound(a, 1) cola = UBound(a, 2) rowb =
UBound(b, 1) col = UBound(b, 2)
If Not (cola = rowb) Then MsgBox ("matprod: a and b are not
compatible")
Stop End If
If (UBound(c, 1) < row Or UBound(c, 2) < col) Then MsgBox ("matprod: c is not
large enough to contain the result") Stop End If
For i = 1 To row
For j = 1 To col s = 0
For k = 1 To cola s = s + a(i, k) *
b(k, j)
Next k c(i, j) =
s Next j Next i End
Sub
Matrix is lower triangular if all entries above main diagonal are zero: a i j = 0 for i < j. Matrix is
upper triangular if all entries below main diagonal are zero: a i j = 0 for i > j.
42
Review linear dependence and rank
2x 3y = 8
4x 5y = 16
2x 3y = 8
4x 6y = 16
Thus the determinant is2 6 3 4 = 0. And indeed the second equation is 2 times the first.
Geometrically this corresponds to two coincident lines. In terms of rank: the coefficient matrix has
rank 1, the augmented matrix has rank 1.
2x 3y = 8
4x 6y = 17
If rank of the coefficient matrix is less than the number of equations, two cases can happen:
Consistent (if rank of coeff matrix = rank of augmented matrix): Infinite number of solutions
Non-Consistent (if rank of coeff matrix < rank of augmented matrix): No solution We can we
but it is usually not a good idea to get the solution via the inverse. The reason for this is simple: to
calculate the inverse of the matrix A is more computational work than to solve the original system of
equations.
43
Naive Gauss elimination A system of linear equations can be represented in matrix forms of
various types: e.g.
To solve the system a method (algorithm) called Gaussian Elimination is used, starting with
the augmented matrix containing the coefficient matrix on the left with one extra column containing
the right hand side values (a similar method is used to find A -1). The augmented matrix is changed by
operations which do not change the solutions of the associated system of equations. The allowed
changes are the three elementary row operations
(a) Interchanging two rows
(b) multiplying or dividing a row by a non-zero constant
(c) Replacing a row by the sum of that row and a multiple of another row (adding a negative
multiple of a row - e.g. adding (-2) x row 2 to row 1) is the same as subtracting the positive multiple
(subtracting 2x row 2 from row 1 - so that this row operation also includes subtraction).
The objective is to change the coefficient matrix to a special form of matrix called upper
triangular in which all entries in the bottom left corner below the diagonal are zero. Sometimes the
goal is to create an echelon form which is upper triangular in which the first non-zero in each row is
a 1.
The equations represented by this upper triangular form have exactly the same solu- tions but
are much easier to solve.
e.g. Solving the example above (a label like R = 2 + (-2) i means replace row 2 by the sum of row 2
plus -2 times row 1). At each stage one entry is a special entry called the pivot. A pivot, shown in
bold face in the next example, is the value in the pivot row which is used to modify the other rows
and is in the column where zeros are being created.
1 2
2 R + (-2) i
2 1
s s + i
11
2 -3 5
7
-
0 3 -5 5
1 2 5"
-
3
0 7
-
7
- 2
7 1
0
1
0
-
2
This has created an upper triangular matrix (the first three columns). To further simplify this, divide
the second row by -7 and the third row by -2, creating an echelon
44
+ 2x2 3x3 =5 xi = 5 + 3x3 2x2 = 2 Solve third
^ x2 x3 = 1 ^ x2 = 1 + x3 = 0 Solve second
form. The corresponding equations can easily be solved by starting with the last equation which gives
the x3 value, then finding x2 from the next equation up and so on (back substitution):
0 1 -1 1
This0 version
1 is-1called naive elimination, because we assumed that we can always divide by the
1
0
Naive Gauss-Jordan elimination In this version of the elimination we reduce the system to an
equivalent system with the identity matrix, and hence the solution can be read off from the last
column.
Option Explicit
Option Base 1
Answer the questions: Where is the coefficient matrix at input? Where is the right
hand side?
For both the naive Gauss and Gauss-Jordan methods, in some cases the diagonal entry is simply
zero and so it is not an option to use it. In general, solving linear systems of equations, it is necessary
to use different pivot choices than the diagonal values used in the first example above.
45
5.3 LU Decomposition and Backsubstitution
An advanced form of the Gauss elimination is called LU decomposition-substitution. The procedure is
divided into two parts: the first one operates on the coefficient matrix and does the computationally
demanding half of the job; the second one (called substitution) uses the results of the first part to
obtain a solution to a given right-hand side.
Note that the decomposition destroys the original coefficient matrix (overwrites it by the L and
U matrix elements.)
An additional vector (Indx) is used to remember the row changes found necessary during
partial pivoting. The Indx vector then enters the substitution routine. (Note that it is declared
private but global for the modul containing the LUDecompose and LUSubstitute routines.)
If in spite of partial pivoting the decomposition can not be done, then the matrix is singular and
the procedure stops with a message. (In other words, partial pivoting is enough, full pivoting does
usually not improve the method.)
The solution comes out from the substitution routine as x.
Option Explicit
Option Base 1
With Worksheets("Sheet1")
.Cells(1, 1) = "Number of equations, n"
End With
With Worksheets("Sheet1") n
= .Cells(1, 2)
End With Dim i As Integer, j As Integer
With Worksheets("Sheet1")
For i = 1 To n For j
= 1 To n A(i, j) =
.Cells(i + 1, j)
Next j
b(i) = .Cells(i + 1, n + 1)
Next i
End
With
With Worksheets("Sheet1")
.Cells(1, n + 3) = "Solution"
For i = 1 To n .Cells(i + 1, n + 3) = x(i)
Next i End With
End Sub
It is a good practice to put the LUDecomposition and LUSubstitute into a separate module.
46
Option Explicit Option Base 1
Sub LUDecompose(A() As Double, Indx() As Integer)
input A(n,n)
output A(n,n) and indx(n)
Const Tiny As Double = 1E-16 Dim n As Integer n =
UBound(A, 1)
Dim i As Integer, j As Integer, k As Integer, m As Integer Dim dum As
Double For k = 1 To n - 1 m = k
For i = k + 1 To n If (Abs(A(i, k)) > Abs(A(m, k))) Then m =
i Next i Indx(k) = m dum = A(m, k)
If m > k Then A(m, k) = A(k, k)
A(k, k) = dum End If
If Abs(dum) > Tiny Then dum = 1 / dum Else
MsgBox ("Singular coefficient matrix")
Stop End If
For i = k + 1 To n A(i, k) = -A(i, k) * dum Next i
For j = k + 1 To n dum = A(m, j)
A(m, j) = A(k, j)
A(k, j) = dum For i = k + 1 To n A(i, j) = A(i, j) + A(i, k) *
dum Next i Next j Next k
If Abs(A(n, n)) < Tiny Then
MsgBox ("Singular coefficient matrix")
Stop End
If End Sub
The LUDecomposition overwrites the coefficient matrix by the decomposed (LU) form. You have
to be careful not to call it more than once.
Notice that the back-substitution can be called as many times as many right-hand- sides we have
(as far as the coefficient matrix of the system is the same.)
47
Sub LUSubstitute(A() As Double, Indx() As Integer, b() As Double, x() As Double) input A(n,n) containing
the LU decomposition input Indx(n) index vector input b(n) right hand side output x(n) solution
Sparse methods can be applied to all kinds matrices of special form and even to a general sparse
matrix of no particular type, but the methods are quite complicated, in general.
A special type of matrix called tridiagonal in which the only non zeros are on the main diagonal
and immediately below it and above it (i.e. on the diagonals immediately below and above the main
diagonal) a very effective algorithm is due to Thomas.
e.g. Given the tridiagonal matrix shown store the three diagonals in a three column array as shown:
2 1 0 0 0 a : 0 1 2 1 2
1 2 1 0 0
A 0 2 4 0 ^ 2 2 4 3 4
bc
0 :: 1 1 0 2 0
0 0 1 3
2
The storage saving in the above example is only from 25 down to 15. However in larger matrices
0 0 0 2
the reduction is very significant. For an n x n matrix with n 2 entries the storage will be reduced to
4
3n. Hence if n = 1000 for a tridiagonal matrix, then the full storage is 1,000, 000 and the reduced
storage is 3000. The algorithm not only saves storage, but it enormously reduces the number of
computations. This is due to the fact, that we skip multiplications and additions (because we know
that the result will be zero.)
The Thomas algorithm is extensively used in reservoir simulation:
48
Option Explicit Option Base 1
Sub Thomas(a() As Double, b() As Double, c() As Double d() As Double, x() As Double)
Tridiagonal system of equations:
a is subdiagonal, b is diagonal, c is super diagonal a(1) and c(n) are not used d is the right hand side, x is the
result Dim n As Integer, i As Integer n = UBound(b)
ReDim w(n) As Double, g(n) As Double w(1) = b(1) g(1) = d(1) / w(1)
For i = 2 To n w(i) = b(i) - a(i) * c(i - 1) / w(i - 1) g(i) =
(d(i) - a(i) * g(i - 1)) / w(i)
Next i x(n) =
g(n)
For i = n - 1 To 1 Step -1 x(i) = g(i) - c(i) * x(i + 1) /
w(i)
Next i
End Sub
Note that matrix operations and solution of linear systems are also provided by excel as
spreadsheet functions, macros.
49
5.4 System of nonlinear equations, Newton-Raphson
method
The method may be used to solve a general equation f (x) = 0, provided the left-hand-side can be
differentiated analytically. Note that bold-face denotes matrix and vector. We have n equations to
solve and n components of the unknown to find.
If an initial guess is known, and at that location we can calculate the function f (x o) and its
partial derivatives collected into the Jacobian matrix J(x o). The first element in the first row of the
Jacobian matrix will contain the partial derivative of f 1 with respect to x1, the second element in
the first row will contain the partial derivative of f 1 with respect to x2, and so on.
Writing the equation of the tangent plane:
Ax using LU Decomposition.
Cautious Approach: We calculate the Ax and multiply it by a less than one a if we encounter
convergence problems. Alpha can be adjusted during the iteration process.
Questions: How many subroutines does the user have to provide for a Newton-Raphson method? Two
subroutines, one to evaluate f and the other for the Jacobian matrix, J.
N/R Pros and Contras:
Second order convergence
Needs analytical Jacobian
Convergence as in the case of single variable: Good if you start from the vicinity of the solution, but
might diverge.
Basically it is included in most sophisticated engineering codes.
Other issues:
Convergence criterion: Sometimes it is formulated in terms of function values (errors). That is
dangerous, because often the functions are math constructions without clear physical meaning (and
the unit is blurred)
In practice x always has a well defined unit, and eps has to correspond to it.
If the elements of x are of different dimensions or vary several orders of magnitude, use
normalization N/R variants:
If the Jacobian is not available analytically, we can estimate it by finite difference or Quasi Newton
(1): We can continuously improve its estimate with every new function evaluation or
Quasi Newton (2): Even better: continuously improve the estimate of the INVERSE OF THE
JACOBIAN with every new function evaluation
50
Newton-Raphson method example: Solve
fi xi + x22 - 3
f2 21 + 2 J 2 22
starting point xi and x2, from Cells(4, 2) and Cells(5, 2), e.g. (1 and 1)
The program contains a main (driving) subroutine and the two user subroutines. Note that it
also needs some auxiliary subroutines (linear solver: LU decomposition and back-substitution)
presented previously.
The linear solver pair can be placed into another module.
51
Option Explicit Option Base 1
Sub VBA101() Newton-Raphson Method Example
Dim n As Integer n = 2
ReDim x(n) As Double, f(n) As Double, J(n, n) As Double ReDim b(n) As
Double, dx(n) As Double Dim alpha As Double, eps As Double, s As
Double ReDim Indx(n) As Integer
Dim k As Integer, kmax As Integer, i As Integer With
Worksheets("Sheet1")
.Cells(1, 1) = "kmax"
.Cells(2, 1) = "eps"
.Cells(3, 1) = "alpha"
.Cells(4, 1) = "x1 start"
.Cells(5, 1) = "x2 start"
End With With Worksheets("Sheet1")
With
Worksheets("Sheet1")
kmax = .Cells(1, 2) eps =
.Cells(2, 2) alpha =
.Cells(3, 2) x(1) =
.Cells(4, 2) x(2) =
.Cells(5, 2)
End With With Worksheets("Sheet1")
With Worksheets("Sheet1")
.Cells(1, 4) = "Iteration"
.Cells(1, 5) = "x1"
.Cells(1, 6) = "x2"
.Cells(1, 7) = "f1"
.Cells(1, 8) = "f2"
End With iteration loop For k = 1 To kmax Call fun(x, f)
For i = 1 To n b(i) = -f(i)
Next i
Call Jacobian(x, J)
Call LUDecompose(J, Indx)
Call LUSubstitute(J, Indx, b, dx) s=0
For i = 1 To n s = s + dx(i) * 2 x(i) = x(i) + alpha * dx(i)
Next i
With Worksheets("Sheet1")
.Cells(k + 1, 4) = k .Cells(k + 1, 5) = x(1)
.Cells(k + 1, 6) = x(2)
.Cells(k + 1, 7) = f(1)
.Cells(k + 1, 8) = f(2)
End With
If (Sqr(s) < eps) Then Exit For Next k End Sub
Sub fun(x() As Double, f() As Double) f(1) = x(1) + x(2) * 2 - 3 f(2) = x(1)
/ x(2) + 2 End Sub
Sub Jacobian(x() As Double, J() As Double)
J(1, 1) = 1: J(1, 2) = 2 * x(2)
J(2, 1) = 1 / x(2): J(2, 2) = -x(1) / x(2) * 2 End Sub
52
5.5 Multivariable extrema
Direct Methods Not using any more information, only calculated function values at previous
points and at new point.
A popular direct method is Method of polytopes due to Nelder-Mead
A simplex or polytope is a cluster of n+1 points, for instance for two-variable functions a
simplex consists of three points, forming a triangle.
The Nelder-Mead simplex method (method of polytopes) uses the idea of a wan- dering
simplex, with possible deformation (shrinkage, for instance.)
Given a starting simplex, the program calculates the function values at the nodes and,
depending on the results, tries to improve the simplex by deleting the worst point and bringing
in a better one. If the attempts are not successful, the simplex is shrinks. In case of success, it
becomes larger. Geometrically this means the simplex is wandering and deforming in each
iteration step, adopting itself to the shape of the contour lines while systematically
approaching the best location.
Other methods Gradient based (steepest descent) and second derivative based (Newton)
Excel service: Solver
Fully nonlinear least squares LS objective function is a sum to be minimized. Linear least
squares (straight line fit)
Generalized Linear Least Squares: after transform of variables straight line fit (max 2
parameters)
y = mx + b
53
Observations:
(xi,i, xi,2, yi) (x2,i, x2,2, y2) . . . (x,i, x,2, y)
The Objective function is:
x1 x2 y
1 2 1.154
Given the observations: 3 4 2.045
5 4 3.125
7 2 5.526
x x_i + h
y i yi_i + h (ki + 2k2 + 2ka + k4 )
where
ki f [x_i, yi_i] k 2 f[x_i + h,
yi_i + h ki] ka f[x_i + h, yi_i +
h k2] k4 f [x_i + h, yi_i + h
ka]
The vector operations can be done by simple loops.
54
5.7 Partial differential equations and reservoir sim-
ulation
Review
Transient solution of the diffusivity equation: Finite difference implicit Method Linear
reservoir, constant pressure boundaries.
d 2p dp n
dx2 dt
where
k
n ^Mct
^ porosity U
viscosity
ct compressibility (total) k
permeability
We want to solve the transient problem: The evolution of the pressure field, if the initial
condition is constant pressure and the pressures at the two ends are kept constant, p le/t and
pright, respectively.
55
First discretize the region on interest over both space: x 1, x 2 , . . . , x , . . . , xnx and
^0, t1 , . . . , tn . . . , tnt.
Replace the analytical derivatives by numerical approximations.
Approximation of the second partial derivative in the x-direction: (central):
Approximation of the first partial derivative in time between time levels n and n +1:
pw+1 pin At
So far we have not stated what timestep (n or n+1) the terms on the left are being evaluated.
An implicit solution scheme means that the pressures at the nodes will be evalu- ated at the
new timestep (n+1 ) in the second derivative (space) term.
n+11 +pn+1n+1
pn+11 2pn+ 1 pin + 1 pin
n
(Ax)2 At
1 (Ax) 2
a = -
n At
n+1
p 1 + = given pie ft
p+1 1 (2 + a)pn+1 + p1 1 = api n for i = 2, 3, ... n-1
p1+1 = given pright
b1 c1 0 0 0 p1 1
a2 b2 c2 0 0 p2 d2
0 a3 b3 c3 0 p3 = 3
0 0 a4 b4 C4 p4 d4
0 0 0 a5 b5 p5 5
For instance, b 1 = 1, c 1 =0 and d1 is nothing else, but the given p left. The system can be solved
by the Thomas algorithm.
56
Example data
porosity viscosity 0.2
compressibility 0.8 cp
permeability reservoir ct 1 10_ 5 1 /psi
length initial pressure k 5 md
pressure at x 0 ft L 3000 ft
pressure at x 3000 ft Pi 2800 psi
time ple/ 1000 psi
Prigh 2800 psi
t 0-100 days
With the units indicated,
0.00633k
and hence
\ (Ax)2
. 0.00633k / At
57
Program
Option Explicit
Option Base 1
Sub VBA111() Linear reservoir: constant pressures at the two end
points
Dim nx As Integer, nt As Integer, ipr As Integer
Dim it As Integer, ix As Integer
Dim phi As Double, mu As Double, ct As Double, k As Double
Dim xlen As Double, pleft As Double, pright As Double, pini As Double, tend As Double
Dim alpha As Double, dx As Double, dt As Double, t As Double
Dim i As Integer, ip As Integer
i=1
With Worksheets("sheet1")
.Cells(i, 1) = "Porosity (fraction): ": phi = .Cells(i, 2)
+ 1 .Cells(i, 1) = "Viscosity (cp): ": mu = .Cells(i, 2)
+ :1 .Cells(i, 1) = "Compressibility (1/psi): ": ct = .Cells(i, 2)
+ :
1 .Cells(i, 1) = "Permeability (md): ": k = .Cells(i, 2)
+ :1 .Cells(i, 1) = "Length of reservoir (ft) : ": xlen = .Cells(i, 2)
+ :
1 .Cells(i, 1) = "Initial pressures (psi): ": pini = .Cells(i, 2)
+ :1 .Cells(i, 1) = "Left pressure (psi): ": pleft = .Cells(i, 2)
+ :1 .Cells(i, 1) = "Right pressure (psi): ": pright = .Cells(i, 2)
:
+ 1 .Cells(i, 1) = "Number of grid points in x: ": nx = .Cells(i, 2)
:
+ 1 .Cells(i, 1) = "End time (day): ": tend = .Cells(i, 2)
+ 1 : .Cells(i, 1) = "Number of time steps: ": nt = .Cells(i, 2)
+ 1 : .Cells(i, 1) = "Print at every: ": ipr = .Cells(i, 2)
:
ReDim a(nx) As Double, b(nx) As Double, c(nx) As Double ReDim d(nx) As Double, p(nx) As Double dx = xlen /
(nx - 1) dt = tend / nt
alpha = phi * mu * ct / (0.00633 * k) * dx * 2 / dt a
For i = 2 To nx - 1: a(i) = -1: Next i: a(nx) = 0 b
b(1) = 1: For i = 2 To nx - 1: b(i) = 2 + alpha: Next i: b(nx) = 1 c
c(1) = 0: For i = 2 To nx - 1: c(i) = -1: Next i d
d(1) = pleft: d(nx) = pright Initialize t=0
For i = 1 To nx: p(i) = pini: Next i ip = 1
.Cells(1, 6) = "Loc (ft)->"
For i = 1 To nx: .Cells(1, 6 + i) = (i - 1) * dx: Next i .Cells(ip, 4) = "time (day)": ip = ip + 1 Marching...
For it = 1 To nt .Cells(ip, 6) = "p (psi)->" t = it * dt
For i = 2 To nx - 1: d(i) = alpha * p(i): Next i Call Thomas(a, b, c, d, p)
If ((it Mod ipr) = 0) Then .Cells(ip, 4) = t
For i = 1 To nx: .Cells(ip, 6 + i) = p(i): Next i ip = ip + 1 End If Next it End With
End Sub
58
00633kkrohAy /p+1 - pA / kr \
Note that this simulator uses the Thomas procedure. It is a good practice to put the Thomas
solver into a separate module.
Explicit Solution Scheme Rewrite the above program to obtain an explicit solution
algorithm. Experiment with the time step. Verify the following statement:
where
V p = AxAyh^
Bo
to
n+1
VpSo
Bo
n+1 n
1 Vp Vp
At So So
Bo Bo
Tw i (Pi
-1 - P ) + ( P i +1 - P)
Vow oE
Including a possible well term q o and the previously determined accumulation term we obtain
the gridblock (cell) balance
59
J
)
1 ((VpSo\"+i (VpSo\nN _ 1 ( VpSo\n+i (VpSo\n Ai Bo [ ^o ) ) + qo A^( ^o j l
Bo
60
Chapter 6
6.1 Transformations
Function to function (Laplace)
Data set to data set (Discrete Fourier Transform)
Spectral methods (Seismics, Ultra-Sound etc)
FFT algorithm
Put the Laplace Inversion (Stehfest) Module and the program into Excel
Run it. The program calculates the Inverse of the Laplace transform F(s)=1/s. The result
should be the unit step function (one fore every positive time.) Check if you get back the value
1.0 for the given time points.
Run it. Modify the program to calculate the integral (from zero to t) of the unit step
function, by dividing the Laplace transform by s.
61
Now change the Laplace transform to another function and run again for times between
zero and 10. (Select one of the following pairs and check the result using the known inverse.)
Now calculate the integral of the function using the inverse Laplace transform method.
Check the value of the integral.
F(s) f(t)
1 e-0l5t
s+ 1
0.5 Tn
Ts sin 2.L
arctan 011
s
The main program first initializes the algorithm with StInit. The parameter is the number of
terms (n) in the Stehfest algorithm. In this case we select 16. The actual calculation is invoked
by:
Option Explicit
Sub Fun(s As Double, F As Double)
F=1/s
End Sub
Sub VBA13b()
Dim i As Integer, np As Integer Dim t As
Double, ft As Double Call StInit(16)
With Worksheets("sheet1")
.Cells(1, 1) = "number of time points: " np =
.Cells(1, 2)
.Cells(1, 3) = "t"
.Cells(1, 4) = "f(t)"
For i = 1 To np t = .Cells(i + 1, 3)
Call StCalc(t, ft)
.Cells(i + 1, 4) = ft Next i End With
End Sub
It is a good programming practice to put the following Stehfest procedures (initial- ization and
calculation) into a separate module:
62
Option Explicit
63
Part IV
Appendix
64
Chapter 7
Study guide
65
19. Qualitatively compare the convergence rates of bisection, false-position and Newtons
method
20. Plot any f (x) as a means of graphically identifying the location of roots.
21. Describe the role of global variables in finding the roots of f (x, a, b , . . . ) = 0 where a, b,
... are parameters, and the method returns the value of x that gives f = 0 for fixed values
of a, b, ...
22. Compare effects of noise on numerical differentiation and integration
23. Derive the approximation order of a simple finite difference formula
24. Manually perform multiple application of Simpsons rule
25. Manually perform Richardson extrapolation
26. Recognize if a least squares problem is linear, can be linearized, is nonlinear
27. Be able to determine degrees of freedom
28. Identify connection between linearized and original model parameters
29. Identify the criteria for obtaining a least squares fit. (What is minimized?)
30. Manually evaluate the formulas for the slope and intercept of a line fit to data.
31. Derive the transformations for fitting data to non-linear two-parameter func- tions of
various type.
32. Make the connection between slope and intercept of the linearized model with the
original model parameters.
33. Recognize initial value versus boundary value problem
34. Manually calculate a few steps of Eulers and Heuns methods
35. Program the RK-4 method
36. Describe concepts: single-multi step (self starting or not), predictor-corrector
37. Express conditions of linear dependence of vectors (row or column) in terms of matrix
rank and vice-versa.
38. Explain the condition of singularity of an n n matrix in terms of linear inde- pendence.
39. Express matrix rank as a measure of linear independence.
40. Write the formal solution to Ax = b.
41. Explain why it is not a good idea to use the formal solution as a computational
procedure for solving Ax = b.
42. Name the solution algorithm most commonly used for solving Ax = b.
43. Describe the reason for pivoting.
44. Answer the question: Is pivoting a remedy for ill-conditioned systems?
45. Write the preferred expression for solving Lx = b or Ux = b when L is lower triangular
and U is upper triangular. What algorithm makes use of the solution for these systems?
46. Convert a higher order ODE to an equivalent system of coupled first order ODEs.
66
47. Use Newton-Raphson method for a system of nonlinear equations
48. Recognize the basic PDE types
49. Relate transient to steady state
50. Name methods to solve PDEs numerically
51. Solve the transient diffusivity equation by implicit finite difference
52. Be able to use the Thomas algorithm
53. Describe basic approaches to Reservoir simulation
54. Discuss explicit versus implicit approach
55. Work with units involved
56. Discuss relation between well scheduling and boundary conditions
57. Be able to use a simulator and interpret results
58. Have an overview of spectral methods
59. Be able to use an available module (in this case for numerical Laplace inversion)
67
Chapter 8
68
8.5 Root finding; Newton iteration: z factor
An equation of state written in z-factor form results in the following nonlinear equation to be
solved:
z4 + 2.500z - 1.300 = 0
You use Newtons method to find the root, starting from z = 1.000 (we will call it the zero -th
approximation of the root.) Carry out the first iteration step and give the first approximation
of the root. (Do not continue the iteration process!)
where R is the universal gas constant, p is the pressure, T is the temperature, V M is the molar
volume, and at and b are defined as follows:
TC
b = 0.07780 -------C
Pc
R2T 2
ac = 0.45724 ---------- c
PC
It is somewhat easier to work with dimensionless quantities such as the deviation factor z = .
Then the equation takes the form
2T
or simply : 2
z3 + C2z2 + ciz + co = 0
69
For n-butane:
TC = 425.2 K
pC = 3.75 MPa
w = 0.193
What is the deviation factor, z (and the corresponding molar volume) of butane at
T = 373.15 K and p = 1.5 MPa ?
Note: there are three roots. The smallest one might be the right value, if the
substance is liquid. The largest one is the solution if the substance is in gas state.
The middle root has no physical meaning.
So which is the right one? We can turn to the saturation curve for help. The
tension of butane at 100 oC is 1.53 MPa, so the actual pressure given is less than
the gas-liquid equilibrium pressure. Therefore, at 1.5 MPa the butane is gas!
(Advanced: Another way is to calculate Gibbs free energies from the equation of
state for the two extreme roots and select the one which has less free energy.)
h = -4662.+1.983-T-0.001031-T2+4.243-10-6-T3-2.946-10-9 T4+7.218-10-13-T5
where h is the (specific) enthalpy in kJ/kg and T is the absolute temperature in K.
We wish to determine the temperature of 5 kg methane in the final state, if the
initial state is 300 K and the enthalpy is increased by 12,000 kJ.
Write a complete VBA function that has two inputs: the values of Re and e/D. It should
calculate the friction factor, f.
2.
5 ln(f). Why?
Hint: For bracketing methods choose f as unknown, for open methods choose
1
R
e
f
70
8.9 Root finding: Well deliverability
The Inflow Performance Relationship (IPR) gives the production rate depending on the
reservoir pressure and wellbore flowing pressure. It characterizes the ability of the reservoir to
deliver. For instance:
, \ 0.62 ?/p
=4 . 38 (p 2 - pW/J
where the production rate q is in MSCF/D and both the average reservoir pressure ( p ) and
wellbore flowing pressure ( pw/ ) are in psi.
The Tubing Performance Relationship (TPR) gives the production rate depending on wellbore
flowing pressure. It characterizes the ability of the wellbore to provide vertical lift. For
instance:
/ \ 0.5
qTPR = 93 . 68 ( P W/ - 106 J
From the TPR it can be seen, that (in this case) the minimum well bore flowing pressure to
provide natural lift is 103 psi.
In steady-state production q1PR = qTPR.
Find the production rate at various reservoir average pressures. Hint: first rearrange the
equation into f(x)= 0 form.
yV + Xi(1 - V) = Zi
Rachford combined all these equations into one nonlinear equation with one un- known:
= (Ki - 1)zi = 0
1 = (Ki - 1)V + 1 =
Once solved for V the composition of the individual phases can be calculated from
K i zi
yi = (Ki - 1)V + 1
Zi
Xi =
(Ki - 1)V +1
71
Solve the problem for the following input data:
Component Ki z
C1 61.0 0.3396
C2 9.0 0.0646
Ce 0.035 0.6068
Benefit comes from increased production rate. The production rate in MSCF/D is given by
30, 000
q = ------------
1 6.3 + s
where v is the acid spent, and s is the skin factor modified by the acid treatment. The pre-
treatment skin factor is 7. Depending on the injected volume, the skin factor decreases
according to the relation
1 +0.1 v1/3
(but never becomes negative.)
The discounted benefit for a 3-year horizon is therefore expressed (in US$) as
72
8.12 Integration of a function: PV
The production rate q (STBD) from an oil well declines according to the Hyperbolic Decline
Law:
qn
q = ------------------ r
(1 + Knt) n
where
ce-3651
The Present Value of the cumulative production from now to abandonment can be calculated
from the integral
a q
6
A reservoir of area, A = 3208 acre ( 1.3974 10 8 ft2 ) has a pay thickness of 40 ft. The pore space
is completely filled with oil. The porosity was measured as at various depths from the top of
the formation:
Depth, ft porosity, ^
0 0.2916
10 0.3265
20 0.3187
30 0.3193
40 0.2854
73
Calculate the oil in place (in reservoir cubic feet) using a) multiple application of the trapezoid
rule and b) multiple application of Simpsons 1/3 rule.
(observed):
P psi Bo resBBL/STB
1500 1.262
1600 1.279
1800 1.298
Bo 1
a + b^Jp h - p
where Bo is in resBBL/STB, p in psi, and p b is the known bubble point pressure: (p b = 3007
psi). The model parameters a and b are to be find. The following laboratory data are available:
p psi Bo resBBL/STB
500 1.070
1500 1.175
2500 1.301
Determine the Formation Volume Factor at the bubble point (p b) using the above model.
74
8.16 Straight-line: gas in place
Production and static (field) pressure data for a gas field is given below. (Craft and Hawkins)
p = pi _ f Pi ^ G z zi {ziGj p
where p (psia) is static field pressure, z (-) is deviation factor for corresponding to the pressure,
Gi (MSCF) original (initial) gas in place, G p produced gas (MSCF) and the subscript i refers to
initial. Find original gas in place from the above data using straight-line form and least-
squares fit! Hint: from the straight line form estimate slope and intercept, then interpret them
to obtain first pi/zi then Gi.
G = G - ( ^) P
pi z
Data (observed):
pwf psi q BOPD
2500 109.9
2000 192.5
1500 258.7
1000 309.2
75
Find the Absolute Open Flow Potential: Hint: fill out the following table first.
Consider the following model of oil viscosity (Mo) for a certain field as a function of pore
pressure, p and layer temperature T:
Mo = a Tb (1 + c p1'5)
where MO is in cp, p in psia, T in .
The model parameters a, b and c are to be determined by the method of nonlinear least
squares using a general purpose minimization program (e.g, Solver).
P, pS T, Mo, cp
500 460 0.764
1500 480 1.24
2500 500 2.19
1500 520 1.02
500 540 0.833
Write module containing a VBA function that calculates the objective function to be
minimized. The first line of the function should read as:
Use global (module-level) variables for all data (except for the three model parameters: a, b, c
which are arguments of the function.)
76
, , ^ 0.00025 15 /racdq0dt = 1 + 01
^Q1 q .
where qo is the production rate in BOPD and t is time in days. The initial production rate is
498 BOPD. (The factor 1+l12o.1 describes the decrease of flow efficiency with time that is a
consequence of evolving damage.)
Use the RK4 method with step-size h: 100, 100, 100, 65 days to calculate the production rate
at t = 365 days.
(Advanced: derive and solve another ODE for the cumulative production.)
The solution of the differential equation should be a function: providing pressure (p, Pa) as a
function of depth (D, m).
What is the order of the ODE? What is the initial value condition in this case? What method
can be used to obtain the solution numerically?
Repeat the derivation for a fluid with density p = p (1 + Cfp) where p = 235 kg/m 3 and Cf =
0.3-10-6 Pa-1 and the pressure at the surface is as before: p = 3.2 10 6 Pa
77
P1 P2 P3
RO 83.5 91.2 96.1
MO 84.3 88.7 97.9
You wish to create a mixture which is of Octane number (RO+MO)/2 = 89 deter- mined by any
of the methods.
The question is what mass fractions to use (if there is a solution at all)? It is rea- sonable to
assume that the resulting RO and MO numbers are the weighted sums of those of the
constituents with the weighting factors being the mass fractions. (Note: Since the density of
the fluids is the same, corresponding mass fractions and volume fractions are also the same.)
Since logs are taken densely (say meter by meter) we need to solve for the unknown V1, V 2 and
V3 with many right-hand-sides.
78
Write a program to do the calculation calling the LUDecomposition (once) and the
LUsubstitute (many times.)
Depth, ft p, g/cm3
1000 19.3 2.32
1001 16.56 2.37
1002 15.89 2.38
1003 10.54 2.47
1004 19.95 2.33
1005 19.74 2.32
1006 18.3 2.34
1007 27.6 2.19
1008 23.06 2.25
1009 14.49 2.40
1010 30. 2.15
(Additional challenge: Using the LU solver pair, calculate the inverse of the coefficient matrix.
Check if the original matrix multiplied by the obtained inverse is the identity matrix.)
Hint: Let us start from 1 mole mixture and let x 1 and x2 denote the reaction extents expressed
in mole. The reaction extent tells us how many moles of the left-hand-side of a chemical
reaction has been converted into the right-hand-side. The following
79
table shows the mole fractions as the function of the two reaction extents (notice that the first
reaction causes mole number change, while the second does not.)
1 CH4 U'4-xi ^ +
2x i
2 H2 O U'O Xi
X2
3 CO Xi
1+2xiX2
1+2xi
4 CO2 X2
5 1+2xi
3X1+X2
H2
1+2xi
We can reduce the problem to two equations and two unknowns using the reaction extents.
80
Chapter 9
Tables
Area, A L2 2
Volume, V m2
L3 3
m3
Specific volume V/m m
3
kg
1
Acceleration, a AV/At 2
ms 2
Force, F ma kg m s 2 N
Pressure, p (also stress) F/A kg m
1 s-2 Pa = N/m2 = J/m3
Work LF kg m2 s 2 J = N m = Pa m3
Heat Work kg m2 s -2 J
Int. Energy, Enthalpy Work kg m2 s -2 J
Power Work/ t kg m2 s -3 W = J/s = Pa m3/s
Specific heats (mass based) Heat/(m A T) m2 s2 K -1 J / (kg K)
Electr. Potential (Voltage) Power/Current kg m2 s -3 A-1 V= W/A
Electr. Resistance Potential/Current kg m2 s -3 A-2 Q= V/A
Electr. charge Current Time As C
Flow rate A Volume / At 31 m3 s 1
Shear rate A Velocity/ A Length s-1
Viscosity Stress / shear rate Pa s
Permeability Pressure/ Length Velocity 2
m2
Viscosity
Compressibility 1/Pressure Pa-1
81
Multiplier Prefix name Symbol
109 giga G
106 mega M
103 kilo k
10-3 milli m
10-6 micro
0.1 deci d
0.01 centi c
Circumference of circle dn
Area of circle d2 n/4
3
Volume of sphere d n/6
2
Surface area of sphere d n
Volume of right circular cylinder
hd2 n/4
Pyramid (area of base) -h/3
Temperature
Abs zero Water freezing p. (1 atm) Water boiling p. (1 atm)
0K 273.15 K 373 K
-273.15 oC 0 oC
O
O
o
o
1
0R 491.67 R 671.67 R
-459.67 oF 32 oF 212 oF
82
pV = nT
Cp cv =
Cp
Ideal gas: Gv
K=
3
R= 8 . 3 1 4 = 8.314 = 8.314^+
mol-K mol-K mol-K
R= 1.986 lb BTUUoR = 1545 lb ftf = 10.73 lbpsi
__________ lb-moloR _________ lb-moloR__________ lb-moloR
Other units
Other unit Expressed with SI unit
inch 0.0254 m
ft 0.3048 m
mile 1609.3 m
acre 4046.9 m2
gal 3.7854 x 10-3 m3
bbl 0.158987 3
Liter 0.001 m33
m3
lbm 0.45359 kg
lbf 4.4482 N
psi 6894.8 Pa
bar 1 x 105 Pa
Atmosphere 101325 Pa
mm-of-Hg 133.32 Pa
deg-R 0.555556 K
hp 745.70 W
BTU 1055.1 J
min 60 s
hour 3600 s
day 86400 s
md 9.869 x 10-16 m2
cP 0.001 Pa s
ft
1 hp = 42.41 BTU lbm
min ft3
1 sp. grav. (liq) = 8.34 lbm = 62.4
gal
sp. grav. (liq) : 141.5 ' 131.5+AP1o
lb-mole : 453.59 mol : 379 SCF
M : multiply by 1000
83