Practical Numerical Methods for Chemical Engineers Using Excel with VBA By Richard A. Davis Copyright 2012. Richard A. Davis. All rights reserved. No part of this book may be reproduced in any form or by any means, without written permission from the publisher. Excel is a registered trademark of Microsoft Corp. (www.microsoft.com) Other company and product names mentioned herein are the trademarks of their respective owners. Disclaimer Use the examples, programs, and algorithms in this book or associated web site at your own risk! No warranties are expressed or implied, that the examples, programs, and algorithms in this book or associated web site are free of error, or will meet your requirements for any particular application. The publisher disclaims all liability for direct or consequential damages resulting from any use of the examples, programs or algorithms in this book or associated web site, including, but not limited to, Excel worksheets, VBA programs, and macros. 10 9 8 7 6 5 4 3 2 ISBN-13: 978-1479146437 ISBN-10: 1479146439 Contact the publisher (rdavis@d.umn.edu) to inquire about options for customizing any part of the content of this book for your unique courses or particular needs. www.d.umn.edu/~rdavis/pnm
Contents CHAPTER 1 DO YOU HAVE A CAREER IN MODELING?... 1 1.1 INTRODUCTION TO NUMERICAL METHODS... 3 1.1.1 Linear versus Nonlinear Equations... 6 1.1.2 Common Features of Numerical Methods... 6 1.1.3 Computer Round-off Error... 7 1.2 MATHEMATICAL MODEL BUILDING... 9 1.2.1 Research versus Design... 11 1.2.2 Laws of Conservation... 12 1.2.3 Steady-state versus Equilibrium... 13 1.2.4 Generation... 16 1.2.5 Lumped vs. Distributed Systems... 17 1.2.6 Modeling Assumptions... 21 1.2.7 Dimensionless Equations... 23 1.2.8 Degrees of Freedom... 24 1.2.9 What Do We Mean by Order?... 24 1.2.10 Model Validation... 25 1.2.11 Stochastic Modeling... 27 1.3 EXPERT PROBLEM SOLVERS... 27 1.4 APPENDIX: PRIMER ON REACTION ENGINEERING... 33 CHAPTER 2 REAL ENGINEERS USE EXCEL... 38 2.1 GETTING STARTED WITH EXCEL... 38 2.1.1 Calculators vs. Spreadsheets... 39 2.1.2 Moving Around in a Worksheet (Things you may not have tried )... 41 2.2 AUTO FILL... 42 2.3 ARRAY OPERATIONS... 42 2.4 DOCUMENT YOUR CALCULATIONS... 43 2.4.1 Cell Referencing and Named Cells... 44 2.4.2 Significant Figures and Scientific Notation... 45 2.4.3 Use Symbols and Combine Text with Numbers... 47 2.4.4 Troubleshoot with Formula Auditing Tools... 49 2.4.5 Text Wrapping and Breaking Text... 49 2.4.6 Drawing Tools... 50 2.4.7 Charts and Graphs... 50 2.5 FUNCTION LIBRARY... 58 2.5.1 Unit Conversions... 61 2.5.2 Logical, Lookup and Reference Functions... 62 2.6 WHAT-IF ANALYSIS USING GOAL SEEK AND DATA TABLE... 64 2.7 EXCEL ADD-INS... 66 2.7.1 Analysis ToolPak... 67 2.7.2 Solver... 71 2.8 DATA VALIDATION TOOLS... 74 CHAPTER 3 LEARN TO SPEAK VBA... 76 3.1 MACRO ENABLED WORKBOOKS... 76 3.2 VISUAL BASIC EDITOR (VBE)... 77 3.2.1 Properties Window... 79 3.3 PROCEDURES: USER FUNCTIONS AND SUBROUTINES... 79 3.3.1 Text, Breaks and Multiple Statements per Line... 82 3.3.2 Descriptive Comments... 82 3.4 VARIABLES AND CONSTANTS... 83 3.5 STRUCTURED PROGRAM FLOW CONTROL... 84 3.5.1 Loop Program Flow Control... 85 3.5.2 If-Then-Else Program Flow Control... 87 3.5.3 Case Program Flow Control... 89
iv PRACTICAL NUMERICAL METHODS 3.6 ARITHMETIC AND WORKSHEET FUNCTIONS IN VBA... 94 3.6.1 Speed up VBA Calculations... 96 3.6.2 Pseudo Random Number Generator... 97 3.7 MACRO CONTROLS ON THE WORKSHEET... 100 3.8 ARRAYS... 103 3.8.1 Range Variables and Worksheet References for Communication between VBA and Excel... 103 3.8.2 Sorting Arrays... 105 3.9 COMMUNICATION WITH THE USER: MSGBOX AND INPUTBOX... 106 3.9.1 MsgBox... 106 3.9.2 InputBox... 108 3.9.3 User Forms... 109 3.9.4 Read/Write Data To/From Files... 113 3.9.5 Status Bar... 115 3.9.6 FORMAT Numbers for Output... 115 3.10 DEBUGGING YOUR CODE... 115 3.11 PNM SUITE OF MACROS... 116 CHAPTER 4 LINEAR EQUATIONS... 120 4.1 LINEAR FUNCTIONS... 120 4.2 SMALL LINEAR ALGEBRAIC SYSTEMS... 121 4.3 GAUSSIAN ELIMINATION... 122 4.3.1 Crout Reduction... 123 4.3.2 Thomas Algorithm for Tridiagonal Matrix Inversion... 130 4.3.3 Cholesky Decomposition of a Symmetric Matrix... 131 4.4 MATRIX ALGEBRA USING EXCEL WORKSHEET ARRAY FUNCTIONS... 131 4.4.1 Matrix Solution Improvement... 134 4.5 SUCCESSIVE SUBSTITUTION (ITERATION)... 134 4.5.1 Iteration on Circular References in Excel... 141 4.5.2 Relaxation... 143 4.6 EPILOGUE ON LINEAR EQUATIONS... 145 CHAPTER 5 TAYLOR SERIES ANALYSIS... 146 5.1 DERIVATIVE APPROXIMATIONS... 152 5.1.1 Finite-difference Derivative Approximations... 153 5.1.2 Derivatives Approximations Using Unevenly Spaced Data... 158 5.1.3 Richardson s Extrapolation... 164 5.2 SENSITIVITY ANALYSIS... 171 5.2.1 Maximum Uncertainty... 176 CHAPTER 6 NONLINEAR EQUATIONS... 180 6.1 GRAPHICAL SOLUTION... 181 6.2 FINDING ROOTS OF A SINGLE EQUATION... 185 6.2.1 Bisection... 185 6.2.2 Regula Falsi... 188 6.2.3 Excel s Goal Seek... 191 6.2.4 Successive Substitution (Ordinary Iteration)... 191 6.2.5 Wegstein s Acceleration of Fixed-point Iteration... 192 6.2.6 Newton s Method... 193 6.2.7 Secant Method... 203 6.3 ROOTS FOR SIMULTANEOUS EQUATIONS... 206 6.3.1 Ordinary Iteration... 206 6.3.2 Aitken s 2 and Steffensen Methods of Series Convergence Acceleration... 207 6.3.3 Equation Sequence and Arrangement... 210 6.3.4 Newton s Method for Simultaneous Equations... 216 6.3.5 Bairstow s Method of Polynomial Factorization... 221 6.3.6 Excel s Solver for Simultaneous Equations... 227 6.3.7 Newton s Method with Continuation for Root Finding... 232 6.4 TRANSFORMATION AND SCALING... 239 6.5 INITIALIZING ITERATIVE METHODS... 241 6.6 EPILOGUE ON NONLINEAR EQUATION SOLUTION METHODS... 242
CONTENTS v CHAPTER 7 OPTIMIZATION...244 7.1 DEFINE THE OPTIMIZATION PROBLEM...246 7.2 CALCULUS MAXIMUM VS. MINIMUM...246 7.3 DIRECT SEARCH METHODS...249 7.3.1 Graphical Direct Search Methods...249 7.3.2 Bracketing with Quadratic Interpolation...251 7.3.3 Golden Sections...253 7.3.4 Powell s Method for Multiple Dimensions...257 7.3.5 Luus-Jaakola Global Optimization...259 7.4 INDIRECT SEARCH METHODS...266 7.4.1 Response Surfaces...266 7.4.2 Newton s Method...267 7.4.3 Excel s Solver...268 7.5 CONSTRAINTS ON THE SEARCH REGION...269 7.5.1 Penalty Functions for Inequality Constraints...270 7.5.2 Slack Variables...270 7.5.3 Variable Transformation...271 7.5.4 Lagrange Multipliers...274 7.5.5 Redundancy and Separation...278 7.5.6 Desirability Functions for Multiobjective Optimization...280 7.6 VARIABLE AND PARAMETER SCALING...281 7.7 SENSITIVITY ANALYSIS...283 7.8 EPILOGUE ON OPTIMIZATION...288 CHAPTER 8 UNCERTAINTY ANALYSIS...290 8.1 MODELS OF MEASUREMENT UNCERTAINTY...292 8.1.1 Precision versus Accuracy...293 8.1.2 Standard Uncertainty from Random Errors...294 8.1.3 Confidence Intervals for Expanded Uncertainty...295 8.1.4 Fixed Uncertainty from Systematic Errors...296 8.1.5 Uncertainty in Quoted Values...299 8.1.6 How (Not) to Report Uncertainty...301 8.2 UNCERTAINTY PROPAGATION...301 8.2.1 The Law of Propagation of Uncertainty...302 8.2.2 How (Not) to Handle Unit Conversions in Uncertainty Analysis...305 8.2.3 Correlation Between Variables...306 8.2.4 Expanded Uncertainty from Degrees of Freedom in Combined Uncertainty using the Welch- Satterthwaite Formula...308 8.2.5 Uncertainty in Replicated Experiments...309 8.2.6 Jitter Macro...310 8.3 MONTE CARLO SIMULATION OF UNCERTAINTY PROPAGATION...321 8.3.1 Latin Hypercube Sampling...326 8.3.2 Correlation in Monte Carlo Analysis...328 8.4 LOG NORMAL DISTRIBUTION...336 8.5 EPILOGUE ON UNCERTAINTY ANALYSIS...337 CHAPTER 9 LEAST-SQUARES REGRESSION AND DATA MODELING...338 9.1 LEAST-SQUARES REGRESSION...340 9.2 LINEAR REGRESSION...341 9.2.1 Excel s Chart Tools and Trend line...342 9.2.2 Slope and Intercept Functions...344 9.3 MULTIVARIABLE LINEAR REGRESSION...345 9.3.1 Excel s LINEST Array Function...347 9.4 NONLINEAR REGRESSION...348 9.4.1 Select Good Initial Guesses for Nonlinear Model Parameters...351 9.5 MODEL VALIDATION: ASSESSMENT AND UNCERTAINTY ANALYSIS...354 9.5.1 Linearization...354 9.5.2 Average Absolute and Relative Deviation...362 9.5.3 Coefficient of Determination: R 2 and Adjusted R 2...363 9.5.4 Standard Uncertainty in Regression with Parameter Correlation...364
vi PRACTICAL NUMERICAL METHODS 9.5.5 Centering Data to Eliminate Correlation in Linear Model Parameters... 365 9.5.6 P-values and t-statistics for Hypothesis Testing in Linear Models... 365 9.5.7 Residual Plots and Analysis... 366 9.5.8 Normal Residual Plots... 367 9.5.9 Weighted Least-Squares Regression... 375 9.5.10 Jackknife Method of Regression Parameter Uncertainty Analysis... 378 9.5.11 Gauss-Newton with Uncertainty Analysis... 382 9.5.12 Levenberg-Marquardt Least Squares Regression... 400 9.5.13 Monte Carlo Analysis of Uncertainty in Regression Parameters... 406 9.5.14 Plot Confidence Intervals and Error Bars... 419 9.6 CHOOSING AN EMPIRICAL MODEL... 422 CHAPTER 10 INTERPOLATION... 429 10.1 LINEAR INTERPOLATION... 430 10.2 LAGRANGE POLYNOMIAL INTERPOLATION... 432 10.3 RATIONAL INTERPOLATING FUNCTIONS... 437 10.4 CUBIC SPLINES... 441 10.4.1 Cubic Spline Derivative Approximation... 450 10.4.2 Integrating with Cubic Splines... 451 10.4.3 Constrained Cubic Splines... 454 10.5 EPILOGUE ON INTERPOLATION... 459 CHAPTER 11 INTEGRATION... 461 11.1 GRAPHICAL INTEGRATION (RECTANGLE RULE)... 462 11.2 TRAPEZOIDAL RULE... 465 11.2.1 Improper Integrals and the Midpoint Rule... 469 11.2.2 Integration Accuracy and Precision... 471 11.2.3 Romberg Integration with Error Control... 472 11.2.4 Data Integration by the Trapezoidal Rule with Uncertainty Propagation... 476 11.3 SIMPSON S RULES... 477 11.4 GAUSS QUADRATURE WITH ADAPTIVE ERROR CONTROL... 485 11.5 MULTIPLE INTEGRATION... 492 11.5.1 Double Integration by Simpson s Rules... 493 11.5.2 Multiple Integration by Gauss-Kronrod Quadrature... 497 11.5.3 Monte Carlo Integration... 501 11.6 EPILOGUE ON INTEGRATION... 506 CHAPTER 12 INITIAL-VALUE PROBLEMS... 508 12.1 EULER S METHOD... 509 12.1.1 Stability and Stiffness... 513 12.1.2 Simultaneous First-order Differential Equations... 515 12.2 TRAPEZOIDAL RULES FOR IVPS... 517 12.3 RUNGE-KUTTA METHODS... 522 12.3.1 Integration Accuracy versus Precision... 532 12.3.2 Adaptive Runge-Kutta Methods... 532 12.4 MULTI-STEP METHODS... 543 12.5 DIFFERENTIAL-ALGEBRAIC SYSTEMS... 546 12.6 EPILOGUE ON IVP... 550 CHAPTER 13 BOUNDARY-VALUE PROBLEMS AND PDES... 551 13.1 SHOOTING METHOD... 553 13.2 FINITE-DIFFERENCE... 565 13.2.1 Symmetric Boundary Conditions... 567 13.2.2 Finite-differences for Elliptic PDEs... 570 13.3 ORTHOGONAL COLLOCATION... 573 13.3.1 Orthogonal Collocation on Finite Elements... 578 13.4 PARABOLIC PDES... 578 13.4.1 Method of Lines... 579 13.4.2 Crank-Nicholson Implicit Method... 582 CHAPTER 14 REVIEW OF PRACTICAL NUMERICAL METHODS... 586
CONTENTS vii 14.1 APPENDIX A: EXCEL WORKSHEET FUNCTIONS FOR NUMERICAL METHODS...587 14.2 APPENDIX B: VBA FUNCTIONS AND OBJECTS FOR NUMERICAL METHODS...588 14.3 APPENDIX C: VBA USER FUNCTIONS IN THE EXCEL WORKBOOK PNMSUITE...591 14.4 APPENDIX D: VBA MACROS IN THE EXCEL WORKBOOK PNMSUITE...592 REFERENCES...598 INDEX...602 Book Type Conventions for Excel and VBA menus and formulas / sub menu tree in the Visual Basic Editor > sub menu tree from Excel ribbon tabs and groups Calibri Excel menu syntax, Excel worksheet text Courier Visual Basic of Applications syntax Italics keyboard operation (or emphasis)