Академический Документы
Профессиональный Документы
Культура Документы
with
by
Jeffery H. Harwell
Asahi Glass Chair of Chemical Engineering
and
John O. Bennett
Adjunct Professor of Chemical Engineering and
The Mewbourne School of Petroleum and Geological
Engineering
PREFACE
For the student:
Numerical methods are part of the core of science and mathematics core of engineering and
applied science. As an engineer or applied scientist, you will likely use many sophisticated
software packages over the course of your career. The core courses in your major give you a
foundation for understanding the science and engineering principles underlying the software. A
course in numerical methods should give you a good foundation for understanding how we use
numerical methods to approximate the solutions to the mathematical expressions we formulate
from basic principles. These are the same types of algorithms used in the software packages that
represent state-of-the-art simulation in your chosen discipline. This course is especially designed
to teach you the inherent limitations to these programs, which arise from the need to estimate the
solution by comparing successive approximations, and to prepare you for further work in
simulation, should your interests take you in that direction.
In addition, the course is designed to use your study of numerical methods to increase your
proficiency with Excel, especially in the area of creating your own user defined functions with
Excel's powerful Visual Basic editor.
While Excel originated in the business world, it has evolved into a power tool for doing
engineering calculations. In practically every business around the world you will find scientists
and engineers using Excel for everything from research calculations and routine estimates, to the
design of major projects, even when there is a major software simulation package that does the
bulk of the heavy-duty computing.
If you spend the time to understand and do the exercises required to complete this course, you
will greatly increase your proficiency with Excel. This will be real plus to you in your first job.
One former student told us that the only thing she learned as an undergraduate that impressed her
engineer father was programming in Excel with Visual Basic. If you want to impress an
engineer, show them how to use a VB macro to simplify a calculation in Excel.
We are going to open up the Visual Basic editor in Excel and use it to create powerful programs
for solving basic engineering equations. By learning to use VB with Excel you will also be
learning the basics of applied programming in a very versatile computer language. You will learn
how a computer algorithm works as well as learning the basic tools for constructing a simple
computer program. These are the same tools that underlie everything from the program your
calculator uses to calculate sine(x) to the CFD (Computational Fluid Dynamics) program
General Motors or Boeing uses to estimate the drag coefficient on a new design.
While it would be beneficial if you had prior experience in computer programming, this is not
necessary. The examples and exercises are introduced in a way that will teach you the basics of
writing simple computer programs as you go through the course. The techniques you learn will
be cumulative, however, so that you will be writing some sophisticated programs by the end of
the course.
This is a course that should make you a better student in every subsequent engineering and
science course you take. We hope that this will be your experience. In fact, if you are studying
this material in the course the way you should, you will probably start using what you learn here
in the other courses you take this semester.
For the educator or working scientist or engineer:
The spreadsheet is a very powerful tool for routine engineering calculations as well as for data
analysis. Ask a graduate student to summarize their experimental data and they will lately show
up with an Excel spreadsheet. Ask an engineer to do routine calculations on a new process, and
they will probably start with an Excel spreadsheet.
This was driven home to us recently by two incidents:
1. While teaching a short course (on another subject) at a Fortune 50 manufacturer, we
witnessed an internal course for scientist and engineers, being taught to support the
introduction of a new manufacturing process into the company. Every person in the class
had their laptop computer open. Up on the screen at the front of the room: An Excel
spreadsheet.
2. In the chemical engineering program at the University of Oklahoma we recently surveyed
our students and asked them how well they were prepared for their first jobs. As part of
this we asked them what they wished they had learned that would have been the biggest
help to them. The surprising answer? Learning to use Excel with VBA.
This is why weve written this book.
The book you are holding springs from a course, Numerical Methods for Engineers, taught by
one of the authors for 27 years, and by the co-authors together since 2003. We designed the book
to bridge several realities faced by engineering and applied science educators at the beginning of
the 21st Century:
1. The computer has forever changed the way we work. It is an indispensable tool for the
engineer or applied scientist.
2. Most of our graduates will never write a single computer program during their entire
careers. This is true even of the majority of those who go on to obtain graduate degrees.
3. Advanced design software, such as process design software, computational fluid
dynamics (CFD) software, or circuit design software, is sophisticated far beyond what the
average engineer or academic researcher could write for their own application.
In other words, our graduates will likely depend frequently on sophisticated computer programs
that they could never hope to produce themselves. These realities constitute a dilemma for the
educator. Can we truly envision graduates who spend their careers using sophisticated software
packages that are essentially black boxes? But can we afford to dedicate an entire course in the
The
is closely related to the number of significant figures in the numerical approximation by
the expression
log |
| 2
The
in the equation above will take several different forms as applied to the various
numerical methods presented in the text; however, its calculation and use in determining the
significant figures in a numerical approximation constitute an underlying theme or thread of
continuity that is used in our approach to guarantee that every one of the numerical methods has
the appropriate number of significant figures. The 1 in Equation (2) is added because the
number of significant figures in a calculation is the number of figures that are exact plus one
additional figure carried to prevent the loss of significant figures through round-off error.
Almost every numerical method in the book is derived using some form or variation of a Taylor
series expansion; hence, it is presented in some detail and in several different forms in Chapter 1.
The Taylor series will also provide us a relatively simple algorithm to program using VB, thus
providing a good introduction to the skills needed to program an algorithm for use in an Excel
spreadsheet using VB.
The reader who already has a background in numerical methods will observe that all of the
numerical methods covered by the text are not derived in full detail. Instead, we emphasize an
intuitive development of the method to prevent the student from becoming bogged down in a
detailed derivation. We believe that while this might not be appropriate for an advanced
treatment, it is the best approach for an introductory text.
After a numerical method is presented, then a simple example is solved using the method. The
example usually has an analytic solution with which to verify the numerical solution. The
example is then used to develop an Excel spreadsheet and a user defined function written with
Visual Basic. In developing an Excel implementation of a numerical method, the student must
have the VB code, the algorithm, and the spreadsheet all correct before getting a usable answer.
Having an analytical solution for comparison greatly facilitates this process.
Once the Excel spreadsheet and VB code are developed from the theoretical presentation of a
numerical method, they are used to solve examples to a specified number of significant figures.
While some of the problems are from various areas of engineering, chemistry, or physics, many
are simply mathematical expressions. We believe that students learn the numerical method best
when they can focus on the logic of the algorithm. Often the application to an equation from
science or engineering can be distracting, as the student may feel they have to understand the
physics of the equation before being able to solve it. This can add significantly to their confusion
when they are already struggling with the math, the logic of the algorithm, and the programming
code.
We dedicate this book to the next generation of engineers and applied scientists who will
graduate from our colleges and universities in the coming decade, and go out from there to
change the world.
BASIC, Visual Basic, Visual Basic for Applications (VBA), and Visual Basic.NET
While most scientists and engineers are seasoned users of Microsoft's EXCEL spreadsheet
software, many are either unfamiliar with or completely unaware of the powerful, FORTRANlike programming language, Visual Basic for Applications (VBA), which it includes. Every since
version 4, which was released in 1992, Excel has included a programming language to facilitate
the automation of repetitive or complex computational tasks. Beginning with Excel 5 in 1993,
the language provided for task automation has been Visual Basic for Applications. VBA is a
version of the Microsoft Visual Basic (VB) programming language adapted specifically to make
it easy to work within the Microsoft Office environment.
It is important to distinguish between VB and the original BASIC (Beginner's All-purpose
Symbolic Instruction Code) programming language. BASIC was introduced by John Kemeny
and Thomas Kurtz at Dartmouth in 1964 to provide computer accessibility to students outside of
the sciences. Based partly on FORTRAN II and ALGOL 60, an original goal for BASIC was to
have a simple, easy to learn programming language for beginners who were unfamiliar with
computer architecture. It introduced simplified input and output commands and shielded the
beginner from the operating system. BASIC became very popular with the introduction of
microcomputers in the late 1970s and 1980s, with major implementations in Apple BASIC,
Commodore BASIC, Microsoft BASIC, and TI-BASIC.
But VB is not BASIC. The early versions of BASIC listed above are categorized as nonstructured programming languages, which are frequently criticized as producing hard-to-read
code and promoting poor programming practices. Historically, non-structured programming
languages were followed first by structured or procedural languages, then later by objectoriented programming languages. VB is a third-generation, event-driven programming language.
It was created by Microsoft to facilitate development of software designed to run with the
Windows operating system.
Because of its FORTRAN (IBM Mathematical Formula Translating System) heritage, VB is
very well suited to numeric computation and scientific and engineering computing. After over a
half century of use Fortran still dominates much of scientific and engineering computing in such
computationally intensive applications as weather prediction, finite element analysis,
computational fluid dynamics, computational physics and chemistry, and engineering design and
optimization. Fortran is one of the most popular languages for high-performance computing and
is the language used for benchmarking and ranking the world's fastest supercomputers. The
many similarities between Fortran and VB make it easy to transition from VB programming to
Fortran programming for scientific and engineering computations. When you are learning to
program in VB, you are also learning the basic elements of Fortran programming.
Not only is there some confusion about the relationship between BASIC and VB, but there is
also confusion about the future of VB. What is the future of Microsoft Visual Basic? Microsoft is
committed to co-developing VB.Net along with its C# programming language to support
software developers in the Windows environment. Microsoft shipped Excel for Office 2007 with
VB version 6.5. VB version 10 was included in Microsoft Visual Studio Tools for Applications
(VSTA), the developer's software suite for Office 2010, and VB is included in Office 2010 as a
macro programming tool. The confusion about Microsoft's commitment to VB first arose in that
VB 6 is to be the last version of VB that will be called Visual Basic for Applications. All later
versions of VB are to be called VB, not VBA; this lead to some uninformed comments that
Microsoft was abandoning VBA, when in fact it was integrating it more fully into its developer
support framework. Additional confusion arose regarding Microsoft's continued commitment to
Visual Basic when it released Office 2008 for Mac without VBA. After explaining that technical
difficulties with making VBA work for both the PowerMac and Intel versions of Apple hardware
had lead to the decision to ship Office 2008 without VBA, Microsoft quickly committed to
including VB in the next release of Office for Mac. While Mac Office EXCEL 2008 does not
support VB macros, Mac Office 2011 will again include it.
A 2006 survey by Forrester Research showed that 37 percent of all enterprises use Visual
Basic.NET for in-house software applications and that 59 percent of all .NET developers used
Visual Basic.NET as their only programming language. Microsoft has also publically stated that
it will continue to develop Visual Basic along with its C# programming language for future
versions of VSTA. There is no basis for any concern that Microsoft might abandon VB anytime
in the near future.
John O. Bennett
John O. Bennett has been an adjunct professor in the School of Chemical, Biological and
Materials Engineering and in the Mewbourne School of Petroleum and Geological Engineering
at the University of Oklahoma since 2003. He was formally an Instructor in the Department of
Mathematics at The University of Texas in Austin, Texas (1970).
Bennett received a BA in Petroleum Geophysics (1962) and a MA in Mathematics (1967) from
the University of Oklahoma. He received his Ph D in Mathematical Sciences (1974) from Rice
University. This degree was supported by a NASA grant in remote sensing and image processing
from the Johnson Space Center, Houston, Texas.
His 13 years of industry experience includes: Geophysicist [Digital Seismic Data processing]:
Amoco, Sr. Research Mathematician: Exxon Production Research Company [Digital Seismic
Data Processing and Display, Remote Sensing and Digital Image Processing for Oil and Gas],
Sr. Geophysicist: Chevron Production Research Company [Digital Seismic Filter design],
Superintendent of Computer Mapping: Superior Oil Company [Supervised Mapping and Digital
Data Base for Company], Supervisor of Geological Development: Mobil Exploration and
Production Company [Liaison between Mobil Research Laboratory and Operating Affiliates],
Technical Staff: The Analytical Sciences Corporation [Supervised Computer Graphics and
Animation on CIA Project]. He also has 13 years of government experience with the Department
of the Interiors Minerals Management Service (MMS) as a mathematician and statistician
[Statistical and Stochastic Modeling of MMS Offshore bidding strategies, Regression Models of
Offshore Production Decline Curves, Computer Mapping, Statistical Data Analysis of MMS 5
year Oil and Gas Forecast, Digital computation of the foot of the continental slope to determine
maritime boundary limits. He has published over 100 papers in the above areas.
He currently Director of The Oklahoma University Law of the Sea Fellowship (OULOSF) which
trains United Nations Nippon Fellows from coastal countries to present credentials to the United
Nations to extend their mineral rights past the 200 mile Exclusive Economic Zone.
Jeffery H. Harwell
Jeffrey H. Harwell holds the Asahi Glass Chair in Chemical Engineering at the University of
Oklahoma, where he has taught since 1983. He received his PhD in Chemical Engineering from
the University of Texas in 1983. He also holds the MS degree in Chemical Engineering (1979)
and the BA in Chemistry (1974) from Texas A&M University, as well as the M Div. (1977) from
Western Seminary.
Dr. Harwells research focuses on the science and applications of surfactants, including
especially the application of surfactants in enhanced oil recovery, environmental remediation,
and nanotechnology. He has co-edited 4 books, published over 140 technical papers, and is an
inventor on 12 patents.
At Oklahoma he served for seven years as the Director of Chemical Engineering and seven years
as the Associate Dean of Engineering. In addition Dr. Harwell is a principal in Surfactant
Associates, Inc., Surbec Environmental, LLC, and SouthWest NanoTechnology, Inc., Norman,
Oklahoma based university spin-offs.
Dr. Harwell is the recipient of the American Chemical Societys Victor K. LaMer award (1984).
He has received the National Ground Water Associations Outstanding Ground Water
Remediation Project Award (2006), the American Oil Chemists Societys Soap and Detergent
Association Distinguished Paper Award (2003), and Outstanding Paper Awards from the
American Institute of Chemical Engineering in 1992 and 1996.
At the University of Oklahoma he has taught "Numerical Methods for Engineers" since 1983,
when he used Fortran 77. He began using Excel with VB to teach the class in 2003.
Table of Contents
Preface
About the Authors
Chapter
1
2
3
4
5
6
7
8
9
10
Title
Taylor Series, Truncation Error, and Significant Figures
Numerical Derivatives and Round-Off Error
Nonlinear Equations and Root Finding Methods
Matrices and Systems of Linear Equations
Interpolation, Approximation, and Least Squares Regression
Least Squares Curve Fitting of Linearized Non-Linear Models
Numerical Integration
Ordinary Differential Equations and First Order Initial Value
Problems
Systems of ODE and Boundary Value Problems
Introduction to Partial Differential Equations
References
Visual Basic Operators and Commands
Pages
40
30
40
41
38
19
39
36
35
41
Example
x=x+2
x = x / 10
i = x \ 10
y = x^3
y = 3*x
y=x+2
x=x-2
x>y
/
\
^
*
+
>
Normal division
Integer division
Exponential operator
Multiplication
Addition
Subtraction
Greater than
<
Less than
<>
Not equal to
x <> y
>=
x <= y
Defines a Boolean
value that is the AND
of two values
Defines a Boolean
value that is the AND
of two values
Returns the integer
remainder of a
division
<=
AND
OR
MOD
x <y
x <= y
x < y OR y > 0
i = 27 MOD 5
Comments
Assigns results of a calculation
to a labeled memory location
Truncates answer
Exp
Comments
Uses bankers rounding
y = Atn(x)
y = Atn(x)
y is returned in radians.
x = Abs(x)
y = Sqr(x)
y = Log(x)
y = Exp(x)
. Use to define
hyperbolic functions for use in
VB
Option
Base
Determines whether
the lowest range in an
array is 0 or 1
Option Base 1
Redim
Redim A(10,10) as
Double
Array
Public
Private
Option
Explicit
A = Array(10,20,30)
Public i as Integer
Private n as Integer
Option Explicit
Comments
Important types include Single,
Double, and Integer. Also
used to define array
(subscripted) variables. Use
speeds execution by allowing
allocation of memory during
compilation of code
When a subscripted variable is
returned to Excel, every
element in the array is returned,
but only those that fit into the
selected block of spreadsheet
cells are displayed
Allows the program to decide
how large the array needs to be
after receiving data from the
spreadsheet
Both creates the array and
assigns values to array elements
Not used in any examples in
this text
Not used in this text. Used to
avoid an operation in one
procedure changing a variable
in another procedure.
Useful both for catching typos
and misspelling of variable
names, and for creating code
that executes faster
Open
Close
Write
Write #1, A
Print #1, A;
Tab(2);B
Input #1, A
Print
Tab
Input
Close #1
Print #1, A
Comments
Very useful for debugging a
program, to be sure it is doing
what you think it is doing
Useful for obtaining data from
a specific cell in the
spreadsheet for use in a
function or subroutine
Opens a file on the hard drive
that can be read for data. We
did not use this powerful tool in
the text, but it very close to a
similar command in Fortran
that allows writing or reading
large amounts of data
Must be used to close files
opened during a procedure if
the data is to be available after
the program ends
Data written to file with
separators between values
Data written to file with no
sparators
Allows organization of output
for easier reading
Reads data from an open file
and stores it in program
variables
If...Then...Else
While...Wend
End
GoTo
Executes one or
more blocks of code
based on results of a
logical test
Executes a block of
code while a logical
test remains true
If A > 5 Then x = 1
Else x = 0
Immediately stops
execution of a
program
Switches execution
to a new line of code
End
GoTo Line1
Comments
Each time the block of code
executes the counter is
incremented by Step. When
the counter exceeds the limit
program control transfers to
the first executable statement
after the Next statement
Blocks of code can be
executed based on whether
results of test are True or
False
Can be used to execute a
block of code until some
condition is met. Very
powerful but must be used
carefully in order not to
create an infinite loop
Returns control of computer
to Excel
Not used in this text. A
notorious way of creating
"spaghetti code" that no one
can understand, even its
creator
Call
Transfers execution
of code to the block
of code in a Sub
Function
MyProc(var1, var2)
{Block of
executable code}
End Function
Call NewProc(var1,
var2)
Comments
Powerful tool for organizing a
program and allowing one
block of code to be used many
times. VB HELP explains
how to transfer variables
between the subroutine and
the program that calls the
subroutine
Differs from a Sub in that it
can only return one variable,
though this can be an array.
Most of the programs in this
text are written as Functions.
Used in a program to execute
the code in a Sub
REFERENCES
1. Acton, F. S.: Numerical Methods that Work, The Mathematical Association of America,
Washington, D. C. , 1997.
2. Billo, E. J.: Excel for Scientist and Engineers: Numerical Methods, Wiley-Interscience, 1st
Edition, Maiden, MA, 2007.
3. Bourg, D.: Excel Scientific and Engineering Cookbook, OReilly Media, Sabastopol, CA,
2006.
4. Burden, J. C. and J. D. Fears: Numerical Analysis, 8th Edition. PWS Publishing, Boston,
2005.
5. Butcher, J. C.: On Runge-Kutta Processes of Higher Order, Journal of the Australian
Mathematical Society, Vol. 4, pp 179-194, 1964.
6. Chapara, S. C., R. P. Canale:Numerical Methods for Engineers, McGraw-Hill, 6th Edition,
New York, 2010.
7. Chapara, S. C.: Applied Numerical Methods with MATLAB, 2nd ed. McGraw-Hill, New York
2007.
8. Cheney, E. W.: Introduction to Approximation Theory, McGraw-Hill, New York, 1966.
9. deBoor, C.: A Practical Guide to Splines, Springer-Verlag, New York, 1978.
10. Dlngarra, J. J. et al.: LINPACK Users Guide, SIAM, Philadelphia, 1979.
11. Fadeev, D. H., and V. N. Fadeeva: Computational Methods of Linear Algebra, Freeman, San
Francisco, 1963.
12. Forsyth, G. E. and W. R. Wasow: Finite Difference Methods for Partial Differential
Equations, John Wiley, New York, 1960.
13. Fortythe, G. E., M. A. Malcolm, and C. B. Moler: Computer Methods for Mathematical
Computations, Prentice Hall, Englewood Cliffs, NJ , 1977.
14. Fox, L.: An Introduction to Numerical Linear Algebra, Oxford University Press, New York,
1965.
15. Fox, L.: Numerical Solution of Ordinary and Partial Differential Equations: Pergamon Press,
dist. by Addison-Wesley, 1962.
16. Gear, C. W.: The Automatic Integration of Ordinary Differential Equations,
Communications of the Association for Computing Machinery, Vol. 14, pp 176-190, 1971.
17. Gear, C.W.: Numerical Initial Value Problems in Ordinary Differential Equations, PrenticeHall, Englewood Cliffs, NJ, 1971.
18. Golub, G. H., and C. F. Van Loan: Matrix Computation, 3rd ed., Johns Hopkins University
Press, Baltimore, MD, 1966.
19. Gottfried, B.: Spreadsheet Tools for Engineers Using Excel, 3rd ed. McGraw-Hill, New York
2005.
20. Hall, T. E., W. H. Enright et al,: Comparing Numerical Methods for Ordinary Differential
Equations, SIAM Journal on Numerical Analysis, Vol. 9, No. 4, pp 603-637, 1972.
21. Hamming, R. W.: Numerical Methods for Scientist and Engineers, McGraw-Hill, 1962.
22. Henrici, P.: Discrete Variable Methods in Ordinary Differential Equations, John Wiley,
1962.
23. Hiestand, J. W.: Numerical Methods with VBA Programming, Jones and Bartlett, Sudbury,
Massachusetts, 2009.
24. Hornbeck, Robert W.: Numerical Methods with Numerous Examples and Solved Illustrative
Problems, Prentice- Hall, Inc. /Quantum Publishers, 1975.
25. Householder, A. S.: Principles of Numerical Analysis , McGraw-Hill, New York, 1953.
26. Householder, A. S.: The Theory of Matrices in Numerical Analysis, Blaisdell, New York,
1964.
27. Isaacson, E, and H. B. Keller: Analysis of Numerical Methods, John Wiley, New York,
1966.
28. Kammer, W. J. el al.: Quadratic Splines, Numerische Mathematik, Vol. 22, pp241- 259,
1974.
29. Kincaid, D. and W. Cheney: Numerical Mathematics and Computing, 6th ed., Brooks/Cole,
Monterey, CA, 2008.
30. Kolman, B.: Introduction to Linear Algebra with applications, 6ed., Prentice Hall, Upper
Saddle River, NJ, 1998.
31. Lancaster, P. and Salkauskas: Curve and Surface Fitting: An Introduction, Academic Press,
Boston, MA, 1986.
32. Lapidus, L.: Digital Computation for Engineers, McGraw-Hill, New Your, 1962.
33. Leon, S. J.: Linear Algebra with Applications, 5th ed., Prentice Hall, Upper Saddle River, NJ,
1998.
34. Liengme, B. V.: A Guide to Microsoft Excel for Scientist and Engineers, 4th ed., Academic
Press, 2008.
35. Lomax, P.:VB and VBA in a Nutshell. OReilly, 1998.
36. Press, W. H., B. P. Flanner, S. A. Teukolsky, and W. T. Vettering: Numerical Computing, 3rd
ed., Cambridge University Press, Cambridge, England, 2007.
37. Ralston A., Runge-Kutta Methods with Minimum Error Bounds, Mathematical
Computing. 16: 431, 1962.
38. Ralston, A. and P. Rabinowitz: A First Course in Numerical Analysis, 2nd ed. McGraw-Hill,
New York 1978.
39. Rice, J. R.: Numerical Methods, Software and Analysis, McGraw-Hill, New York. 1983.
40. Shepard, R.: Excel VBA Macro Programming, McGraw-Hill, New York, 2004.
41. Stewart, G. W.: Introduction to Matrix Computation, Academic Press, New York. 1973.
42. Strang, G.: Linear Algebra and Its Applications, 3rd ed., Harcourt Brace Jovanovich, San
Diego, CA, 1988.
43. Taylor, J. R.: An Introduction to Error Analysis, University Science Books, Mill Valley, CA,
1982.
44. Thomas, S. J.: Numerical Partial Differential Equations: Finite Difference Methods,
Springer, New York, 1998.
45. Varga, R. S.: Matrix Iterative Analysis, Prentice-Hall, Englewood Cliffs, NJ, 1962.
46. Walkenbach, J.: Excel Power Programming wit VBA, 2nd ed. IDG Books, 1996.
47. Wilkerson, J. H. and C. Reinsch: Linear Algebra: Handbook for Automatic Computation,
Vol. 11, Springar-Verlag, Berlin, Germany, 1971.
48. Wilkerson, J. H.: The Algebraic Eigenvalue Problem, Oxford University Press, Fair Lawn,
NJ, 1965.
49. Young, D. M.: Iterative Solutions of Large Linear Systems, Academic Press, New York,
1971.