NUMERICAL METHODS lor CHEMICAL ENGINEERS Using Excel', VBA, and MATLAB* VICTOR J. LAW CRC Press Taylor & Francis Group Boca Raton London New York CRC Press is an imprint of the Taylor & Francis Croup, an informa business
Contents Preface Author Notes to the Instructor xiii xv xvii Chapter 1 Roots of a Single Nonlinear Equation 1 1.1 Introduction 1 1.2 Algorithms for Solving/(jc) 0 = 2 1.2.1 Plotting the Equation 2 1.2.2 Fixed-Point Iteration (Direct Substitution) 2 1.2.3 Bisection 4 1.2.4 Newton's Method 6 1.2.5 Secant Method 7 1.3 Using Excel to Solve Nonlinear Equations (Goal Seek) 8 1.4 A Note on In-Ceil Iteration 16 Exercises 16 References 20 Chapter 2 Visual Basic for Applications Programming 21 2.1 Introduction 21 2.2 Algorithm Design 21 2.3 VBA Coding 24 2.4 Example VBA Project 24 2.5 Getting Help and Documentation on VBA 29 2.6 VBA Statements and Features 29 2.6.1 Assignment Statement 30 2.6.2 Expressions 30 2.6.2.1 Numerical Expressions 30 2.6.2.2 Logical (Boolean) Expressions 31 2.7 Objects and OOP 31 2.8 Built-in Functions of VBA 33 2.9 Program Control 35 2.9.1 Branching 35 2.9.1.1 If-Then-Else 35 2.9.2 Looping 35 2.9.2.1 For...Next 35 2.9.2.2 While...Wend Statement 36 2.10 VBA Data Types 36 2.11 Subs and Functions 37
vjjj Contents 2.12 Input and Output 38 2.12.1 Getting Data from the Worksheet 38 2.12.2 Putting Data onto the Worksheet 39 2.13 Array Data Structures 39 2.13.1 Array Arguments 40 2.13.2 Dynamic Arrays in VBA 42 2.14 Alternative I/O Methods 43 2.14.1 Using Range.Select 43 2.14.2 Using Message Box 43 2.14.3 Using Input Box 43 2.15 Using Debugger 44 Exercises 50 References 54 Chapter 3 Linear Algebra and Systems of Linear Equations 55 3.1 Introduction 55 3.2 Notation 55 3.3 Vectors 56 3.4 Vector Operations 57 3.4.1 Vector Addition and Subtraction 57 3.4.1.1 Multiplication by a Scalar 57 3.4.2 Vector Transpose 57 3.4.3 Linear Combinations of Vectors 58 3.4.4 Vector Inner Product 58 3.4.5 Vector Norm 58 3.4.6 Orthogonal Vectors 59 3.4.7 Orthonormal Vectors 59 3.5 Matrices 60 3.6 Matrix Operations 60 3.6.1 Matrix Addition and Subtraction 60 3.6.2 Multiplication by a Scalar 60 3.6.3 Transposition of Matrices 61 3.6.4 Special Matrices 61 3.6.5 Matrix Multiplication 61 3.6.6 Matrix Determinant 62 3.6.7 Matrix Inverse 62 3.6.8 More Special Matrices 63 3.7 Solving Systems of Linear Algebraic Equations 63 3.7.1 Gaussian Elimination 63 3.7.2 Determinant Revisited 66 3.7.3 Gauss-Jordan Elimination 67 3.7.4 Rank of Matrix 68 3.7.5 Existence and Uniqueness of Solutions for Ax = b 69 3.8 Linear Equations and Vector/Matrix Operations in Excel... 69 3.9 More about Matrix.xla 71
Contents ix 3.10 SVD and Pseudo-Inverse of a Matrix 73 Exercises 76 Reference 80 Chapter 4 Numerical Differentiation and Integration 81 4.1 Numerical Differentiation 81 4.1.1 Approximation of a Derivative in One Variable 81 4.1.2 Approximation of Partial Derivatives 84 4.2 Numerical Integration 86 4.2.1 Trapezoidal Rule 86 4.2.2 Simpson's Rule 87 4.2.3 Gauss Quadrature 88 4.3 Curve Fitting for Integration 91 Exercises 93 Chapter 5 Ordinary Differential Equations (Initial Value Problems) 99 5.1 Introduction 99 5.1.1 General Statement of the Problem 99 5.2 Euler-Type Methods 99 5.2.1 Euler's Method for Single ODE 99 5.2.2 Stability of Numerical Solutions of ODEs 102 5.2.3 Euler Backward Method 102 5.2.4 Trapezoidal Method (Modified Euler) 103 5.2.5 Accuracy of Euler-Type Methods 105 5.3 RK Methods 105 5.3.1 Second-Order RK Method 106 5.3.2 Fourth-Order RK Method 107 5.4 Stiff ODEs 108 5.5 Solving Systems of ODE-IVPs 109 5.6 Higher-Order ODEs Ill Exercises 116 Chapter 6 Ordinary Differential Equations (Boundary Value Problems) 123 6.1 Introduction 123 6.2 Shooting Method 124 6.3 Split BVPs Using Finite Differences 125 6.4 More Complex Boundary Conditions with ODE-BVPs 127 Exercises 129 Reference 135 Chapter 7 Regression Analysis and Parameter Estimation 137 7.1 Introduction and the General Method of Least Squares 137
X Contents 7.2 Linear Regression Analysis 139 7.2.1 Straight Line Regression 139 7.2.2 Curvilinear Regression 142 7.3 How Good Is the Fit from a Statistical Perspective? 145 7.3.1 Residual Plots 145 7.3.2 Correlation Coefficient 146 7.3.3 Parameter Standard Deviations 146 7.3.4 Parameter Confidence Intervals 147 7.3.5 Using f-ratios (f-statistics) for Individual Parameter Significance 148 7.4 Regression Using Excel's Regression Add-In 153 7.5 Numerical Differentiation and Integration Revisited 155 Exercises 156 Chapter 8 Partial Differential Equations 161 8.1 Introduction 161 8.2 Parabolic PDEs 161 8.2.1 Explicit Method (Centered Difference in x, Forward Difference in 0) 163 8.2.2 Centered Difference in x, Backward Difference in 6 165 8.2.3 Crank-Nicholson Method (Centered Difference in x, Centered Difference in 6) 165 8.3 Thomas Algorithm for Tridiagonal Systems 167 8.4 Method of Lines 172 8.5 Successive Overrelaxation for Elliptic PDEs 173 Exercises 177 Chapter 9 Linear Programming, Nonlinear Programming, Nonlinear Equations, and Nonlinear Regression Using Solver 179 9.1 Introduction 179 9.2 Linear Programming 179 9.3 Nonlinear Programming 184 9.4 Nonlinear Equations 186 9.5 Nonlinear Regression Analysis 189 Exercises 191 Chapter 10 Introduction to MATLAB 195 10.1 Introduction 195 10.2 MATLAB Basics 196 10.2.1 MATLAB Colon Operator 201 10.2.2 MATLAB, M-Files, and Input from Command Window 202
Contents xi 10.3 MATLAB Programming Language Statements 204 10.3.1 If-Then-Else Statements 204 10.3.2 Looping Statements (For, While) 205 10.4 MATLAB Function Arguments 206 10.5 Plotting in MATLAB 207 10.5.1 Plotting Two Functions on the Same Graph 207 10.6 Example MATLAB Programs 208 10.7 Closing Comment Regarding MATLAB 216 Exercises 217 Appendix: Additional Features of VBA 219 A.l Introduction 219 A.2 Calling Excel Built-in Functions in VBA Macros 219 A.3 Calling Excel Add-In Functions in VBA Macros 220 A.4 Variant Data Type 221 A.5 VBA Function That Returns an Array 223 A.6 Creating Excel Add-Ins 225 Index 227