Numerical Analysis Algorithms in C Version 4.2 A Examples Book For "Numerical Analysis", fourth edition Richard L. Burden and J. Douglas Faires 1988 Written by: Harold A. Toomey, MSEE Care-Free Software 3rd Quarter 1991 Technical Publications: Harold A. Toomey Programming: Harold A. Toomey © Copyright 1988-1993, Harold A. Toomey - All rights reserved This document contains proprietary information of Harold A. Toomey and is protected by Federal copyright law. The contents of this document may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without prior written consent of Harold A. Toomey. Limited rights exist for individual and university site licenses. The software may be used or copied only in accordance with the terms of the license agreement. Students may copy this software with the intent to join the $20.00 Club, paying for the right to use this software. See the sample license agreements in the User's Manual. The information in this document is subject to change without notice. "Numerical Analysis Algorithms in C" Examples Book Version 4.2 Document Number 9307-42C-EB2 Care-Free Software Attn: Harold Allen Toomey 464 North 750 East Lindon, UT 84042 1-801-785-0464 Table of Contents CHAPTER 1 Mathematical Preliminaries COMPLEX.C EQEVAL.C GAUSSJ.C NAAUTIL.C NAAUTIL2.C NAAUTIL3.C ROUND.C TRUNC.C 011B.C* - "Numerical Recipes in C" Complex Number Routines - Equation Evaluator Routines - "Numerical Recipes in C" Gauss-Jordan Matrix Solver - "Numerical Analysis Algorithms in C" Utilities I (standard) - "Numerical Analysis Algorithms in C" Utilities II (extended) - "Numerical Analysis Algorithms in C" Utilities III (complex) - Rounds a floating point value to SIG significant digits - Truncates a floating point value to SIG significant digits - Taylor Polynomial Approximation Algorithm 1.1B CHAPTER 2 Solutions of Equations in One Variable 021.C* 022.C* 023.C 024.C* 024B.C* 024C.C 025.C* 026.C* 027.C* 028A.C* + Bisection (or Binary-Search) Fixed-Point Newton-Raphson Secant Method of False Position (or Regula Falsi) Modified Newton-Raphson Method Steffensen Horner Müller Complex Polynomial Solver (CPOLY) Algorithm 2.1 Algorithm 2.2 Algorithm 2.3 Algorithm 2.4 Algorithm 2.4B Algorithm 2.4C Algorithm 2.5 Algorithm 2.6 Algorithm 2.7 Algorithm 2.8A CHAPTER 3 Interpolation and Polynomial Approximation 031.C* 031B.C* 031C.C* 032.C* 033.C* 034.C* 035.C* - Neville's Iterated Interpolation Neville's Iterated Interpolation (with rounding) Aitken's Iterated Interpolation Newton's Interpolatory Divided-Difference Formula Hermite Interpolation Natural Cubic Spline Clamped Cubic Spline CHAPTER 4 Numerical Differentiation and Integration i Algorithm 3.1 Algorithm 3.1B Algorithm 3.1C Algorithm 3.2 Algorithm 3.3 Algorithm 3.4 Algorithm 3.5 040B1.C 040B2.C* 040B3.C 040C1.C 040C2.C* 040D1.C* 040D2.C* 041.C* 041B.C* 041C.C* 041D.C* 042.C* 043.C* 043B.C* 044.C 044B.C 044C.C 045.C 045B.C 045C.C - 1st Derivative Approximation (for functions) Algorithm 4.0B1 1st Derivative Approximation (for tabulated data) Algorithm 4.0B2 1st Derivative Approximation (for functions w/TOL) Algorithm 4.0B3 2nd Derivative Approximation (for functions) Algorithm 4.0C1 2nd Derivative Approximation (for tabulated data) Algorithm 4.0C2 Richardson's Extrapolation Algorithm 4.0D1 Richardson's Extrapolation (with rounding) Algorithm 4.0D2 Composite Simpson's Rule Algorithm 4.1 Composite Trapezoidal Rule Algorithm 4.1B Composite Midpoint Rule Algorithm 4.1C Newton-Cotes Formulas for Integrals (8 total) Algorithm 4.1D Adaptive Quadrature Algorithm 4.2 Romberg Algorithm 4.3 Gaussian Quadrature Algorithm 4.3B Composite Simpson's Rule for Double Integrals Algorithm 4.4 Composite Trapezoid Rule for Double Integrals Algorithm 4.4B Gaussian Quadrature for Double Integrals Algorithm 4.4C Composite Simpson's Rule for Triple Integrals Algorithm 4.5 Composite Trapezoid Rule for Triple Integrals Algorithm 4.5B Gaussian Quadrature for Triple Integrals Algorithm 4.5C CHAPTER 5 Initial-Value Problems for Ordinary Differential Equations 051.C* 051B.C* 052.C* 053.C 054.C* 054B.C* 054C.C* 055.C* 056.C* 057.C 057B.C 058.C + - Euler Algorithm 5.1 Midpoint, Modified Euler, and Heun's Methods Algorithm 5.1B Runge-Kutta (Order Four) Algorithm 5.2 Runge-Kutta-Fehlberg Algorithm 5.3 Adam's Fourth-Order Predictor-Corrector Algorithm 5.4 Adams-Bashforth (all four) and Milne's Methods Algorithm 5.4B Milne-Simpson Predictor-Corrector Algorithm 5.4C Adam's Variable Step-size Predictor-Corrector Algorithm 5.5 Extrapolation Algorithm 5.6 Runge-Kutta for Systems of Differential Equations Algorithm 5.7 Euler's Variable Step-Size for Systems Algorithm 5.7B Trapezoidal with Newton Iteration Algorithm 5.8 CHAPTER 6 Direct Methods for Solving Linear Systems 060B.C* 060C.C* 060D.C* 061.C* 061B.C* - Matrix Inverter Determinant of a Matrix Matrix Multiplier Gaussian Elimination with Backward Substitution Gaussian Elimination with Backward Substitution ii Algorithm 6.0B Algorithm 6.0C Algorithm 6.0D Algorithm 6.1 Algorithm 6.1B 061C1.C* 061C2.C* 061D1.C* 061D2.C* 062.C* 062B.C* 063.C* 063B.C* 064.C* 064B.C* 064C.C* 065.C* 065B.C* 066.C* 066B.C* 067.C* (with rounding) - Gauss-Jordan Method Algorithm 6.1C1 - Gauss-Jordan Method (with rounding) Algorithm 6.1C2 - Gaussian-Elimination - Gauss-Jordan Hybrid Method Algorithm 6.1D1 - Gaussian-Elimination - Gauss-Jordan Hybrid Method Algorithm 6.1D2 (with rounding) - Gaussian Elimination with Maximal Column Pivoting Algorithm 6.2 - Gaussian Elimination with Maximal Column Pivoting Algorithm 6.2B (with rounding) - Gaussian Elimination with Scaled Column Pivoting Algorithm 6.3 - Gaussian Elimination with Scaled Column Pivoting Algorithm 6.3B (with rounding) - Direct Factorization Algorithm 6.4 - Direct Factorization which solves AX=B Algorithm 6.4B - Direct Factorization with Maximal Column Pivoting Algorithm 6.4C (3rd edition) - LDLt Factorization Algorithm 6.5 - LDLt Factorization which solves AX=B Algorithm 6.5B - Choleski Algorithm 6.6 - Choleski which solves AX=B Algorithm 6.6B - Crout Reduction for Tridiagonal Linear Systems Algorithm 6.7 CHAPTER 7 Iterative Techniques in Matrix Algebra 070B.C* 071.C* 072.C* 073.C* 074.C* 074B.C* - Vector and Matrix Norms Jacobi Iterative Gauss-Seidel Iterative Successive Over Relaxation (SOR) Iterative Refinement (with rounding) Iterative Refinement (single-precision) Algorithm 7.0B Algorithm 7.1 Algorithm 7.2 Algorithm 7.3 Algorithm 7.4 Algorithm 7.4B CHAPTER 8 Approximation Theory 080B.C* 081.C* - Least-Squares Polynomial Approximation + Fast Fourier Transformation Algorithm 8.0B Algorithm 8.1 CHAPTER 9 Approximating Eigenvalues 091.C* 091B.C* 092.C* 093.C* - Power Method Power Method with Aitken's Delta^2 Method Symmetric Power Method Inverse Power Method iii Algorithm 9.1 Algorithm 9.1B Algorithm 9.2 Algorithm 9.3 094.C* 094B.C* 095.C* 095B.C* 095C.C* 095D.C* 096.C* 096B.C* - Wielandt's Deflation Algorithm 9.4 - Wielandt's Deflation using Power Method for Algorithm 9.4B lambda1 - Householder Method Algorithm 9.5 - Householder Method (3rd edition) Algorithm 9.5B - Householder Method for Non-Symmetric Matrices Algorithm 9.5C (Upper Hessenberg) - Householder Method (with rounding) Algorithm 9.5D - QR Algorithm Algorithm 9.6 - QL Algorithm (3rd edition) Algorithm 9.6B CHAPTER 10 Numerical Solutions of Nonlinear Systems of Equations 101.C 101A.C 102.C 103.C 103B.C - Newton's Method for Systems Steffensen's Method for Systems Broyden's Method for Systems Steepest Descent Method (with F(x) and J(x)) Steepest Descent Method (with G(x) and gradG(x)) Algorithm 10.1 Algorithm 10.1A Algorithm 10.2 Algorithm 10.3 Algorithm 10.3B CHAPTER 11 Boundary-Value Problems for Ordinary Differential Equations 111.C 112.C 112B.C 113.C 113B.C 114.C 114B.C 115.C 116.C - Linear Shooting Nonlinear Shooting with Newton's Method Nonlinear Shooting with Secant Method Linear Finite Difference Linear Finite Difference (Richardson's Extrapolation) Nonlinear Finite Difference Nonlinear Finite Difference (Richardson's Extrap.) Piecewise Linear Rayleigh-Ritz Cubic Spline Rayleigh-Ritz Algorithm 11.1 Algorithm 11.2 Algorithm 11.2B Algorithm 11.3 Algorithm 11.3B Algorithm 11.4 Algorithm 11.4B Algorithm 11.5 Algorithm 11.6 CHAPTER 12 Numerical Solutions to Partial-Differential Equations 121.C 122.C* 122B.C* 122C.C* 123.C* 124.C 125.C 126A.C 127A.C - Poisson Equation Finite-Difference (Elliptic) Heat Equation Backward-Difference (Parabolic) Heat Equation Forward-Difference (Parabolic) Heat Equation Richardson's Method (Parabolic) Crank-Nicolson (Parabolic) Wave Equation Finite-Difference (Hyperbolic) Finite-Element Parabolic Equations With Newton Iteration in 1-D Parabolic Equations With Newton Iteration in 2-D iv Algorithm 12.1 Algorithm 12.2 Algorithm 12.2B Algorithm 12.2C Algorithm 12.3 Algorithm 12.4 Algorithm 12.5 Algorithm 12.6A Algorithm 12.7A 128A.C 129A.C - Elliptic Equations With Newton Iteration in 2-D - Biharmonic Equation Using Gauss-Jordan Method Algorithm 12.8A Algorithm 12.9A APPENDIX A "Numerical Analysis Algorithms in C" Supporting C Source Code 041EE.C* 041FUN.C CONVERT.C* EE.C* - Composite Simpson's Rule Using the Equation Evaluator Routines - Composite Simpson's Rule as a Function - Converts Files from 8-bit Extended ASCII to 7-bit Standard ASCII - Command-Line Calculator using the Equation Evaluator Routines v This Examples Book contains the C source code for over a hundred algorithms found in the text Numerical Analysis, fourth edition, Richard L. Burden and J. Douglas Faires. The algorithms include listings of the C source code as well as the accompanying output files for the examples problems given in the text. The inputs can be easily extracted from the output listings or can be obtained from the diskettes. Other needed programs and routines are printed in the appendix. The '+'s above mean the program may need a larger stack when compiled and linked. The '*'s after the above algorithms mean the programs need to be compiled only once. After the initial compilation, the executable can be used over and over again. See the User's Manual for a detailed description on how to use these algorithms most effectively. The header file "naautil.c" is included inside every algorithm. This is the only file which may need to be modified. It contains some flags which can be set to make the code more portable for various compilers. It also contains flags which allow the user to turn on or off the following options: 1. Prompt for an optional title 2. Save output to a file 3. Use the Equation Evaluator Routines The following compilers have successfully run all of the "Numerical Analysis Algorithms in C" v4.2 programs: 1. 2. 3. 4. Microsoft C 5.0 for MS-DOS on an IBM PC THINK C 4.0 on a Macintosh SE (Set ANSI_FUNCT == TRUE) MIPS C compiler (IRIX 3.3.1) on a Silicon Graphics 4D workstation VAX C v3.1 and v3.2 compilers on a VAX vi CHAPTER 1 Mathematical Preliminaries A 7 CHAPTER 1 Mathematical Preliminaries COMPLEX.C EQEVAL.C GAUSSJ.C NAAUTIL.C NAAUTIL2.C NAAUTIL3.C ROUND.C TRUNC.C 011B.C* - "Numerical Recipes in C" Complex Number Routines - Equation Evaluator Routines - "Numerical Recipes in C" Gauss-Jordan Matrix Solver - "Numerical Analysis Algorithms in C" Utilities I (standard) - "Numerical Analysis Algorithms in C" Utilities II (extended) - "Numerical Analysis Algorithms in C" Utilities III (complex) - Rounds a floating point value to SIG significant digits - Truncates a floating point value to SIG significant digits - Taylor Polynomial Approximation Algorithm 1.1B 8 CHAPTER 2 Solutions of Equations in One Variable A 9 CHAPTER 2 Solutions of Equations in One Variable 021.C* 022.C* 023.C 024.C* 024B.C* 024C.C 025.C* 026.C* 027.C* 028A.C* + Bisection (or Binary-Search) Fixed-Point Newton-Raphson Secant Method of False Position (or Regula Falsi) Modified Newton-Raphson Method Steffensen Horner Müller Complex Polynomial Solver (CPOLY) 10 Algorithm 2.1 Algorithm 2.2 Algorithm 2.3 Algorithm 2.4 Algorithm 2.4B Algorithm 2.4C Algorithm 2.5 Algorithm 2.6 Algorithm 2.7 Algorithm 2.8A CHAPTER 3 Interpolation and Polynomial Approximation A 11 CHAPTER 3 Interpolation and Polynomial Approximation 031.C* 031B.C* 031C.C* 032.C* 033.C* 034.C* 035.C* - Neville's Iterated Interpolation Neville's Iterated Interpolation (with rounding) Aitken's Iterated Interpolation Newton's Interpolatory Divided-Difference Formula Hermite Interpolation Natural Cubic Spline Clamped Cubic Spline 12 Algorithm 3.1 Algorithm 3.1B Algorithm 3.1C Algorithm 3.2 Algorithm 3.3 Algorithm 3.4 Algorithm 3.5 CHAPTER 4 Numerical Differentiation and Integration A 13 CHAPTER 4 Numerical Differentiation and Integration 040B1.C 040B2.C* 040B3.C 040C1.C 040C2.C* 040D1.C* 040D2.C* 041.C* 041B.C* 041C.C* 041D.C* 042.C* 043.C* 043B.C* 044.C 044B.C 044C.C 045.C 045B.C 045C.C - 1st Derivative Approximation (for functions) Algorithm 4.0B1 1st Derivative Approximation (for tabulated data) Algorithm 4.0B2 1st Derivative Approximation (for functions w/TOL) Algorithm 4.0B3 2nd Derivative Approximation (for functions) Algorithm 4.0C1 2nd Derivative Approximation (for tabulated data) Algorithm 4.0C2 Richardson's Extrapolation Algorithm 4.0D1 Richardson's Extrapolation (with rounding) Algorithm 4.0D2 Composite Simpson's Rule Algorithm 4.1 Composite Trapezoidal Rule Algorithm 4.1B Composite Midpoint Rule Algorithm 4.1C Newton-Cotes Formulas for Integrals (8 total) Algorithm 4.1D Adaptive Quadrature Algorithm 4.2 Romberg Algorithm 4.3 Gaussian Quadrature Algorithm 4.3B Composite Simpson's Rule for Double Integrals Algorithm 4.4 Composite Trapezoid Rule for Double Integrals Algorithm 4.4B Gaussian Quadrature for Double Integrals Algorithm 4.4C Composite Simpson's Rule for Triple Integrals Algorithm 4.5 Composite Trapezoid Rule for Triple Integrals Algorithm 4.5B Gaussian Quadrature for Triple Integrals Algorithm 4.5C 14 CHAPTER 5 Initial-Value Problems for Ordinary Differential Equations A 15 CHAPTER 5 Initial-Value Problems for Ordinary Differential Equations 051.C* 051B.C* 052.C* 053.C 054.C* 054B.C* 054C.C* 055.C* 056.C* 057.C 057B.C 058.C + - Euler Algorithm 5.1 Midpoint, Modified Euler, and Heun's Methods Algorithm 5.1B Runge-Kutta (Order Four) Algorithm 5.2 Runge-Kutta-Fehlberg Algorithm 5.3 Adam's Fourth-Order Predictor-Corrector Algorithm 5.4 Adams-Bashforth (all four) and Milne's Methods Algorithm 5.4B Milne-Simpson Predictor-Corrector Algorithm 5.4C Adam's Variable Step-size Predictor-Corrector Algorithm 5.5 Extrapolation Algorithm 5.6 Runge-Kutta for Systems of Differential Equations Algorithm 5.7 Euler's Variable Step-Size for Systems Algorithm 5.7B Trapezoidal with Newton Iteration Algorithm 5.8 16 CHAPTER 6 Direct Methods for Solving Linear Systems A 17 CHAPTER 6 Direct Methods for Solving Linear Systems 060B.C* 060C.C* 060D.C* 061.C* 061B.C* 061C1.C* 061C2.C* 061D1.C* 061D2.C* 062.C* 062B.C* 063.C* 063B.C* 064.C* 064B.C* 064C.C* 065.C* 065B.C* 066.C* 066B.C* 067.C* - Matrix Inverter Algorithm 6.0B Determinant of a Matrix Algorithm 6.0C Matrix Multiplier Algorithm 6.0D Gaussian Elimination with Backward Substitution Algorithm 6.1 Gaussian Elimination with Backward Substitution Algorithm 6.1B (with rounding) - Gauss-Jordan Method Algorithm 6.1C1 - Gauss-Jordan Method (with rounding) Algorithm 6.1C2 - Gaussian-Elimination - Gauss-Jordan Hybrid Method Algorithm 6.1D1 - Gaussian-Elimination - Gauss-Jordan Hybrid Method Algorithm 6.1D2 (with rounding) - Gaussian Elimination with Maximal Column Pivoting Algorithm 6.2 - Gaussian Elimination with Maximal Column Pivoting Algorithm 6.2B (with rounding) - Gaussian Elimination with Scaled Column Pivoting Algorithm 6.3 - Gaussian Elimination with Scaled Column Pivoting Algorithm 6.3B (with rounding) - Direct Factorization Algorithm 6.4 - Direct Factorization which solves AX=B Algorithm 6.4B - Direct Factorization with Maximal Column Pivoting Algorithm 6.4C (3rd edition) - LDLt Factorization Algorithm 6.5 - LDLt Factorization which solves AX=B Algorithm 6.5B - Choleski Algorithm 6.6 - Choleski which solves AX=B Algorithm 6.6B - Crout Reduction for Tridiagonal Linear Systems Algorithm 6.7 18 CHAPTER 7 Iterative Techniques in Matrix Algebra A 19 CHAPTER 7 Iterative Techniques in Matrix Algebra 070B.C* 071.C* 072.C* 073.C* 074.C* 074B.C* - Vector and Matrix Norms Jacobi Iterative Gauss-Seidel Iterative Successive Over Relaxation (SOR) Iterative Refinement (with rounding) Iterative Refinement (single-precision) 20 Algorithm 7.0B Algorithm 7.1 Algorithm 7.2 Algorithm 7.3 Algorithm 7.4 Algorithm 7.4B CHAPTER 8 Approximation Theory A 21 CHAPTER 8 Approximation Theory 080B.C* 081.C* - Least-Squares Polynomial Approximation + Fast Fourier Transformation 22 Algorithm 8.0B Algorithm 8.1 CHAPTER 9 Approximating Eigenvalues A 23 CHAPTER 9 Approximating Eigenvalues 091.C* 091B.C* 092.C* 093.C* 094.C* 094B.C* - 095.C* 095B.C* 095C.C* - 095D.C* 096.C* 096B.C* - Power Method Algorithm 9.1 Power Method with Aitken's Delta^2 Method Algorithm 9.1B Symmetric Power Method Algorithm 9.2 Inverse Power Method Algorithm 9.3 Wielandt's Deflation Algorithm 9.4 Wielandt's Deflation using Power Method for Algorithm 9.4B lambda1 Householder Method Algorithm 9.5 Householder Method (3rd edition) Algorithm 9.5B Householder Method for Non-Symmetric Matrices Algorithm 9.5C (Upper Hessenberg) Householder Method (with rounding) Algorithm 9.5D QR Algorithm Algorithm 9.6 QL Algorithm (3rd edition) Algorithm 9.6B 24 CHAPTER 10 Numerical Solutions of Nonlinear Systems of Equations A 25 CHAPTER 10 Numerical Solutions of Nonlinear Systems of Equations 101.C 101A.C 102.C 103.C 103B.C - Newton's Method for Systems Steffensen's Method for Systems Broyden's Method for Systems Steepest Descent Method (with F(x) and J(x)) Steepest Descent Method (with G(x) and gradG(x)) 26 Algorithm 10.1 Algorithm 10.1A Algorithm 10.2 Algorithm 10.3 Algorithm 10.3B CHAPTER 11 Boundary-Value Problems for Ordinary Differential Equations A 27 CHAPTER 11 Boundary-Value Problems for Ordinary Differential Equations 111.C 112.C 112B.C 113.C 113B.C 114.C 114B.C 115.C 116.C - Linear Shooting Nonlinear Shooting with Newton's Method Nonlinear Shooting with Secant Method Linear Finite Difference Linear Finite Difference (Richardson's Extrapolation) Nonlinear Finite Difference Nonlinear Finite Difference (Richardson's Extrap.) Piecewise Linear Rayleigh-Ritz Cubic Spline Rayleigh-Ritz 28 Algorithm 11.1 Algorithm 11.2 Algorithm 11.2B Algorithm 11.3 Algorithm 11.3B Algorithm 11.4 Algorithm 11.4B Algorithm 11.5 Algorithm 11.6 CHAPTER 12 Numerical Solutions to Partial-Differential Equations A 29 CHAPTER 12 Numerical Solutions to Partial-Differential Equations 121.C 122.C* 122B.C* 122C.C* 123.C* 124.C 125.C 126A.C 127A.C 128A.C 129A.C - Poisson Equation Finite-Difference (Elliptic) Heat Equation Backward-Difference (Parabolic) Heat Equation Forward-Difference (Parabolic) Heat Equation Richardson's Method (Parabolic) Crank-Nicolson (Parabolic) Wave Equation Finite-Difference (Hyperbolic) Finite-Element Parabolic Equations With Newton Iteration in 1-D Parabolic Equations With Newton Iteration in 2-D Elliptic Equations With Newton Iteration in 2-D Biharmonic Equation Using Gauss-Jordan Method 30 Algorithm 12.1 Algorithm 12.2 Algorithm 12.2B Algorithm 12.2C Algorithm 12.3 Algorithm 12.4 Algorithm 12.5 Algorithm 12.6A Algorithm 12.7A Algorithm 12.8A Algorithm 12.9A APPENDIX A "Numerical Analysis Algorithms in C" Supporting C Source Code A 31 APPENDIX A "Numerical Analysis Algorithms in C" Supporting C Source Code 041EE.C* 041FUN.C CONVERT.C* EE.C* - Composite Simpson's Rule Using the Equation Evaluator Routines - Composite Simpson's Rule as a Function - Converts Files from 8-bit Extended ASCII to 7-bit Standard ASCII - Command-Line Calculator using the Equation Evaluator Routines 32