You are on page 1of 84

PETE 301 PETROLEUM ENGINEERING

NUMERICAL METHODS LECTURE NOTES AND


STUDY GUIDE
Peter P. Valko
August 18, 2004
Contents
I Introduction 4
0.1 Course Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
0.2 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
0.3 Course structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
II Tools 8
1 Introduction to Excel and VBA 9
1.1 Starting Excel, saving a le . . . . . . . . . . . . . . . . . . . . . . . . . . 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
III Engineering models and numerical methods 23
3 Mathematical Modeling and Engineering Problem Solving 24
3.1 Conservation Laws and Engineering . . . . . . . . . . . . . . . . . . . . . 24
3.2 Accuracy and Precision, Approximations and Round-O Errors . . . . . . 24
3.3 Error propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4 Existence and uniqueness, Convergence criterion . . . . . . . . . . . . . . 26
3.5 Order of the method, rate of convergence, stability, sensitivity . . . . . . . 27
3.6 Classication of problems and methods . . . . . . . . . . . . . . . . . . . . 27
4 Methods related to single variable problems 29
4.1 Roots of equations and extrema of functions . . . . . . . . . . . . . . . . . 29
4.1.1 Bisection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.1.2 False position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1
4.1.3 Newtons method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.1.4 Optimization: Golden ratio search for a minimum . . . . . . . . . 32
4.2 Fixed-point iteration as a general framework for iterative processes . . . . 32
4.3 Representing, manipulating functions . . . . . . . . . . . . . . . . . . . . . 33
4.3.1 Numerical Dierentiation of functions that can be evaluated every-
where . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.3.2 Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . 34
4.3.3 Simpsons 1/3 rule . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.4 Interpolation, Smoothing, Curve t, Least squares . . . . . . . . . . . . . 36
4.4.1 Least Squares and its Numerical Aspects . . . . . . . . . . . . . . 37
4.5 Numerical solution of ordinary dierential equations . . . . . . . . . . . . 39
4.5.1 Basic methods for solving ODE . . . . . . . . . . . . . . . . . . . . 39
5 Methods related to multi-variable problems 41
5.1 Matrices, vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.2 System of linear equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.3 LU Decomposition and Backsubstitution . . . . . . . . . . . . . . . . . . . 46
5.4 System of nonlinear equations, Newton-Raphson method . . . . . . . . . . 50
5.5 Multivariable extrema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.6 Solution of system of ordinary dierential equations: RK4 . . . . . . . . . 54
5.7 Partial dierential equations and reservoir simulation . . . . . . . . . . . . 55
5.7.1 Reservoir Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6 Integral Transforms, Spectral methods, inversion of the Laplace trans-
form 61
6.1 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6.2 Laplace transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6.2.1 Numerical inversion of the Laplace transform . . . . . . . . . . . . 61
IV Appendix 64
7 Study guide 65
7.1 Be able to ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
8 Assignments, test problems 68
8.1 Error propagation: borehole volume . . . . . . . . . . . . . . . . . . . . . 68
8.2 Error propagation: one-gallon cube . . . . . . . . . . . . . . . . . . . . . . 68
8.3 Error propagation: barrel . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.4 Error propagation: hydrocarbon volume . . . . . . . . . . . . . . . . . . . 68
8.5 Root nding; Newton iteration: z factor . . . . . . . . . . . . . . . . . . . 69
8.6 Root nding: solution of the PR equation of state . . . . . . . . . . . . . 69
8.7 Root nding: heat balance . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.8 Root nding: friction factor . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.9 Root nding: Well deliverability . . . . . . . . . . . . . . . . . . . . . . . 71
8.10 Root nding: isoterm ash . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.11 Optimization: Maximizing Net Present Value . . . . . . . . . . . . . . . . 72
8.12 Integration of a function: PV . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.13 Numerical integration of discrete data: pore volume . . . . . . . . . . . . 73
2
8.14 Straight-line: formation volume factor model 1 . . . . . . . . . . . . . . . 74
8.15 Straight-line: formation volume factor model 2 . . . . . . . . . . . . . . . 74
8.16 Straight-line: gas in place . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.17 Straight-line: Flow-After-Flow Test (IPR) . . . . . . . . . . . . . . . . . . 75
8.18 Nonlinear least squares: oil viscosity as a function of pressure and temper-
ature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.19 ODE: Production rate decline from a dierential equation . . . . . . . . . 76
8.20 ODE: pressure versus depth in a shut-in well . . . . . . . . . . . . . . . . 77
8.21 Meaning of matrices and vectors: chemical component balance . . . . . . 77
8.22 System of linear equations: mixing . . . . . . . . . . . . . . . . . . . . . . 77
8.23 System of linear equations with many right-hand-sides: log interpretation 78
8.24 Nonlinear System of Equations: Chemical Equilibrium of Methan Combus-
tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
8.25 Units: Darcys law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9 Tables 81
3
Part I
Introduction
4
0.1 Course Objectives
Learn the underlying principles behind some commonly used numerical methods.
Understand the route from arithmetic and calculus to numerical methods.
Understand the basic concepts including uniqueness, approximation, error, con-
vergence, and stability.
Learn how to program a numerical method using modern programming concepts
Problem solving steps
Structured programming
Use of subroutines, modules
Learn how to make use of Visual Basic for Applications.
Learn how to use an integrated program development system providing debug-
ging facilities.
Recognize the main features of a mathematical problem arising in a technical con-
text.
Single- or multi-variable
Linear or non-linear
Involves algebraic, dierential 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.
Start with conservation principles
Include material properties
Add problem specic conditions
Learn how to use various resources in solving a complex problem
Use of software systems; Subroutine libraries, modules
Internet resources
Improve ability to critically analyze numerical results
Improve documentation skills
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 ecient
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 specic 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)
2) Either of the following (or any similar title)
a) Larsen: Engineering with Excel, Prentice Hall, 2002
b) Liengme: A guide to Microsoft Excel 2002 for scientists and engineers, Butterworth
Heinemann, 2002
c) Bloch : Excel for Engineers and Scientists, 2002
3) All of your Petroleum Engineering Books
Special thanks go to L. Piper, B. Maggard, R. Archer, R. Wattenbarger .
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) Classication 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 dierentiation and integration Finite dierence; Trapezoid rule;
Simpsons rule
(c) Interpolation, Smoothing, Curve t, Least squares
(d) Numerical solution of ordinary dierential equations
4. Methods related to multi-variable problems
(a) Matrices, vectors
(b) System of linear equations
(c) Gauss, Gauss-Jordan, LU decomposition Special cases, Iterative methods
(d) Multivariable nonlinear equations: Newton-Raphson
(e) Multivariable extrema: Nelder-Mead simplex, Fully nonlinear least squares
5. Partial dierential equations and reservoir simulation
(a) Classication and Numerical Solution of PDEs
(b) Transient solution of the diusivity equation
(c) Reservoir simulation
6. Transformation methods
(a) Spectral methods
(b) Laplace transform inversion
7
Part II
Tools
8
Chapter 1
Introduction to Excel and VBA
1.1 Starting Excel, saving a le
To create a new workbook: On the File menu, click New, and then click Blank Workbook
on the New Workbook task pane.
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 le format (if you want other than .xls)
(In general, it is useful to click the right mouse button. Most of the time it will oer a
menu item you really want.)
1.2 Workbook, worksheet, views, printing
1.2.1 Review
A Microsoft Excel workbook is a le that contains one or more worksheets, which you
can use to organize various kinds of related information. You can enter and edit data on
several worksheets simultaneously and perform calculations based on data from more than
one worksheet. When you create a chart, you can place the chart on the same worksheet
as its related data or on a separate chart sheet.
To view and scroll independently in dierent parts of a worksheet, you can split a
worksheet horizontally and vertically into separate panes. Splitting a worksheet into
panes allows you to view dierent parts of the same worksheet side by side and is useful,
for example, when you want to paste data between dierent areas of a large worksheet.
To see how it will look like in print: On the View menu, click Page Break Preview.
Before you do any work, make sure that you macros are allowed (security level is low)
and you can save an excel spreadsheet to your own disk.
1.3 Spreadsheet basics
The dierence between relative and absolute references is the key concept to understand:
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 automatically 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 specic 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.
1.4 Graphing with Excel
Import a text le Click the cell where you want to put the data from the text le. 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 rst enter the data for the chart on the worksheet.
Arrange your data in columns, with x values in the rst 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-t 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
Editing an Existing Graph Check out.
Creating Graphs with Multiple Curves Check out.
More graphs types Check out.
1.5 Numerical methods inside Excel
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.
1.6 Database Management with Excel
Check out Search, Filter and Sort in the Data menu.
1.7 Excel and the WWW
Excel les may be converted to HTML (web) documents. The HTML Wizard guides you
through the process.
1.8 Keyboard macros and what they cant do
If you perform a task repeatedly in Microsoft Excel, you can automate the task with a
macro. A macro is a series of commands and functions that are stored in a Microsoft
Visual Basic module and can be run whenever you need to perform the task.
For example, if you often enter long text strings in cells, you can create a macro to
format those cells so that the text wraps.
Recording keyboard macros: When you record a macro, Excel stores information
about each step you take as you perform a series of commands. You then run the macro
to repeat, or play back, the commands. If you make a mistake when you record the
macro, corrections you make are also recorded. Visual Basic stores each macro in a new
module attached to a workbook.
Making a macro easy to run: You can run a macro by choosing it from a list in the
Macro dialog box. To make a macro run whenever you click a particular button or press
a particular key combination, you can assign the macro to a toolbar button, a keyboard
shortcut, or a graphic object on a worksheet.
Keyboard macros are to replay your keystrokes. If you want to program your own
algorithms, you need VBA programming.
1.9 VBA, variable, sub, function, module
Visual Basic for Applications The programming language of this course is Visual Basic
for Applications (VBA), which is included with the Excel spreadsheet application. This
combination of programming language and spreadsheet has many powerful features, which
make it an excellent tool for problem solving.
1. A modern, structured programming language, with many intrinsic procedures
(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
(a) Data management
(b) Graphics
(c) Solver (for optimization and root nding)
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
(a) Dialog and input boxes
(b) Custom user forms with spinners,checkboxes, etc.
(c) Custom menus and toolbars
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:
1. Place the values of g, u and t into variables which can be used for calculations
2. Compute the value of s according to the formula.
3. Output the value of s which is the result
4. 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 rst-if pen
and paper turns you o 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
To make this program run do the following:
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:
1. VBA performs automatic syntax checking and formatting of code.
2. The single quote character () denotes the beginning of a comment.
3. In PETE 301 every module should require explicit variable typing by using Option
Explicit module option.
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.
Built-in (or intrinsic) functions
Visual Basic has many built-in functions. You will often need the math functions:
Abs, Atn, Cos, Exp, Fix, Int, Rnd, Sgn, Sin, Sqr, Tan.
Log is natural-log. If you wish ten-based logarithm, use Log(x)/Log(10).
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 nd 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
Byte 1 byte 0 to 255
Boolean 2 bytes True or False
Integer 2 bytes -32,768 to 32,767
Long(long integer) 4 bytes -2,147,483,648 to 2,147,483,647
Single (single oating-point) 4 bytes -3.402823E38 to 3.402823E38
Double (double oating-point) 8 bytes -1.E308 to 1.8E308
String (variable-length) 10+length 0 to appr 2 billion
Variant (with numbers) 16 bytes see range of a Double
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.
Dim Statement: Declares variables and allocates storage space.
Dim NumberOfEmployees As Integer
Dim x As Double, y as Double
When Option Explicit appears in a module, (as it should in PETE301) you must ex-
plicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements.
If you attempt to use an undeclared variable name, an error occurs at compile time.
Note:
Dim a,b As Integer
will declare b as an Integer, but a will be the default (Variant). You have to write
Dim a As Integer, b As Integer
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.
Dim n As Integer, m As Double
n = 4/3 !result is 1
n = 5/3 !result is 2
n = 7/2 !result is 2
n = 5/2 !result is 4
m = 5/2 !result is 2.5
You should be aware of rules regarding priorities. For instance a frequent error is to
write
z = p*v/R*T
instead of the correct:
z = p*v/(R*T)
Strings To convert any other value to a string, use the Cstr intrinsic function.
Objects,input and output
The structure With/End With and the operator point can be understood if you
are familiar with the concept of object.
ThisWorkbook is a hierarchical object. It has several Workseets (a whole collection
of them). One of them is called Sheet2. The following example shows how to read a
number into variable a from cell A4 of Sheet2:
With ThisWorkbook.Worksheets("Sheet2")
a =.Cells(1, 4)
End With
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.
Advanced topics: Argument passing by Value or by Reference All arguments are
passed to procedures by reference, unless you specify otherwise. This is ecient because
all arguments passed by reference take the same amount of time to pass and the same
amount of space (4 bytes) within a procedure regardless of the arguments data type.
(You can pass an argument by value if you include the ByVal keyword in the pro-
cedures declaration. Arguments passed by value consume from 2 16 bytes within the
procedure, depending on the arguments data type. Larger data types take slightly longer
to pass by value than smaller ones.)
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 comma instead of decimal point
x = 1 + (2 * 3 unpaired parentheses
.Cell(2,1)=5 Wrong function name: Cell instead of Cells
If (a=b) Missing then
x=3
Else
x=4
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
1. Floating point division by zero
16
2. Overow
3. Negative argument in the function sqr()
is generated.
Overow 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
dierent from what we know to be the correct mathematical solution. This can be due
to rounding error, which results from the nite precision available on the computer. , e.g.
two or four bytes per variable, instead of an innite number.
Run the following program :
Option Explicit
Sub VBA022()
Dim sumx As Double
sumx = 0.1
Do
sumx = sumx + 0.001
Debug.Print sumx
If (sumx = 0.2) Then Exit Do
Loop
End Sub
Watch the Immediate Window! You will nd 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 almost impossible. Real programming is about localizing your errors by debugging and
correcting them. Check your work 100+1 times. Use test problems. Print out intermediate
results. Investigate limiting cases.
Message Boxes, Custom Dialogue Boxes
1.10 Structured Programming: Subs, Decisions and
Loops
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 dened 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 dened 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
Calculate the Slope and Intercept
Call My_Line(X1, Y1, X2, Y2, Slope)
Display the result
With ThisWorkbook.Worksheets("Sheet3")
.Cells(3, 1)=" Slope:"
.Cells(3, 2)= Slope
.Cells(4, 1)= "Intercept:"
.Cells(4, 2)= Intercept
End With
End Sub
A subroutine used inside:
Sub My_Line(x As Double,y As Double,xx As Double,yy As Double,m As Double)
m = (yy - y) / (xx - x)
Intercept = y - m * x
End Sub
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.
The If/Then/Else Decision Structure
Example (after Hahn) Most banks oer dierential interest rates-more for the rich, less
for the poor. (The Else part may be missing and you may create more complex structure
with Else If.)
If balance < 3000 Then
apr = 0.03
Else
apr = 0.06
End If
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
Count down: Do it for j = 100, then j = 90 etc.:
For i = 100 To 50 Step -10
block
Next i
Forever: Loop forever unless If stops it:
Do
If "logical_expression..." Then Exit Do
"block..."
Loop
Condition controlled: Do while logical expression is true:
While "logical_expression..."
"block..."
Wend
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 dierent values. While If...Then...Else statements can evaluate a dierent
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 dierence 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 specied is a xed-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 specied, all array indexes begin at zero.
Declaring a xed array In the following line of code, a xed-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 rst 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 y, 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 modied 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 scientic 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, -
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.
Living Textbook Content
LTS021 Taylor Series Expansion, Remainder Term and Truncation Error
LTS031 Root-Finding
LTS032 More about iterations
LTS033 Extrema of single variable function
LTS041 Numerical Integration of functions
LTS061 Ordinary Dierential Equations Basic Concepts
LTS071 Numerical Solution of the ODE Initial Value Problem
LTS072 Reservoir Material-Balance
LTS081 Vectors, Matrices, Linear system of Equations
LTS091 Direct Solution of Systems of Linear Equation
LTS092 Systems of Linear Equations: Gauss-Siedel Iterative Method
LTS101 Solution of System of Non-Linear Equations
LTS102 Minimization of a Multi-Variable Function
LTS121 Oil-Water 2D IMPES
LTS131 Spectral Methods: Discrete Fourier Transform
22
Part III
Engineering models and
numerical methods
23
Chapter 3
Mathematical Modeling and
Engineering Problem Solving
3.1 Conservation Laws and Engineering
3.2 Accuracy and Precision, Approximations and Round-
O Errors
Precision is related to repeatability or to the number of digits obtained. Accuracy is
related to how near we are to the true value. Obviously, accuracy is more important
than precision.
In analysis of the numerical methods we often start with the Taylor series expansion:
f(x +h) = f(x) +h f

(x)/1! +h
2
f

(x)/2! +. . .
Several terms on the right hand side will enter the formula of the method. The rst
term left out is the leading error term. The additional terms are usually neglected in the
analysis.
Truncation error: dierence 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: dierence 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, E
t
= x x
Relative Error,
xx
x
Percentage Relative Error,
xx
x
100.
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.
3.3 Error propagation
A new perspective: The result of a computation is a multi-variable function of the input
values.
For simplicity, consider two variables. The rst order Taylor approximation:
f(x + x, y + y) = f(x, y) +
f
x
x +
f
y
y +. . .
Rearranging and introducing f = f(x + x, y + y) f(x, y) we obtain the basic
relatiuon of error calculus:
f =
f
x
x +
f
y
y +. . .
In this error calculus x means always a positive quantity, it represents the maximum
possible value of the actual error.
Derive what are the two partial derivatives of
f = x +y
f = x y
f = x y
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 = 3245 psi p = 3 psi (meaning: 3 psi)
v = 1.977 ft
3
v = 0.003 ft
3
n = 1 lb mole n = 0.001 lb mole
T = 739.67
o
R T = 0.03
o
R
Dierentiating z = pv/(nRT) we obtain
25
z =
v
nRT
p +
p
nRT
v +
pv
n
2
RT
n +
pv
nRT
2
T
A simpler form is
z = z
_
p
p
+
v
v
+
n
n
+
T
T
_
= 2.8 10
3
(Does the result have any dimension? Why did we not use more decimal gures? 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
o
R ) is the universal gas constant in the
English or eld 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-
o) are not amplied 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)
3.4 Existence and uniqueness, Convergence criterion
Review the following concepts:
Existence and uniqueness of solution
Graphical versus numerical solution
Analytical versus numerical method to get the results numerically
Direct vs iterative (explicit vs implicit)
Convergence
Mathematically a series converges to a limit if the dierence of the n-th term and the limit
can be made smaller than any selected positive epsilon by selecting a large enough
n.
Typically in many numerical problems, a sequence of approximate values will be ob-
tained which, hopefully, converge towards the desired solution:
x
0
, x
1
, x
2
, x
3
, x
4
, ......
If they appear to be converging, then a measure something like the percentage relative
error is used:

a
=
x
i+1
x
i
x
i+1
100
(If, however, the x can be near-zero, an absolute error criterion is better. Why?)
If this error approximation
a
drops below some user-specied 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 dierentiation section. Broadly speaking: the order of the method
is the exponent of the variable step-size in the leading error term.
Total error: truncation and round-o
Error propagation in a numerical method is as important as the magnitude of the error.
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 eect of step-size we are more interested in the eect of number
of iterations.
Trade-O 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-o
errors. Therefore there exists always an optimum.
Trade-O 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 backre the same way as to many small steps.
An algorithm is stable if errors (originated from truncation and round-o) are not
amplied 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.
3.6 Classication of problems and methods
Single variable, multi variable (our main classication) Other can be linear vs. nonlinear,
direct vs. iterative, explicit vs. implicit.
Single variable methods: Root of a single equation Minimum of function Manipula-
tion of functions (dierentiation, integration) functions given in form of expression
or algorithm discrete data point (smoothing, curve tting + di and int) Ordinary
Dierential 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 Dieren-
tial Equations Reservoir simulation
Other: Transformation methods
28
Chapter 4
Methods related to single
variable problems
4.1 Roots of equations and extrema of functions
Single and multiple roots, bracketing versus open methods
Nonlinear equation may have multiple root, where both function and derivative are zero.
4.1.1 Bisection
In the bisection method rst 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 rst 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.
4.1.2 False position
The search starts with an interval [a, b]. It is assumed that the function changes sign in
the interval, f(a)f(b) 0 as in the case of the bisection method.
Writing the equation of the line passing through the two points (a, f(a)) and (b, f(b))
y f(b) =
f(b) f(a)
b a
(x b)
29
and requiring that y should be zero, we obtain for the next approximation of the root:
c = b
f(b)
f(b)f(a)
ba
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:
c = a
f(a)
f(a)f(b)
ab
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 specied tolerance.
4.1.3 Newtons method
The method (also called Newton-Raphson method) may be used to solve a general equa-
tion f(x) = 0, provided the left-hand-side can be dierentiated analytically.
If an initial guess is known, we can calculate the function and its derivative at x
0
.
Writing the equation of the tangent line:
y f(x
0
) = f

(x
0
)(x
0
x)
Substituting y = 0 we nd that the line crosses the x axis at
x
1
= x
0

f(x
0
)
f

(x
0
)
This is the iteration formula of the Newtons method. The obtained x
1
can be then
renamed to x
0
and hence we can continue the iterations.
Example. Suppose that
f(x) = x
3
+x 3
Then
f

(x) = 3x
2
+ 1
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 indenitely.
30
Option Explicit
Globals Group 1: Coefficients
Dim c3 As Double, c2 As Double, c1 As Double, c0 As Double
Globals Group 2: Iteration control parameters
Dim MaxIts As Integer, Eps As Double, x0 As Double
left-hand-side function
Function F(x As Double) As Double
F = c3 * x ^ 3 + c2 * x ^ 2 + c1 * x + c0
End Function
derivative
Function DF(x As Double) As Double
DF = 3 * c3 * x ^ 2 + 2 * c2 * x + c1
End Function
Main sub
Sub Newton(x As Double, fx As Double, Converged As Boolean)
Dim Its As Integer, d As Double
Initialize
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
Driver program
Sub VBA041()
Values Group 1: Coefficients
c3 = 1: c2 = 0: c1 = 1: c0 = -3
Values Group 2: Iteration control
MaxIts = 20: Eps = 0.000001: x0 = 2
Declaration of Group 3: Arguments
Dim x As Double, fx As Double, Converged As Boolean
Call Newton(x, fx, Converged)
With Worksheets(1)
.Cells(1, 1) = "x"
.Cells(1, 2) = x
.Cells(2, 1) = "fx"
.Cells(2, 2) = fx
End With
If Converged Then
MsgBox ("Convergence criterion satisfied ")
Else
MsgBox ("Convergence criterion NOT satisfied ")
End If
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

(x
i
) is very large in which case the graph y = f (x) is
curving very rapidly.
A second problem occurs if f

(x
i
) 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.
4.1.4 Optimization: Golden ratio search for a minimum
For minimizing a function of one variable, we need a bracket for the solution analogous
to sign change for nonlinear equation.
A real-valued function f() is unimodal on interval [a; b] if there is unique location x
0
such that f is strictly decreasing for x < x
0
(that is on the left), and strictly increasing
for x > x
0
(that is on the right).
Suppose f is unimodal on [a; b], and let x
1
and x
2
be two points within interval, with
x
1
< x
2
. Evaluating and comparing f(x
1
) and f(x
2
), we can discard either [x
2
; b] or
[a; x
1
], with minimum secured to lie in the remaining subinterval.
To repeat the iteration, we need to carry out only textbfone new function evaluation.
To reduce the length of interval by a xed 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: = (
_
(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.
4.2 Fixed-point iteration as a general framework for
iterative processes
Many iterative methods for solving nonlinear equations can be viewed as
x
k+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 xed-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.
4.3 Representing, manipulating functions
4.3.1 Numerical Dierentiation of functions that can be evalu-
ated everywhere
When an analytical solution to the rst derivative of a given function is dicult or in-
convenient then a numerical method can be used to provide an approximate, though
often highly accurate, computation. Many methods exist, with increasing complexity
they perform, in general, computations to a higher accuracy. A basic understanding of
the meaning of Truncation Error and Round-o Error is crucial.
Intuitive error analysis of numerical dierentiation
Take the function f(x) = sin(x) Calculate the derivative at x = /3 using the simplest
forward divided dierence 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 Dierence Approximation (rst derivative)
The Forward-dierence approximation method for the numerical dierentiation of a func-
tion can be derived by considering dierentiation from rst principles or by considering
Taylors expansion.
The dierentiation from rst principles derivation is intuitive:
f

(x) = lim
h0
f(x +h) f(x)
h
As a computer cannot divide by zero the computed (nite) version of this expression is:
f

(x)
f(x +h) f(x)
h
This is the Forward-dierence 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:
f(x +h) = f(x) +
h
1!
f

(x) +
h
2!
f

(x) +. . .
Rearrange and represent all remaining terms in one remainder term
f

(x) =
f(x +h) f(x)
h
+
h
2
f

(z)
We do not know z and hence neglect the term and hence obtain the forward dierence
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 rst 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-o errors in
the machine arithmetic increase as h decreases.
Backward dierence approximation (of the rst derivative) is very similar:
f

(x) =
f(x) f(x h)
h
+
h
2
f

(z)
Again, we neglect the term containing z, Therefore
f

(x)
f(x) f(x h)
h
and the method is of rst order.
The central-dierence approximation of the rst derivative gives reasonably accurate
results and is often implemented:
f

(x)
f(x +h) f(x h)
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-dierence approximation of the second derivative is:
f

(x)
f(x h) 2f(x) +f(x +h)
h
2
This is a second order method.
4.3.2 Numerical Integration
Review geometric meaning Review physical (engineering) meaning
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 specic 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:
I =
h
2
(f(a) +f(b))
h
3
12
f

(z)
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:
I = h
_
1
2
f(a) +f(x
1
) +f(x
2
) +... +f(x
n1
) +
1
2
f(b)
_
where
x
0
= a
x
1
= a +h
x
2
= a + 2 h
x
n1
= a + (n 1) h
x
n
= b
h = (b a)/n
4.3.3 Simpsons 1/3 rule
The simple Simpsons rule uses a parabolic approximation to the curve.
The multiple application of Simpsons rule is usually the rst choice of engineers, because
of its reliability and simplicity:
I
h
3
(f(a) + 4f(x
1
) + 2f(x
2
) +... + 4f(x
n1
) +f(b))
where n is even and h = (b a)/n.
General idea of Gauss quadratures A quadrature rule is weighted sum of nite 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.
Richardson extrapolation (applied to numerical integration) An error estimate
can be formulated by making use of the fact that the error has a known order.
The plan is the following:
Do calculation with h and with h/2;
Express remainder term as a power of h;
The dierence between the two results can be used to improve our nal approxima-
tion; Try to extrapolate to innitely 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: I
1
35
and I
2
. However, what we really want to know is I, that is still unknown.
I
1
= I +Ch
4
I
2
= I +C
h
4
2
4
In the above equations C is also unknown. To eliminate it, multiply by 2
4
and 1:
2
4
I
2
= 2
4
I +Ch
4

_
I
1
= I +Ch
4
_
Then subtract
2
4
I
2
I
1
= (2
4
1)I
Finally rearrange and obtain estimate of the unknown I:
I
16I
2
I
1
15
Note that we do not really need C and we have not calculated it.
4.4 Interpolation, Smoothing, Curve t, Least squares
Review
Purposes for Interpolation:
Plotting smooth curve through discrete data points
Reading between lines of table, etc.
If the data is noise corrupted (is of nite accuracy) we rather use approximation (smooth-
ing) instead of strict interpolation.
Interpolating by simple function families
Polynomials
Piecewise polynomials (splines)
Trigonometric functions (will be considered among spectral methods)
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 dierentiable. It is used
extensively to interpolate a large number of data points. Rational function is a fraction:
both the numerator and the denominator are polynomials.
36
Dierentiation and integration of noise-corrupted data
Dierentiation 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.
Approximation by least-squares: The meaning of the criterion Sum of Squares
as a measure of how good the t is.
Other requirements might include:
Smoothness,
Least number of parameters,
Extrapolating power, etc.
Two basic cases:
Model is based on rst principles
Model is just a convenient vehicle
4.4.1 Least Squares and its Numerical Aspects
When we want smooth approximation, we need some degrees of freedom. For instance,
to tting a straight line (2 parameters) to ten points leaves us with 8 degrees of freedom.
There are innitely many straight lines and we need a criterion to select a unique one.
The least squares criterion is the most popular one. It minimizes the sum of squared
deviations between observations and values calculated from the model. The LSQ straight
line t is also called linear regression line.
A word of caution: Least squares methods are very adversely aected by bad data.
For example, if a data set is basically linear but one faulty data point is a long way outside
of the linear trend of the rest of the data, then this one point can drastically change the
regression line. This happens because we square the deviation of the data point from
the line. Hence one point with a extra large deviation contributes a huge amount to the
objective function. The regression line will therefore be moved a long way towards the
one deviant point and away from the main linear trend in order to minimize the deviation
of this faulty point. This is a big factor in many engineering applications and special
methods are used to try to detect faulty data points and eliminate them. (Or to use other
possible measures of the goodness of t such as sum of absolute deviations.)
Straight line t - linear regression Suppose that some the data points should t a
straight line, y = mx + b, if it were not for experimental and measurement errors. The
problem is to nd the straight line characterized by m and

b which best ts the data
points in the least squares sense.
Model:
y = mx +b
37
Observations:
(x
1
, y
1
) (x
2
, y
2
) . . . (x
n
, y
n
)
The derivation of formulae for the slope and intercept should be familiar by now. The
nal formulae are
m =
n(

n
i=1
x
i
y
i
) (

n
i=1
x
i
) (

n
i=1
y
i
)
n(

n
i=1
x
2
i
) (

n
i=1
x
i
)
2
b =
(

n
i=1
y
i
) m(

n
i=1
x
i
)
n
Useful subroutines for straight-line t:
Option Explicit
Option Base 1
Sub StraightLineFit(x() As Double, y() As Double, m As Double, b As Double)
Dim n As Integer
n = UBound(x)
m = (n * scalprod(x, y) - sum(x) * sum(y)) / (n * scalprod(x, x) - sum(x) ^ 2)
b = (sum(y) - m * sum(x)) / n
End Sub
Function sum(x() As Double) As Double
Dim n As Integer, i As Integer
n = UBound(x)
sum=0
For i=1 to n
sum=sum + x(i)
Next
End Function
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 gure 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 dierential equa-
tions
Meaning Ordinary dierential 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 rst order
equations. The ODE f

= f(t, y) is non-autonomous. The ODE f

= f(y) is autonomous.
Solution embeds into the directional eld. 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
0
of the solution function at some point t
0
. This is called initial value. Order of ODE
determined by highest-order of derivative involved. Higher order ODEs may be narrowed
down also by using boundary values.
4.5.1 Basic methods for solving ODE
Euler Eulers method uses the forward dierence approximation for
dy
dx
and can be sum-
marized by the marching formula:
x
i
= x
i1
+h
y
i
= y
i1
+h f[x
i1
, y
i1
]
RK-4 This method for solving y

= f (x, ) , starting at (x
0
, y
0
) is one of the most popular
ones because it is quite accurate and stable. The formulae are:
x
i
= x
i1
+h
y
i
= y
i1
+
h
6
(k
1
+ 2k
2
+ 2k
3
+k
4
)
where
k
1
= f[x
i1
, y
i1
]
k
2
= f[x
i1
+
h
2
, y
i1
+
h
2
k
1
]
k
3
= f[x
i1
+
h
2
, y
i1
+
h
2
k
2
]
k
4
= f[x
i1
+h, y
i1
+h k
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
, y
n
) to the next point (x
n+1
, y
n+1
). Instead of choosing
this slope as
dy(x
n
)
dx
= f (x
n
, y
n
) 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
x
i
= x
i1
+h
y
0
i
= y
i1
+h f[x
i1
, y
i1
]
y
1
i
= y
i1
+
h
2

_
f[x
i1
, y
i1
] +f[x
i
, y
0
i
]
_
39
Iterated Heun (simplest predictor-corrector)
y
k
i
= y
i1
+
h
2

_
f[x
i1
, y
i1
] +f[x
i
, y
k1
i
]
_
continue until convergence.
This is a predictor - corrector (implicit, iterative) method. Needs a stopping criterion
for inner iteration. It is a one-step, second order method and is stable. In practice we use
higher order variants.
Single-multi step, predictor-corrector, explicit-implicit So far weve considered
self-starting (one step) methods. They use only y
i
. Multiple-step methods use more
than one previously calculated value: that is to use y
i
and y
i1
etc. The traditional
predictor-corrector solution methods use one equation to predict the value of y
n+1
using
the previously computed y
n
, and other previously computed y
i
values. This is followed
by a second equation which is used to correct the value given by the rst equation. They
provide some improvement especially for sti problems, but need a jump-starter (for
instance RK4)!
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.
Adaptive: step size is adapted automatically from error estimate.
A method is called implicit, if some kind of iteration is involved. Predictor-corrector
methods are implicit.
A sti 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
Methods related to
multi-variable problems
5.1 Matrices, vectors
Whats an Array? Whats a Matrix? Whats the Dierence? An array is a collection of
objects, each occupying a position relative to the other objects in the collection. An array
of antennas, for example, usually refers to a group of antennas laid-out in either a straight
line, or in a plane. The objects of the array can be real, physical objects like antennas or
spark plugs, or intangible objects like numbers. When an engineer talks about a matrix,
she is generally talking about a numerical array. There is the further implication that the
laws of Matrix Algebra dictate how an array can be combined with and/or operated on
by other numerical arrays. Although the matrices encountered by engineering students
are usually one-dimensional (numbers positioned in a straight line) or two-dimensional
(numbers positioned in a plane), they can be higher-dimensional, as well.
A subroutine to get the scalar product of two vectors
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
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
A subroutine to multiply matrix A by a (column) vector b:
Sub matvecprod(a() As Double, b() As Double, c() As Double)
Dim row As Integer, col As Integer
Dim i As Integer, j As Integer
Dim s As Double
row = UBound(a, 1)
col = UBound(a, 2)
If ( row < Ubound(b) OR row < Ubound(c) ) Then
MsgBox ("matvecprod: vector b or c is not compatible with matrix a")
Stop
End If
For i = 1 To row
s = 0
For j = 1 To col
s = s + a(i, j) * b(j)
Next j
c(i) = s
Next i
End Sub
Matrix is lower triangular if all entries above main diagonal are zero: a
ij
= 0 for i < j.
Matrix is upper triangular if all entries below main diagonal are zero: a
ij
= 0 for i > j.
42
Review linear dependence and rank
5.2 System of linear equations
Given m n matrix A and m-vector b, and unknown n-vector x satisfying Ax = b.
System of equations asks: Can b be expressed as linear combination of columns of A? If
so, coecients of linear combination given by components of solution vector x.
Example 1 Consider the system
2x 3y = 8
4x 5y = 16
The determinant is non-zero. Two lines crossing at one point.
Example 2 Consider the system
2x 3y = 8
4x 6y = 16
Thus the determinant is 2 6 3 4 = 0. And indeed the second equation is 2 times
the rst. Geometrically this corresponds to two coincident lines. In terms of rank: the
coecient matrix has rank 1, the augmented matrix has rank 1.
Example 3 Consider the system
2x 3y = 8
4x 6y = 17
Thus the determinant is 2 6 3 4 = 0. There is no solution. Geometrically this
corresponds to two parallel lines never crossing. In terms of rank: the coecient matrix
has rank 1, the augmented matrix has rank 2.
Existence and Uniqueness Statement with Rank The rank of a matrix tells us how
many independent rows (or how many independent columns) the coecient matrix has
it may be less than the total number of equations.
If rank of the coecient matrix is less than the number of equations, two cases can
happen:
Consistent (if rank of coe matrix = rank of augmented matrix): Innite number
of solutions
Non-Consistent (if rank of coe matrix < rank of augmented matrix): No solution
We can we express the solution with the inverse of the coecient matrix, as
x = A
1
b
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.
x
1
+ 2x
2
3x
3
= 5
2x
1
3x
2
+x
3
= 3
x
1
+x
2
2x
3
= 0
_
_
_
becomes
_
_
1 2 3
2 3 1
1 1 2
_
_
_
_
x
1
x
2
x
3
_
_
=
_
_
5
3
0
_
_
To solve the system a method (algorithm) called Gaussian Elimination is used, starting
with the augmented matrix containing the coecient matrix on the left with one extra
column containing the right hand side values (a similar method is used to nd 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) row 2 to row 1) is the same as subtracting
the positive multiple (subtracting 2 row 2 from row 1 so that this row operation also
includes subtraction).
The objective is to change the coecient 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 rst 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
= R
2
+ (2) R
1
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 3 5
2 3 1 3
1 1 2 0
_

_
R

2
= R
2
+ (2) R
1
R

3
= R
3
+R
1

_
1 2 3 5
0 7 7 7
0 3 5 5
_

_
R

3
= R
3
+
_
3
7
_
R
2

_
_
1 2 3 5
0 7 7 7
0 0 2 2
_
_
This has created an upper triangular matrix (the rst three columns). To further
simplify this, divide the second row by 7 and the third row by 2, creating an echelon
44
form. The corresponding equations can easily be solved by starting with the last equation
which gives the x
3
value, then nding x
2
from the next equation up and so on (back
substitution):
_
_
1 2 3 5
0 1 1 1
0 0 1 1
_
_

x
1
+ 2x
2
3x
3
= 5
x
2
x
3
= 1
x
3
= 1

x
1
= 5 + 3x
3
2x
2
= 2
x
2
= 1 +x
3
= 0
x
3
= 1
Solve third
Solve second
Solve rst
This version is called naive elimination, because we assumed that we can always divide
by the diagonal element in the next row (e.g. 7 in iteration No two.)
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 o from the last column.
Option Explicit
Option Base 1
Sub GaussJordan(A() As Double) Augmented matrix
Dim PivElt As Double, TarElt As Double
Dim n As Integer number of equations
Dim PivRow As Integer, TarRow As Integer pivot row; target row
Dim i As Integer, j As Integer
n = UBound(A, 1)
For PivRow = 1 To n process every row
PivElt = A(PivRow, PivRow) choose pivot element
If PivElt = 0 Then
MsgBox ("Zero pivot element encountered")
Stop
End If
For j = 1 To n + 1
A(PivRow, j) = A(PivRow, j) / PivElt divide whole row
Next
For TarRow = 1 To n now replace all other rows
If Not (TarRow = PivRow) Then
TarElt = A(TarRow, PivRow)
For j = 1 To n + 1
A(TarRow, j) = A(TarRow, j) - A(PivRow, j) * TarElt
Next j
End If
Next TarRow
Next PivRow
End Sub
Answer the questions: Where is the coecient matrix at input? Where is the right
hand side?
At output: Where is the solution? What happened to the coecient matrix?
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 dierent pivot choices than the diagonal values used in
the rst 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 rst one operates on the coecient matrix and
does the computationally demanding half of the job; the second one (called substitution)
uses the results of the rst part to obtain a solution to a given right-hand side.
Note that the decomposition destroys the original coecient 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
Sub VBA092() LU decomposition-substitution driver
Dim n As Integer
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
ReDim A(n, n) As Double, b(n) As Double, x(n) As Double
ReDim Indx(n) 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
Call LUDecompose(A, Indx)
Call LUSubstitute(A, Indx, b, x)
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 coecient 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 coecient 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
Dim n As Integer n =UBound(A, 1)
Dim i As Integer, j As Integer, k As Integer
Dim dum As Double
For i = 1 To n
x(i) = b(i)
Next i
For k = 1 To n - 1
i = Indx(k)
dum = x(i)
x(i) = x(k)
x(k) = dum
For j = k + 1 To n
x(j) = x(j) + A(j, k) * dum
Next j
Next k
For k = n To 1 Step -1
x(k) = x(k) / A(k, k)
For j = 1 To k - 1
x(j) = x(j) - A(j, k) * x(k)
Next j
Next k
End Sub
Sparse systems Tridiagonal: Thomas algorithm
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 eective algorithm is due to Thomas.
e.g. Given the tridiagonal matrix shown store the three diagonals in a three column array
as shown:
A =
_

_
2 1 0 0 0
1 2 1 0 0
0 2 4 0 0
0 0 1 3 2
0 0 0 2 4
_

a : 0 1 2 1 2
b : 2 2 4 3 4
c : 1 1 0 2 0
The storage saving in the above example is only from 25 down to 15. However in larger
matrices the reduction is very signicant. For an nn matrix with n
2
entries the storage
will be reduced to 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.
Direct vs Iterative Gauss elimination, Gauss-Jordan elimination, LU-decomposition/substitution
are DIRECT methods. In direct methods we can predict the number of multiplications
and additions if the number of equations (n) is known.
Iterative methods are generalization of the direct iteration we learned for one variable
x = g(x) equations. Iterative methods are used for very large but sparse systems (reservoir
simulation). You can not predict the number of operations for an indirect method and
you need a starting guess and a convergence criterion. The iterative method we are going
to learn is the Gauss-Seidel method.
The method is illustrated for a general 3 3 system. The strategy is to express x
1
from the rst row, x
2
from the second row, etc. Then start with a guess and calculate a
new x
1
from the rst equation. Then with the best available approximants calculate x
2
and so forth. In other words, as soon as a variable is updated in one equation its new
value is used on the right hand side of the next equation:
a
11
x
1
+a
12
x
2
+a
13
x
3
= b
1
a
21
x
1
+a
22
x
2
+a
23
x
3
= b
2
a
31
x
1
+a
32
x
2
+a
33
x
3
= b
3
_
_
_

a
11
x
1
= b
1
a
12
x
2
a
13
x
3
a
22
x
2
= b
2
a
21
x
1
a
23
x
3
a
33
x
3
= b
3
a
31
x
1
a
32
x
2
_
_
_

a
11
x
k+1
1
= b
1
a
12
x
k
2
a
13
x
k
3
a
22
x
k+1
2
= b
2
a
21
x
k+1
1
a
23
x
k
3
a
33
x
k+1
3
= b
3
a
31
x
k+1
1
a
32
x
k+1
2
_
_
_
It works ne on diagonally dominant matrices.
Diagonally dominant matrices: the absolute value of the diagonal element is greater
than the sum of the absolute values of all the other elements in a given row. Identity
matrix is diagonally dominant
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 dierentiated analytically. Note that bold-face denotes matrix and vector. We
have n equations to solve and n components of the unknown to nd.
If an initial guess is known, and at that location we can calculate the function f (x
0
)
and its partial derivatives collected into the Jacobian matrix J(x
0
). The rst element in
the rst row of the Jacobian matrix will contain the partial derivative of f
1
with respect
to x
1
, the second element in the rst row will contain the partial derivative of f
1
with
respect to x
2
, and so on.
Writing the equation of the tangent plane:
f (x
0
) +J(x
0
)(x x
0
) = 0
Substituting y = 0 we nd that the line crosses the x axis at x
1
where
(x
1
x
0
) = x = [J(x
0
)]
1
f (x
0
)
This is the iteration formula of the Newtons method. The obtained x
1
can be then
renamed to x
0
and hence we can continue the iterations.
In practice we do not use the matrix inverse, we just solve
J(x
0
)x = f (x
0
)
for x using LU Decomposition.
Cautious Approach: We calculate the x and multiply it by a less than one 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 dened unit, and eps has to correspond to it.
If the elements of x are of dierent dimensions or vary several orders of magnitude,
use normalization
N/R variants:
If the Jacobian is not available analytically, we can estimate it by nite dierence 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
f
1
= x
1
+x
2
2
3
f
2
=
x
1
x
2
+ 2
We will use a VBA program. The input is from the spreadsheet:
kmax from Cells(1, 2), maximum number of iterations, (20)
eps from Cells(2, 2), tolerance, (1.0E-6)
alpha from Cells(3, 2), , ( 1 )
starting point x
1
and x
2
, 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
f(x) min where f is scalar, x is vector
There are two basic types: unconstrained and constrained.
Visualization: Surface plot and contur plot.
Most frequent application: Nonlinear loeast-squares t
Equivalence (for unconstrained and dierentiable) in principle equivalent:
f(x) > min and gradient = 0
(Just to solve gradient = 0 is somewhat dangerous, it is better watch f(x) )
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 t)
Generalized Linear Least Squares: after transform of variables straight line t (max
2 parameters)
y = mx +b
more than two variables but still linear
y = p
1
x
1
+p
2
x
3
+p
3
x
1
x
3
because the parameters p
1
, p
2
, p
3
go into the model linearly.
Nonlinear least squares: several parameters p
1
, p
2
, p
3
. . ., the dependence of the
response is nonlinear on them
y =
p
1
x
1
1 +p
2
x
1
+p
3
x
2
53
Observations:
(x
1,1
, x
1,2
, y
1
) (x
2,1
, x
2,2
, y
2
) . . . (x
n,1
, x
n,2
, y
n
)
The Objective function is:
Q =
_
y
1

p
1
x
1,1
1 +p
2
x
1,1
+p
3
1, 2
_
+
_
y
2

p
1
x
2,1
1 +p
2
x
2,1
+p
3
x
2,2
_
+. . .+
_
y
n

p
1
x
n,1
1 +p
2
x
n,1
+p
3
x
n,2
_
The minimization problem is: nd p
1
, p
2
, p
3
which minimizes the objective function
Q, is usually solved by a special method called Gauss-Newton-Marquardt.
Example
Given the observations:
x1 x2 y
1 2 1.154
3 4 2.045
5 4 3.125
7 2 5.526
The estimated parameters are: p
1
= 3.26, p
2
= 0.225, p
3
= 0.777
Excels all-purpose Solver is also an ecient tool to minimize the objective function
(even when additional constraints have to be taken into account.)
5.6 Solution of system of ordinary dierential equa-
tions: RK4
A system of rst order equations has one function variable (the variable being dif-
ferentiated) for each equation in the system and all of these function variables are
functions of one independent variable such as x or t. The Initial Value Problem
(IVP) is of the form
dy
1
dx
= f
1
(x, y
1
, y
2
)
dy
2
dx
= f
2
(x, y
1
, y
2
)
_
with initial conditons, for some known a, k
1
, k
2
:
y
1
(a) = y
0
1
, y
2
(a) = y
0
2
where the right hand side functions f
1
, f
2
are any functions of the independent
variable x and the two function variables y
1
, y
2
(but not of the derivatives of these).
A two equation system is shown above, but it can be extended in the obvious way
to more dierential equations.
The algorithm now involves vectors (watch for boldface):
x
i
= x
i1
+h
y
i
= y
i1
+
h
6
(k
1
+2k
2
+2k
3
+k
4
)
where
k
1
= f [x
i1
, y
i1
]
k
2
= f [x
i1
+
h
2
, y
i1
+
h
2
k
1
]
k
3
= f [x
i1
+
h
2
, y
i1
+
h
2
k
2
]
k
4
= f [x
i1
+h, y
i1
+h k
3
]
The vector operations can be done by simple loops.
54
5.7 Partial dierential equations and reservoir sim-
ulation
Review
Physical meaning (Often from conservation principles)
Initial and boundary conditions.
Transient versus steady-State
With initial-value problem, solution is obtained by starting with initial values and
marching forward in time, step by step, generating successive rows in the solution
table.
Coordinate systems: Cartesian or Radial
Classication
Heat equation: u
t
= u
xx
(parabolic)
Wave equation: u
tt
= u
xx
(hyperbolic)
Laplace equation: u
xx
+u
yy
= 0 (elliptic)
Numerical approaches Finite dierence, nite element, Semi-analytic (transfor-
mation)
Finite dierence approximation of derivatives
The diusivity equation includes derivatives of pressure in space and in time.
To solve the diusivity equation numerically we must nd ways to represent these
derivatives.
Getting the derivatives approximated correctly is an important part of getting an
accurate numerical solution.
Transient solution of the diusivity equation: Finite dierence implicit
Method Linear reservoir, constant pressure boundaries.

2
p
x
2
=
p
t
where
=
k
c
t
porosity
viscosity
c
t
compressibility (total)
k permeability
We want to solve the transient problem: The evolution of the pressure eld, if the
initial condition is constant pressure p
i
and the pressures at the two ends are kept
constant, p
left
and p
right
, respectively.
55
First discretize the region on interest over both space: x
1
, x
2
, . . . , x
i
, . . . , x
nx
and
time:t
0
, t
1
, . . . , t
n
, . . . , t
nt
.
Replace the analytical derivatives by numerical approximations.
Approximation of the second partial derivative in the x-direction: (central):
p
i+1
2p
i
+p
i1
(x)
2
Approximation of the rst partial derivative in time between time levels n and n+1:
p
i
n+1
p
i
n
t
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.

p
n+1
i+1
2p
n+1
i
+p
n+1
i1
(x)
2
=
p
i
n+1
p
i
n
t
It is useful to introduce a new notation
=
1

(x)
2
t
Then the system of equations become:
p
n+1
1
= given p
left
p
n+1
i+1
(2 +)p
n+1
i
+p
n+1
i1
= p
i
n
for i = 2, 3, ... n-1
p
n+1
n
= given p
right
This is a tridiagonal system with the unknowns:
p
n+1
1
, p
n+1
2
, . . . p
n+1
n1
, p
n+1
n
In matrix representation (for n = 5, as an example):
_

_
b
1
c
1
0 0 0
a
2
b
2
c
2
0 0
0 a
3
b
3
c
3
0
0 0 a
4
b
4
c
4
0 0 0 a
5
b
5
_

_
_

_
p
1
p
2
p
3
p
4
p
5
_

_
=
_

_
d
1
d
2
d
3
d
4
d
5
_

_
For instance, b
1
= 1, c
1
= 0 and d
1
is nothing else, but the given p
left
. The system
can be solved by the Thomas algorithm.
56
Example data
porosity 0.2
viscosity 0.8 cp
compressibility ct 1 10
5
1/psi
permeability k 5 md
reservoir length L 3000 ft
initial pressure p
i
2800 psi
pressure at x = 0 ft p
left
1000 psi
pressure at x =3000 ft p
right
2800 psi
time t 0-100 days
With the units indicated,
=
0.00633k
c
t
and hence
=
_
c
t
0.00633k
_
(x)
2
t
Choose appropriate nx and nt.
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)
i = i + 1: .Cells(i, 1) = "Viscosity (cp): ": mu = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Compressibility (1/psi): ": ct = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Permeability (md): ": k = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Length of reservoir (ft): ": xlen = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Initial pressures (psi): ": pini = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Left pressure (psi): ": pleft = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Right pressure (psi): ": pright = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Number of grid points in x: ": nx = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "End time (day): ": tend = .Cells(i, 2)
i = i + 1: .Cells(i, 1) = "Number of time steps: ": nt = .Cells(i, 2)
i = i + 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
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 solu-
tion algorithm. Experiment with the time step. Verify the following statement:
Stability Implicit: stable Explicit: at less than 2 it is unstable!
5.7.1 Reservoir Simulation
Gridblock (Finite Volume) approach (Two dimensional: x and y direction)
Oil Material Balance Equation
Oil in place in block i =
N =
V
p
S
o
B
o
where
V
p
= xyh
Oil in place changes from
_
V
p
S
o
B
o
_
n
to
_
V
p
S
o
B
o
_
n+1
during timestep n+1. Thus, the rate of accumulation is
1
t
_
_
V
p
S
o
B
o
_
n+1

_
V
p
S
o
B
o
_
n
_
Flow from block i+1 to block i (EAST direction):
q
i+
1
2
=
0.00633kk
ro
hy

o
B
o
_
p
i+1
pi
x
_
= T
E
_
k
r
B
_
oE
(p
i+1
p
i
)
A similar equation can be written from the WEST direction.
The net inow is:
T
W
_
k
r
B
_
oW
(p
i1
p
i
) +T
E
_
k
r
B
_
oE
(p
i+1
p
i
)
Including a possible well term q
o
and the previously determined accumulation term
we obtain the gridblock (cell) balance
59
1
t
_
_
V
p
S
o
B
o
_
n+1

_
V
p
S
o
B
o
_
n
_
+q
o
=
1
t
_
_
V
p
S
o
B
o
_
n+1

_
V
p
S
o
B
o
_
n
_
Flow term can be implicit (n + 1) or explicit (n).
We have similar equations for water and gas.
The most important unknowns are the cell pressures. Often we solve an implicit
scheme for the pressures, then update everything else (saturations) by an explicit
scheme (IMPES).
Special issues: Well models, Fully implicit schemes, various solvers, ADI (method
of alternate directions)
60
Chapter 6
Integral Transforms, Spectral
methods, inversion of the
Laplace transform
6.1 Transformations
Function to function (Laplace)
Data set to data set (Discrete Fourier Transform)
Spectral methods (Seismics, Ultra-Sound etc)
FFT algorithm
6.2 Laplace transform
Forward and Inverse Transform
Some Rules
Analytical integration or numerical solution?
6.2.1 Numerical inversion of the Laplace transform
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
s+0.5
e
0.5t
1

s
1

t
arctan
0.1
s
1
sin
0.1t
t
The main program rst 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:
Call StCalc(t, ft)
The whole driving program is only a couple of lines:
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
Private Stn As Integer
Private Stc() As Double
Function Min(i1 As Integer, i2 As Integer)
If i1 < i2 Then Min = i1 Else Min = i2
End Function
Function Max(i1 As Integer, i2 As Integer)
If i1 > i2 Then Max = i1 Else Max = i2
End Function
Sub StInit(n As Integer)
Stn = Max(2, Min(16, 2 * Int(n / 2)))
ReDim Stc(Stn) As Double
ReDim Fact(Stn) As Double
Dim n2 As Integer, i As Integer, k As Integer
Dim ck As Double, sk As Double
n2 = Stn / 2
Fact(0) = 1
For i = 1 To Stn
Fact(i) = i * Fact(i - 1)
Next i
For i = 1 To Stn
ck = 0
For k = Int((i + 1) / 2) To Min(i, n2)
sk = k
ck = ck + sk^n2 *Fact(2*k)/(Fact(n2-k)*Fact(k)*Fact(k-1)*Fact(i-k)*Fact(2*k-i))
Next k
Stc(i) = (-1) ^ (i + n2) * ck
Next i
End Sub
Sub StCalc(t As Double, ft As Double)
Dim ln2pt As Double, s As Double, F As Double
Dim i As Integer
ln2pt = Log(2#) / t
ft = 0
For i = 1 To Stn
s = i * ln2pt
Call Fun(s, F)
ft = ft + Stc(i) * F
Next i
ft = ln2pt * ft
End Sub
63
Part IV
Appendix
64
Chapter 7
Study guide
7.1 Be able to ...
1. Compare scope of module level and procedure level variables
2. Use arrays
3. Write meaningful comments to code.
4. Subdivide a programming project into smaller units
5. Use defensive programming strategies to check for obvious errors in input pa-
rameters.
6. Use the interactive debugger to trace logic and numerical errors in code
7. Give a simple explanation of the term oating point number.
8. Give a denition of roundo error.
9. Give a simple example of overow.
10. Identify the truncation error from a Taylor series expansion.
11. Identify (at least) two important dierences between symbolic and numeric
computations.
12. Estimate the error of a simple calculation if errors associated with the input
are given
13. Write any scalar equation in the form f(x) = 0
14. Dene multiple root.
15. Explain the role of bracketing.
16. Write a simple logical expression that expresses the condition for an interval
to contain (bracket) the root,
17. Manually perform a few steps of the bisection method.
18. Identify situations that cause Newtons method to fail.
19. Calculate the number of steps for the bisection method to reduce the uncer-
tainty interval x-times
65
20. Qualitatively compare the convergence rates of bisection, false-position and
Newtons method
21. Plot any f(x) as a means of graphically identifying the location of roots.
22. Describe the role of global variables in nding 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 xed values of a, b, . . .
23. Compare eects of noise on numerical dierentiation and integration
24. Derive the approximation order of a simple nite dierence formula
25. Manually perform multiple application of Simpsons rule
26. Manually perform Richardson extrapolation
27. Recognize if a least squares problem is linear, can be linearized, is nonlinear
28. Be able to determine degrees of freedom
29. Identify connection between linearized and original model parameters
30. Identify the criteria for obtaining a least squares t. (What is minimized?)
31. Manually evaluate the formulas for the slope and intercept of a line t to data.
32. Derive the transformations for tting data to non-linear two-parameter func-
tions of various type.
33. Make the connection between slope and intercept of the linearized model with
the original model parameters.
34. Recognize initial value versus boundary value problem
35. Manually calculate a few steps of Eulers and Heuns methods
36. Program the RK-4 method
37. Describe concepts: single-multi step (self starting or not), predictor-corrector
38. Express conditions of linear dependence of vectors (row or column) in terms of
matrix rank and vice-versa.
39. Explain the condition of singularity of an n n matrix in terms of linear inde-
pendence.
40. Express matrix rank as a measure of linear independence.
41. Write the formal solution to Ax = b.
42. Explain why it is not a good idea to use the formal solution as a computational
procedure for solving Ax = b.
43. Name the solution algorithm most commonly used for solving Ax = b.
44. Describe the reason for pivoting.
45. Answer the question: Is pivoting a remedy for ill-conditioned systems?
46. 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?
47. Convert a higher order ODE to an equivalent system of coupled rst order
ODEs.
66
48. Use Newton-Raphson method for a system of nonlinear equations
49. Recognize the basic PDE types
50. Relate transient to steady state
51. Name methods to solve PDEs numerically
52. Solve the transient diusivity equation by implicit nite dierence
53. Be able to use the Thomas algorithm
54. Describe basic approaches to Reservoir simulation
55. Discuss explicit versus implicit approach
56. Work with units involved
57. Discuss relation between well scheduling and boundary conditions
58. Be able to use a simulator and interpret results
59. Have an overview of spectral methods
60. Be able to use an available module (in this case for numerical Laplace inversion)
67
Chapter 8
Assignments, test problems
8.1 Error propagation: borehole volume
A 6000 ft ( 1 %) deep borehole has a diameter: 5 inch ( 0.04 inch). What is the
volume in bbl? (Give your answer with error estimate!)
8.2 Error propagation: one-gallon cube
What is the side length of a one-gallon (5 %) cube? Indicate the unit and error
estimate of your answer!
8.3 Error propagation: barrel
Consider a right circular cylinder that has a volume 1 BBL (British barrel) (0.06 %).
The height is exactly twice the diameter. Estimate the diameter, indicating the unit
and the uncertainty.
8.4 Error propagation: hydrocarbon volume
The volume occupied by hydrocarbons in a certain reservoir can be calculated from
V
HC
= V (1S
w
) where V is the reservoir volume, is the porosity, and S
w
is the
water saturation.
Assuming V = 2.5 10
5
ft
3
(15%) and S
w
= 0.65 ( 0.12) calculate the hydro-
carbon volume and give both the absolute and the relative errors of the calculated
value. Note that the water saturation error is given as an absolute error!
68
8.5 Root nding; Newton iteration: z factor
An equation of state written in z-factor form results in the following nonlinear
equation to be solved:
z
4
+ 2.500z 1.300 = 0
You use Newtons method to nd the root, starting from z = 1.000 (we will call it
the zero-th approximation of the root.) Carry out the rst iteration step and give
the rst approximation of the root. (Do not continue the iteration process!)
8.6 Root nding: solution of the PR equation of
state
he Peng-Robinson equation is one of the cubic equations of state:
_
p +
a
t
V
M
(V
M
+b) +b(V
M
b)
_
(V
M
b) = RT
where R is the universal gas constant, p is the pressure, T is the temperature, V
M
is the molar volume, and a
t
and b are dened as follows:
b = 0.07780
RT
C
p
C
a
C
= 0.45724
R
2
T
2
C
p
C
a
t
= a
C
_
1 + (0.37464 + 1.54226 0.26992
2
)(1
_
T/T
C
)
_
2
It is somewhat easier to work with dimensionless quantities such as the deviation
factor z =
pV
M
RT
. Then the equation takes the form
z
3
+
_
bp
RT
1
_
z
2
+
_
p
_
a
t
R
2
T
2

2b
RT
_

3b
2
p
2
R
2
T
2
_
z+p
2
_

a
t
b
R
3
T
3
+
b
2
R
2
T
2
_
+
b
3
p
3
R
3
T
3
= 0
or simply :
z
3
+c
2
z
2
+c
1
z +c
0
= 0
where the coecients can be read of from the previous expression.
A substance is characterized by three properties: critical temperature, T
C
, critical
pressure, p
C
and acentric factor, .
69
For n-butane:
T
C
= 425.2 K
p
C
= 3.75 MPa
= 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
o
C 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.)
8.7 Root nding: heat balance
The enthalpy of 1 kg methane can be calculated from the following polynomial:
h = 4662.+1.983T0.001031T
2
+4.24310
6
T
3
2.94610
9
T
4
+7.21810
13
T
5
where h is the (specic) enthalpy in kJ/kg and T is the absolute temperature in K.
We wish to determine the temperature of 5 kg methane in the nal state, if the
initial state is 300 K and the enthalpy is increased by 12,000 kJ.
8.8 Root nding: friction factor
The Darcy/Moody friction factor (f) is a function of the Reynolds number, Re and
the relative roughness / D . If the Reynolds number is less than (or equal to) 2100,
then the ow regime is laminar and f = 64 /Re . If the Reynolds number is greater
than 2100, then the friction factor can be obtained solving the following nonlinear
equation (called Colebrook equation):
1

f
= 2.0log
10
_
/D
3.7
+
2.51
Re

f
_
Write a complete VBA function that has two inputs: the values of Re and /D. It
should calculate the friction factor, f.
Hint: For bracketing methods choose f as unknown, for open methods choose ln(f).
Why?
70
8.9 Root nding: Well deliverability
The Inow Performance Relationship (IPR) gives the production rate depending on
the reservoir pressure and wellbore owing pressure. It characterizes the ability of
the reservoir to deliver. For instance:
q
IPR
= 4.38
_
p
2
p
2
wf
_
0.62
where the production rate q is in MSCF/D and both the average reservoir pressure
( p ) and wellbore owing pressure ( p
wf
) are in psi.
The Tubing Performance Relationship (TPR) gives the production rate depending
on wellbore owing pressure. It characterizes the ability of the wellbore to provide
vertical lift. For instance:
q
TPR
= 93.68
_
p
2
wf
10
6
_
0.5
From the TPR it can be seen, that (in this case) the minimum well bore owing
pressure to provide natural lift is 10
3
psi.
In steady-state production q
IPR
= q
TPR
.
Find the production rate at various reservoir average pressures. Hint: rst rearrange
the equation into f(x)=0 form.
8.10 Root nding: isoterm ash
n the simplest isoterm ash calculation we know the inlet composition of the mixture,
z
1
, z
2
,..., z
n
(mole fractions) and the distribution factors K
1
, K
2
,..., K
n
. We wish
to nd the mole fraction of vapor, V and the composition of the vapor phase ( y
i
)
and the composition of the liquid phase ( x
i
).
The equlibrium relations are:
K
i
=
y
i
x
i
The material balance relations are:
y
i
V +x
i
(1 V ) = z
i
Rachford combined all these equations into one nonlinear equation with one un-
known:
n

i=1
=
(K
i
1)z
i
(K
i
1)V + 1
= 0
Once solved for V the composition of the individual phases can be calculated from
y
i
=
K
i
z
i
(K
i
1)V + 1
x
i
=
z
i
(K
i
1)V + 1
71
Solve the problem for the following input data:
Component K
i
z
i
C
1
61.0 0.3396
C
2
9.0 0.0646
C
6
0.035 0.6068
8.11 Optimization: Maximizing Net Present Value
One MSCF additionally produced gas brings 3.5 US$ revenue. Since the revenue
is realized at the end of the year, it has to be discounted. For optimum sizing a
well stimulation treatment we need to know the costs and benets. Let us consider
matrix acidizing.
The size of the treatment is best characterized by the volume of acid spent. All costs
depend on this extensive variable. The benets can be measured by the revenue from
the additional oil production in the next three years. However, the revenue we get
back in the future is less valuable than the dollar we spend today, hence it has to
be discounted depending on the year it is realized in. The discount ratio is 1.2 .
NPV is the dierence of discounted revenue and cost. Our goal is to maximize the
NPV.
Cost of one gallon of organic acid mixture is 34 US$. Pumping charge 9 US$ /gal.
Fixed cost is 10,000 US$ for a single treatment. Total cost (in US$) depends on the
acid spent: v (in gal):
cost = 10, 000 + (34 + 9)v
Benet comes from increased production rate. The production rate in MSCF/D is
given by
q =
30, 000
6.3 +s
where v is the acid spent, and s is the skin factor modied by the acid treatment.
The pre-treatment skin factor is 7. Depending on the injected volume, the skin
factor decreases according to the relation
s =
7
1 + 0.1v
1/3
(but never becomes negative.)
The discounted benet for a 3-year horizon is therefore expressed (in US$) as
benefit =
3

i=1
3.5
1.2
i
365
_
30, 000
6.3 +
7
1+0.1v
1/3

30, 000
6.3 + 7
_
Find the optimum amount of acid to spend on this well!
72
8.12 Integration of a function: PV
The production rate q (STBD) from an oil well declines according to the Hyperbolic
Decline Law:
q =
q
n
i
(1 +Knt)
1
n
where
K = 0.00207
1
day
is the decline constant
n = 0.3 is the Arp exponent
q
i
=100 STBD is the initial production rate
t is the number of days elapsed from the start of the production
It can be shown that for the given decline the time when the production rate reaches
the economic limit of 20 STBD is t
a
= 1000 days.
If we wish to calculate the discounted revenue (Present Value) of the production, we
have to multiply produced quantities by the oil price, c ($/STB). A dollar tomorrow
is, however, less than a dollar today so we have to discount the revenue using a
yearly discount rate, i (% /Year) and a discounting method (which is continuous
in this case.)
c = 25 $/STB
i = 12 % /Year
Using the continuous discounting method the Present Value of one barrel oil pro-
duced in the future, t (days) from now, can be expressed by
ce

i
365
t
The Present Value of the cumulative production from now to abandonment can
be calculated from the integral
PV =
_
t
a
0
ce

i
365
t
q
n
i
(1 +Knt)
1
n
dt
8.13 Numerical integration of discrete data: pore
volume
A reservoir of area, A = 3208 acre ( 1.3974 10
8
ft
2
) has a pay thickness of 40 ft. The
pore space is completely lled 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.
8.14 Straight-line: formation volume factor model
1
Given: p
b
= 2012 psi, bubble point pressure
Data (observed):
p psi B
o
resBBL/STB
1500 1.262
1600 1.279
1800 1.298
Determine the parameters of the nonlinear model describing the Bo:
B
o
=
1
a +b

p
b
p
What is the best estimate of the B
o
at the bubble point?
8.15 Straight-line: formation volume factor model
2
Consider the following model of Formation Volume Factor, B
o
as a function of
pressure, p.
B
o
= ae
b(pp
b
)
where B
o
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 nd. The following
laboratory data are available:
p psi B
o
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 (eld) pressure data for a gas eld is given below. (Craft and
Hawkins)
p/z, psia G
p
(MMM SCF)
6553 0.393
6468 1.642
6393 3.226
6329 4.260
6246 5.504
6136 7.539
6080 8.749
The material balance for a volumetric gas reservoir is
p
z
=
p
i
z
i

_
p
i
z
i
G
i
_
G
p
where p (psia) is static eld pressure, z (-) is deviation factor for corresponding to
the pressure, G
i
(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 t! Hint: from the straight line form
estimate slope and intercept, then interpret them to obtain rst p
i
/z
i
then G
i
.
Alternative linear form:
G
p
= G
_
z
i
G
i
p
i
_
p
z
8.17 Straight-line: Flow-After-Flow Test (IPR)
A frequently used IPR equation:
q = q
max
_
1
_
p
wf
p
_
2
_
n
where q production rate, BOPD
p average reservoir pressure, psi
p
wf
welbore owing pressure, psi
Data (observed):
p
wf
psi q BOPD
2500 109.9
2000 192.5
1500 258.7
1000 309.2
75
Find the Absolute Open Flow Potential:
Hint: ll out the following table rst.
Original model Linearized model
Parameter 1 q
max
m
Parameter 2 n b
Independent variable p x
Dependent variable q y
8.18 Nonlinear least squares: oil viscosity as a func-
tion of pressure and temperature
Consider the following model of oil viscosity (
o
) for a certain eld as a function of
pore pressure, p and layer temperature T:

o
= a T
b
_
1 +c p
1.5
_
where
o
is in cp, p in psia, T in
o
R.
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).
The available data are:
p, psi T,
o
R
o
, 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 rst line of the function should read as:
function SSQ(a As Double, b As Double, c As Double) As Double
Use global (module-level) variables for all data (except for the three model param-
eters: a, b, c which are arguments of the function.)
8.19 ODE: Production rate decline from a dier-
ential equation
Consider the following dierential equation describing the production rate decline
of a well:
76
fracdq
o
dt =
0.00025
1 + 0.1 t
0.1
q
1.5
where q
o
is the production rate in BOPD and t is time in days. The initial production
rate is 498 BOPD. (The factor
0.00025
1+0.1t
0.1
describes the decrease of ow eciency 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.)
8.20 ODE: pressure versus depth in a shut-in well
A gas well is shut-in at the wellhead (no ow). Write a dierential equation for
the pressure assuming the deviation factor is a known function of pressure. The
temperature can be considered constant.
Density =
M
V
= M
p
RT
kg/m
3
Molar mass of gas M 0.27 kg/mol
Temperature T 375 K
Deviation factor z =
1
1+c
1
p
c
1
= 3 10
6 1
Pa
Pressure at the surface p
0
3.2 10
6
Pa
The solution of the dierential 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 uid with density =
0
(1 +c
f
p) where
0
= 235 kg/m
3
and c
f
= 0.310
6
Pa
1
and the pressure at the surface is as before: p
0
= 3.2 10
6
Pa
.
8.21 Meaning of matrices and vectors: chemical
component balance
(Right a conservation equation in matrix-vector form, for instance for a distillation
column.)
8.22 System of linear equations: mixing
Three petroleum products P
1
, P
2
and P
3
are available. The Octane number deter-
mined by two independent methods: research (RO) and motor (MO) are shown in
the following table:
77
P
1
P
2
P
3
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 uids is the same, corresponding mass fractions and volume
fractions are also the same.)
Select a reasonable notation for the mass fractions
Write a system of equations for the unknown fractions.
If there are less equations than variables, think about one using the denition of
mass fractions.
If there are more equations than variables, think about dependence and delete the
unnecessary equation.)
Is there a unique solution?
Are the fractions all positive? Do they sum up yielding one?
8.23 System of linear equations with many right-
hand-sides: log interpretation
A basic approach to determine lithology of simple formations is neutron-density
crossplot. Let V
1
, V
2
and V
3
denote the volume fractions of sand, clay and pore
space (which is assumed to be lled by water). The sum of the volumes fractions is,
of course, unity. Let
N
denote the neutron value (dimensionless) and the density
in (g/cm
3
). (Note: the ocial SI density unit is kg/m
3
. As we all know 1 g/cm
3
= 1000 kg/m
3
) The log evaluation is based on the simultaneous application of the
two relations:

N
= 2V
1
+ 37V
2
+ 100V
3
= 2.65V
1
+ 2.41V
2
+ 1.0V
3
expressing that both the neutron value and the density is a linear combination of
the volume fractions.
For instance, if V
1
= 0.76, V
2
= 0.05 and V
3
= 0.19, then
N
= 19.33 and =
2.3245 g/cm
3
.
If we could measure
N
and , then we could nd out the volume fraction, solving
the system
2V
1
+ 37V
2
+ 100V
3
=
N
2.65V
1
+ 2.41V
2
+ 1.0V
3
=
V
1
+V
2
+V
3
= 1
Since logs are taken densely (say meter by meter) we need to solve for the unknown
V
1
, V
2
and V
3
with many right-hand-sides.
78
Write a program to do the calculation calling the LUDecomposition (once) and the
LUsubstitute (many times.)
Depth, ft
N
, g/cm
3
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 coe-
cient matrix. Check if the original matrix multiplied by the obtained inverse is the
identity matrix.)
8.24 Nonlinear System of Equations: Chemical Equi-
librium of Methan Combustion
Consider the following two chemical reactions
CH
4
+H
2
O = CO + 3H
2
lnK
y
1
= 3.235
CO +H
2
O = CO
2
+H
2
lnK
y
2
= 0.3726
Notation: y means mole fraction. The sum of mole fractions is 1. K
y
is the equi-
librium constant (expressed with mole fractions, dimensionless).
lnK
y
1
= lny
CH
4
lny
H
2
O
+ lny
CO
+ 3 lny
H
2
lnK
y
2
= lny
CO
lny
H
2
O
+ lny
CO
2
+ lny
H
2
The lnK values are given for 1000 K ame temperature and atmospheric pressure.
The following is the molar composition of the initial mixture:
Component initial mole frac
1 CH
4
0.4
2 H
2
0.6
3 CO 0
4 CO
2
0
5 H
2
0
What is the equilibrium composition?
Hint: Let us start from 1 mole mixture and let x
1
and x
2
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 rst reaction causes mole number change, while the second does not.)
1 CH
4
0.4x
1
1+2x
1
2 H
2
O
0.6x
1
x
2
1+2x
1
3 C
O
x
1
x
2
1+2x
1
4 CO
2
x
2
1+2x
1
5 H
2
3x
1
+x
2
1+2x
1
We can reduce the problem to two equations and two unknowns using the reaction
extents.
8.25 Units: Darcys law
If the permeability is k = 10 md ( 9.87 10
15
m
2
) and the uid viscosity is = 5 cp
(0.005 Pa s), what is the Darcy velocity caused by 0.5 psi/ft (11310 Pa/m) pressure
gradient? Assume the cross sectional area perpendicular to the ow is 1 ft
2
(0.0929
m
2
).
What is the ow rate in resft
3
/s and in resBBL/D ? (in m
3
/s and in m
3
/day?)
80
Chapter 9
Tables
Quantity Remark SI Unit SI Other name
Length, L basic [L] m
Time, t basic [t] s
Mass, m basic [M] kg
Chem substance, n basic [N] mol
Temperature, T basic [T] K
Current, I basic [I] A
Velocity, V L/ t m s
1
Area, A L
2
m
2
Volume, V L
3
m
3
Specic volume V/m m
3
kg
1
Molar volume V/n m
3
mol
1
Density, m/V kg m
3
Acceleration, a V/t m s
2
Force, F m a kg m s
2
N
Pressure, p (also stress) F/A kg m
1
s
2
Pa = N/m
2
= J/m
3
Work L F kg m
2
s
2
J = N m = Pa m
3
Heat Work kg m
2
s
2
J
Int. Energy, Enthalpy Work kg m
2
s
2
J
Power Work/ t kg m
2
s
3
W = J/s = Pa m
3
/s
Specic heats (mass based) Heat/(m T) m
2
s
2
K
1
J / (kg K)
Electr. Potential (Voltage) Power/Current kg m
2
s
3
A
1
V= W/A
Electr. Resistance Potential/Current kg m
2
s
3
A
2
= V/A
Electr. charge Current Time A s C
Flow rate Volume / t m
3
s
1
Shear rate Velocity/ Length s
1
Viscosity Stress / shear rate Pa s
Permeability
Pressure/ Length
Velocity Viscosity
m
2
Compressibility 1/Pressure Pa
1
81
Multiplier Prex name Symbol
10
9
giga G
10
6
mega M
10
3
kilo k
10
3
milli m
10
6
micro
0.1 deci d
0.01 centi c
Circumference of circle d
Area of circle d
2
/4
Volume of sphere d
3
/6
Surface area of sphere d
2

Volume of right circular cylinder hd
2
/4
Pyramid (area of base) h/3
Kinetic energy KE =
1
2
m(V
2
)
Potential energy PE = mgh
Hydrostatic pressure p = gh
Darcys Law Q/A = V
Darcy
=
k

p
x
Pipe Mech Energ Bal
p
g
= h +
1
2g
(V
2
) +f
L
d
V
2
2g
Pipe Mech Energ Bal p = gh +

2
(V
2
) +f
L
d
V
2
2
Pump power pV A = pQ
Pipe Reynolds number denition: Re =
dV

Friction factor def (Darcy,Moody,Stanton) f = p


frict
d
L
2
V
2
for laminar ow (Hagen-Poissule) f:
64
Re
for turbulent ow f: function(Re, relative roughness)
Acceleration of gravity 9.8066 m/s
2
Max Water Density 999.973 kg/m
3
Liquid water spec. heat (1 atm) 4180 J/(kg K)
Heat of vap. of water (1 atm) 2.257 10
6
J/kg
Air Density (273 K, 1 atm) 1.293 kg/m
3
Air c
p
/c
v
1.4
Air relative molecular mass 0.2891 kg/mol
Temperature
Abs zero Water freezing p. (1 atm) Water boiling p. (1 atm)
0 K 273.15 K 373 K
-273.15
o
C 0
o
C 100
o
C
0 R 491.67 R 671.67 R
-459.67
o
F 32
o
F 212
o
F
82
Ideal gas:
pV = nRT
c
p
c
v
= R
=
c
p
c
v
R= 8.314
J
molK
= 8.314
Pam
3
molK
= 8.314
Nm
molK
R= 1.986
BTU
lbmol
o
R
= 1545
ftlb
f
lbmol
o
R
= 10.73
psift
3
lbmol
o
R
Other units
Other unit Expressed with SI unit
inch 0.0254 m
ft 0.3048 m
mile 1609.3 m
acre 4046.9 m
2
gal 3.7854 10
3
m
3
bbl 0.158987 m
3
Liter 0.001 m
3
lbm 0.45359 kg
lbf 4.4482 N
psi 6894.8 Pa
bar 1 10
5
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 10
16
m
2
cP 0.001 Pa s
Inside the English system
1 ft = 12 in.
1 mi = 5280 ft
1 ac = 43,560 ft
2
1 sq mi = 640 ac
1 BBL = 42 gal = 5.6146 ft
3
1 lb
f
= 32.174
lb
m
ft
s
2
1 psi = 1
lb
f
in.
2
= 144
lb
f
ft
2
1 atm = 14.696
lb
f
in.
2
1 BTU = 778.17
lb
f
ft
= 25,037
lb
m
ft
2
s
2
1 hp = 42.41
BTU
min
1 sp. grav. (liq) = 8.34
lb
m
gal
= 62.4
lb
m
ft
3
sp. grav. (liq) :
141.5
131.5+API
o
lb-mole : 453.59 mol : 379 SCF
M : multiply by 1000
83

You might also like