Applied Numerical Methods Using MATLAB 9781683928683, 2022950636

The book is designed to cover all major aspects of applied numerical methods, including numerical computations, solution

303 84 58MB

English Pages 1225 Year 2023

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Cover
Half-Title
Title
Copyright
Contents
Preface
Chapter 1: Numerical Computations
1.1 Taylor’s Theorem
1.2 Number Representation
1.3 Error Considerations
1.3.1 Absolute and Relative Errors
1.3.2 Inherent Errors
1.3.3 Round-off Errors
1.3.4 Truncation Errors
1.3.5 Machine Epsilon
1.3.6 Error Propagation
1.4 Error Estimation
1.5 General Error Formula
1.5.1 Function Approximation
1.5.2 Stability and Condition
1.5.3 Uncertainty in Data or Noise
1.6 Sequences
1.6.1 Linear Convergence
1.6.2 Quadratic Convergence
1.6.3 Aitken’s Acceleration Formula
1.7 Summary
Exercises
Chapter 2: Linear System of Equations
2.1 Introduction
2.2 Methods of Solution
2.3 The Inverse of a Matrix
2.4 Matrix Inversion Method
2.4.1 Augmented Matrix
2.5 Gauss Elimination Method
2.5.1 MATLAB Program for the Gauss Elimination Method
2.6 Gauss-Jordan Method
2.6.1 MATLAB Program for the Gauss Jordan Method
2.7 Cholesky’s Triangularization Method
2.8 Crout’s Method
2.8.1 MATLAB Program for Crout’s Method
2.9 Thomas Algorithm for Tridiagonal System
2.9.1 MATLAB Program for the Thomas Method for Tridiagonal Systems
2.10 Jacobi’s Iteration Method
2.10.1 MATLAB Program for the Jacobi Iteration Method
2.11 Gauss-Seidel Iteration Method
2.11.1 MATLAB Program for the Gauss Seidel Method
2.12 Symmetric Matrix Eigenvalue Problems
2.12.1 The Jacobi Method
2.12.2 MATLAB Function for the Jacobi Method
2.12.3 Householder Reduction to Tridiagonal Form
2.12.4 Gerschgorin’s Circle Theorem
2.12.5 Sturm Sequence
2.12.6 QR Method
2.12.7 Power Method
2.12.8 Inverse Power Method
2.13 Summary
Exercises
Chapter 3: Solution of Algebraic and Transcendental Equations
3.1 Introduction
3.2 Bisection Method
3.2.1 Error Bounds
3.3 Method of False Position
3.3.1 MATLAB Program for the False Position Method
3.4 Newton-Raphson Method
3.4.1 Convergence of the Newton-Raphson Method
3.4.2 Rate of Convergence of the Newton-Raphson Method
3.4.3 MATLAB Program for the Newton Raphson Method
3.4.4 Modified Newton-Raphson Method
3.4.5 Rate of Convergence of Modified Newton-Raphson Method
3.5 Successive Approximation Method
3.5.1 Error Estimate in the Successive Approximation Method
3.6 Secant Method
3.6.1 Convergence of the Secant Method
3.6.2 MATLAB Program to Search for a Root of the Function f(x) in the Interval (a,b)
3.6.3 MATLAB Program for Secant Method
3.7 Muller’s Method
3.7.1 MATLAB Program for Muller’s Method
3.8 Chebyshev Method
3.9 Aitken’s Δ2 Method
3.10 Brent’s Method
3.10.1 MATLAB Program for Brent’s Method
3.11 Newton Method for a System of Nonlinear Equations
3.12 Comparison of Iterative Methods
3.13 MATLAB Built-in Function: fzero
3.14 Summary
Exercises
Chapter 4: Numerical Differentiation
4.1 Introduction
4.2 Derivatives Based on Newton’s Forward Integration Formula
4.2.1 MATLAB Program for Derivatives Based on Newton’s Forward Integration Formula—Equally Spaced Points
4.3 Derivatives Based on Newton’s Backward Interpolation Formula
4.4 Derivatives Based on Stirling’s Interpolation Formula
4.5 Maxima and Minima of a Tabulated Function
4.6 Cubic Spline Method
4.7 Richardson Extrapolation
4.8 Differentiation of Unequally Spaced Data
4.9 MATLAB Built-in Functions: diff and gradient
4.10 Summary
Exercises
Chapter 5: Finite Differences and Interpolation
5.1 Introduction
5.2 Finite Difference Operators
5.2.1 Forward Differences
5.2.2 Backward Differences
5.2.3 Central Differences
5.2.4 Error Propagation in a Difference Table
5.2.5 Properties of the Operator Δ
5.2.6 Difference Operators
5.2.7 Relation Among the Operators
5.2.8 Representation of a Polynomial using Factorial Notation
5.3 Interpolation with Equal Intervals
5.3.1 Missing Values
5.3.2 Newton’s Binomial Expansion Formula
5.3.3 Newton’s Forward Interpolation Formula
5.3.4 MATLAB M-file: Newtonint
5.3.5 Newton’s Backward Interpolation Formula
5.3.6 Error in the Interpolation Formula
5.4 Interpolation with Unequal Intervals
5.4.1 Lagrange’s Interpolating Polynomial for Equal Intervals
5.4.2 function yint = Lagrangeint (x,y,xx)
5.4.3 Lagrange’s Formula for Unequal Intervals
5.4.4 Hermite’s Interpolation Formula
5.4.5 Inverse Interpolation
5.4.6 Lagrange’s Formula for Inverse Interpolation
5.5 Central Difference Interpolation Formulae
5.5.1 Gauss’s Forward Interpolation Formula
5.5.2 Gauss Backward Interpolation Formula
5.5.3 Bessel’s Formula
5.5.4 Stirling’s Formula
5.5.5 Laplace-Everett’s Formula
5.5.6 Selection of an Interpolation Formula
5.6 Divided Differences
5.6.1 Newton’s Divided Difference Interpolation Formula
5.7 Cubic Spline Interpolation
5.8 Generalized Spline Method
5.8.1 Splines
5.8.2 Linear Splines
5.8.3 Quadratic Splines
5.8.4 Cubic Splines
5.8.5 End Conditions
5.8.6 MATLAB Built-in Function: spline
5.8.7 Multidimensional Interpolation
5.8.8 MATLAB Built-in Function: interpl
5.9 Summary
Exercises
Chapter 6: Curve Fitting, Regression, and Correlation
Approximating Curves
Linear Regression
6.1 Linear Equation
6.2 Curve Fitting With a Linear Equation
6.3 Criteria for a Best Fit
6.4 Linear Least-Squares Regression
6.5 Linear Regression Analysis
6.5.1 MATLAB built-in function: polyfit
6.5.2 MATLAB built-in function: polyval
6.6 Interpretation of a and b
Assumptions in the Regression Model
6.7 Standard Deviation of Random Errors
6.8 Coefficient of Determination
6.9 Linear Correlation
Properties of the Linear Correlation Coefficient r
Explained and Unexplained Variation
6.10 Linearization of Nonlinear Relationships
6.11 Polynomial Regression
6.11.1 Polynomial Fit
6.11.2 MATLAB Built-in Functions for Polynomial Fit
6.12 Quantification of Error of Linear Regression
6.13 Multiple Linear Regression
6.14 Weighted Least-Squares Method
6.15 Orthogonal Polynomials and Least-Squares Approximation
6.16 Least-Squares Method for Continuous Data
6.17 Approximation Using Orthogonal Polynomials
6.18 Gram-Schmidt Orthogonalization Process
6.19 Fitting a Function Having a Specified Power
6.20 Fitting a Cubic Spring Model
6.21 Additional Example Problems and Solutions
6.22 Summary
Exercises
Chapter 7: Numerical Integration
7.1 Introduction
7.1.1 Relative Error
7.2 Newton-Cotes Closed Quadrature Formula
7.3 Trapezoidal Rule
7.3.1 Error Estimate in Trapezoidal Rule
7.3.2 MATLAB Functions: trapz and cumtrapz
7.4 Simpson’s 1/3 Rule
7.4.1 Error Estimate in Simpson’s 1/3 Rule
7.4.2 MATLAB Program for Simpson’s Integration: simpsonint
7.4.3 MATLAB Built-in Functions: quad and quad1
7.5 Simpson’s 3/8 Rule
7.6 Boole’s and Weddle’s Rules
7.6.1 Boole’s Rule
7.6.2 Weddle’s Rule
7.7 Romberg’s Integration
7.7.1 Richardson’s Extrapolation
7.7.2 Romberg Integration Formula
7.7.3 MATLAB Program for Romberg Integration: Romberg
7.8 Gaussian Quadrature
7.8.1 Gaussian Integration Formulas
7.8.2 Orthogonal Polynomials
7.8.3 Gauss-Lagendre Quadrature
7.8.4 Gauss-Chebyshev Quadrature Method
7.8.5 Gauss-Laguerre Quadrature
7.8.6 Gauss-Hermite Quadrature
7.8.7 MATLAB Programs for Gaussian Quadrature: gaussnodes and gaussquad
7.9 Double Integration
7.9.1 Trapezoidal Method
7.9.2 Simpson’s 1/3 Rule
7.9.3 MATLAB Built-in Function for Double Integration: dblquad
7.10 Summary
Exercises
Chapter 8: Numerical Solution of Ordinary Differential Equations
8.1 Introduction
8.2 One-Step Methods or Single-Step Methods
8.2.1 Picard’s Method of Successive Approximation
8.2.2 The Taylor’s Series Method
8.3 Step-by-Step Methods or Marching Methods
8.3.1 Euler’s Method
8.3.2 MATLAB Program for Euler’s Method: euler
8.3.3 Modified Euler’s Method
8.3.4 MATLAB Program for the Modified Euler’s Method: modeuler
8.3.5 Runge-Kutta Methods
8.3.6 Predictor-Corrector Methods
8.4 MATLAB Functions for Ordinary Differential Equations: ode45, ode23, ode113, ode15s, ode23s, ode23t, ode23tb
8.5 System of First-order Ordinary Differential Equations
8.6 Initial Value Problems
8.6.1 The Taylor Series Method
8.6.2 Picard’s Method
8.6.3 Second-Order Runge-Kutta Method
8.6.4 Fourth-Order Runge-Kutta Method
8.6.5 Euler’s Formula
8.6.6 Modified Euler’s Formula
8.6.7 Burlirsch-Stoer Method (Mid-Point Method)
8.6.8 The Runge-Kutta-Fehlberg Method
8.6.9 The Runge-Kutta-Butcher Method
8.7 Two-Point Boundary Value Problems
8.7.1 Finite Difference Method
8.7.2 Second-Order Differential Equations
8.7.3 The Shooting Method
8.8 Second-Order Initial Value Problem (IVP)
8.9 Second-Order Boundary Value Problem (BVP)
8.10 MATLAB Built-in Functions
8.11 Summary
Exercises
Chapter 9: Direct Numerical Integration Methods
9.1 Introduction
9.2 Single Degree of Freedom System
9.2.1 Finite Difference Method
9.2.2 Central Difference Method
9.2.3 The Runge-Kutta Method
9.3 Multi-degree of Freedom Systems
9.4 Explicit Schemes
9.4.1 Central Difference Method
9.4.2 Two-Cycle Iteration with Trapezoidal Rule
9.4.3 Fourth-Order Runge-Kutta Method
9.5 Implicit Schemes
9.5.1 The Houbolt Method
9.5.2 Wilson Theta Method
9.5.3 The Newmark Beta Method
9.5.4 Park Stiffly Stable Method
9.6 Example Problems and Solutions Using MATLAB
9.7 Summary
Exercises
Additional Exercises
Chapter 10: Matlab Basics
10.1 Introduction
10.1.1 Starting and Quitting MATLAB
10.1.2 Display Windows
10.1.3 Entering Commands
10.1.4 MATLAB Expo
10.1.5 Abort
10.1.6 The Semicolon (;)
10.1.7 Typing %
10.1.8 The clc Command
10.1.9 Help
10.1.10 Statements and Variables
10.2 Arithmetic Operations
10.3 Display Formats
10.4 Elementary Math Built-In Functions
10.5 Variable Names
10.6 Predefined Variables
10.7 Commands For Managing Variables
10.8 General Commands
10.9 Arrays
10.9.1 Row Vector
10.9.2 Column Vector
10.9.3 Matrix
10.9.4 Addressing Arrays
10.9.5 Adding Elements to a Vector or a Matrix
10.9.6 Deleting Elements
10.9.7 Built-in Functions
10.10 Operations with Arrays
10.10.1 Addition and Subtraction of Matrices
10.10.2 Dot Product
10.10.3 Array Multiplication
10.10.4 Array Division
10.10.5 Identity Matrix
10.10.6 Inverse of a Matrix
10.10.7 Transpose
10.10.8 Determinant
10.10.9 Array Division
10.10.10 Left Division
10.10.11 Right Division
10.11 Element-By-Element Operations
10.11.1 Built-In Functions For Arrays
10.12 Random Numbers Generation
10.12.1 The Random Command
10.13 Polynomials
10.14 System of Linear Equations
10.14.1 Matrix Division
10.14.2 Matrix Inverse
10.15 Script Files
10.15.1 Creating and Saving a Script File
10.15.2 Running a Script File
10.15.3 Input to a Script File
10.15.4 Output Commands
10.16 Programming in Matlab
10.16.1 Relational and Logical Operators
10.16.2 Order of Precedence
10.16.3 Built-in Logical Functions
10.16.4 Conditional Statements
10.16.5 Nested if Statements
10.16.6 else AND elseif Clauses
10.16.7 MATLAB while Structures
10.17 Graphics
10.17.1 Basic 2-D Plots
10.17.2 Specialized 2-D Plots
10.17.3 3-D Plots
10.17.4 Saving and Printing Graphs
10.18 Input/Output In Matlab
10.18.1 The fopen Statement
10.19 Symbolic Mathematics
10.19.1 Symbolic Expressions
10.19.2 Solution to Differential Equations
10.19.3 Calculus
10.23 Summary
References
Exercises
Chapter 11: Optimization
11.1 Introduction
11.2 Unconstrained Minimization of Functions
11.3 Minimization with Constraints Using Lagrange Multipliers
11.4 Numerical Optimization
11.4.1 Optimization Involving Single Variables
11.4.2 Local and Global Optima
11.4.3 Bracketing
11.4.4 Golden-Section Search
11.4.5 MATLAB Program for Bracketing Method
11.4.6 MATLAB Program for Golden-Section Search Method
11.5 Multidimensional Optimization
11.6 Gradient Methods
11.7 Newton’s Method
11.7.1 MATLAB Program for Newton’s Method
11.8 Methods Based on the Concept of Quadratic Convergence
11.8.1 Conjugate Directions for a Quadratic Function
11.9 Powell’s Method
11.9.1 MATLAB Program for Powell’s Optimization Method
11.10 Fletcher-Reeves Method
11.10.1 MATLAB Program for Fletcher-Reeves Optimization Method
11.11 The Hooks and Jeeves Method
11.12 Method of Successive Linear Approximation
11.13 Interior Penalty Function Method
11.14 MATLAB Built-in Functions
11.14.1 MATLAB Function: fminbnd
11.14.2 MATLAB Function: fminsearch
11.15 Additional Example Problems and Solutions
11.16 Summary
References
Exercises
Chapter 12: Partial Differential Equations
12.1 Introduction
12.2 Classification of Linear Second-Order Partial Differential Equation
12.3 Types of Problems
12.4 Finite-Difference Approximation to Partial Derivatives
12.5 Physical Phenomena
12.5.1 Laplace’s Equation
12.5.2 Heat Equation
12.5.3 Wave Equation
12.5.4 Equation Classification
12.6 Elliptic Equations
12.6.1 Central Difference Method
12.6.2 Boundary Conditions
12.6.3 Iterative Solution Methods
12.6.4 The Jacobi Method
12.6.5 Gauss-Seidel Method
12.6.6 Successive Over-Relaxation or S.O.R. Method
12.7 One-Dimensional Parabolic Equations
12.7.1 Explicit Forward Euler Method
12.7.2 Implicit Backward Euler Method
12.7.3 The Crank-Nicolson Implicit Method
12.7.4 function [t,x,U] =Heatone(T,a,m,n,beta,c,f,g)
12.7.5 function [x,y,U] = Heattwo(T,a,b,m,n,p,beta,f,g)
12.7.6 function [t,x,U] = Waveone(T,a,m,n,beta,f,g)
12.7.7 function [x,y,U] = Wavetwo (T,a,b,m,n,p,beta,f,g)
12.7.8 function [alpha,r,x,y,U] = Poisson (a,b,m,n,q,tol,f,g)
12.8 Two-Dimensional Parabolic Equations
12.9 One-Dimensional Hyperbolic Equations
12.9.1 D’Alembert’s Solution
12.9.2 Explicit Central Difference Method
12.10 Two-Dimensional Hyperbolic Equations
12.10.1 Explicit Central Difference Method
12.11 MATLAB Built-in Function: pdepe
12.12 Summary
Exercises
Appendix A: Partial Fraction Expansions
Case-I
Partial Fraction Expansion when Q(s) has Distinct Roots
Case-II
Partial Fraction Expansion when Q(s) has Complex Conjugate Roots
Case-III
Partial Fraction Expansion when Q(s) has Repeated Roots
Exercises
Appendix B: Basic Engineering Mathematics
B.1 Algebra
B.1.1 Basic Laws
B.1.2 Sums of Numbers
B.1.3 Progressions
B.1.4 Powers and Roots
B.1.5 Binomial Theorem
B.1.6 Absolute Values
B.1.7 Logarithms
B.2 Trigonometry
B.2.1 Trigonometric Identities
B.2.2 Cosine Law (Law of Cosines)
B.2.3 Sine Law (Law of Sines)
B.3 Differential Calculus
B.3.1 List of Derivatives
B.3.2 Expansion in Series
B.4 Integral Calculus
B.4.1 List of Most Common Integrals
Appendix C: Cramer’s Rule
Exercises
Appendix D: Matlab Built-In M-File Functions
Appendix E: Matlab Programs
Appendix F: Answers to Odd Numbered Exercises
Bibliography
Index
Recommend Papers

Applied Numerical Methods Using MATLAB
 9781683928683, 2022950636

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Applied Numerical Methods using matlab

ANMF.CH00_FM_2PP.indd 1

2/24/2023 5:48:37 PM

license, disclaimer of liability, and limited warranty By purchasing or using this book and disc (the “Work”), you agree that this license grants permission to use the contents contained herein, including the disc, but does not give you the right of ownership to any of the textual content in the book / disc or ownership to any of the information or products contained in it. This license does not permit uploading of the Work onto the Internet or on a network (of any kind) without the written consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc. contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any portion of the textual material (in any media) that is contained in the Work. Mercury Learning and Information (“MLI” or “the Publisher”) and anyone involved in the creation, writing, or production of the companion disc, accompanying algorithms, code, or computer programs (“the software”), and any accompanying Web site or software of the Work, cannot and do not warrant the performance or results that might be obtained by using the contents of the Work. The author, developers, and the Publisher have used their best efforts to insure the accuracy and functionality of the textual material and/or programs contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs. The Work is sold “as is” without warranty (except for defective materials used in manufacturing the book or due to faulty workmanship). The author, developers, and the publisher of any accompanying content, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual material contained in this publication. This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work. The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book and/or disc, and only at the discretion of the Publisher. The use of “implied warranty” and certain “exclusions” vary from state to state and might not apply to the purchaser of this product.

ANMF.CH00_FM_2PP.indd 2

2/24/2023 5:48:37 PM

Applied Numerical Methods using matlab

R. V. Dukkipati, PhD

MERCURY LEARNING AND INFORMATION Dulles, Virginia Boston, Massachusetts New Delhi

ANMF.CH00_FM_2PP.indd 3

2/24/2023 5:48:37 PM

Reprint & Revision Copyright © 2023 by Mercury Learning and Information LLC. All rights reserved. Original Copyright © 2019 by NEW AGE International Publishers. This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means, media, electronic display or mechanical display, including, but not limited to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher. Publisher: David Pallai Mercury Learning and Information 22841 Quicksilver Drive Dulles, VA 20166 [email protected] www.merclearning.com (800) 232-0223 ®

R. V. Dukkipati. Applied Numerical Methods Using MATLAB . ISBN: 978-1-68392-868-3 The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to infringe on the property of others. Library of Congress Control Number: 2022950636 232425321  Printed on acid-free paper in the United States of America. Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact the Customer Service Dept. at (800) 232-0223(toll free). Digital versions of our titles are available at numerous electronic vendors. The sole obligation of Mercury Learning and Information to the purchaser is to replace the book and/ or disc, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product.

ANMF.CH00_FM_2PP.indd 4

2/24/2023 5:48:37 PM

Contents Prefacexiii CHAPTER 1: NUMERICAL COMPUTATIONS 1 1.1 Taylor’s Theorem 1 1.2 Number Representation 4 1.3 Error Considerations 11 1.3.1 Absolute and Relative Errors 12 1.3.2 Inherent Errors 14 1.3.3 Round-off Errors 14 1.3.4 Truncation Errors 19 1.3.5 Machine Epsilon 21 1.3.6 Error Propagation 22 1.4 Error Estimation 22 1.5 General Error Formula 24 1.5.1 Function Approximation 24 1.5.2 Stability and Condition 25 1.5.3 Uncertainty in Data or Noise 27 1.6 Sequences 28 1.6.1 Linear Convergence 28 1.6.2 Quadratic Convergence 29 1.6.3 Aitken’s Acceleration Formula 30 1.7 Summary 32 Exercises33

ANMF.CH00_FM_2PP.indd 5

2/24/2023 5:48:37 PM

vi • Contents CHAPTER 2: LINEAR SYSTEM OF EQUATIONS 39 2.1 Introduction 39 2.2 Methods of Solution 40 2.3 The Inverse of a Matrix 41 2.4 Matrix Inversion Method 44 2.4.1 Augmented Matrix 50 2.5 Gauss Elimination Method 50 2.5.1 MATLAB Program for the Gauss Elimination Method 52 2.6 Gauss-Jordan Method 58 2.6.1 MATLAB Program for the Gauss Jordan Method 59 2.7 Cholesky’s Triangularization Method 68 2.8 Crout’s Method 79 2.8.1 MATLAB Program for Crout’s Method 81 2.9 Thomas Algorithm for Tridiagonal System 88 2.9.1 MATLAB Program for the Thomas Method for Tridiagonal Systems 89 2.10 Jacobi’s Iteration Method 94 2.10.1 MATLAB Program for the Jacobi Iteration Method 95 2.11 Gauss-Seidel Iteration Method 104 2.11.1 MATLAB Program for the Gauss Seidel Method 106 2.12 Symmetric Matrix Eigenvalue Problems 113 2.12.1 The Jacobi Method 114 2.12.2  MATLAB Function for the Jacobi Method 120 2.12.3  Householder Reduction to Tridiagonal Form 133 2.12.4  Gerschgorin’s Circle Theorem 137 2.12.5  Sturm Sequence 139 2.12.6  QR Method 140 2.12.7  Power Method 157 2.12.8  Inverse Power Method 161 2.12 Summary 163 Exercises164

ANMF.CH00_FM_2PP.indd 6

2/24/2023 5:48:37 PM

Contents • vii

CHAPTER 3: SOLUTION OF ALGEBRAIC AND TRANSCENDENTAL EQUATIONS 177 3.1 Introduction 177 3.2 Bisection Method 178 3.2.1 Error Bounds 180 3.3 Method of False Position 186 3.3.1 MATLAB Program for the False Position Method 188 3.4 Newton-Raphson Method 194 3.4.1 Convergence of the Newton-Raphson Method 196 3.4.2 Rate of Convergence of the Newton-Raphson Method 197 3.4.3 MATLAB Program for the Newton Raphson Method 198 3.4.4 Modified Newton-Raphson Method 203 3.4.5 Rate of Convergence of Modified Newton-Raphson Method 204 3.5 Successive Approximation Method 206 3.5.1 Error Estimate in the Successive Approximation Method 207 3.6 Secant Method 211 3.6.1 Convergence of the Secant Method 212 3.6.2 MATLAB Program to Search for a Root of the Function f(x) in the Interval (a,b) 214 3.6.3 MATLAB Program for Secant Method 214 3.7 Muller’s Method 219 3.7.1 MATLAB Program for Muller’s Method 222 3.8 Chebyshev Method 225 2 3.9 Aitken’s Δ Method 226 3.10 Brent’s Method 229 3.10.1 MATLAB Program for Brent’s Method 231 3.11 Newton Method for a System of Nonlinear Equations 236 3.12 Comparison of Iterative Methods 238 3.13 MATLAB Built-in Function: fzero239 3.14 Summary 243 Exercises244

ANMF.CH00_FM_2PP.indd 7

2/24/2023 5:48:37 PM

viii • Contents CHAPTER 4: NUMERICAL DIFFERENTIATION 251 4.1 Introduction 251 4.2 Derivatives Based on Newton’s Forward Integration Formula 252 4.2.1 MATLAB Program for Derivatives Based on Newton’s Forward Integration Formula—Equally Spaced Points 253 4.3 Derivatives Based on Newton’s Backward Interpolation Formula 260 4.4 Derivatives Based on Stirling’s Interpolation Formula 263 4.5 Maxima and Minima of a Tabulated Function 268 4.6 Cubic Spline Method 270 4.7 Richardson Extrapolation 271 4.8 Differentiation of Unequally Spaced Data 273 4.9 MATLAB Built-in Functions: diff and gradient 274 4.10 Summary 278 Exercises278 CHAPTER 5: FINITE DIFFERENCES AND INTERPOLATION 285 5.1 Introduction 285 5.2 Finite Difference Operators 286 5.2.1 Forward Differences 287 5.2.2 Backward Differences 288 5.2.3 Central Differences 289 5.2.4 Error Propagation in a Difference Table 293 5.2.5 Properties of the Operator Δ296 5.2.6 Difference Operators 298 5.2.7 Relation Among the Operators 299 5.2.8 Representation of a Polynomial using Factorial Notation 304 5.3 Interpolation with Equal Intervals 310 5.3.1 Missing Values 310 5.3.2 Newton’s Binomial Expansion Formula 310 5.3.3 Newton’s Forward Interpolation Formula 313 5.3.4 MATLAB M-file: Newtonint 314

ANMF.CH00_FM_2PP.indd 8

2/24/2023 5:48:37 PM

Contents • ix

5.3.5 Newton’s Backward Interpolation Formula 322 5.3.6 Error in the Interpolation Formula 328 5.4 Interpolation with Unequal Intervals 331 5.4.1 Lagrange’s Interpolating Polynomial for Equal Intervals 331 5.4.2 function yint = Lagrangeint (x,y,xx) 333 5.4.3 Lagrange’s Formula for Unequal Intervals 334 5.4.4 Hermite’s Interpolation Formula  338 5.4.5 Inverse Interpolation 340 5.4.6 Lagrange’s Formula for Inverse Interpolation 340 5.5 Central Difference Interpolation Formulae 342 5.5.1 Gauss’s Forward Interpolation Formula 343 5.5.2 Gauss Backward Interpolation Formula 346 5.5.3 Bessel’s Formula 348 5.5.4 Stirling’s Formula 351 5.5.5 Laplace-Everett’s Formula 352 5.5.6 Selection of an Interpolation Formula 355 5.6 Divided Differences 355 5.6.1 Newton’s Divided Difference Interpolation Formula 357 5.7 Cubic Spline Interpolation 360 5.8 Generalized Spline Method 366 5.8.1 Splines 366 5.8.2 Linear Splines 367 5.8.3 Quadratic Splines 368 5.8.4 Cubic Splines 371 5.8.5 End Conditions 376 5.8.6 MATLAB Built-in Function: spline 376 5.8.7 Multidimensional Interpolation 378 5.8.8 MATLAB Built-in Function: interpl 381 5.9 Summary 390 Exercises391

ANMF.CH00_FM_2PP.indd 9

2/24/2023 5:48:37 PM

x • Contents CHAPTER 6: CURVE FITTING, REGRESSION, AND CORRELATION 405 Approximating Curves 406 Linear Regression 407 6.1 Linear Equation 407 6.2 Curve Fitting With a Linear Equation 408 6.3 Criteria for a Best Fit 410 6.4 Linear Least-Squares Regression 412 6.5 Linear Regression Analysis 414 6.5.1 MATLAB built-in function: polyfit 418 6.5.2 MATLAB built-in function: polyval 419 6.6 Interpretation of a and b 420 Assumptions in the Regression Model 421 6.7 Standard Deviation of Random Errors 421 6.8 Coefficient of Determination 422 6.9 Linear Correlation 425 Properties of the Linear Correlation Coefficient r 427 Explained and Unexplained Variation 428 6.10 Linearization of Nonlinear Relationships 429 6.11 Polynomial Regression 435 6.11.1  Polynomial Fit 436 6.11.2  MATLAB Built-in Functions for Polynomial Fit 438 6.12 Quantification of Error of Linear Regression 443 6.13 Multiple Linear Regression  445 6.14 Weighted Least-Squares Method 448 6.15 Orthogonal Polynomials and Least-Squares Approximation 449 6.16 Least-Squares Method for Continuous Data 449 6.17 Approximation Using Orthogonal Polynomials 451 6.18 Gram-Schmidt Orthogonalization Process 453 6.19 Fitting a Function Having a Specified Power 455 6.20 Fitting a Cubic Spring Model 455 6.21 Additional Example Problems and Solutions 456 6.22 Summary 468 Exercises468

ANMF.CH00_FM_2PP.indd 10

2/24/2023 5:48:37 PM

Contents • xi

CHAPTER 7: NUMERICAL INTEGRATION 483 7.1 Introduction 483 7.1.1 Relative Error 484 7.2 Newton-Cotes Closed Quadrature Formula 485 7.3 Trapezoidal Rule 486 7.3.1 Error Estimate in Trapezoidal Rule 489 7.3.2 MATLAB Functions: trapz and cumtrapz 490 7.4 Simpson’s 1/3 Rule 493 7.4.1 Error Estimate in Simpson’s 1/3 Rule 495 7.4.2 MATLAB Program for Simpson’s Integration: simpsonint 496 7.4.3 MATLAB Built-in Functions: quad and quad1 497 7.5 Simpson’s 3/8 Rule 502 7.6 Boole’s and Weddle’s Rules 506 7.6.1 Boole’s Rule 506 7.6.2 Weddle’s Rule 507 7.7 Romberg’s Integration 512 7.7.1 Richardson’s Extrapolation 512 7.7.2 Romberg Integration Formula 513 7.7.3 MATLAB Program for Romberg Integration: Romberg 516 7.8 Gaussian Quadrature  523 7.8.1 Gaussian Integration Formulas 523 7.8.2 Orthogonal Polynomials 525 7.8.3 Gauss-Lagendre Quadrature  527 7.8.4 Gauss-Chebyshev Quadrature Method 533 7.8.5 Gauss-Laguerre Quadrature 535 7.8.6 Gauss-Hermite Quadrature 537 7.8.7 MATLAB Programs for Gaussian Quadrature: gaussnodes and gaussquad 539 7.9 Double Integration 544 7.9.1 Trapezoidal Method 544 7.9.2 Simpson’s 1/3 Rule 548 7.9.3 MATLAB Built-in Function for Double Integration: dblquad 549 7.10 Summary 549 Exercises550

ANMF.CH00_FM_2PP.indd 11

2/24/2023 5:48:37 PM

xii • Contents CHAPTER 8: NUMERICAL SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS 8.1 Introduction 8.2 One-Step Methods or Single-Step Methods 8.2.1 Picard’s Method of Successive Approximation 8.2.2 The Taylor’s Series Method 8.3 Step-by-Step Methods or Marching Methods 8.3.1 Euler’s Method 8.3.2 MATLAB Program for Euler’s Method: euler 8.3.3 Modified Euler’s Method 8.3.4 MATLAB Program for the Modified Euler’s Method: modeuler 8.3.5 Runge-Kutta Methods 8.3.4 Predictor-Corrector Methods 8.4 MATLAB Functions for Ordinary Differential Equations: ode45, ode23, ode113, ode15s, ode23s, ode23t, ode23tb 8.5 System of First-order Ordinary Differential Equations 8.6 Initial Value Problems 8.6.1 The Taylor Series Method 8.6.2 Picard’s Method 8.6.3 Second-Order Runge-Kutta Method 8.6.4 Fourth-Order Runge-Kutta Method 8.6.5 Euler’s Formula 8.6.6 Modified Euler’s Formula 8.6.7 Burlirsch-Stoer Method (Mid-Point Method) 8.6.8 The Runge-Kutta-Fehlberg Method 8.6.9 The Runge-Kutta-Butcher Method 8.7 Two-Point Boundary Value Problems 8.7.1 Finite Difference Method 8.7.2 Second-Order Differential Equations 8.7.3 The Shooting Method 8.8 Second-Order Initial Value Problem (IVP)

ANMF.CH00_FM_2PP.indd 12

557 557 560 561 564 569 569 571 579 581 586 604 614 626 627 628 630 631 632 634 635 635 636 637 637 638 639 641 646

2/24/2023 5:48:37 PM

Contents • xiii

8.9 Second-Order Boundary Value Problem (BVP) 648 8.10 MATLAB Built-in Functions 649 8.11 Summary 650 Exercises650 CHAPTER 9: DIRECT NUMERICAL INTEGRATION METHODS 661 9.1 Introduction 661 9.2 Single Degree of Freedom System 661 9.2.1 Finite Difference Method 662 9.2.2 Central Difference Method 663 9.2.3 The Runge-Kutta Method 664 9.3 Multi-degree of Freedom Systems 666 9.4 Explicit Schemes 667 9.4.1 Central Difference Method 667 9.4.2 Two-Cycle Iteration with Trapezoidal Rule 670 9.4.3 Fourth-Order Runge-Kutta Method 671 9.5 Implicit Schemes 673 9.5.1 The Houbolt Method 673 9.5.2 Wilson Theta Method 676 9.5.3 The Newmark Beta Method  679 9.5.4 Park Stiffly Stable Method 682 9.6 Example Problems and Solutions Using MATLAB 684 9.7 Summary 747 Exercises748 Additional Exercises 753 CHAPTER 10: MATLAB BASICS 10.1 Introduction 10.1.1 Starting and Quitting MATLAB  10.1.2 Display Windows 10.1.3 Entering Commands 10.1.4 MATLAB Expo 10.1.5 Abort 10.1.6 The Semicolon (;)

ANMF.CH00_FM_2PP.indd 13

757 757 758 758 758 759 759 759

2/24/2023 5:48:37 PM

xiv • Contents 10.1.7  Typing % 10.1.8  The clc Command 10.1.9 Help 10.1.10  Statements and Variables 10.2 Arithmetic Operations 10.3 Display Formats 10.4 Elementary Math Built-In Functions 10.5 Variable Names 10.6 Predefined Variables 10.7 Commands For Managing Variables 10.8 General Commands 10.9 Arrays 10.9.1 Row Vector 10.9.2 Column Vector 10.9.3 Matrix 10.9.4 Addressing Arrays 10.9.5 Adding Elements to a Vector or a Matrix 10.9.6 Deleting Elements 10.9.7 Built-in Functions 10.10  Operations with Arrays 10.10.1  Addition and Subtraction of Matrices 10.10.2  Dot Product 10.10.3  Array Multiplication 10.10.4  Array Division 10.10.5  Identity Matrix 10.10.6  Inverse of a Matrix 10.10.7 Transpose 10.10.8 Determinant 10.10.9  Array Division 10.10.10  Left Division 10.10.11  Right Division 10.11  Element-By-Element Operations 10.11.1  Built-In Functions For Arrays

ANMF.CH00_FM_2PP.indd 14

759 759 759 759 760 760 761 764 764 764 765 766 766 767 767 767 768 768 768 770 770 770 770 771 771 771 771 771 771 772 772 772 773

2/24/2023 5:48:37 PM

Contents • xv

10.12  Random Numbers Generation 774   10.12.1  The Random Command 775 10.13 Polynomials 775 10.14  System of Linear Equations 778  10.14.1 Matrix Division 778  10.14.2  Matrix Inverse 778 10.15  Script Files 784 10.15.1  Creating and Saving a Script File 784 10.15.2  Running a Script File 785 10.15.3  Input to a Script File 785 10.15.4  Output Commands 786 10.16  Programming in Matlab 787   10.16.1  Relational and Logical Operators 787   10.16.2  Order of Precedence  788   10.16.3  Built-in Logical Functions 788  10.16.4 Conditional Statements 789   10.16.5  Nested if Statements 790   10.16.6  else AND elseif Clauses 790   10.16.7  MATLAB while Structures 790 10.17 Graphics 792   10.17.1  Basic 2-D Plots 792   10.17.2  Specialized 2-D Plots 793  10.17.3 3-D Plots 795   10.17.4  Saving and Printing Graphs 802 10.18  Input/Output In Matlab  802   10.18.1  The fopen Statement 802 10.19  Symbolic Mathematics 803  10.19.1 Symbolic Expressions 804   10.19.2  Solution to Differential Equations 806  10.19.3 Calculus 807 10.23  Summary 843 References844 Exercises845

ANMF.CH00_FM_2PP.indd 15

2/24/2023 5:48:37 PM

xvi • Contents CHAPTER 11: OPTIMIZATION 857 11.1 Introduction 857 11.2 Unconstrained Minimization of Functions 859 11.3 Minimization with Constraints Using Lagrange Multipliers  860 11.4 Numerical Optimization  864 11.4.1 Optimization Involving Single Variables 865 11.4.2 Local and Global Optima 873 11.4.3 Bracketing 875 11.4.4 Golden-Section Search 875 11.4.5 MATLAB Program for Bracketing Method 878 11.4.6 MATLAB Program for Golden-Section Search Method 879 11.5 Multidimensional Optimization 882 11.6 Gradient Methods 885 11.7 Newton’s Method 887 11.7.1 MATLAB Program for Newton’s Method 889 11.8 Methods Based on the Concept of Quadratic Convergence 893 11.8.1 Conjugate Directions for a Quadratic Function 893 11.9  Powell’s Method 901 11.9.1 MATLAB Program for Powell’s Optimization Method 901 11.10  Fletcher-Reeves Method 906   11.10.1  MATLAB Program for Fletcher-Reeves    Optimization Method 909 11.11  The Hooks and Jeeves Method 917 11.12  Method of Successive Linear Approximation 924 11.13  Interior Penalty Function Method 925 11.14  MATLAB Built-in Functions 931   11.14.1  MATLAB Function: fminbnd 931   11.14.2  MATLAB Function: fminsearch 935 11.15  Additional Example Problems and Solutions 940 11.16 Summary 949 References950 Exercises950

ANMF.CH00_FM_2PP.indd 16

2/24/2023 5:48:37 PM

Contents • xvii

CHAPTER 12: PARTIAL DIFFERENTIAL EQUATIONS 12.1 Introduction 12.2 Classification of Linear Second-Order Partial Differential Equation 12.3 Types of Problems 12.4 Finite-Difference Approximation to Partial Derivatives 12.5 Physical Phenomena 12.5.1 Laplace’s Equation 12.5.2 Heat Equation 12.5.3 Wave Equation 12.5.4 Equation Classification 12.6 Elliptic Equations 12.6.1 Central Difference Method 12.6.2 Boundary Conditions 12.6.3 Iterative Solution Methods 12.6.4 The Jacobi Method 12.6.5 Gauss-Seidel Method 12.6.6 Successive Over-Relaxation or S.O.R. Method 12.7 One-Dimensional Parabolic Equations 12.7.1 Explicit Forward Euler Method 12.7.2 Implicit Backward Euler Method 12.7.3 The Crank-Nicolson Implicit Method 12.7.4 function [t,x,U] =Heatone(T,a,m,n,beta,c,f,g) 12.7.5 function [x,y,U] = Heattwo(T,a,b,m,n,p,beta,f,g) 12.7.6 function [t,x,U] = Waveone(T,a,m,n,beta,f,g) 12.7.7 function [x,y,U] = Wavetwo (T,a,b,m,n,p,beta,f,g) 12.7.8 function [alpha,r,x,y,U] = Poisson (a,b,m,n,q,tol,f,g) 12.8 Two-Dimensional Parabolic Equations 12.9 One-Dimensional Hyperbolic Equations 12.9.1 D’Alembert’s Solution 12.9.2 Explicit Central Difference Method 12.10 Two-Dimensional Hyperbolic Equations 12.10.1  Explicit Central Difference Method

ANMF.CH00_FM_2PP.indd 17

959 960 961 965 967 968 968 969 970 971 972 972 975 977 985 986 986 988 989 992 993 996 999 1005 1008 1013 1030 1037 1038 1043 1050 1051

2/24/2023 5:48:37 PM

xviii • Contents 12.11  MATLAB Built-in Function: pdepe 1060 12.12 Summary 1079 Exercises1081 APPENDIX A: PARTIAL FRACTION EXPANSIONS 1093 Case-I1094 Partial Fraction Expansion when Q(s) has Distinct Roots 1094 Case-II1096 Partial Fraction Expansion when Q(s) has Complex Conjugate Roots 1096 Case-III1097 Partial Fraction Expansion when Q(s) has Repeated Roots 1097 Exercises1098 APPENDIX B: BASIC ENGINEERING MATHEMATICS B.1 Algebra B.1.1 Basic Laws B.1.2 Sums of Numbers B.1.3 Progressions B.1.4 Powers and Roots B.1.5 Binomial Theorem B.1.6 Absolute Values B.1.7 Logarithms B.2 Trigonometry B.2.1 Trigonometric Identities B.2.2 Cosine Law (Law of Cosines) B.2.3 Sine Law (Law of Sines) B.3 Differential Calculus B.3.1 List of Derivatives B.3.2 Expansion in Series B.4 Integral Calculus B.4.1 List of Most Common Integrals

ANMF.CH00_FM_2PP.indd 18

1101 1101 1101 1101 1102 1103 1103 1104 1104 1105 1105 1107 1108 1108 1108 1111 1114 1114

2/24/2023 5:48:37 PM

Contents • xix

APPENDIX C: CRAMER’S RULE 1119 Exercises1123 APPENDIX D: MATLAB BUILT-IN M-FILE FUNCTIONS

1125

APPENDIX E: MATLAB PROGRAMS

1129

APPENDIX F: ANSWERS TO ODD NUMBERED EXERCISES

1135

BIBLIOGRAPHY1175 INDEX1187

ANMF.CH00_FM_2PP.indd 19

2/24/2023 5:48:37 PM

ANMF.CH00_FM_2PP.indd 20

2/24/2023 5:48:37 PM

Preface The objective of numerical analysis is to solve complex numerical problems using only the simple operations of arithmetic, to develop and evaluate methods for computing numerical results from given data. The methods of computation are called algorithms. An algorithm is a finite sequence of rules for performing computations on a computer such that at each instant the rules determine exactly what the computer has to do next. Numerical methods tend to emphasize the implementation of the algorithms. Thus, applied numerical methods are methods for solving problems on computers by numerical calculations, often giving a table of numbers and /or graphical representations or figures. The purpose of this book is to impart a basic understanding, both physical and mathematical, of the basic theory of numerical analysis/methods and their applications using MATLAB. In this book, an attempt is made to present in a simple and systematic manner the techniques that can be applied to the study of applied numerical methods. Special emphasis is placed on analytical developments, algorithms and computational solutions. The objective of this text book is to introduce students from a variety of disciplines and backgrounds to the vast array of problems that are amenable to numerical solution. The emphasis is placed on application rather than pure theory, which, although kept to a minimum and presented in a mostly heuristic and intuitive manner. This is deemed sufficient for the student to fully understand the workings, efficiency and shortcomings or failings of each technique. The computer language of MATLAB is incorporated throughout the text. Hence, most of the problems are executed in MATLAB code. Since I intended this book as a first course on the numerical methods, the concepts have been presented in simple terms and the solution procedures have been explained in detail.

ANMF.CH00_FM_2PP.indd 21

2/24/2023 5:48:38 PM

xxii • Preface Audience This book is a comprehensive text on applied numerical methods. It is selfcontained and the subject matter is presented in an organized and systematic manner. No previous knowledge of numerical analysis and numerical methods is assumed. This book is quite appropriate for several groups of audience including: - senior undergraduate and graduate students in mathematics, science and engineering taking the introductory course on applied numerical methods. - the book can be adapted for a short professional course on applied numerical methods. - design and research engineers will be able to draw upon the book in selecting and developing applied numerical methods for analytical and design purposes. - practicing engineers and managers who want to learn about the basic principles and concepts involved in applied numerical methods and how they can be applied at their own work place concerns. Because the book is aimed at a wider audience, the level of mathematics is kept intentionally low. All the principles presented in the book are illustrated by numerous worked examples. The book draws a balance between theory and practice. Contents Books differ in content and organization. I have striven hard in the organization and presentation of the material in order to introduce the student gradually the concepts and in their use to solve problems in applied numerical methods. The subject of applied numerical methods deals with the methods and means of formulation of mathematical models of physical systems and discusses the methods of solution using MATLAB. In this book, I have concentrated on both of these aspects: the tools for formulating the mathematical equations and also the methods of solving them using MATLAB. The study of applied numerical methods is a formidable task. Each chapter in this book consists of a concise but thorough fundamental statement of the theory; principles and methods, followed by a selected number of illustrative worked examples. There are ample unsolved exercise problems and projects for student’s practice, to amplify and extend the theory, and methods are also

ANMF.CH00_FM_2PP.indd 22

2/24/2023 5:48:38 PM

Preface • xxiii

included. The bibliography provided at the end of the book serves as helpful source for further study and research by interested readers. In Chapter 1, Taylor’s theorem, a few basic ideas and concepts regarding numerical computations, number representation, including binary, decimal, and hexadecimal numbers, errors considerations, absolute and relative errors, inherent errors, round-off errors and truncation errors, machine epsilon, error propagation, error estimation, general error formulae including approximation of a function, stability and condition, uncertainty in data or noise, sequences: linear convergence, quadratic convergence, and Aitken’s acceleration formulae are described. Chapter 2 deals with the solution of linear system of equations. The topics covered are the methods of solution, the inverse of a matrix, matrix i­nversion method, augmented matrix, Gauss elimination method, Gauss Jordan method, Cholesky’s triangularization method, Crout’s method, Thomas algorithm for triangular system, Jacobi’s iteration method, and Gauss-Seidal i­teration method. Chapter 3 deals with the solution of algebraic and transcendental equations. Here, we cover the topics such as the bisection method, method of false position, Newtonian-Raphson method, successive approximation method, secant method, Muller’s method, Chebyshev method, Aitken’s method, and ­comparison of iterative methods. In Chapter 4, we cover the topics on numerical differentiation. The topics covered include the derivatives based on Newton’s forward interpolation formula, the derivatives based on Newton’s backward interpolation formula, the derivatives based on Stirling’s interpolation formula, maxima and minima of a tabulated function, and cubic spline method. Chapter 5 deals with finite differences and interpolation. It includes topics on finite differences, forward differences, backward differences, central differences, error propagation in a difference table, properties of operator delta, difference operators, relations among the operators, representation of a polynomial using factorial notation, interpolation with equal intervals, missing values, Newton’s binomial expansion formula, Newton’s forward interpolation formula, Newton’s backward interpolation formula, error in interpolation formula, interpolation with unequal intervals, Lagrange’s formula for unequal intervals, inverse interpolation, Lagrange’s formula for inverse ­interpolation, central difference interpolation formulae, Gauss’s forward interpolation ­formula, Gauss’s backward interpolation formula, Bessel’s formula, Stirling’s

ANMF.CH00_FM_2PP.indd 23

2/24/2023 5:48:38 PM

xxiv • Preface formula, Laplace-Everett’s formula, divided differences. Newton’s divided differences, interpolation formula, selection of an interpolation formula, and cubic spline interpolation. In Chapter 6, we present the curve fitting, regression, and correlation. we discuss here the topics on linear equation, curve fitting with a linear equation, criteria for a “best” fit, linear least-squares regression, linear regression analysis, interpretation of a and b, standard deviation of random errors, coefficient of determination, linearization of nonlinear relationship, polynomial regression, quantification of error of linear regression, multiple linear regression, weighted least squares method, orthogonal polynomials and least squares approximation, least squares method for continuous data, approximation using orthogonal polynomials, and Gram-Schmidt orthogonalization process. Chapter 7 presents numerical integration. Here, we cover the topics on Newton-Cotes closed quadrature formula, trapezoidal rule, error estimate in trapezoidal rule, Simpson’s 1/3 rule, error estimate in Simpson’s 1/3  rule, Simpson’s 3/8 rule, Boole’s and Weddle’s rules, Romberg’s integration, Richardson’s extrapolation, and Romberg’s integration formula. In Chapter 8, we discuss the numerical solution of ordinary differential equations. The methods covered include one-step methods or single-step methods, Picard’s method of successive approximations, Taylor’s series method, step-by-step methods or marching methods, Euler’s method, modified Euler’s method, Runge-Kutta methods, Runge-Kutta method of order two and four, predictor-corrector methods, Adam-Moulton predictor-corrector method, and Milne’s predictor-corrector method. In Chapter 9, we have briefly reviewed the direct numerical integration methods for the solution of a single or system of differential equations. Many numerical methods are available for the solutions of the response of dynamic systems. We have discussed several widely used step-by-step numerical integration methods for linear dynamic response analysis. A brief description of these integration methods is presented, and their application is illustrated. The integration schemes considered were three explicit and four implicit methods. They are the explicit schemes (the central difference method, twocycle interaction with trapezoidal rule and fourth order Runge-Kutta method) and the implicit schemes (Houbolt method, Wilson Theta method, Newmark Beta method and the Park Stiffly stable method). Application of these direct numerical integration methods is illustrated with a case study of a linear dynamic system.

ANMF.CH00_FM_2PP.indd 24

2/24/2023 5:48:38 PM

Preface • xxv

In Chapter 10, the MATLAB environment which is an interactive environment for numeric computation, data analysis, and graphics was presented. The following are described: starting and quitting MATLAB, display windows, entering commands, MATLAB Expo, abort, the semicolon, typing %, the clc command, help, statements and variables, arithmetic operations, display formats, elementary math built-in functions, variable names, predefined variables, commands for managing variables, general commands, arrays, row vector, column vector, matrix, addressing arrays, adding elements to a vector or a matrix, deleting elements, built-in functions, operations with arrays, addition and subtraction of matrices, dot product, array multiplication, array division, identity matrix, inverse of a matrix, transpose, determinant, array division, left division, right division, eigenvalues and eigenvectors, elementby-element operations, built-in functions for arrays, random number generation, the Randn command, polynomials, system of linear equations, matrix division, matrix inverse, script files, creating and saving a script file, running a script file, input to a script file, output commands, programming in MATLAB, relational and logical operator order of precedence, built-in logical functions, conditional statements, nested if statements, else AND else if clauses, MATLAB while structures, graphics, basic 2-D plots, specialized 2-D plots,3-D plots, saving and printing graphs, input/output in MATLAB, the open statement, and symbolic mathematics. Symbolic mathematics for determining analytical expressions for the derivative and integral of an expression was also presented. Chapter 11 deals with an introduction to optimization. Topics covered include the minimization of a line, bracketing, golden section search, conjugate gradient methods, conjugate directions, Powell’s method, Fletcher-Reeves method, and MATLAB built-in functions. Chapter 12 an introduction to the solution of partial differential equations. Topics covered include classification of linear second order partial differential equations, types of problems, finite difference approximation to partial derivatives, physical phenomena, Laplace’s equation, heat equation, wave equation, equation classification, elliptic equations, central difference method, boundary conditions, iterative solution methods, Jacobi’s method, Gauss-Seidel’s method, Successive Over-Relaxation or S.O.R. method ,one-dimensional parabolic equations, explicit forward Euler’s method, implicit backward Euler’s method, Crank-Nicholson implicit method, MATLAB programs: heatone, heattwo, wavetone, wavetwo, poisson, two-dimensional parabolic equations, one-dimensional hyperbolic equations, D Alembert’s solution, explicit central

ANMF.CH00_FM_2PP.indd 25

2/24/2023 5:48:38 PM

xxvi • Preface difference method, two-dimensional hyperbolic equations, explicit central difference method, and MATLAB built-in function: pdepe. I sincerely hope that the final outcome of this book will help the students in developing an appreciation for the topic of numerical methods. An important requirement for effective use and application of MATLAB is ease and proficiency in partial fraction expansion, engineering mathematics, and Cramer’s rule. A basic review of partial fraction expansions, basic engineering mathematics, and Cramer’s rule are outlined in Appendices A, B, and C, respectively. MATLAB Built-In Functions and MATLAB M-File Functions are summarized in Appendices D and E, respectively. A bibliography provided at the end of the book serves as helpful sources for further study and research by interested readers. Answers to the odd, end-of-chapter exercises are given in the book. All of the end-of chapter exercises are available to instructors upon adoption for class use. Most of the solutions to the problems are given in terms of MATLAB script files and graphics. Rao V. Dukkipati

ANMF.CH00_FM_2PP.indd 26

2/24/2023 5:48:38 PM

Acknowledgments I am grateful to all those who have had a direct impact on this work. I am greatly indebted to my colleagues and to numerous authors who have made valuable contributions to the literature on the subject of numerical methods and more so to the authors of the articles listed in the ­bibliography of this book. My ­sincere thanks to Kosovka Cupic for solving many of the ­problems and solutions by long hand and to Mr. P.R. Naidu of Andhra University, Andhra Pradesh, India, for the excellent preparation of the complete ­manuscript. Finally, I express my heartfelt thanks to my family members: Sudha, Ravi, Madhavi, Anand, Ashwin, Raghav, and Vishwa, not only for their ­encouragement and support but also for sharing all the pain, frustration, and fun of producing a textbook manuscript.

ANMF.CH00_FM_2PP.indd 27

2/24/2023 5:48:38 PM

ANMF.CH00_FM_2PP.indd 28

2/24/2023 5:48:38 PM

CHAPTER

1

Numerical Computations Numerical methods are methods for solving problems on computers by numerical calculations, often giving a table of numbers and/or graphical representations or figures. Numerical methods tend to emphasize the implementation of algorithms. The aim of numerical methods is therefore to provide systematic methods for solving problems in a numerical form. The process of solving problems generally involves starting from an initial data, using highprecision digital computers, following the steps in the algorithms, and finally obtaining the results. Often the numerical data and methods used are approximate. Hence the error in a computed result may be caused by the errors in the data, or the errors in the method, or both. In this chapter, we will describe Taylor’s theorem, a few basic ideas and concepts regarding numerical computations, number representation— including binary, decimal, and hexadecimal numbers; errors considerations, absolute and relative errors, inherent errors, round-off errors and truncation errors, error estimation, general error formulae including approximation of a function, stability and condition, uncertainty in data, linear convergence, quadratic convergence, and Aitken’s acceleration formulae.

1.1 TAYLOR’S THEOREM Taylor’s theorem allows us to represent exactly and fairly the general functions in terms of polynomials with a known, specified, and boundable error. Taylor’s theorem is stated as follows: Let f(x) have n + 1 continuous derivatives on [a, b] for some n ≥ 0, and let x, x0 ∈[a, b]. Then   f(x) = pn(x) + Rn(x) 

ANMF.CH01_4PP.indd 1

(1.1)

3/1/2023 12:35:06 PM

2 • Applied Numerical Methods Using MATLAB

for pn ( x)   ( x  x0 ) f ( k) ( x0 ) (1.2) k! k0 k

n

and

Rn ( x) 

x

1 ( x  t)n f n1 (t) dt (1.3) n! x0

Also, there exists a point ξx, between x and x0 such that

Rn ( x) 

( x  x0 )n1 ( n1) f ( x ) (1.4) (n  1)!

where Rn(x) is the remainder. Taylor’s series is an associated formula of Taylor’s theorem. Taylor’s series gives us a means to predict a function value at one point in terms of the function value and its derivatives at another point. Taylor’s series expansion is defined by

f ( xi 1 )  f ( xi )  f ( xi )( xi 1  xi ) 

f ( xi ) ( xi 1  xi )2 2!

f (3) ( xi ) f n ( xi )    ( xi 1  xi )3    ( xi 1  xi )n  Rn (1.5) 3! n! We note that Equation (1.5) represents an infinite series. The remainder term Rn is included to account for all terms from (n + 1) to infinity: f ( n1) ( ) ( xi 1  xi )n1 (1.6) (n  1)! where the subscript n connotes that this is the remainder for the nth order approximation and ξ is a value of x that lies somewhere between xi and xi+1. Rn 

We can rewrite the Taylor’s series in Equation (1.6) by defining a step size h = xi+1 − xi as

f ( xi 1 )  f ( xi )  f ( xi )h 

f ( xi ) 2 f (3) ( xi ) 3 h  h  2! 3!

f ( n) ( x i ) n h + Rn (1.7) n! where the remainder term Rn is given by +

f ( n1) ( ) n1   Rn  h (1.8) (n  1)!

ANMF.CH01_4PP.indd 2

3/1/2023 12:35:33 PM

Numerical Computations • 3

The estimation of function at a point b which is fairly close to a is desired, then the Taylor’s series is written as an infinite series: (b − a)2 f (b) = f (a) + (b − a) f ′(a) + f ″(a) +... 2! (b − a)n (n) + f (a) + ... (1.9) n! If b is very close to a, then only a few terms can give good estimation. The Taylor’s series expansion for ex, sin x, and cos x are given below:

ex  1  x 





1 k x  Rn ( x) (1.10) k0 k! n



sin x  x 



(1)n 2 n1 (1)n1 2 n 3 1 3 1 5 x  x  ...  x  x cos  x (2 n  1)! (2 n  3)! 3! 5! (1)k 2 k 1 x  Rn ( x) (1.11) k  0 (2 k  1)! n



cos x  1 



1 2 1 3 1 1 x  x  ...  x n  x n1 e x 2! 3! n! (n  1)!

1 2 1 4 (1)n 2 n (1)n1 2 n 2 x  x  ...  x  x cos  x 2! 4! (2 n)! (2 n  2)! (1)k 2 k x  Rn ( x) (1.12) k  0 (2 k)! n



The error in Taylor’s series when the series is terminated after the term containing (x − a) n will not exceed ( n 1 )

 x  a n1

f (1.13) max (n  1)! where max corresponds to the maximum magnitude of the derivative in the interval a to x. When the Taylor’s series is truncated after n terms, then f(x) will be accurate to O(x − a) n. EXAMPLE 1.1 Use the Taylor series expansion to approximate f(x) = cos x at xi+1 = π/3 with n = 0 to 6 on the basis of the value of f(x) and its derivatives at xi = π/4 which   implies that h =  = π/12. 3 4

ANMF.CH01_4PP.indd 3

3/1/2023 12:36:21 PM

4 • Applied Numerical Methods Using MATLAB Solution: The zero approximation is given by

( i + 1 ) ∧ f ( x i ) + f ′ ( x i )( x i + 1 − x i )



f x



f (p / 3 ) ∧ cos

p π = 0.70710678 ; f   = 0.5 4 3

The % relative error is 0.5 − 0.70710678 ∈ = (100 ) =−41.4% t 0.5 f ′(x) = −sin x

       f    cos    sin      0.52198666 3 4  4   12  ∈t = −4.4% f ″(x) = −cos x 2         cos   / 4     f    cos    sin         0.49775449 2 3 4  4   12   12 

with ∈t = 0.449%. Table 1.1 shows the Taylor series approximation for n = 0 to 6. TABLE 1.1 Order n

f(n)(x)

F(π/3)

εt

0 1 2 3 4 5 6

cos x −sin x −cos x sin x cos x −sin x −cos x

0.70710678 0.52198666 0.49775449 0.49986915 0.50000755 0.50000030 0.49999999

−41.4 −4.4 0.449 2.62 × 10−2 −1.51 × 10−3 −6.08 × 10−5 2.40 × 10−6

1.2 NUMBER REPRESENTATION A base-b number is made up of individual digits. In the positional numbering system, the position of a digit in the number determines that digit’s contribution to the total value of the number.

ANMF.CH01_4PP.indd 4

3/1/2023 12:36:26 PM

Numerical Computations • 5

For decimal numbers, the base (radix) is 10. Hence (an an−1 … a2 a1 a0)b = an bn + an−1 bn−1 + … + a2 b2 + a1b + a0. an contributes to the number’s magnitude and is called the most significant digit (MSD). Similarly, the right most digit, a0, contributes the least and is known as the least significant digit (LSD). Conversion of base-b fractions to base-10 is done by (0.a1 a2 … am)b = a1b−1 + a2b−2 + … + amb−m. This is known as the expansion method. There are two binary digits (bits) in the binary number system: zero and one. The left-most bit is called the most significant bit (MSB) and the right most bit is the least significant bit (LSB). The rules of bit additions are: 0 + 0 = 0; 0 + 1 = 1; 1 + 0 = 1; 1 + 1 = 0 carry 1. The first ten digits 1, 2, 3, . . . 10 in base 10 and their representation in base-2 are shown in Figure 1.1. Base 10  1  2  3  4  5  6  7  8  9 10

Base 2 23

22

21

20

0 0 0 0 0 0 0 1 1 1

0 0 0 1 1 1 1 0 0 0

0 1 1 0 0 1 1 0 0 1

1 0 1 0 1 0 1 0 1 0

FIGURE 1.1  Representation of numbers in decimal and binary forms.

Most computer languages use floating-point arithmetic. Every number is represented using a (fixed, finite) number of binary digits referred to as bits. In this method, the computer representation a number in the following form:

ANMF.CH01_4PP.indd 5



Number = σ mbt−p(1.14)

where

σ  = sign of the number (±), denoted by a single bit.



m = mantissa or a fraction (a value which lies between 0.1 and 1).



b   = the base of the internal number system (b = 2 for binary, b = 10 for decimal or b = 16 for hexadecimal computers).



t  = shifted exponent (the value that is actually stored).

3/1/2023 12:36:26 PM

6 • Applied Numerical Methods Using MATLAB

p = shift required to recover the actual exponent. Shifting in the exponent is normally done to avoid the need for a sign bit in the exponent itself.

The number is then stored by storing only the values of s, m, and t. The normal way to represent and store numbers is to use a binary or base 2 number system which contains the following two digits.

binary digits = {0 1}

(1.15)

For positive integers the binary form is

dn2n + dn−12n−1 + ... + d121 + d020 

(1.16)

while for positive numbers less than one it is

d−12−1 + d−22−2 + d−32−3 + ...

(1.17)

with all binary digits di either 0 or 1. Such representations are unique. Conversion between base 10 and base 2 is performed automatically by programming languages. Thus, conversion of an n-bit binary integer b = bn−1 ... b0 to its decimal equivalent x is done as a sum of n powers of 2:

n 1

x   bk 2 k (1.18) k0

A positive decimal integer x, in the range 0 to 2n − 1 is converted to its n-bit binary equivalent b = bn−1 ... b0 by conducting a sequence of n divisions by decreasing powers of 2. In other words, the digits of the binary numbers are computed starting with the most significant bit, bn−1, and ending with the least significant, b0. Noting that the hexadecimal numbers have a larger base or radix than decimal numbers, the first six letters of the alphabet are used to augment the decimal digits as follows: Hexadecimal digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}

(1.19)

The conversion between binary, decimal, and hexadecimal numbers can be accomplished using Table 1.2.

ANMF.CH01_4PP.indd 6

3/1/2023 12:36:30 PM

Numerical Computations • 7

TABLE 1.2  Binary, decimal and hexadecimal numbers. Binary

Decimal

Hexadecimal

Binary

Decimal

Hexadecimal

0000 0001 0010 0011 0100

00 01 02 03 04

0 1 2 3 4

1000 1001 1010 1011 1100

08 09 10 11 12

8 9 A B C

0101 0110 0111

05 06 07

5 6 7

1101 1110 1111

13 14 15

D E F

EXAMPLE 1.2 Determine the decimal values of the following numbers: a) x = (10010110)2 b) x = (777)8 Solution: 7

a) x   bk 2 k using Equation (1.18) k0



= 21 + 22 + 24 + 27



= 2 + 4 + 16 + 128 = 150

b) x = (777)8 x =

2

b 8 k0

k

k



= 7(8)0 + 7(8)1 + 7(8)2



= 7 + 56 + 448 = 511

EXAMPLE 1.3 Convert (1011)2 to base-10. Solution: (1) (2)3 + (0) (2)2 + (1) (2)1 + 1 = 11

ANMF.CH01_4PP.indd 7

3/1/2023 12:36:41 PM

8 • Applied Numerical Methods Using MATLAB The remainder method is used to convert base-10 numbers to base-b numbers. Converting a base-10 fraction to base-b requires multiplication of the base-10 fraction and subsequent fractional parts by the base. The base-b ­fraction is formed from the integer parts of the products taken into same order in which they were determined. The octal (base-8) numbering system is one of the alternatives to working with long binary numbers. Only the digits 0 to 7 are employed. For instance,

7 + 1 = 6 + 2 = 5 + 3 = (10)8



7 + 2 = 6 + 3 = 5 + 4 = (11)8



7 + 3 = 6 + 4 = 5 + 5 = (12)8

EXAMPLE 1.4 Perform the following operations: a) (7)8 + (6)8 b) Convert (0.14)10 to base-8 c) Covert (27.52)8 to base-10. Solution: a) The sum of 7 and 6 in base-10 is 13. This is greater than 8. Using the remainder method, we have 13/8 = 1

remainder 5

1/8 = 0

remainder 1



The answer is (15)8.

b) 0.14 × 8 = 1.12

0.12 × 8 = 0.96



0.96 × 8 = 7.68



0.68 × 8 = 5.44



0.44 × 8 = etc.

The answer is (0.1075….)8 which is obtained from the integer parts of the products above.

ANMF.CH01_4PP.indd 8

3/1/2023 12:36:41 PM

Numerical Computations • 9

c) (2) (8)1 + (7) (8)0 + (5) (8)−1 + (2) (8)−2 5 2 = 16 + 7 + + = (23.656)10 8 64 The hexadecimal (base-16) system is a shorthand way of representing the value of four binary digits at a time. EXAMPLE 1.5 a) Convert (1475)10 to base-16. b) Convert (0.8)10 to base-16. Solution: a) Using the remainder method 1475 = 92 remainder 3 16 92 = 5 remainder 12 16 5 = 0 remainder 5 16 Now, (12)10 is (C)16 or (hex C). Hence, the answer is (5C3)16. b) 0.8 × 16 = 12.8 0.8 × 16 = 12.8 0.8 × 16 = etc. Since (12)10 = (C)16, we have the answer as (0.CCCCC…)16. EXAMPLE 1.6 a) Convert (5431)8 to base-2. b) Convert (1011111101111001)2 to base-16. Solution: a) First convert each octal digit to binary digits. (5)8 = (101)2 (4)8 = (100)2

ANMF.CH01_4PP.indd 9

3/1/2023 12:36:53 PM

10 • Applied Numerical Methods Using MATLAB (3)8 = (011)2 (1)8 = (001)2 Hence, the answer is (101100011001)2. b) Grouping the bits into fours starting at right-hand-bit, we have 1011 1111 0111 and 1001. Converting these groups into their hexadecimal equivalents, we get (1011)2 = (B)16 (1111)2 = (F)16 (0111)2 = (7)16 (1001)2 = (9)16 Therefore, the answer is (BF79)16. EXAMPLE 1.7 a) Convert the following base-2 numbers to base-10: 1011001 and 110.00101 b) Convert the following base-8 numbers to base 10: 71563 and 3.14. Solution: a) (1011001)2 = (1 × 26) + (0 × 25) + (1 × 24) + (1 × 23) + (0 × 22) + (0 × 21) + (1 × 20)

= 1(64) + 0(32) + 1(16) + 1(8) + 0(4) + 0(2) + 1(1) = 89

(110.00101)2 = (1 × 22) + (1 × 21) + (0 × 20) + (0 × 2−1) + (0 × 2−2) + (1 × 2−3) + (0 × 2−4) + (1 × 2−5)

= 1(4) + 1(2) + 0(1) + 0(0.5) + 0(0.25) + 1(0.125) + 0(0.0625) + .03125) = 6.15625

b) (71563)8 = (7 × 84) + (1 × 83) + (5 × 82) + (6 × 81) + (3 × 20) = 7(4096) + 1(512) + 5(64) + 6(8) + 3(1) = 29,555 (3.14)8 = (3 × 80) + (3 × 8−1) + (4 × 8−2) = 3(1) + 1(0.125) + 4(0.015625) = 3.1875

ANMF.CH01_4PP.indd 10

3/1/2023 12:36:53 PM

Numerical Computations • 11

1.3 ERROR CONSIDERATIONS Sources of Errors: When a computational procedure is involved in solving a scientific-mathematical problem, errors often will be involved in the process. A rough classification of the kinds of original errors that might occur is as follows: Modeling Errors: Mathematical modeling is a process when mathematical equations are used to represent a physical system. This modeling introduces errors called modeling errors. Blunders and Mistakes: Blunders occur at any stage of the mathematical modeling process and consist of all other components of error. Blunders can be avoided by sound knowledge of fundamental principles and with taking proper care in approach and design to a solution. Mistakes are due to the programming errors. Machine Representation and Arithmetic Errors: These errors are inevitable when using floating-point arithmetic while using computers or calculators. Examples are rounding and chopping errors. Mathematical Approximation Errors: This error is also known as a truncation error or discretization error. These errors arise when an approximate formulation is made to a problem that otherwise cannot be solved exactly. Accuracy and Precision: Accuracy refers to how closely a computed or measured value agrees with the true value. Precision refers to how closely individual computed or measured values agree with each other. Inaccuracy (also known as bias) is the systematic deviation from the truth. Imprecision (uncertainty) refers to the magnitude of a scatter. These concepts are illustrated graphically using an analogy from target practice as shown in Figure 1.2. Figure 1.2 illustrates the concepts of accuracy and precision from marksmanship using the following examples: (a) inaccurate and imprecise, (b) accurate and imprecise, (c) inaccurate and precise, and (d) accurate and precise. Errors are introduced by the computational process itself. Computers perform mathematical operations with only a finite number of digits. If the number xa is an approximation to the exact result xe, then the difference xe − xa is called error. Hence

ANMF.CH01_4PP.indd 11

Exact value = approximate value + error

3/1/2023 12:36:53 PM

12 • Applied Numerical Methods Using MATLAB In numerical computations, we come across the following types of errors: a) Absolute and relative errors b) Inherent errors c) Round-off errors d) Truncation errors

FIGURE 1.2  Concepts of accuracy and precisions.

1.3.1 Absolute and Relative Errors If XE is the exact or true value of a quantity and XA is its approximate value, then |XE − XA| is called the absolute error Ea. Therefore, absolute error

ANMF.CH01_4PP.indd 12

Ea = |XE − XA| 

(1.20)

3/1/2023 12:36:54 PM

Numerical Computations • 13

and relative error is defined by

Er 

XE  X A (1.21) XE

provided XE ≠ 0 or XE is not too close to zero. The percentage relative error is

Ep  100 Er  100

XE  X A (1.22) XE

Significant digits The concept of a significant figure, or digit, has been developed to formally define the reliability of a numerical value. The significant digits of a number are those that can be used with confidence. If XE is the exact or true value and XA is an approximation to XE, then XA is said to approximate XE to t significant digits if t is the largest nonnegative integer for which

XE  X A  5  10  t (1.23) | XE |

EXAMPLE 1.8 If XE = e (base of the natural algorithm = 2.7182818) is approximated by XA = 2.71828, what is the significant number of digits to which XA approximates XE? Solution:

X E  X A e  2.71828  which is < 5 × 10−6 | XE | e

Hence XA approximates XE to 6 significant digits. EXAMPLE 1.9 Let the exact or true value = 20/3 and the approximate value = 6.666. The absolute error is 0.000666... = 2/3000.

ANMF.CH01_4PP.indd 13

3/1/2023 12:37:13 PM

14 • Applied Numerical Methods Using MATLAB The relative error is (2/3000)/ (20/3) = 1/10000. The number of significant digits is 4. 1.3.2 Inherent Errors Inherent errors are the errors that preexist in the problem statement itself before its solution is obtained. Inherent errors exist because of the data being approximate or due to the limitations of the calculations using digital computers. Inherent errors cannot be completely eliminated but can be minimized if we select better data or employ high-precision computer computations. 1.3.3 Round-off Errors Round-off error happens when inaccuracies arise due to a finite number of digits of precision used to represent numbers. All computers represent numbers, except for integers and some fractions, with imprecision. Digital computers use floating-point numbers of fixed word length. This type of representation will not express the exact or true values correctly. Error introduced by the omission of significant figures due to computer imperfection is called the round-off error. Round-off errors are avoidable in most of the computations. When n digits are used to represent a real number, one method to avoid the error is to keep the first n digits and chop off all remaining digits. Another method is to round to the nth digit by examining the values of the remaining digits. The two steps involved in rounding to n digits are as follows: b 1. Add sgn(x) to digit n + 1 of x. 2 2. Chop x to n digits. where x is the nonzero real number, b is the base and sgn(x) = x/|x| denotes the sign of x with sgn (0) ≈ 0. Thus, the effect of the add and chop method of rounding is to round digit n up (away from zero) if the first digit to be chopped, digit n + 1, is greater than equal to b/2, otherwise digit n is left as is. Errors which result from this process of chopping or rounding method are known as round-off errors. Rounding to k decimal places To round x, a positive decimal number, to k decimal places, we chop x + 0.5 × 10−5 after kth decimal digit. Similarly, to a round a negative number, we round

ANMF.CH01_4PP.indd 14

3/1/2023 12:37:14 PM

Numerical Computations • 15

its absolute value and then restore the sign. Table 1.3 illustrates rounding the numbers 234.0065792 and −234.00654983 to k decimal digits. TABLE 1.3  Rounding numbers to k decimal digits. k

234.0065792

−234.00654983

0 1 2 3 4 5 6 7 8

234 234.0 234.01 234.007 234.0065 234.00658 234.006579 234.0065792 234.0065792

−234 −234.0 −234.01 −234.007 −234.0065 −234.00655 −234.006550 −234.0065498 −234.00654983

Accurate to k decimal places When we state that Y approximates y to k decimal places provided |y − Y| < −k th 1 2 × 10 and if both y and Y are rounded to k decimal places, then the k decimals in the rounded versions differ by no more than one unit. Consider for instance, the two numbers y = 57.34 and Y = 57.387 differ by |y − Y| = 0.047 < 0.5 × 10−1 = 0.05 hence Y approximates y to 1 decimal place. Rounding y to Y to the k = 1 decimal place, we find yr = 57.3 and Yr = 57.4, respectively. Therefore, yr and Yr differ in the first decimal place by no more than one unit. Also, when Y approximate y to k decimal places, then these two numbers are said to agree to k decimal places. It should be noted here that these two numbers are not necessarily the same when rounded to k decimal places. Significant figures The most significant figure in a decimal number is the left-most nonzero digit and the least significant figure is the right-most digit. Significant figures are all the digits that lie in between the most significant and least significant figures. However, it should be noted here that zeros on the left of the first significant figure are not treated as significant digits. For instance, in the number Y = 0.0078560, the first significant digit is 7 and the right-most zero is the fifth ­significant digit. Table 1.4 shows the results of rounding Y to k significant figure.

ANMF.CH01_4PP.indd 15

3/1/2023 12:37:15 PM

16 • Applied Numerical Methods Using MATLAB

TABLE 1.4  Significant figures. Y = 0.0078560

k significant digits 1 2 3 4 5

0.008 0.0078 0.00786 0.007856 0.0078560

Accurate to k significant figures If

1 x  X   10  k | x | 2

1 1 k k or x   10 | x |  X  x   10 | x | 2 2 then we say that the floating-point number X approximates x to k significant figures. Table 1.5 shows k, the intervals [x − d (k), x + d (k)], where d(k) = 12 × 10−k |x|, interval rounded, x rounded for x = π = 3.141592654. The last column in Table 1.4 shows the value of π rounded to k significant digits. TABLE 1.5  Approximation of π to k significant figures. k

Interval [x − d(k), x + d(k)]

Interval rounded

x rounded

1 2 3 4 5 6 7 8

[2.984513021, 3.298672287] [3.125884691, 3.157300617] [3.140021858, 3.143163450] [3.141435574, 3.141749734] [3.141576946, 3.141608362] [3.141591083, 3.141594225] [3.141592497, 3.141592811] [3.141592638, 3.141592670]

[3.0, 3.3] [3.1, 3.2] [3.14, 3.14] [3.141, 3.142] [3.1416, 3.1416] [3.14159, 3.14459] [3.141592, 3.141593] [3.1415926, 3.1415927]

3.0 3.1 3.14 3.142 3.1416 3.14159 3.141593 3.1415927

EXAMPLE 1.10 Given the number π is approximated using n = 5 decimal digits. a) Determine the relative error due to chopping and express it as a percent. b) Determine the relative error due to rounding and express it as a percent.

ANMF.CH01_4PP.indd 16

3/1/2023 12:37:18 PM

Numerical Computations • 17

Solution: a) The relative error due to chopping is given by 3.1415   Er (chopping) = = 2.949 × 10−5 or 0.002949%  b) The relative error due to rounding is given by 3.1416   Er (rounding) = = 2.338 × 10−6 or 0.0002338%.  EXAMPLE 1.11 If the number π = 4 tan−1(1) is approximated using 5 decimal digits, find the percentage relative error due to a) chopping b) rounding. Solution: a) Percentage relative error due to chopping  3.1415    5    100   2.949  10  100 or −0.002949%.    b) Percentage relative error due to rounding  3.1416    6    100   2.338  10  100 = 0.00023389%.    EXAMPLE 1.12 Use Taylor series expansions (zero through fourth order) to predict f(2) for f(x) = ln(x) with a base point at x = 1. Determine the true percentage relative error for each approximation. Solution: The true value of ln (2) = 0.693147… Zero order: f (2)  f (1)  0 Et 

ANMF.CH01_4PP.indd 17

0.693147  0 100%  100% 0.693147

3/1/2023 12:37:32 PM

18 • Applied Numerical Methods Using MATLAB First order:

1 f (x)  f  ′(1) = 1 x f (2)  0  1(1)  1



Et 



0.693147  1 100%  44.27% 0.693147

Second order: 1 ″(1) = −1 x2



f (x)  



f (2)  1  1



12  0.5 2 0.693147  0.5 Et  100%  27.87% 0.693147

Third order:

2 x3



f ( 3 ) (x ) =



f (2)  0.5  2



f″(1) = 2

13  0.833333 6 0.693147  0.833333 Et  100%  20.22% 0.693147

Fourth order:

f ( 4 ) (x )  

6 x4

f(4)(1) = −6



f (2)  0.833333  6

14  0.583333 24



Et 

0.693147  0.583333 100%  15.84% 0.693147

The above results show that the series is converging at a slow rate. A smaller step size would be required to obtain more rapid convergence. EXAMPLE 1.13 Given two numbers a = 8.8909 × 103 and b = 8.887 × 103. Calculate the difference between the two numbers (a − b) using the decimal floating point approximation (scientific notation) with three significant digits in the mantissa by (a) chopping and (b) rounding.

ANMF.CH01_4PP.indd 18

3/1/2023 12:38:12 PM

Numerical Computations • 19

Solution: a) In chopping, when three significant digits are in the mantissa, then

a = 8.890 × 103 and b = 8.887 × 103

and

a − b = 8.890 × 103 − 8.887 × 103 = 0.003 × 103 = 3.

b) In rounding, we have

a = 8.890 × 103 and b = 8.887 × 103

and

a − b = 8.890 × 103 − 8.887 × 103 = 0.004 × 103 = 4.

The exact (true) difference between the numbers is 3.8, which shows that the rounding gives a value much closer to the real answer. 1.3.4 Truncation Errors Truncation errors are defined as those errors that result from using an approximation in place of an exact mathematical procedure. Truncation error results from terminating after a finite number of terms known as formula truncation error or simply truncation error. The function f(x) is infinitely differentiable in an interval which includes the point x = a. Then the Taylor series expansion of f(x) about x = a is given by



f ( x)   k0

f ( k) (a) ( x  a)k (1.23) k!

where f (k) (a) denotes the kth derivative of f(x) evaluated at x = a k  d f ( x) ] (1.24) f (k) (a)  x0 dx k If the series is truncated after n terms, then it is equivalent to approximating f(x) with a polynomial of degree n − 1.

or



 fn(x) 

n 1

 k0

f ( k) (a)( x  a)k  k!

(1.25)

The error in approximating En(x) is equal to the sum of the neglected higherorder terms and is often called the tail of the series. The tail is given by

ANMF.CH01_4PP.indd 19

En ( x) f ( x)  fn ( x) 

f ( x) ( )( x  a)n n!

(1.26)

3/1/2023 12:38:32 PM

20 • Applied Numerical Methods Using MATLAB Sometimes it is possible to place an upper bound on x of En(x) depending on the nature of function f(x). If the maximum value of | fn (x)| over the interval [a, x] is known or can be estimated, then

Mn ( x) max  f ( n) ( )  a   x

(1.27)

From Equations (1.26) and (1.27), the worst bound on the size of the truncation error can be written as Mn ( x) | x  a |n (1.28) n! If h = x − a, then the truncation error En(x) is said to be of order O (hn). In other words, as h → 0, En(x) → 0 at the same rate as hn.

| En ( x) |

Hence O (hn) ≈ chn |h| 1 (relative error is amplified) Condition number < 1 (relative error is attenuated)

(1.41)

Condition number > very large number (the function is ill-conditioned) EXAMPLE 1.16 Compute and interpret the condition number for a) f(x) = sin x for a = 0.51π b) f(x) = tan x for a = 1.7 Solution: a) The condition number is given by

Condition number =

a f ′(a) f (a)

for a = 0.51π, f ′(a) = cos(0.51π) = −0.03141, f(a) = sin(0.51π) = 0.99951 a f ′(a) (0.51)(0.03141) Condition number = = = −0.05035 f (a) (0.99951) Since the condition number is < 1, from Equation (1.41), we conclude that the relative error is attenuated. b) f(x) = tan x, f ′(a) = −7.6966 for a = 1.7 f ′(x) = 1/cos2x,  f ′(a) = 1/cos2(1.7) = 60.2377 a f ′(a) 1.7(60.2377) Condition number = = = −13.305 f (a) −7.6966

ANMF.CH01_4PP.indd 26

Thus, the function is ill-conditioned.

3/1/2023 12:40:07 PM

Numerical Computations • 27

1.5.3 Uncertainty in Data or Noise Uncertainty or error in the physical data based on which the computation model is based can introduce errors in the analysis. This type of error is known as noise. The data can affect the accuracy of the numerical computations performed. The errors can exhibit both inaccuracy and imprecision. If the input data has d significant digits of accuracy, then the results obtained from the numerical computation should be reported in d significant digits of accuracy. For instance, if a = 5.358 and b = 0.06437 both have four significant digits of accuracy, then although a − b = 5.29363, we should report the correct answer as a − b = 5.293. TABLE 1.7  Exact arithmetic rounded to four significant digits. No.

Exact arithmetic

Rounded to four significant digits

1. 2. 3. 4. 5.

a − c = 237.6581 − 0.014897 = 237.643203 b + d = 237.8389 + 137476 = 1377103.8389 bd = (237.8389)(137476) = 32697140.62 a/c = 237.6581/0.014897 = 15953.42015 a − b = 237.6581 − 237.8389 = −0.1808

237.6 1.377 × 105 3.270 × 107 1.595 × 104 −0.1808

TABLE 1.8  Calculations with a device carrying four significant digits. No.

Calculations

1. 2. 3. 4. 5.

A − C = 238.0 − 0.01480 = 238.0 B + D = 238.1 + 1.375 × 105 = 1.377 × 105 BD = (238.1)(1.375 × 105) = 3.274 × 107 A/C = 238.0/0.01480 = 1.608 × 104 A − B = 238.0 − 238.1 = −0.1

The following observations can be made from the results in Tables 1.7 and 1.8. a)  Negligible addition: Round-off error has crept into the fourth significant digit when we compare a − c (rounded) to A − C. There is a difference in the fourth significant digit when b + d (rounded) are compared to B + D. b) Error magnification: Comparing a/c (rounded) to A/C we find a difference in the fourth significant digit when bd (rounded) and BD are compared, the two answers differ substantially by −40000. c)  Creeping round-off: In the calculations of a − c, bd, a/c, and b + d, we find the result of working in four significant digits as opposed to working “exactly” when rounding would lead to a loss of precision in the fourth

ANMF.CH01_4PP.indd 27

3/1/2023 12:40:07 PM

28 • Applied Numerical Methods Using MATLAB significant digit. These calculations show the creeping round-off that is the gradual loss of precision as repeated rounding errors accumulate. d) Subtractive calculations: Comparing a − b = −0.1808 to A − B = −0.1, we find significant error introduced by working in fixed-precision arithmetic.

1.6 SEQUENCES A sequence may converge to a limit in a linear fashion or in a nonlinear fashion. If the sequence is convergent, then an iterative operation produces a sequence of better and better approximate solutions. 1.6.1 Linear Convergence Here, we consider a sequence {x0, x1, ..., xn} generated by the iteration xk+1 = g(xk). Table 1.9 lists k, xk, Dxk (= xk+1 − xk) and Δxk+1/Δxk for g(x) = 1 + x/2 where the starting value is taken as 0.85. TABLE 1.9  Linear convergence of the iteration process for x k +1 = 1+ k

xk

Δxk = xk+1 − xk

∆x k +1 ∆x k

0 1 2 3 4 5 6 7 8 9 10 11 12

0.850000000 1.425000000 1.712500000 1.856250000 1.928125000 1.964062500 1.982031250 1.991015625 1.995507813 1.997753906 1.998876953 1.999438477 1.999719238

0.575 0.2875 0.14375 0.071875 0.0359375 0.01796875 0.008984375 0.004492188 0.002246094 0.001123047 0.000561523 0.000280762 0.000140381

1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2

xk 2

Notice that the ratios of successive increments in the last column of Table 1.9 are all exactly equal to 1/2 and the convergence of the sequence to x = 2 are linear. We call this sequence exactly linear since Δxk+1 = cl Δxk for all k > 0. Here cl = 1/2. The sequence {xk} is said to converge linearly provided the ratio of increments Δxk+1/Δxk tends to a constant cl, where 0 < | cl| < 1.

ANMF.CH01_4PP.indd 28

3/1/2023 5:33:26 PM

Numerical Computations • 29

Linear Convergence Theorem a) X = g(X), so x = X is a fixed point of the iteration xk+1 = g(xk). b) g′(x) is continuous in a neighborhood of the fixed point X.(1.42) c) g′(X) ≠ 0. Therefore, a) {xk} converges to X linearly, with Cl = g′(X) if 0 < |g′(X)| < 1. b) {xk} diverges linearly, with Cl = g′(X) if |g′(X)| > 1.

(1.43)

c) {xk} converges or diverges slowly if g′(X) = ≠ 1. If s0 = 0 and s1 = r, the general term in a sequence that converges exactly linearly with convergence constant C is given by

k2

sk  r  C n  r n0

C k 1  1 (1.44) C 1

The increments are then given by Dsk = sk+1 − sk = rCk. Appropriate conditions on C and r would then guarantee convergence. 1.6.2 Quadratic Convergence Consider a sequence {x0, x1, ..., xn} generated by the iteration xk+1 = g(xk), where

g( x) 

2.15 x2  2.87 , 3.96 x  1.2

is a function with fixed points. Table 1.10 lists k, xk, Dxk [= (xk+1 − xk)], and Δxk+1/(Δxk) 2 for which the starting value is x0 = 2. We observe that the sequence converges very rapidly to the point x = 1. The last column of Table 1.10 shows that the ratios Δxk+1/(Δxk) 2 are tending toward the constant CQ = −0.4. This confirms the quadratic convergence of the sequence {xk} to the point x = 1. The sequence {xk} is said to converge quadratically provided the ratio of increments Dxk+1/(Dxk)2 tends to a constant Cq ≠ 0, ≠ ∞. If Dxk+1 = Cq(Dxk)2 for all k > 0, then the sequence is said to be exactly quadratically convergent.

ANMF.CH01_4PP.indd 29

3/1/2023 12:40:18 PM

30 • Applied Numerical Methods Using MATLAB

TABLE 1.10  Quadratic convergence of the iteration process for x k +1 = 2.15x + 2.87 3.96x + 1.2 2

k

xk

Δxk = xk+1 − xk

Δxk+1/(Δxk)2

0 1 2 3 4 5

2.000000000 1.257675439 1.022019537 1.000190587 1.000000015 1.000000000

−0.7423245614 −0.2356559011 −0.0218289508 −0.0001905722 −0.0000000145

−0.4276521490 −0.3930757235 −0.3999390216 −0.3999999952

TABLE 1.11  The structure of a sequence which is exactly quadratically convergent. k

sk

Δs = sk+1 − sk

Δsk+1/(Δsk)2

0 1 2 3 4 5 6

0 r r2C + r r4C3 + r2C + r r8C7 + r4C3 + r2C + r r16C15 + r8C7 + r4C3 + r2C + r r32C31 + r16C15 + r8C7 + r4C3 + r2C + r

r r2C r4C3 r8C7 r16C15 r32C31

C C C C C

Quadratic Convergence Theorem a) X = g(X), so x = X is a fixed point of the iteration xk+1 = g(xk). b) g″(x) is continuous in a neighborhood of the fixed point X.(1.45) c) g′(X) = 0.

1 That is, {xk} converges to X quadratically, with Cq   g( X ) . 2 Table 1.12 lists sk, Dsk = sk+1 − sk, and the ratios Dsk+1/(Dsk)2 for a sequence whose convergence is exactly quadratic, with convergence constant C, and with starting values s0 = 0 and s1 = r. 1.6.3 Aitken’s Acceleration Formula Quadratic convergence or any convergence of order higher than 2 is faster than the linear convergence. Aitken’s acceleration process is used to accelerate a linearly converging sequence to a quadratically converging sequence. Aitken’s process will give better results in an iterative numerical technique with fewer number of iterative operations.

ANMF.CH01_4PP.indd 30

3/1/2023 12:40:20 PM

Numerical Computations • 31

Let x be the limit of the sequence. That is,

lim xk  x k 

If {xk}, k = 1 to ∞, is a linearly convergent sequence with limit x, and en = xk − x, then

lim

n 

ek 1 ek

= l and 0 < l < 1



(1.46)

where α ∝ is the order of convergence and l is the asymptotic error constant. If α = 1, convergence is linear and if α = 2, convergence is quadratic. Aitken’s process assumes that the limiting case in Equation (1.46) occurs for all k ≥ 1. That is,

ek+1 = λek

Therefore, we can write

xk+2 = ek+2 + x = lek+1 + x(1.47)

or

xk+2 = l(xk+1 − x) + x for all k ≥ 1

(1.48)

Reducing the subscript by 1, we have

xk+1 = λ(xk − x) + x (1.49)

Eliminating λ between xk+1 and xk+2 from Equations (1.48) and (1.49), we obtain xk  2 xk  xk21 xk  2  2 xk 1  xk



x





or

x  xk 

xk2  xk xk  2  2 xk xk 1  2 xk xk 1  xk2  xk21 xk  2  2 xk 1  xk ( xk 1  xk )2 (1.50) xk  2  2 xk 1  xk

The sequence {xk} defined by

xk  xk 

( xk 1  xk )2 xk  2  2 xk 1  xk

converges more rapidly to x than the original sequence {xk} for n = 1 to ∞.

ANMF.CH01_4PP.indd 31

3/1/2023 12:40:40 PM

32 • Applied Numerical Methods Using MATLAB EXAMPLE 1.17 The sequence {xk}, n = 1 to ∞, where xk = 3x4 − 2x3 − 2x2 + 2.8 converges linearly to x = 1 with s0 = 0.75. Using Aitken’s acceleration formula, obtain another sequence, which converges faster to x = 2. Solution: The results obtained using both linear convergence algorithm and Aitken’s acceleration formula are shown in Table 1.12. TABLE 1.12  Results obtained from linear convergence and Aitken’s process. Linear convergence k 0 1 2 3 4 5 6 7 8 9 10 11

xk

Δxk = xk+1 − xk

0.750000000 1.780468750 1.890234375 1.945117188 1.972558594 1.986279297 1.993139648 1.996569824 1.998284912 1.999142456 1.999571228 1.999785614

1.03046875 0.109765625 0.054882813 0.027441406 0.013720703 0.006860352 0.003430176 0.001715088 0.000857544 0.000428772 0.000214386 0.000107193

Aitken’s process ∆x k +1 ∆x k 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2

xk −

( x k +1 − x k )2 x k +2 − 2 x k +1 + x k

1.903320561 2.000000000

1.7 SUMMARY In this chapter, we described the Taylor’s theorem and number representation including binary, decimal, and hexadecimal numbers. We have defined absolute and relative errors, inherent errors, round-off errors, truncation errors, machine epsilon, and error propagation. Methods for estimation were briefly outlined. General error formulae for approximating a function, stability and condition, uncertainty in data, linear convergence, quadratic convergence, and Aitken’s acceleration formulae were presented.

ANMF.CH01_4PP.indd 32

3/1/2023 12:40:40 PM

Numerical Computations • 33

EXERCISES NOTE

In addition to solving the following exercises through analytical solutions or long-hand numerical computations, try to solve them using user-defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 1.1

Determine the following hyperbolic trigonometric functions to O (0.9)4. (a) sinh (0.9) (b) cosh (0.9)

1.2

Determine when f(x) = 0, given that f (1.7) = −1.7781 and f ′(1.7) = 4.3257.

1.3

Determine f (1.2), given the first order differential equation



df = 2 x with f(1) = 1. dx

1.4 (a)  Convert (327)10 to binary. (b) Convert (0.3125)10 to binary. 1.5

Represent the number 50824.6135 in the decimal system (base-10).

1.6

Find the binary and hexadecimal values of the following numbers, (a) 329 (b) 203

1.7

Convert (75)10 to base-2.

1.8

Perform the following operations: (a) (2)8 + (5)8 (b) Convert (75)10 to base-8. (c) Convert (13)8 to base-10.

1.9

Convert (4D3)16 to base-10.

1.10 Convert (1001011)2 to base-8.

ANMF.CH01_4PP.indd 33

3/1/2023 12:40:44 PM

34 • Applied Numerical Methods Using MATLAB 1.11 Show that the relative error Erxy of the product where x = xe + Dx and y = ye + Dy is Erxy = Erx + Ery. Assume |Erx| %The inverse can then be computed as >> InverseA = inv(A) When the program is executed, the following result is displayed in the ­command window: InverseA = −0.0769 0.2308 0.3846

−0.1538

2.4 MATRIX INVERSION METHOD Consider a set of three simultaneous linear algebraic equations a11x1 + a12x2 + a13x3 = b1 a21x1 + a22x2 + a23x3 = b2 a31x1 + a32x2 + a33x3 = b3(2.7) Equation (2.7) can be expressed in the matrix form Ax = b(2.8)



−1

Premultiplying by the inverse A , we obtain the solution of x as x = A−1b(2.9)



If the matrix A is nonsingular, that is, if determinant (A) is not equal to zero, then Equation (2.9) has a unique solution. The solution for x1 is



ANMF.CH02_4PP.indd 44

b1 1 x1  b2 |A| b3 

a 12 a 22 a 32

a 13 1  a 22 a 23   b1 | A |  a 32 a 33

a 23 a12  b2 a 32 a 33

a 12 a 13  b3 a 33 a 22

a13 a 23

  

1 {b1 C11  b2 C21  b3 C31 } |A|

3/1/2023 12:44:02 PM

Linear System of Equations • 45

where A is the determinant of the coefficient matrix A, and C11, C21 and C31 are the cofactors of A corresponding to element 11, 21 and 31. We can also write similar expressions for x2 and x3 by replacing the second and third columns by the y column respectively. Hence, the complete solution can be written in matrix form as follows:

x1   C11   1  x 2   C12  x  | A | C  3  13

= {x } or Hence

C21 C22 C23

C31  b1    C32  b2  (2.10) C33  b3 

1 1 = [C ji ]{b} [adj A]{b} |A| |A|

A 1 

1 adj A and Adj A = A−1 abs [A](2.11) |A|

Although this method is quite general, it is not quite suitable for large systems because evaluation of A−1 by co-factors becomes very cumbersome. EXAMPLE 2.2 Obtain the solution of the following linear simultaneous equations by the matrix inversion method. 1 3   x1   5  a)        4 1  x 2  12   1 1 3   x 1   5       b)  4 2 1  x 2   0   1 3 1   x 3   5  Solution: 1 3   x1   5  a)        4 1  x 2  12  C11 = (−1)1+1|−1| = −1 C12 = (−1)1+2|4| = −4 C21 = (−1)1+|3| = −3 C22 = (−1)2+2|1| = 1

ANMF.CH02_4PP.indd 45

3/1/2023 12:44:17 PM

46 • Applied Numerical Methods Using MATLAB

Hence

 1 4  C   3 1 



 1 3  CT     4 1 



A 1 

Hence

 x 1  1  1 3   5  1  5 36  1  41         x    2  13  4 1  12  13  20 12  13  8 

Therefore

x1 

41  3.1538 13

and

x2 

8  0.6154 13

CT 1  1 3     | A | 13  4 1 

MATLAB Solution: The following MATLAB built-in function x = A\ b is used to find the solution of equations: >> A = [1 3;4 −1]; >> b = [5;12]; >> x = A\b When the program is executed, the following result is displayed in the command window: x= 3.1538 0.6154 >> x=inv(A)*b x= 3.1538 0.6154

ANMF.CH02_4PP.indd 46

3/1/2023 12:44:32 PM

Linear System of Equations • 47

1 1 3 b) | A | 4 2 1  40 1 3 1 The matrix of cofactors is given by



 5 5 10    C  10 2 4   5 13 6 

The transpose of C is the adjoint of A or

 5 10 5    Adj A  C   5 2 13  10 4 6  T

1

 5 10 5  1    Adj A / | A | 5 2 13  40  10 4 6 

Hence

A

Therefore

 5 10 5   5   0  0  1    1     XA Y 5 2 13  0   40  1 40  40     10 4 6   5   80  2  1

or x1 = 0, x2 = 1, x3 = 2. MATLAB Solution: The following MATLAB built-in function x = A\b is used to find the solution of equations: >> A = [1 −1 3;4 2 −1;1 3 1]; >> b = [5;0;5]; >> x = A\b

ANMF.CH02_4PP.indd 47

3/1/2023 12:44:52 PM

48 • Applied Numerical Methods Using MATLAB When the program is executed, the following result is displayed in the command window: x= 0 1 2 >> x = inv(A)*b x= 0 1 2 EXAMPLE 2.3 Find the inverse of the matrix

 2 2 0   A   2 1 1   3 0 1 

6    and solve the system of equations [A] {x} = {b} where {b} = 3  . 6    Solution:

 2 2 0   The determinant of A   2 1 1  = 2[1(1) − 0(1)] + 2[2(1) − 0(0)] + 3[2(1) − 1(0)] = 12  3 0 1  Since determinant A = 12 ≠ 0, the given matrix is nonsingular. The cofactors corresponding to the entries in each row of determinant A are

ANMF.CH02_4PP.indd 48

1 1 1 0 1



C11 



C21  

C12  

2 1 5 3 1

2 0 2 0  2 C22  2 0 1 3 1

C13 

2 1  3 3 0

C23  

2 2 6 3 0

3/1/2023 12:45:02 PM

Linear System of Equations • 49

C31 



2 0 2 1 1

C32  

2 0 2 2  2 C33  6 2 1 2 1

 1 2 2   121 T C 1    1   5 2 2    125 Hence A  | A | 12  6    14  3 6

 16 1 6 1 2

    1  2  1 6

1 6

It is easy to verify that A−1A = AA−1 = I.  x1   121    1 Therefore   x 2   [A ]b   125  x 3    14

 16 1 6 1 2

6  12  1   6   126  36  66   6 12          30  16  3    12  36  66    30 126 12   2  1    46  32  62   6 46 12  3  2  6   1 6

MATLAB Solution: The following MATLAB built-in function inv(A) is used to find the solution of equations: >> A = [2 2 0;−2 1 1;3 0 1]; >> InverseA = inv(A) When the program is executed, the following result is displayed in the command window: InverseA = 0.0833 −0.1667 0.1667 0.4167 0.1667 −0.1667

−0.2500 0.5000 0.5000

>> A = [2 2 0;−2 1 1;3 0 1]; >> b = [6;3;6]; >> x = inv(A)*b x= 1.0000 2.0000 3.0000

ANMF.CH02_4PP.indd 49

3/1/2023 12:45:11 PM

50 • Applied Numerical Methods Using MATLAB 2.4.1 Augmented Matrix A system of linear equations in matrix notation takes the form Ax = b, where A is of order m × n, x is of order n × 1. The augmented matrix [Ab] can be obtained by adjoining column b to matrix A. In terms of partitioned matrices, we have [Ab] = [A: b]. As an example, let us consider a set of linear equations x+y+z=8 x+y−z=5 x−y+z=2 Then we have, 1 1 1  8      [A]  1 1 1 ; {b}  5    2  1 1 1    1 1 1 8    [A b ]  1 1 1 5  and   1 1 1 2 

2.5 GAUSS ELIMINATION METHOD Consider the following system of linear simultaneous equations: a11x1 + a12x2 + a13x3 = b1(2.12) a21x1 + a22x2 + a23x3 = b2

(2.13)

a31x1 + a32x2 + a33x3 = b3(2.14) Gauss elimination is a popular technique for solving simultaneous linear algebraic equations. It reduces the coefficient matrix into an upper triangular matrix through a sequence of operations carried out on the matrix. The vector b is also modified in the process. The solution vector {x} is obtained from a backward substitution procedure. Two linear systems Ax = b and A′x = b′ of equations are said to be equivalent if any solution of one is a solution of the other. Also, let Ax = b is a linear

ANMF.CH02_4PP.indd 50

3/1/2023 12:45:17 PM

Linear System of Equations • 51

nonhomogeneous system of n equations. Suppose we subject this system to the system of following operations: 1. multiplication of one equation by a nonzero constant 2. addition of a multiple of one equation to another equation 3. interchange of two equations If the sequence of operations produce the new system A′x = b′, then both the systems Ax = b and A′x = b′ are equivalent. In particular, A is invertible if A′ is invertible. In Gauss elimination method, we adopt this and the elimination process is based on this theorem. In the Gauss elimination method, the unknowns are eliminated such that the elimination process leads to an upper triangular system and the unknowns are obtained by back substitution. It is assumed a11 ≠ 0. The method can be described in the following steps: Step 1: Eliminate x1 from the second and third equations. Using the first Equation (2.12), the following operations are performed:

a  a  (2.13)   21  (2.12)  and  (2.14)   31  (2.12)  a 11   a 11 

gives

a11x1 + a12x2 + a13x3 = b1(2.15)

a′22x2 + a′23x3 = b′2(2.16) a′32x2 + a′33x3 = b′3(2.17) Equation (2.15) is called the pivotal equation and the coefficient a11 is the pivot. Step 2: Eliminate x2 from the Equation (2.17) using Equation (2.16) by assuming a′22 ≠ 0. We perform the following operation:  a  (2.17)   32  (2.16)  a 22  to obtain

a11x1 + a12x2 + a13x3 = b1(2.18)

a′22x2 + a′23x3 = b′2(2.19) and a′′33x3 = b′′3(2.20)

ANMF.CH02_4PP.indd 51

3/1/2023 12:45:26 PM

52 • Applied Numerical Methods Using MATLAB Here Equation (2.19) is called the pivotal equation and the coefficient a′22 is the pivot. Step 3: To find x1, x2, and x3, we apply back substitution starting from Equation (2.20) giving x3, then x2 from Equation (2.19) and x1 from Equation (2.18). 2.5.1 MATLAB Program for the Gauss Elimination Method function [x,det] = Gauss(A,b) % This program solves A*x = b by using the Gauss elimination method and also % computes determinant of matrix A % Usage: [x,det] = Gauss(A,b) if size(b,2) > 1; b = b’; end % Note that b must be column vector n = length(b); for k = 1:n-1 % Elimination process for i= k+1:n if A(i, k) ~= 0 lambda = A(i, k)/A(kaki); A(i,k+1:n) = A(i,k+1:n) - lambda*A(k,k+1:n); b(i)= b(i) - lambda*b(k); end end end if nargout = = 2; det = prod(diag(A)); end for k = n:-1:1 % Back substitution process b(k) = (b(k) - A(k,k+1:n)*b(k+1:n))/A(k, k); end x = b;

ANMF.CH02_4PP.indd 52

3/1/2023 12:45:26 PM

Linear System of Equations • 53

Pivoting: Gauss elimination method fails if any one of the pivots in the above Equations (2.12) to (2.20) becomes zero. To overcome this difficulty, the equations are to be rewritten in a slightly different order such that the pivots are not zero. Partial pivoting method: Step 1: The numerically largest coefficient of x1 is selected from all the equations are pivot and the corresponding equation becomes the first Equation (2.12). Step 2: The numerically largest coefficient of x2 is selected from all the remaining equations as pivot and the corresponding equation becomes the second Equation (2.16). This process is repeated till an equation into a simple variable is obtained. Complete pivoting method: In this method, we select at each stage the numerically largest coefficient of the complete matrix of coefficients. This procedure leads to an interchange of the equations as well as interchange of the position of variables. EXAMPLE 2.4 Solve the following equations by Gauss elimination method:

2x + 4y − 6z = −4



x + 5y + 3z = 10



x + 3y + 2z = 5

Solution:

2x + 4y − 6z = −4(E.1)



x + 5y + 3z = 10

(E.2)



x + 3y + 2z = 5

(E.3)

To eliminate x from (E.2) and (E.3) using (E.1):

ANMF.CH02_4PP.indd 53



2x + 4y − 6z = −4



x + 5y + 3z = 10

1x (−2)



x + 3y + 2z = 5

1x (−2)

3/1/2023 12:45:26 PM

54 • Applied Numerical Methods Using MATLAB

2x + 4y − 6z = −4



−2x − 10y − 6z = −20



−2x − 6y − 4z = −10

2x + 4y − 6z = −4 Row 1+ Row 2:

−6y − 12z = −24(E.6)

Row 1 + Row 3:

2y − 10z = −14 1x (−3)(E.5)

To eliminate y from (E.5) using (E.4):

2x + 4y − 6z = −4



−6y − 12z = −24



6y + 30z = 42

2x + 4y − 6z = −4

−6y − 12z = −24

Row 2 + Row 3: 18z = 18   ⇒ z = 1 Evaluation of the unknowns by back substitution:

−6y − 12z = −24



6y = 24 − 12z



2x + 4y − 6z = −4



2x = −4 − 4y + 6z

y

24  12 x1  y 2 6

x

4  4 x 2  6 x1  x  3 2

MATLAB Solution: The following program (script file) uses the user-defined Gauss (A, b) function to solve the linear equations: >> A = [2 4 −6;1 5 3;1 3 2]; >> b = [−4;10;5]; >> [x,det] = Gauss(A,b)

ANMF.CH02_4PP.indd 54

3/1/2023 12:45:34 PM

Linear System of Equations • 55

When the program is executed, the following result is displayed in the command window: x= −3 2 1 det = 18 EXAMPLE 2.5 Use the method of Gaussian elimination to solve the following system of linear equations: x1 + x2 + x3 − x4 = 2 4x1 + 4x2 + x3 + x4 = 11 x1 − x2 − x3 + 2x4 = 0 2x1 + x2 + 2x3 − 2x4 = 2

(E.1)

Solution: In the first step, eliminate x1 terms from second, third and fourth equations of the set of Equations (E.1) to obtain: x1 + x2 + x3 − x4 = 2

−3x3 + 5x4 = 3 −2x2 − 2x3 + 3x4 = −2 −x2 = −2(E.2)

Interchanging columns in Equation (E.2) putting the variables in the order x1, x4, x3, and x2 as x1 − x4 + x3 + x2 = 2

−5x4 − 3x3 = 3

3x4 − 2x3 − 2x2 = −2

ANMF.CH02_4PP.indd 55

−x2 = −2(E.3)

3/1/2023 12:45:34 PM

56 • Applied Numerical Methods Using MATLAB In the second step, eliminate x4 term in third equation of the set of Equations (E.3) x1 − x4 + x3 + x2 = 2 5x4 − 3x3 = 3 −1/5x3 − 2x2 = −19/5



−x2 = −2



Now, by the process of back substitution, we have x2 = 2, x3 = −1, x4 = 0, x1 = 1. MATLAB Solution: The following program (script file) uses the user-defined Gauss (A, b) function to solve the linear equations: The given system of equations is rearranged so that the Gaussian elimination method can be applied as shown below: >> A = [4 4 1 1; 2 1 2 −2;1 1 1 −1;1 −1 −1 2]; >> b = [11;2;2;0]; >> [x, t] = Gauss(A,b) When the program is executed, the following result is displayed in the command window: x= 1 2 −1 0 t= 1.0000

ANMF.CH02_4PP.indd 56

3/1/2023 12:45:34 PM

Linear System of Equations • 57

EXAMPLE 2.6 Using the Gaussian elimination method, solve the system of equations [A] {x} = {b} where 3   1 1 1 1 3   2 1 3 0      and { b }  [A]     0 2 0 3 1    0   1 0 2 1  Solution: The augmented matrix is



1 1  2 1  [A b ]   0 2  0 2  1 0

1 3 0 0 2

1 0 3 3 1

3 3  1  1 0 

From the augmented matrix, we apply elementary transformations:



1 1 0 3 Row 2  2  Row 1  0 2 Row 4  Row 1  0 2 0 1

1 1 1 2 0 3 0 3 3 2

3 3  1  1 3 

which gives



1 1 0 3  0 0  0 0

1 1 2 3 10 3

1 3 2 3   5 1  3  4 2 3

3 1 1 1 1 0 3 1 2 3    0 0 23 35 1    0 0 0 7 7 

Hence, by back substitution the upper triangular matrix, we obtain x4 = −1, x3 = 1, x2 = 2, x1 = 1.

ANMF.CH02_4PP.indd 57

3/1/2023 12:45:46 PM

58 • Applied Numerical Methods Using MATLAB MATLAB Solution: The following program (script file) uses the user-defined Gauss (A, b) function to solve the linear equations: >> A = [1 1 1 1;2 −1 3 0;0 2 0 3;−1 0 2 1]; >> b = [3;3;1;0]; >> [x,det] = Gauss(A,b) When the program is executed, the following result is displayed in the command window: x= 1.0000 2.0000 1.0000 −1.0000 det = 14.0000

2.6 GAUSS-JORDAN METHOD The Gauss-Jordan method is an extension of the Gauss elimination method. The set of equations Ax = b is reduced to a diagonal set Ix = b′, where I is a unit matrix. This is equivalent to x = b′. The solution vector is therefore obtained directly from b′. The Gauss-Jordan method implements the same series of operations as implemented by the Gauss elimination process. The main difference is that it applies these operations below as well as above the diagonal such that all off-diagonal elements of the matrix are reduced to zero. Gauss-Jordan method also provides the inverse of the coefficient matrix A along with the solution vector {x}. The Gauss-Jordan method is widely used due to its stability and direct procedure. The Gauss-Jordan method requires more computational effort than Gauss elimination process. The Gauss-Jordan method is a modification of Gauss elimination method. The series of operations performed are quite similar to the Gauss elimination method. In the Gauss elimination method, an upper triangular matrix is derived while in the Gauss-Jordan method an identity matrix is derived. Hence, back substitutions are not required.

ANMF.CH02_4PP.indd 58

3/1/2023 12:45:46 PM

Linear System of Equations • 59

2.6.1 MATLAB Program for the Gauss Jordan Method function x = Gaussjordan(A, b) % Solves a system of linear equations Ax = b using the Gauss Jordan method % USAGE: x = Gaussjordan(func, A, b) % INPUT: % A = The matrix of coefficients % b = The column vector of constants % OUTPUT: % x = A column vector with the solution Ab=[A, b]; [R, c]=size(Ab); for j=1:R % Pivoting pt=Ab(j, j); kt=j; % Find the row with largest pivot element for k=j+1:R if Ab(k+j)~=0 & abs(Ab(k, j))>abs(pt) pt=Ab(k, j); kt=k; end end %Switch the rows if A row with larger pivot element exists if kt~=j At=Ab(j,:); Ab(j,:)=Ab(kt,:); Ab(kt,:)=At; end % Pivoting ends here Ab(j,:)=Ab(j,:)/Ab(j, j); for i = 1:R

ANMF.CH02_4PP.indd 59

3/1/2023 12:45:46 PM

60 • Applied Numerical Methods Using MATLAB

if i~=j Ab(i, j: c)=Ab(i, j: c)-Ab(i, j)*Ab(j, j: c); end end end x=Ab(:,c)

EXAMPLE 2.7 Solve the following equations by Gauss-Jordan method. x + 3y + 2z = 17 x + 2y + 3z = 16 2x − y + 4z = 13 Solution: Consider x + 3y + 2z = 17

(E.1)

x + 2y + 3z = 16

(E.2)

2x − y + 4z = 13

(E.3)

x + 3y + 2z = 17   (E.1) (−2) + (E.3) x + 2y + 3z = 16   (E.2) (−1) + (E.1) 2x − y + 4z = 13 x + 3y + 2z = 17 y − z = 1  x(2) + (E.1) 7y = 21 ⇒ y = 3 x + 5y = 19 y − z = 1 ⇒ z = y − 1 = 3 − 1 ⇒ z = 2 x + 5y = 19 ⇒ x = 19 − 5x3 ⇒ x = 4

ANMF.CH02_4PP.indd 60

3/1/2023 12:45:46 PM

Linear System of Equations • 61

MATLAB Solution: The following program (script file) uses the user-defined Gaussjordan(A, b) function to solve the linear equations: >> A = [1 3 2;1 2 3;2 −1 4]; >> b = [17;16;13]; >> x = Gaussjordan(A, b) When the program is executed, the following result is displayed in the command window: x= 4 3 2 EXAMPLE 2.8 Solve the following system of equations using the Gauss-Jordan method. x − 2y = −4 −5y + z = −9 4x − 3z = −10 Solution: The augmented matrix is  1 2 0 4     0 5 1 9   4 0 3 10  Multiplying 1st row by −4 and adding the result to the 3rd row, we obtain 1 2 0 4    4R1  R 3  0 5 1 9  0 8 3 6 

ANMF.CH02_4PP.indd 61

3/1/2023 12:45:52 PM

62 • Applied Numerical Methods Using MATLAB Now, multiply the 2nd row by −1/5 0 1 2 1   R 2  0 1 1 / 5 5 0 8 3

4 9 /5 6

   

Multiply the 2nd row and add the result to the 1st row. Then multiply the 2nd row by −8 and add the result to the 3rd row. 1 0 2 / 5 2 / 5  2R 2  R1   0 1 1 / 5 9 / 5     0 0 7 / 5 42 / 5  Multiply 3rd row by −5/7 1 0 2 / 5 2 / 5  5    R 3  0 1 1 / 5 9 / 5  7 0 0 1 6  Multiply the 3rd row by 2/5 and add the result to the 1st row. Then multiply the 3rd row by 1/5 and add the result to the 2nd row.  R1  1 0 0 2    0 1 0 3  1  0 1 6  5 R 3  R 2  0 2 5 R3

Hence, the last matrix above represents the system with x = 2, y = 3 and z = 6. MATLAB Solution: The following program (script file) uses the user-defined Gaussjordan (A, b) function to solve the linear equations: >> A = [1 −2 0; 0 −5 1; 4 0 −3]; >> b = [−4;-9;−10]; >> x = Gaussjordan (A, b)

ANMF.CH02_4PP.indd 62

3/1/2023 12:46:01 PM

Linear System of Equations • 63

When the program is executed, the following result is displayed in the command window: x= 2.0000 3.0000 6.0000 EXAMPLE 2.9 Solve the following set of equations by Gauss-Jordan method. 2x1 + x2 − 3x3 = 11 4x1 − 2x2 + 3x3 = 8 −2x1 + 2x2 − x3 = −6

Solution:

The augmented matrix for the given set of equations is



 2 1 3 11    8  4 2 3  2 2 1 6 

Step 1: Divide Row 1 by 2



1 3   1 2 2  3 4  2   2 2 1 

11  2  8 6 

1 3  1   Row 2  4  Row 1 2 2  Step 2:  9 0 4 Row 3  2  Row 1  0 3 4 

ANMF.CH02_4PP.indd 63

11  2   14  5 

3/1/2023 12:46:09 PM

64 • Applied Numerical Methods Using MATLAB

 1  Step 3: Divide Row 2 by −4 0  0 

1 2 1 3

3 2 9  4 4 

11  2  7 2 5 

3  1 0  8 Row 1  1 / 2  Row 2  9 0 1  Step 4:  Row 3  3  Row 2 4  11 0 3 4 

15  4   7  2  11    2

3  1 0  8  Step 5: Divide Row 3 by 11/4 0 1  9  4 0 0 1 

15  4  7 2 2 

1 0 0 3  Row 1  3 / 8  Row 3  0 1 0 1  Step 6:   Row 2  9 / 4  Row 3 0 0 1 2  Hence the solution is x1 = 3, x2 = −1, x3 = −2. MATLAB Solution: The following program (script file) uses the user-defined Gaussjordan(A, b) function to solve the linear equations: >> A = [2 1 −3; 4 −2 3;−2 2 −1]; >> b = [11; 8;−6]; >> x = Gaussjordan (A, b)

ANMF.CH02_4PP.indd 64

3/1/2023 12:46:27 PM

Linear System of Equations • 65

When the program is executed, the following result is displayed in the ­command window: x= 3 −1 −2 EXAMPLE 2.10 Solve 2x1 + 6x2 + x3 = 7 x1 + 2x2 − x3 = −1 5x1 + 7x2 − 4x3 = 9 Using (a) Gaussian elimination and (b) Gauss-Jordan elimination. Solution: a) Using row operations on the augmented matrix of the system, 2 6 1 7   1 2 1 1  2R R  1 2 1 1   1 2 1 1  R12  2 6 1 7  5R11 R32  0 2 3 9           5 7 4 9   0 3 1 14   5 7 4 9       



 1 2 1 1   1 2 1 1   0 1 3R2 R3 3 9  9      0 1 32 2  2 2    0 0 11 55   0 3 1 14  2 2    



 1 2 1 1  9     0 1 32 2  (E.1) 0 0 1 5   

1 R2 2

2 R3 11

The last matrix is in row-echelon form and represents the system x1 + 2x2 − x3 = −1(E.2) 3 9 x 2  x 3  (E.3) 2 2 x3 = 5 (E.4)

ANMF.CH02_4PP.indd 65

3/1/2023 12:46:45 PM

66 • Applied Numerical Methods Using MATLAB Substituting x3 = 5 into Equation (E.3) gives x2 = −3. Substituting both of these values back into Equation (E.2) finally yields x1 = 10. MATLAB Solution: The following program (script file) uses the user-defined Gauss (A, b) function to solve the linear equations: >> A = [2 6 1; 1 2 −1; 5 7 −4]; >> b = [7 −1 9]; >> [x,] =t] = Gauss (A, b) When the program is executed, the following result is displayed in the command window: x= 10 −3 5 det = −11 b) We start with the last matrix in Equation (E.1) above. Since the first entries in the second and third rows are 1’s, we must, in turn, make the remaining entries in the second and third columns 0’s:  1 2 1 1   1 0 4 10  4R3 3 R1  1 0 0 10   R3 R2   0 1 3    2  R  R 9 9 2 1 2   0 1 0 3    0 1 32 2 2   2    0 0 1 0 0 1 5   0 0 1 5  5      (E.5) The last matrix in Equation (E.5) is now in reduced row-echelon form. It is evident that the solution of the system is x1 = 10, x2 = −3, x3 = 5. MATLAB Solution: The following program (script file) uses the user-defined Gaussjordan(A, b) function to solve the linear equations: >> A = [2 6 1; 1 2 −1; 5 7 −4];

ANMF.CH02_4PP.indd 66

3/1/2023 12:46:46 PM

Linear System of Equations • 67

>> b = [7;−1; 9]; >> x = Gaussjordan (A, b) When the program is executed, the following result is displayed in the command window: x= 10 −3 5 LU Decomposition: It is possible to show that any square matrix A can be expressed as a product of a lower triangular matrix L and an upper triangular matrix U. A = LU For instance  a 11 a  21 a 31

a 12 a 22 a 32

a 13   L11 a 23    L 21   a 33  L 31

0 L 22 L 31

0  U11 0  0  L 33   0

U12 U22 0

U13  U23   U33 

The process of computing L and U for a given A is known as LU Decomposition or LU Factorization. LU decomposition is not unique (the combinations of L and U for a prescribed A are endless), unless certain constraints are placed on L or U. These constraints distinguish one type of decomposition from another. Two commonly used decompositions are given below: 1. Cholesky’s decomposition: Constraints are L = UT 2. Crout’s decomposition: Constrains are Uii = 1, i = 1, 2, ..., n. After decomposing the matrix A, it is easier to solve the equations Ax = b. We can rewrite the equations as LUx = b or denoting Ux = y, the above equation becomes Ly = b

ANMF.CH02_4PP.indd 67

3/1/2023 12:46:48 PM

68 • Applied Numerical Methods Using MATLAB This equation Ly = b can be solved for y by forward substitution. Then Ux = y will yield x by the backward substitution process. The advantage of the LU decomposition method over the Gauss elimination method is that once A is decomposed, we can solve Ax = b for as many constant vectors b as we please. Also, the forward and backward substitutions operations are much less time consuming than the decomposition process.

2.7 CHOLESKY’S TRIANGULARIZATION METHOD Cholesky’s decomposition method is faster than the LU decomposition. There is no need for pivoting. If the decomposition fails, the matrix is not positive definite. Consider the system of linear equations: a11x1 + a12x2 + a13x3 = b1 a21x1 + a22x2 + a23x3 = b2 a31x1 + a32x2 + a33x3 = b3(2.21) The above system can be written as Ax = b  where

 a 11 A  a 21 a 31

a12 a 22 a 32

(2.22)

a 13   b1   x1     a 23 , x  x 2 , b   b2        x 3   b3  a 33 

Let A = LU…

where

1 L  l21 l31

0 1 l32

0 u11  0 and U =  0   1   0

u12 u 22 0

(2.23) u13  u 23   u 33 

Equation (2.21) can be written as

ANMF.CH02_4PP.indd 68

LUX = b

(2.24)

If w write   UX = V

(2.25)

3/1/2023 12:47:06 PM

Linear System of Equations • 69

Equation (2.24) becomes LV = b

(2.26)

Equation (2.26) is equivalent to the system v1 = b1 l21v1 + v2 = b2



l31v1 + l32v2 + v3 = b3(2.27)



The above system can be solved to find the values of v1, v2, and v3 which give us the matrix V. UX = V then becomes u11x1 + u12x2 + u13x3 = v1 u22x2 + u23x3 = v2 u33x3 = v3(2.28) which can be solved for x3, x2, and x1 by the backward substitution process. In order to compute the matrices L and U, we write Equation (2.23) as



1 l  21 l31

0 1 l32

0  u11 0  0  1   0

u12 u 22 0

u13   a 11 u 23   a 21   u 33  a 31

a 12 a 22 a 32

a13  a 23  (2.29)  a 33 

Multiplying the matrices on the left and equating the corresponding elements of both sides, we obtain

u11 = a11, u12 = a12, u13 = a13(2.30) a 21  a 11   (2.31) a l31 u11  a 31  l31  31  a 11  l21 u11  a 21  l21 



a 21  a 12  a 11   (2.32) a l21 u13  u 23  a 23  u 23  a 23  21 a 13   a 11 l21 u12  u 22  a 22  u 22  a 22 



ANMF.CH02_4PP.indd 69

3/1/2023 12:47:12 PM

70 • Applied Numerical Methods Using MATLAB

1 u 22

a 31   a 32  a a 12  (2.33) 11  



l31 u12  l32 u 22  a 32  l32 

and

l31u13 + l32u23 + u33 = a33(2.34)

The value of u33 can be computed from Equation (2.34). To obtain the elements of L and U, first find the first row of U and the first column of L. Then, determine the second row of U and the second column of L. Finally, compute the third row of U. Cholesky’s triangularization method is also known as Crout’s triangularization method or method of factorization. EXAMPLE 2.11 Solve the following equations by Cholesky’s triangularization method. 2x + y + 4z = 12 8x − 3y + 2z = 20 4x + 11y − z = 33 Solution: We have

2 1 4  x  12      A   8 3 2  , X   y  , B  20   4 11 1 z  33 

Let

1 l  21 l31

0 1 l32

0  u11 0  0  1   0

u12 u 22 0

u13   2 1 4  u 23    8 3 2     u 33   4 11 1

Multiplying and equating we get:

ANMF.CH02_4PP.indd 70



lxu11 = 2 ⇒ u11 = 2



lxu12 = 1 ⇒ u12 = 1



lxu13 = 4 ⇒ u13 = 4



l21xu11 = 8  l21 

8 8  4 u11 2

3/1/2023 12:47:20 PM

Linear System of Equations • 71



l21xu12 + u22 = −3 ⇒ u22 = −3 − l21xu12 = −3 − 4x1 = −7



l21xu13 + u23 = 2 ⇒ u23 = 2 − l21xu13 = 2 − 4x4 = −14



l31xu11 = 4  l31 



l31xu12 + l32xu22 = 11  l32 



l31xu13 + l32xu23 + lxu33 = −1 ⇒ u33 = −1 − l31xu13 − l32xu23 = −1 − 2x4  9  −   (14)  = −27  7 

1 0 4 1  A  We get:  9 2  7 

4 4  2 u11 2 11  l31 xu12 11  2 x1 9   7 u 22 7

0 4  2 1 0   0 7 14    1  0 0 27  

and the given system can be written as:



1 0 4 1   9 2  7 

0 4   x  12  2 1 0   0 7 14   y   20       1   0 0 27  z  33  

Writing: LV = B, we get



1 0 4 1   9 2  7 

0  V1  12  0      V2  20     1   V3  33  

which gives

ANMF.CH02_4PP.indd 71

V1 = 12



4V1 + V2 = 20 ⇒ V2 = 20 − 4x12 = −28



2V1 −

9 9 V2 + V3 = 33 ⇒ V3  33  (−28) − 2x12 = −27 7 7

3/1/2023 12:47:31 PM

72 • Applied Numerical Methods Using MATLAB The solution to the original system is given by:

UX = V



4   x   12  2 1 0 7 14   y    28       0 0 27  z   27 



2x + y + 4z = 12



−7y − 14z = −28



−27z = −27 ⇒ z = 1



7y = 28 − 14x1  y 



14  y 2 7 6 2x = 12 − y − 4z = 12 − 2 − 4x1  x   x  3 2

MATLAB Solution: EXAMPLE 2.12 Solve the system of equations using Cholesky’s factorizations. x1 + x2 + x3 − x4 = 2 x1 − x2 − x3 + 2x4 = 0 4x1 + 4x2 + x3 + x4 = 11 2x1 + x2 + 2x3 − 2x4 = 2 Solution: The set of equations can be written in the matrix form [A]{x} = {b}



 1 1 1 1   x 1   2   1 1 1 2   x   0     2      4 4 1 1   x 3  11    2 1 2 2   x 4   2 

Let us decompose [A] in the form [A] = [L] [U]

ANMF.CH02_4PP.indd 72

3/1/2023 12:47:39 PM

Linear System of Equations • 73

where

1 l 21 [L]   l31  l41

0 1 l32 l42

0 0 1 l43

0 u11 0  0  and [U]   0 0   1 0

u12 u 22 0 0

u13 u 23 u 33 0

u14  u 24   u 34   u 44 

The product of [L][U] gives  u11 l u 21 11 [L][U]   l31u11  l41 u11

u12 l21 u12  u 22 l31 u12  l32 u 22 l41 u12  l42 u 22

u13 l21 u13  u 23 l31 u13  l32 u 23  u 33 l41 u13  l42 u 23  l43 u 33

u14   l21 u14  u 24   l31 u14  l32 u 24  u 34  l41 u14  l42 u 24  l43 u 34  u 44 

Equating the elements of this matrix to the [A] matrix yields the following equations u11 = 1

l21u11 = 1

u12 = 1

l21u12 + u22 = −1 l31u12 + l32u22 = 4

u13 = 1

l21u13 + u23 = −1 l31u13 + l32u23 + u33 = 1 l41u13 + l42u23 + l23u33 = 2

u14 = −1 l21u14 + u24 = 2

l31u11 = 4

l41u11 = 2 l41u12 + l42u22 = 1

l31u14 + l32u24 + u34 = 1 l41u14 + l42u24 + l43u34 + u44 = −2

By solving these sixteen equations we get



1 0 0 1 1 0 [L]   4 0 1  1 1 2 2  3

0 1 1 1 1  0 2 2 3  0  and [U]    0 0 3 5  0   1  1 0 6  0 0

To solve [A]{x} = {b} we have to solve the two systems

ANMF.CH02_4PP.indd 73



[L]{Y} = {b}



[U]{x} = {Y}

3/1/2023 12:47:57 PM

74 • Applied Numerical Methods Using MATLAB

i.e.,

1 0 0 0   y 1   2  1 1 0 0   y   0     2      4 0 1 0   y 3  11   1 1  2 2 3 1   y 4   2 

which gives by forward substitution y1 = 2, y2 = −2, y3 = 3, y4 = 0 and hence [U]{x} = {y} becomes



1 1  x 1   2  1 1 0 2 2 3   x  2     2     0 0 3 5   x 3   3   1    0  0 6  x 4  0 0

Then by back substitution we obtain x4 = 0, x3 = −1, x2 = 2, x1 = 1. MATLAB Solution: The following MATLAB built-in function lu(A) is used to solve the linear equations using Cholesky’s method: >> A = [1 1 1 −1;1 −1 −1 2;4 4 1 1;2 1 2 −2]; >> b = [2;0;11;2]; >> [L,U] = lu(A) When the program is executed, the following result is displayed in the command window: L= 0.2500 0

0.3529 1.0000

0.2500 1.0000

0

0

1.0000 0

0

0

0.5000 0.5000 1.0000

ANMF.CH02_4PP.indd 74

0

3/1/2023 12:48:00 PM

Linear System of Equations • 75

U= 4.0000 4.0000 1.0000 1.0000 0

−2.0000

0

−1.2500 1.7500

0 2.1250 −3.3750

0 0 0 −0.0588 >> L*U ans = 1.0000 1.0000 1.0000 −1.0000 1.0000 −1.0000 −1.0000 2.0000 4.0000 4.0000 1.0000 1.0000 2.0000 1.0000 2.0000 −2.0000 >> d = L\b d= 11.0000 −2.7500 −2.1250 −0.0000 >> x = U\d x= 1.0000 2.0000 −1.0000 0.0000

ANMF.CH02_4PP.indd 75

3/1/2023 12:48:00 PM

76 • Applied Numerical Methods Using MATLAB EXAMPLE 2.13 Solve the system of linear equations using Cholesky’s factorization method. 2x − 6y + 8z = 24 5x + 4y − 3z = 2 3x + y + 2z = 16 Solution:



 u11 l u  21 11 l31u11



u11 = 2,  u12 = −6,  u13 = 8



= l21

5 = 2.5 u11



= l31

3 = 1.5 u11



u22 = 4 − l21u12 = 19



u23 = −3 − l21u13 = −23



l32 



l33 = 2 − l31u13 − l32u23 =



1 L  2.5 1.5



ANMF.CH02_4PP.indd 76

0 1 l32

0  u11 0  0  1   0

u13  2 6 8  u 23    5 4 3     2  u 33  3 1

1 l  21 l31

u12 u 22 0

u12 l21 u12  u 22 l31 u12  l32 u 22

1  l31 u12 10  u 22 19

0 1 10 19

u13  2 6 8     5 4 3  l21 u13  u 23    l31 u13  l32 u 23  u 33  3 1 2 

40 19

0 2 6 8   0 , U  0 19 23     40 0 0  1  19 

LV = B ⇒

3/1/2023 12:48:09 PM

Linear System of Equations • 77



1 2.5  1.5

0 1 10 19

0   v 1  24  0  v 2    2       1   v 3  16 



v1 = 24



v2 = 2 − 2.5 × 24 = −58



v3 = 16 − 1.5 × 24 −



UX = V ⇒



 24  2 6 8   x    0 19 23   y    58       200  40 0 0    19   z    19 



2x − 6y + 8z = 24



19y − 23z = −58(E.2) 40 200 z  z  5 (E.3) 19 19



200 10 (58)  19 19

(E.1)

From Equations (E.2), and (E.3), we have y=3 From Equations (E.1), (E.2), and (E.3), we get x=1 MATLAB Solution: The following MATLAB built-in function lu(A) is used to solve the linear equations using Cholesky’s method: >> A = [2 −6 8;5 4 −3;3 1 2]; >> b = [24;2;16]; >> [L,U] = lu(A)

ANMF.CH02_4PP.indd 77

3/1/2023 12:48:14 PM

78 • Applied Numerical Methods Using MATLAB When the program is executed, the following result is displayed in the command window: L= 0.4000 1.0000

0

1.0000 0 0 0.6000 0.1842 1.0000 U= 5.0000 4.0000 −3.0000 0 0

−7.6000 9.2000 0 2.1053

>> L*U ans = 2.0000

−6.0000 8.0000

5.0000 4.0000 −3.0000 3.0000 1.0000 2.0000 >> d = L\b d= 2.0000 23.2000 10.5263 >> x = U\d x= 1.0000 3.0000 5.0000

ANMF.CH02_4PP.indd 78

3/1/2023 12:48:14 PM

Linear System of Equations • 79

2.8 CROUT’S METHOD This method is based on the fact that every square matrix A can be expressed as the product of a lower triangular matrix and an upper triangular matrix, provided all the principle minors of A are nonsingular. Also, such a factorization, if exists, is unique. This method is also called triangularization or factorization method. Here, we factorize the given matrix as A = LU, where L is a lower triangular matrix with unit diagonal elements and U is an upper triangular matrix. Then, A−1 = (LU)−1 = U−1L−1 Consider the system a11x1 + a12x2 + a13x3 = b1 a21x1 + a22x2 + a23x3 = b2 a31x1 + a32x2 + a33x3 = b3(2.35) The above system can be written as Ax = b Let   A = LU where

l11 L  l21 l31

0 l22 l32

(2.36)

1 u12 0  0  and U = 0 1  0 0 l33 

u13  u 23  (2.37)  1 

Here, L is a lower triangular matrix and U is an upper triangular matrix with diagonal elements equal to unity.

ANMF.CH02_4PP.indd 79



A = LU ⇒ A−1 = U−1L−1(2.38)

Now

 a 11 A  LU  a 21 a 31

or

 a 11 a  21 a 31

a 12 a 22 a 32

a12 a 22 a 32

a 13  l11 a 23   l21   a 33  l31

a 13  l11 a 23   l21   a 33  l31 l11 u12 l21 u12  l22 l31u12 + l32

0 l22 l32

0  1 u12 0  0 1  l33  0 0

u13  u 23   1 

l11 u13  l21 u13  l22 u 23   l31u13 + l32 u23 + l33 

3/1/2023 12:48:20 PM

80 • Applied Numerical Methods Using MATLAB Equating the corresponding elements, we obtain

l11 = a11

l21 = a21

l31 = a31(2.39)



l11u12 = a12

l11u13 = a13(2.40)



l21u12 + l22 = a22

l31u12 + l32 = a32(2.41)

l21u13 + l22u23 = a23(2.42) and l31u13 + l32u23 + l33 = a33(2.43) from (2.40) we find u12 = a12/l11 = a12/a11 from (2.41) we obtain    l22 = a22 − l21u12(2.44)    l32 = a32 − l31u12(2.45) Equation (2.42) gives   u23 = (a23 − l21u23)/l22 

(2.46)

from the relation (2.43) we get    l33 = a33 − l31u13 − l32u23(2.47) Thus, we have determined all the elements of L and U. From Equations (2.36) and (2.37) we have LUx = b

(2.48)

Let UX = V  v1  v  where V   2  M    vn  From Equation (2.48) we have LV = b, which on forward substitution yields V. From UX = V, we find x (by backward substitution).

ANMF.CH02_4PP.indd 80

3/1/2023 12:48:22 PM

Linear System of Equations • 81

2.8.1 MATLAB Program for Crout’s Method function [L,U] = Crout(A) % Uses Crout’s method of decomposition such that A = LU % Input: % A = Matrix of coefficients % Output: % L = Lower triangular matrix % U = Upper triangular matrix [n, C] = size(A); for i = 1:n L(i,1) = A(i,1); U(i, i) = 1; end for j = 2:n U(1,j) = A(1,j)/L(1,1); end for i = 2:n for j = 2:i L(i, j) = A(i, j)−L(i,1:j−1)*U(1:j−1,j); end for j = i + 1:n U(i, j) = (A(i, j)−L(i,1:i−1)*U(1:i−1,j))/L(i, i); end  end

ANMF.CH02_4PP.indd 81

3/1/2023 12:48:22 PM

82 • Applied Numerical Methods Using MATLAB EXAMPLE 2.14 Solve the following set of equations by Crout’s method: 2x + y + 4z = 12 8x − 3y + 2z = 20 4x + 11y − z = 33 Solution: 2 1 4  x  12      We have A   8 3 2  , X   y  , B  20   4 11 1 z  33  AX = B Let    A = LU

ANMF.CH02_4PP.indd 82



l11 L  l21 l31



 2 1 4  l11  8 3 2   l    21  4 11 1 l31

0 l22 l32



 2 1 4  l11  8 3 2   l    21  4 11 1 l31

l11 u12 l21 u12  l22 l31 u12  l32



l11u12 = 1  u12 

1 2



l11u12 = 4  u13 

4 2 2



1 l22 + l21u12 = −3  l22  3  8    7 2



1 l32 + l21u12 = −3  l32  11  4    9 2

0 l22 l32

0 0  l33 

1 u12 U  0 1 0 0

u13  u 23   1 

0  1 u12 0  0 1  l33  0 0

u13  u 23   1 

l11 u13  l21 u13  l22 u 23   l31 u13  l32 u 23  l33 

3/1/2023 12:48:31 PM

Linear System of Equations • 83

l21u13 + l22u23 = 2  u 23 



l31u13 + l32u23 + l33 = −1 ⇒ l33 = −1 − 4 × 2 − 9 × 2 = −27



0  1 12 2  2 0 L   8 7 0  and U  0 1 2  0 0 1   4 9 27 



LV = B



0   v 1  12  2 0  8 7 0   v   20    2    4 9 27   v 3  33 



2v1 = 12 ⇒ v1 = 6



8v1 − 7v2 = 20  v 2 



4v1 + 9v2 − 27v3 = 33  v 3 



 V1  6  V   V2    4  ; Ux = V  V3  1 



1 12 2   x  6  0 1 2   y    4       0 0 1  z  1 

20  8  6 =4 7 33  4  6  9  4 =1 27



1 x  y  2z  6 2 y + 2z = 4



z=1



y = 4 − 2x1 ⇒ y = 2 1 x=6− ×2−2×1⇒ x=3 2





ANMF.CH02_4PP.indd 83

2  82 2 7



3/1/2023 12:48:42 PM

84 • Applied Numerical Methods Using MATLAB MATLAB Solution Using Crout’s Method: The following program (script file) uses the user-defined [L,U]=Crout(A) function to solve the linear equations: >> A = [2 1 4;8 −3 2;4 11 −1]; >> [L,U] = Crout(A) When the program is executed, the following result is displayed in the command window: L= 2

0  0

8

−7  0

4 9 −27 U= 1.0000 0.5000 2.0000 0 1.0000 2.0000 0

0 1.0000

>> L*U ans = 2 1 4 8

−3 2

4 11 −1 >> d = L\b; >> x = U\d x= 3 2 1

ANMF.CH02_4PP.indd 84

3/1/2023 12:48:42 PM

Linear System of Equations • 85

MATLAB Solution: The following MATLAB built-in function inv(A) is used to find the inverse of matrix A. >> A = [2 1 4;8 −3 2; 4 11 −1]; >> b = [12;20;33]; >> x = inv(A)*b When the program is executed, the following result is displayed in the command window: x= 3.0000 2.0000 1.0000 EXAMPLE 2.15 Solve the following set of equations by using the Crout’s method: 2x1 + x2 + x3 = 7 x1 + 2x2 + x3 = 8 x1 + x2 + 2x3 = 9 Solution:

2 1 1  x  7      A  1 2 1  , x   y  , B  8  1 1 2  z  9 

Let A = LU

ANMF.CH02_4PP.indd 85

l11 L  l21 l31

0 l22 l32

0 0  l33 

1 u12 U  0 1 0 0

u13  u 23   1 

3/1/2023 12:48:45 PM

86 • Applied Numerical Methods Using MATLAB

ANMF.CH02_4PP.indd 86

2 1 1  l11  1 2 1   l    21 1 1 2  l31

l11 u12 l21 u12  l22 l31 u12  l32

l11 = 2, 1 u12 = , 2

l21 = 1, 1 u13 = 2 1 3 l22 = 2 − l21u12 = 2 − 1 × = 2 2

l11 u13  l21 u13  l22 u 23   l31 u13  l32 u 23  l33  l31 = 1

1 1 = 2 2



l32 = 1 − l31u12 = 1 − 1 x



u23 =



l33 = 2 − l31u13 − l32u23 = 2 −



0 0  2 1 1 / 2 1 / 2    0 , U  0 1 1 / 3 L  1 3 / 2    1 1 / 2 4 / 3  0 0 1 



Ax = B,



0 0   v 1  7  2  0   v 2   8  LV  B  1 3 / 2     1 1 / 2 4 / 3   v 3  9 



2 v 1  7  v 1  3.5   3.5  3  v1  v 2  8  v 2  3     V  3  2  3  4 1 v1  v 2  v 3  9  v 3  3 2 3 



1 1 / 2 1 / 2   x 1  3.5  1 1 / 3   x 2   3  Ux  V  0     0 0 1   x 3  3 

1  l21 u13 1  l22 3

LU.x = B,

1 1 1 4 − × = 2 2 3 3

Ux = V

3/1/2023 12:48:59 PM

Linear System of Equations • 87

1 1 x2  x 3  3.5 (E.1) 2 2



x1 



1 x 2  x 3  3 (E.2) 3



x3 = 3 (E.3)

From Equations (E.2) and (E.3), we have

x2 = 2

From Equation (E.1), we get

x1 = 1

MATLAB Solution Using Crout’s Method: The following program (script file) uses the user-defined [L,U] = Crout(A) function to solve the linear equations: >> A = [2 1 1;1 2 1;1 1 2]; >> [L,U] = Crout(A); >> b = [7;8;9]; >> d = L\b; >> x = U\d When the program is executed, the following result is displayed in the command window: x= 1.0000 2.0000 3.0000 MATLAB Solution: The following MATLAB built-in function inv(A) is used to find the inverse of matrix A. >> A = [2 1 1;1 2 1;1 1 2];

ANMF.CH02_4PP.indd 87

3/1/2023 12:49:03 PM

88 • Applied Numerical Methods Using MATLAB >> b = [7;8;9]; >> x = inv(A)*b When the program is executed, the following result is displayed in the command window: x=

1 2 3

2.9 THOMAS ALGORITHM FOR TRIDIAGONAL SYSTEM Consider the system of linear simultaneous algebraic equations given by Ax = b where A is a tridiagonal matrix, x = [x1, x2, …, xn]T and b = [b1, b2, …, bn]T. Hence, we consider a 4 × 4 tridiagonal system of equations given by



 a12 a  21 0  0

a13 a 22 a 31 0

0 a 23 a 32 a 41

0   x1   b1      0   x 2   b 2   (2.48a) a 33   x 3   b3      a 42   x 4   b 4 

Equation (2.48a) can be written as a12x1 + a13x2 = b1 a21x1 + a22x2 + a23x3 = b2 a31x2 + a32x3 + a33x4 = b3 a41x3 + a42x4 = b4(2.48b) The system of equations given by Equation (2.48b) is solved using Thomas Algorithm which is described in three steps as shown below: Step 1: Set y1 = a12 and compute



ANMF.CH02_4PP.indd 88

y i  ai2 

a i 1 a ( i 1 ) 3 y i 1

i = 2, 3, …, n

3/1/2023 12:49:06 PM

Linear System of Equations • 89

Step 2: Set z1 = zi 



b1 and compute a 12

b i  a i 1 z i 1 i = 2, 3, …, n yi

Step 3: x i  z i 

a i 3 x i 1 i = n − 1, n − 2, …, 1, where xn = zn yi

2.9.1 MATLAB Program for the Thomas Method for Tridiagonal Systems function x = Thomas (a, d, b, c) %Solves matrix equation Ax = c, where A is a tridiagonal matrix %Inputs: % a = Upper diagonal of matrix A a (n) = 0 % b = Lower diagonal of A, b (1) = 0 % c = Right-hand side of equation % d = diagonal of A n = length (d); a (1) = a (1)/d (1); c (1) = c (1)/d (1); for i = 2: n−1 den = d (i)−b (i)*a (i−1); if (den= =0), error (‘Division by zero’), end a (i) = a (i)/den; c (i) = (c (i)−b (i)*c (i−1))/den; end c(n) = (c(n)−b(n)*c(n−1))/(d(n)−b(n)*a(n−1)); x (n) = c (n); for i = n−1:−1:1 x (i) =c (i)−a (i)*x (i + 1); end

ANMF.CH02_4PP.indd 89

3/1/2023 12:49:12 PM

90 • Applied Numerical Methods Using MATLAB EXAMPLE 2.16 Solve the following equations by the Thomas Algorithm. 3x1 − x2 = 5 2x1 − 3x2 + 2x3 = 5 x2 + 2x3 + 5x4 = 10 x3 − x4 = 1 Solution:

Here

3 1 2 3  0 1  0 0



[a2, a3, a4] = [2, 1, 1]



[b1, b2, b3, b4] = [3, −3, 2, −1]



[c1, c2, c3] = [−1, 2, 5]

0 0   x1   5  2 0  x2   5       2 5   x 3  10      1 1  x 4   1 

Step 1: Set y1 = b1 and compute a i c i 1 , i = 2, 3, …, n y i 1



y i  bi 



y1 = 3



i = 2, y 2  b2 



i = 3, y 3  b3 

a 2 c1 2(1) 7  3   y1 3 3 a 3 c2 1  2 20  2 7 y2 7  3

a 4c3 1.5 55  1   20 y3 20 7 d d  a i z i 1 5 Step 2: Set z1 = 1 = , z i  i i = 2, 3, …, n b1 3 yi

ANMF.CH02_4PP.indd 90

i = 4, y 4  b 4 

3/1/2023 12:49:33 PM

Linear System of Equations • 91







5 3 5 7 7  3 5 10  1    d 3  a 3z2  7   75  i = 3, z 3  20 y3 20 7 d 2  a 2 z1  i = 2, z 2  y2

d 4  a 4z3  i = 4, z 4  y4

Step 3: Set x i  z i 

75 20  1 55  20

1 1

c i x i 1 , i = n − 1, n − 2, …, 1;  xn = zn yi

x4 = z4 = 1



i = 3, x 3  z 3 



i = 2, x 2  z 2 



5  2

c 3 x 4 75 5  1    2 y3 20 20 7

c2 x 3 5 22 1   y2 7 7 3 cx 5 (1)  1  2 i = 1, x 1  z1  1 2   y1 3 3

MATLAB Solution: The following program (script file) uses the user-defined function x = Thomas

(a, d, b, c) function to solve the linear equations: >> a = [−1 2 5]; >> b = [0 2 1 1]; >> d = [3 −3 2 −1]; >> c = [5;5;10;1]; >> x = Thomas(a,d,b,c)

ANMF.CH02_4PP.indd 91

3/1/2023 12:49:49 PM

92 • Applied Numerical Methods Using MATLAB When the program is executed, the following result is displayed in the command window: x= 2.0000 1.0000 2.0000 1.0000 EXAMPLE 2.17 Solve the following set of tridiagonal set of algebraic equations using the Thomas method. x1 + 4x2 = 10 2x1 + 10x2 − 4x3 = 7 x2 + 8x3 − x4 = 6 x3 − 6x4 = 4 Solution: b1x1 + c1x2 = d1 a2x1 + b2x2 + c2x3 = d2 a3x2 + b3x3 + c3x4 = d3 a4x3 + b4x4 = d4 a2 = 2, a3 = 1, a4 = 1 b1 = 1, b2 = 10, b3 = 8, b4 = −6 c1 = 4, c2 = −4, c3 = −1 d1 = 10, d2 = 7, d3 = 6, d4 = 4 Step 1: Set y1 = b1, y i  b i 

ANMF.CH02_4PP.indd 92



y1 = 1



y 2  10 

a i c i 1 , y i 1

i = 2, 3, …, n

24 2 1

3/1/2023 12:49:54 PM

Linear System of Equations • 93



y3 

8  1(4)  8  2  10 ; 2



y4 

60  1 59  10 10

Step 2: Set z1 =

y 4  6 

1  (1)  10

d1 d i  a i z i 1 , zi  i = 2, 3, …, n b1 yi



z= 1

10 = 10 1



z2 

7  2.10 13  2 2



z3 

6  1(13 / 2) 6  13 / 2 25   10 10 20



z4 

4  1  25 / 20 55  118 59 / 10

Step 3: Set x11 = z11, x i  z i 

c i x i 1 , i = n − 1, n − 2, …, 1 yi

55  0.4661 118



x4  



x3 



x2  



x 1  10 

25 (1)(55 / 118)   1.2034 20 10 13 (4)1.203   4.0932 2 2 4(4.094)  26.3729 1

MATLAB Solution: The following program (script file) uses the user-defined function x = Thomas

(a, d, b, c) function to solve the linear equations: >> a = [4 -4 -1]; >> b = [0 2 1 1];

ANMF.CH02_4PP.indd 93

3/1/2023 12:50:25 PM

94 • Applied Numerical Methods Using MATLAB >> d = [1 10 8 -6]; >> c = [10;7;6;4]; >> x = Thomas(a,d,b,c) When the program is executed, the following result is displayed in the command window: x= 26.3729

−4.0932 1.2034 −0.4661

2.10 JACOBI’S ITERATION METHOD This method is also known as the method of simultaneous displacements. Consider the system of linear equations a11x1 + a12x2 + a13x3 = b1 a21x1 + a22x2 + a23x3 = b2 a31x1 + a32x2 + a33x3 = b3(2.49) Here, we assume that the coefficients a11, a22, and a33 are the largest coefficients in the respective equations so that |a11| > |a12| + |a13| |a22| > |a21| + |a23| |a33| > |a31| + |a32|(2.50) The Jacobi iteration method is applicable only if the conditions given in Equation (2.50) are satisfied. Now, we can write Equation (2.49) 1 x1 = (b − a x − a x ) a 11 1 12 2 13 3

x2 =

1 (b − a x − a x )(2.51) a 22 2 21 1 23 3



x3 =

1 (b − a x − a x ) a 33 3 31 1 32 2

Let the initial approximations be x10, x20, and x30 respectively. The following ­iterations are then carried out.

ANMF.CH02_4PP.indd 94

3/1/2023 12:50:32 PM

Linear System of Equations • 95

Iteration 1: The first improvements are found as

x 11 

1  b  a x 0  a13 x 03  a 11 1 12 2



x 21 

1  b  a 21 x10  a 23 x 03  a 22 2



x 31 

1  b  a 31 x10  a 32 x 02  (2.52) a 33 3

Iteration 2: The second improvements are obtained as 1 x 12 = (b − a x − a x ) a 11 1 12 21 13 31

x 22 =

1 (b − a x − a x ) a 22 2 21 11 23 31



x 32 =

1 (b − a x − a x )(2.53) a 33 3 31 11 32 21

The above iteration process is continued until the values of x1, x2, and x3 are found to a preassigned degree of accuracy. That is, the procedure is continued until the relative error between two consecutive vector norm is satisfactorily small. In the Jacobi method, it is a general practice to assume x10 = x20 = x30 = 0. The method can be extended to a system of n linear simultaneous equations in n unknowns. 2.10.1 MATLAB Program for the Jacobi Iteration Method function [x, k, diff] = Jacobi(A,b,x0,tol,kmax) % Jacobi iteration method to solve a system of linear equations % of the form Ax=b if nargin> b = [85;51;5];x0=[0 0 0]’; >> [x,iterations] = Jacobi(A,b,x0,1.0e−10)

The Jacobi iteration method has converged in 17 iterations.

ANMF.CH02_4PP.indd 99

3/1/2023 12:51:34 PM

100 • Applied Numerical Methods Using MATLAB When the program is executed, the following result is displayed in the command window: x= 5.0000 4.0000 1.0000 iterations = 17 EXAMPLE 2.19 Use the Jacobi iterative scheme to obtain the solutions of the system of equations correct to three decimal places. x + 2y + z = 0 3x + y − z = 0 x − y + 4z = 3 Solution: Rearrange the equations in such a way that all the diagonal terms are dominant. 3x + y − z = 0 x + 2y + z = 0 x − y + 4z = 3 Computing for x, y, and z we get x = (z − y)/3 y = (−x − z)/2 z = (3 + y − x)/4 The iterative equation can be written as (r+1)

x

(r)

(r)

= (z − y )/3

y(r+1) = (−x(r) − z(r))/2 z(r+1) = (3 − x(r) + y(r))/4

ANMF.CH02_4PP.indd 100

3/1/2023 12:51:34 PM

Linear System of Equations • 101

The initial vector is not specified in the problem. Hence, we choose x(0) = y(0) = z(0) = 1 Then, the first iteration gives x(1) = (z(0) − y(0))/3 = (1 − 1)/3 = 0 y(1) = (−x(0) − z(0))/2 = (−1 − 1)/2 = −1.0 z(1) = (3 − x(0) + y(0))/4 = (3 − 1 + 1)/4 = 0.750 similarly, second iteration yields x(2) = (z(1) − y(1))/3 = (0.75 + 1.0)/3 = 0.5833 y(2) = (−x(1) − z(1))/2 = (−0 − 0.75)/2 = −0.3750 z(2) = (3 − x(1) + y(1))/4 = (3 − 0 − 0)/4 = 0.500 Subsequent iterations result in the following:

x(3) = 0.29167

y(3) = −0.47917

z(3) = 0.51042



x(4) = 0.32986

y(4) = −0.40104

z(4) = 0.57862



x(5) = 0.32595

y(5) = −0.45334

z(5) = 0.56728



x(6) = 0.34021

y(6) = −0.44662

z(6) = 0.55329



x(7) = 0.33330

y(7) = −0.44675

z(7) = 0.55498



x(8) = 0.33391

y(8) = −0.44414

z(8) = 0.55498



x(9) = 0.33334

y(9) = −0.44445

z(9) = 0.55557

so to four decimal places the approximate solution

x = 0.3333

y = −0.4444

z = 0.5556

MATLAB Solution: The following program (script file) uses the user-defined [x, k, diff] = Jacobi (A,b,x0,tol,kmax) function to solve the linear equations: [x, k, diff] = Jacobi(A,b,x0,tol,kmax) >> A = [3 1 −1;1 2 1;1 −1 4]; >> b = [0;0;3]; >> x0 = [0 0 0]’; >> [x,iterations] = Jacobi(A,b,x0,1.0e-10)

ANMF.CH02_4PP.indd 101

3/1/2023 12:51:34 PM

102 • Applied Numerical Methods Using MATLAB The Jacobi iteration method has converged in 30 iterations. When the program is executed, the following result is displayed in the command window: x= 0.3333 −0.4444 0.5556 iterations = 30 EXAMPLE 2.20 Use Jacobi the iterative scheme to obtain the solution of the system of equations correct to two decimal places.



 5 2 1   x   4  1 4 2   y    3       1 2 4   z  17 

Solution: The Jacobi method is applicable only if the conditions given by Equation (2.50) are satisfied. Here |5| > |−2| + |1| or 5 > 3

|4| > |1| + |−1| or 4 > 3



|4| > |1| + |2| or 4 > 3

Clearly, the iterative approach will converse. Hence, writing the set of equations in the form of (2.51), we have



ANMF.CH02_4PP.indd 102

0.4 0.2   x  x  0.8   0        0 0.5   y  (E.1)  y   0.75   0.25  z    0  z k  k 1 4.25  0.225 0.5

3/1/2023 12:51:39 PM

Linear System of Equations • 103

 x  0  Assuming the initial approximation  y   0  and substituting into Equation z  0   0   (E.1) gives our first approximation to the solution. Hence 0.4 0.2  0.8   x  0.8   0        0.5  0.75  (E.2) 0  y   0.75   0.25  z  4.25  0.25 0.5 0  4.25 1  2   



The process is continued until successive values of each vector are very close in magnitude. Here, the eleven iterations obtained accurate to two decimal places are shown below in Table 2.1. TABLE 2.1 Variable

x y z

1

2

3

4

5

6

7

8

9

10

11

0.8 0.75 4.25

0.25 2.68 3.68

1.14 2.53 2.85

1.24 1.89 2.70

1.02 1.79 2.99

0.92 1.99 3.10

0.98 2.07 3.02

1.02 2.02 2.97

1.01 1.98 2.98

0.99 1.99 3.01

1 2 3

Hence, the solution is given by x = 1, y = 2, and z = 3. MATLAB Solution: The following program (script file) uses the user-defined [x, k, diff] = Jacobi (A,b,x0,tol,kmax) function to solve the linear equations: >> A = [5 −2 1;1 4 -2;1 2 4]; >> b = [4;3;17]; >> x0 = [0 0 0]’; >> [x,iterations] = Jacobi(A,b,x0,1.0e−10) The Jacobi iteration method has converged in 47 iterations. When the program is executed, the following result is displayed in the ­command window: x= 1.0000 2.0000 3.0000

ANMF.CH02_4PP.indd 103

3/1/2023 12:51:44 PM

104 • Applied Numerical Methods Using MATLAB iterations = 47

2.11 GAUSS-SEIDEL ITERATION METHOD The Gauss-Seidel method is applicable to predominantly diagonal systems. A predominantly diagonal system has large diagonal elements. The absolute value of the diagonal element in each case is larger than the sum of the absolute values of the other elements in that row of the matrix A. For such predominantly diagonal systems, the Gauss-Seidel method always converges to the correct solution, irrespective of the choice of the initial estimates. Since the most recent approximations of the variables are used while proceeding to the next step, the convergence of the Gauss-Seidel method is twice as fast as in Jacobi’s method. The Gauss-Seidel and Jacobi’s methods converge for any choice of the initial approximations, if in each equation of the system, the absolute value of the largest coefficient is greater than the sum of the absolute values of the remaining coefficients. In other words, n



| a ij |

| a i 1 j1

ii

|

 1 , i = 1, 2, 3, …, n

where the inequality holds in case of at least one equation. Convergence is assured in the Gauss-Seidel method if the matrix A is diagonally dominant and positive definite. If it is not in a diagonally dominant form, it should be connected to a diagonally dominant form by row exchanger, before starting the Gauss-Seidel iterative scheme. The Gauss-Seidel method is also an iterative solution procedure which is an improved version of Jacobi’s method. The method is also known as the method of successive approximations. Consider the system of linear simultaneous equations   a11x1 + a12x2 + a13x3 = b1   a21x1 + a22x2 + a23x3 = b2   a31x1 + a32x2 + a33x3 = b3(2.54)

ANMF.CH02_4PP.indd 104

3/1/2023 12:51:46 PM

Linear System of Equations • 105

If the absolute value of the largest coefficient in each equation is greater than the sum of the absolute values of all the remaining coefficients, then the Gauss-Seidel iteration method will converge. If this condition is not satisfied, then the Gauss-Seidel method is not applicable. Here, in Equation (2.54), we assume the coefficients a11, a22, and a33 are the largest coefficients. We can rewrite Equation (2.54) as

x1 =

1 (b − a x − a x ) a 11 1 12 2 13 3



x2 =

1 (b − a x − a x ) a 22 2 21 1 23 3



x3 =

1 (b − a x − a x )(2.55) a 33 3 31 1 32 2

Let the initial approximations be x10, x20, and x30 respectively. The following iterations are then carried out. Iteration 1: The first improvements of x1, x2, and x3 are obtained as

x 11 

1  b  a x  a13 x 03  a 11 1 12 2



x 21 

1  b  a 21 x11  a 23 x 03  a 22 2



x 31 

1  b3  a 31 x11  a 32 x 21  (2.56) a 33

Iteration 2: The second improvements of x1, x2, and x3 are obtained as

x 12 =

1 (b − a x − a x ) a 11 1 12 11 13 31



x 22 =

1 (b − a x − a x ) a 22 2 21 12 23 31



x 32 =

1 (b − a x − a x )(2.57) a 33 3 31 12 32 22

The above iteration process is continued until the values of x1, x2, and x3 are obtained to a preassigned or desired degree of accuracy. In general, the initial approximations are assumed as x10 = x20 = x30 = 0. The Gauss-Seidel method

ANMF.CH02_4PP.indd 105

3/1/2023 12:52:07 PM

106 • Applied Numerical Methods Using MATLAB generally converges for any initial values of x10, x20, x30. The convergence rate of the Gauss-Seidel method is found to be twice to that of the Jacobi method. Like the Jacobi method, the Gauss-Seidel method can also be extended to n linear simultaneous algebraic equations in n unknowns. 2.11.1 MATLAB Program for the Gauss Seidel Method function [Y,iter] = Gausseidel(A,r,yguess,tol) % Gausseidel will iteratively solve Ay=r n = length(r); Y = yguess; dy = ones(n,1); iter = 0; while norm(dy)/norm(Y) > tol for i = 1:n if abs(A(i,i))1000; error(‘Did not converge in 1000 steps’);end end

EXAMPLE 2.21 Solve the following equations by using the Gauss-Seidel method. 8x + 2y − 2z = 8 x − 8y + 3z = −4 2x + y + 9z = 12

ANMF.CH02_4PP.indd 106

3/1/2023 12:52:07 PM

Linear System of Equations • 107

Solution: In the above equations: |8| > |2| + |−2| |−8| > |1| + |3| |9| > |2| + |1| So, the conditions of convergence are satisfied and we can apply the Gauss-Seidel method. Then we rewrite the given equations as follows:

x1 

1 (d 1  b1 y 0  c1 z 0 ) a1



y1 

1 (d 2  a 2 x 1  c 2 z 0 ) b2



z1 

1 (d 3  a 3 x 1  b3 y 1 ) c3

Let the initial approximations be:

x0 = y0 = z0 = 0

Iteration 1:

x= 1

d1 8 = = 1.0 a1 8



y1 

1 1 (d 2  a 2 x 1 )  (4  1x1.0)  0.625 b2 8



z1 

1 1 (d 3  a 3 x 1  b3 y 1 )  (12  2.  2  1.0  1x 0.625)  1.042 c3 9

Iteration 2:

ANMF.CH02_4PP.indd 107



x2 

1 1 (d 1  b1 y 1  c1 z1 )  (8  2  0.625  (2)  1.042)  1.104 a1 8



y2 

1 1 (d 2  a 2 x 2  c 2 z1 )  (4  1  1.104  3  1.042)  1.029 b2 8

3/1/2023 12:52:22 PM

108 • Applied Numerical Methods Using MATLAB



z2 

1 1 (d 3  a 3 x 2  b3 y 2 )  (12  2  1.104  1  1.029)  0.974 c3 9

Iteration 3:

x3 

1 1 (d 1  b1 y 2  c1 z 2 )  (8  2  1.029  (2)  0.974)  0.986 a1 8



y3 

1 1 (d 2  a 2 x 3  c 2 z 2 )  (4  1  0.986  3  0.974)  0.989 b2 8



z3 

1 1 (d 3  a 3 x 3  b3 y 3 )  (12  2  0.986  1  0.989)  1.004 c3 9

Iteration 4:

1 x 4  (8  2  0.989  (2)  1.004)  1.004 8



y4 



1 z 4  (12  2  1.004  1  1.002)  0.999 9

1 (4  1  1.004  3  1.004)  1.002 8

Iteration 5:

1 x 5  (8  2  1.002  (2)  0.999)  0.999 8



y5 



1 z 5  (12  2  0.999  1  1.0)  1.0 9

1 (4  1  0.999  3  0.999)  1.0 8

Iteration 6:

ANMF.CH02_4PP.indd 108

1 x 6  (8  2  1  2  1)  1.0 8

3/1/2023 12:52:53 PM

Linear System of Equations • 109

1 (4  1  1.0  3  1.0)  1.0 8



y6 



1 z 6  (12  2  1.0  1  1.0)  1.0 9

MATLAB Solution: The following program (script file) uses the user-defined function [Y,iter] =

Gausseidel(A,r,yguess,tol)

function to solve the linear equations: >> A=[8 2 -2;1 -8 3;2 1 9]; >> b=[8;-4;12]; >> tol=1.0e-9; >> xguess=[0 0 0]’; >> [x,Iterations]=Gaussseidel(A,b,xguess,tol) When the program is executed, the following result is displayed in the ­command window: x= 1.0000 1.0000 1.0000 Iterations = 16 EXAMPLE 2.22 Using the Gauss-Seidel method, solve the system of equations correct to three decimal places. x + 2y + z = 0 3x + y − z = 0 x − y + 4z = 3

ANMF.CH02_4PP.indd 109

3/1/2023 12:52:57 PM

110 • Applied Numerical Methods Using MATLAB Solution: Rearranging the given equations to give dominant diagonal elements, we obtain 3x + y − z = 0 x + 2y + z = 0 x − y + 4z = 3

(E.1)

,, Equation (E.1) can be rewritten as x = (z − y)/3 y = −(x + z)/2 z = (3 + x + y)/2

(E.2)

Writing Equation (E.2) in the form of the Gauss-Seidel iterative scheme, we get x(r+1) = (z(r) − y(r))/3 y(r+1) = −(x(r+1) − z(r))/2 z(r+1) = (3 − x(r+1) + y(r+1))/4 We start with the initial value x(0) = y(0) = z(0) = 1 The iteration scheme gives x(1) = (z(0) − y(0))/3 = (1 − 1)/3 = 0 y(1) = (−x(1) − z(0))/2 = (0 − 1)/2 = −0.5 z(1) = (3 − x(1) + y(1))/4 = (3 − 0 − 0)/4 = 0.625 The second iteration gives x(2) = (z(1) − y(1))/3 = (0.625 + 0.5)/3 = 0.375 y(2) = (−x(2) − z(1))/2 = (−0.375 − 0.625)/2 = −0.50 z(2) = (3 − x(2) + y(2))/4 = (3 − 0.375 − 0.5)/4 = 0.53125

ANMF.CH02_4PP.indd 110

3/1/2023 12:52:57 PM

Linear System of Equations • 111

Subsequent iterations result in

x(3) = 0.34375

y(3) = −0.4375

z(3) = 0.55469



x(4) = 0.33075

y(4) = −0.44271

z(4) = 0.55664



x(5) = 0.33312

y(5) = −0.44488

z(5) = 0.5555



x(6) = 0.33336

y(6) = −0.44448

z(6) = 0.55552

Hence, the approximate solution is as follows: x = 0.3333



y = −0.4444

z = 0.5555

MATLAB Solution: The following program (script file) uses the user-defined function [Y,iter] =

Gausseidel(A,r,yguess,tol)

function to solve the linear equations: >> A = [3 1 −1;1 2 1;1 −1 4]; >> b = [0;0;3]; >> tol = 1.0e-9; >> xguess = [0 0 0]’; >> [x,Iterations] = Gaussseidel(A,b,xguess,tol) When the program is executed, the following result is displayed in the command window: x= 0.3333 −0.4444 0.5556 Iterations = 15 EXAMPLE 2.23 Solve the following equations by the Gauss-Seidel method. 4x − y + z = 12 −x + 4y − 2z = −1 x − 2y + 4z = 5

ANMF.CH02_4PP.indd 111

3/1/2023 12:52:57 PM

112 • Applied Numerical Methods Using MATLAB Solution: The iteration formula is

1 xi  A ii

Hence, x =

n    b  i  A ij x j  , i = 1, 2, …, n j 1   j i

1 (12 + y − z) 4



y=

1 (−1 + x + 2z) 4



z=

1 (5 − x + 2y) 4

Choosing the starting values x = y = z = 0, we have the first iteration

x=

1 (12 + 0 − 0) = 3 4



y=

1 [−1 + 3 + 2(0)] = 0.5 4



z=

1 (5 − 3 + 2(0.5)] = 0.75 4

The second iteration gives

x=

1 (12 + 0.5 − 0.75) = 2.9375 4



y=

1 [−1 + 2.9375 + 2(0.75)] = 0.8594 4



z=

1 (5 − 2.9375 + 2(0.8594)] = 0.9453 4

The third iteration yields

x=

1 [12 + 0.8594 − 0.9453] = 2.9785 4



y=

1 [−1 + 2.9785 + 2(0.9453)] = 0.9673 4

ANMF.CH02_4PP.indd 112

3/1/2023 12:53:02 PM

Linear System of Equations • 113

z=



1 (5 − 2.9785 + 2(0.9673)] = 0.9890 4

After five more iterations, we obtain the final values for x, y, and z as x = 3, y = 1 and z = 1. MATLAB Solution: The following program (script file) uses the user-defined function [Y,iter] =

Gausseidel(A,r,yguess,tol)

function to solve the linear equations: >> A=[4 -1 1;−1 4 −2;1 −2 4]; >> b=[12;−1;5]; >> tol=1.0e-9; >> xguess=[0 0 0]’; >> [x,Iterations]=Gaussseidel(A,b,xguess,tol) When the program is executed, the following result is displayed in the command window: x= 3.0000 1.0000 1.0000 Iterations = 14

2.12 SYMMETRIC MATRIX EIGENVALUE PROBLEMS The standard form of the matrix eigenvalue problem is Ax = λx(2.57) where A is a given n × n matrix. The objective here is to find the scalar λ and the vector x. Equation (2.57) can be rewritten as (A = λI)x = 0

ANMF.CH02_4PP.indd 113



(2.58)

3/1/2023 12:53:02 PM

114 • Applied Numerical Methods Using MATLAB A nontrivial solution exists only if A  I  0 (2.59) Expansion of Equation (2.59) gives the polynomial equation called the characteristic equation. a 1  n  a 2  n 1  ...  a n   a n 1  0



which has the roots λi, i = 1, 2, …, n, called the eigenvalues of the matrix A. The solutions of xi of (A − λiI)x = 0 are known as eigenvectors. 2.12.1 The Jacobi Method Applying the transformation x = Px* in Equation (2.57) where P is a nonsingular matrix, we can write P−1APx* = λP−1Px* or A*x* = λx*

(2.60)

−1

where A* = P AP. Matrices that have the same eigenvalues are deemed to be similar and the transformation between them is called a similarity transformation. Diagonalizing A*, Equation (2.60) can be written as



 A*11    0 0   x*1  0       A*22    0   x*2  0   0  (2.61)                   A*nn     x*n  0  0  0

Solving Equation (2.61), we obtain

1  A*11 ,  2  A*22 , ...  n  A*nn (2.62)



1  0  x*1        0 

or x*   x*1

ANMF.CH02_4PP.indd 114

0  1  x*2        0 



0  0  x*n        1 

x*2  x*n   I

3/1/2023 12:53:19 PM

Linear System of Equations • 115

Therefore, the eigenvector matrix of A is X = PX* = PI = P The transformation matrix P is the eigenvector matrix of A and the eigenvalues of A are the diagonal terms of A*. Jacobi Rotation Matrix: Consider the special transformation in the plane rotation x = Rx*

where

 1  0  0 R  0  0 0  0 0 

0 1 0 0 0 0 0 0

k 0 0 c 0 0 s 0 0

0 0 0 1 0 0 0 0

0 0 0 0 1 0 0 0

l 0 0 s 0 0 c 0 0

0 0 0 0 0 0 1 0

 0  0 k  0 0  (2.63) 0 0 l  0 1 

R is called the Jacobi rotation matrix and R−1 = RT. Also

A* = R−1AR = RTAR(2.64)

The matrix A* has the same eigenvalues as the original matrix A and it is also symmetric. The relationships in Equation (2.64) changes only the rows or columns k and l of A. The relationship for these changes are

ANMF.CH02_4PP.indd 115



A*kk = c2Akk + s2All − 2cs Akl



A*kl = A*lk = (c2 − s2)Akl + cs(Akk − All)(2.64a)



A*ki = A*ik = cAki − sAli, i ≠ k, i ≠ l



A*li = A*il = cAli + sAki, i ≠ k, i ≠ l

A*ll = c2All + s2Akk − 2cs Akl

3/1/2023 12:53:36 PM

116 • Applied Numerical Methods Using MATLAB Jacobi Diagonalization: From Equation (2.64a), we can have A*kl = 0 if (c2 − s2)Akl + cs(Akk − All) = 0

(2.64b) 1 Using the trigonometric identities c2 − s2 = cos 2θ and cs = sin 2θ, we have 2 from Equation (2.64b). tan 2 

2 A kl (2.64c) A kk  A ll

Equation (2.64c) can be solved for θ noting that c = cos θ and s = sin θ. Let

  cot 2  

A kk  A ll (2.64d) 2 A kl

Now using the trigonometric identity tan 2 

2t 1  t2

where t = tan θ, we can write Equation (2.64c) as t2 + 2ϕt − 1 = 0

(2.64e)

The roots of Equation (2.61e) are 2 t      1

It has been discovered that the root |t| ≤ 1 corresponding to |θ| ≤ 45º leads to the more stable transformation. Hence, we select the + sign if ϕ > 0 and the − sin if |ϕ| ≤ 0. That is to say 2 t  sgn()   |  |    1  (2.64f)

Solving Equation (2.64f) gives t 

ANMF.CH02_4PP.indd 116

sgn() |  |  2  1

(2.64g)

3/1/2023 12:53:56 PM

Linear System of Equations • 117

For large ϕ, Equation (2.64g) can be written as

t

1 (2.64h) 2

Now c and s can be written as

c

1 1  t2

and s = tc

(2.64i)

Solving Equation (2.64b) for All, we get

A ll  A kk  A kl

c 2  s2 (2.64j) cs

Hence, Equation (2.64) can be expressed as

A*kk = Akk − tAkl



A*ll = All + tAkl



A*kl = A*lk = 0



A = A = Aki − s(Ali + τAki), i ≠ k, i ≠ l



A*li = A*il = Ali + s(Aki − τAli) i ≠ k, i ≠ l

where



* ki

(2.64k)

* ik

s (2.64l) 1c

The transformation matrix P is initialized to the identity matrix in the beginning of the Jacobi diagonalization procedure. Then, each Jacobi rotation changes this matrix from P to P* = PR. The changes in the elements of P can be shown to be

Pik*  Pik  s(Pil  Pik )



Pi*l  Pil  s(Pik  Pil ) (2.64m)

when the matrix A is large, then it is convenient to sweep through it by rows or columns and annihilate every element above some predetermined threshold value. Then the threshold is lowered and the progress is repeated.

ANMF.CH02_4PP.indd 117

3/1/2023 12:54:09 PM

118 • Applied Numerical Methods Using MATLAB Transformation to Standard Form: Consider the eigenvalue problem of the form Ax = λBx(2.65) where A and B are symmetric n × n matrices and in addition we assume that B is positive definite. In order to use Jacobi diagonalization, we must first transform Equation (2.65) into the standard form Ax = λx. We can apply Choleski’s decomposition B = LLT, where L is a lower-triangular matrix. Now introducing x  (L1 )T z (2.66) and substituting Equation (2.66) in Equation (2.65), we obtain A(L−1)Tz = λLLT(L−1)Tz(2.67) Since L−1L = LT(L−1)T = I, Equation (2.67) reduces to the standard form Hz = λz(2.68) where H = L−1A(L−1)T(2.69) The matrix H is also a symmetric matrix like the matrix A. Summarizing, the general procedure for solving eigenvalue problem of the form Ax = λBx is given below: 1. Use Choleski’s decomposition B = LLT to compute L. 2. Compute L−1. 3. Compute H from Equation (2.69). 4. Use the Jacobi method to solve the standard eigenvalue problem Hz = λz. 5. Obtain the eigenvectors of the original problem from Equation (2.66). That is, x = (L−1)Tz. It should be noted here that an important case is when B is a diagonal matrix: 1 0  B     0

ANMF.CH02_4PP.indd 118

0 2  0

  

0 0    (2.70)  n 

3/1/2023 12:54:13 PM

Linear System of Equations • 119

11 / 2  0 where   L     0
= Amax Amax = abs(A(i,j)); k = i; L = j; end end end function [A,P] = rotate(A,P,k,L) % Zeros of A(k,L) % Transformation matrix P n = size(A,1); diff = A(L,L) - A(k,k); if abs(A(k,L)) < abs(diff)*1.0e-36 t = A(k,L); else phi = diff/(2*A(k,L)); t = 1/(abs(phi) + sqrt(phi^2 + 1)); if phi < 0; t = -t; end; end

ANMF.CH02_4PP.indd 121

3/1/2023 12:54:44 PM

122 • Applied Numerical Methods Using MATLAB c = 1/sqrt(t^2 + 1); s = t*c; tau = s/(1 + c); temp = A(k,L); A(k,L) = 0; A(k,k) = A(k,k) - t*temp; A(L,L) = A(L,L) + t*temp; for i = 1:k-1 temp = A(i,k); A(i,k) = temp -s*(A(i,L) + tau*temp); A(i,L) = A(i,L) + s*(temp - tau*A(i,L)); end for i = k+1:L-1 temp = A(k,i); A(k,i) = temp - s*(A(i,L) + tau*A(k,i)); A(i,L) = A(i,L) + s*(temp - tau*A(i,L)); end for i = L+1:n temp = A(k,i);

A(k,i) = temp - s*(A(L,i) + tau*temp); A(L,i) = A(L,i) + s*(temp - tau*A(L,i)); end % Update the tranformation matrix for i = 1:n temp = P(i,k); P(i,k) = temp - s*(P(i,L) + tau*P(i,k)); P(i,L) = P(i,L) + s*(temp - tau*P(i,L)); end

ANMF.CH02_4PP.indd 122

3/1/2023 12:54:44 PM

Linear System of Equations • 123

function A = Householdereigen(A) % Housholder method of reduction of A to tridiagonal form A = [c\d\c] % Extracting c and d with d = diag(A), c = diag(A,1) % Usage: % A = Householdereigen(A) n = size(A,1); for k = 1:n-2 u = A(k+1:n,k); umagn = sqrt(dot(u,u)); if u(1) < 0; umagn = -umagn; end u(1) = u(1) + umagn; A(k+1:n,k) = u; H = dot(u,u)/2; v = A(k+1:n,k+1:n)*u/H; g = dot(u,v)/(2*H); v = v - g*u; A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - v*u′ - u*v′; A(k,k+1) = -umagn; end

function P = Householderp(A) % Finds the transformation matrix P once % the Householder reduction of A is obtained % Usage: % P = Householderp(A) n = size(A,1); P = eye(n);

ANMF.CH02_4PP.indd 123

3/1/2023 12:54:44 PM

124 • Applied Numerical Methods Using MATLAB for k = 1:n−2 u = A(k+1:n,k); H = dot(u,u)/2; v = P(1:n,k+1:n)*u/H; P(1:n,k+1:n) = P(1:n,k+1:n) - v*u′; end function p = Strumsequence(c,d,lambda) % Returns Sturm sequence p associated with % the tridiagonal matrix A = [c\d\c] and lambda. % Usage: % p = Strumsequence(c,d,lambda),where |A − lambda*I| = p(n) n = length(d) + 1; p = ones(n,1); p(2) = d(1)−lambda; for i = 2:n−1 p(i+1) = (d(i)−lambda)*p(i)−(c(i−1)^2 )*p(i−1); end function numevals = Countlambda(c,d,lambda) % Counts eigenvalues smaller than lambda of matrix % A = [c\d\c]. Uses the Sturm sequence. % Usage: % numevals = Countlambda(c,d,lambda). p = Strumsequence(c,d,lambda); n = length(p); oldsign = 1; numevals = 0; for i = 2:n psign = sign(p(i));

ANMF.CH02_4PP.indd 124

3/1/2023 12:54:44 PM

Linear System of Equations • 125

if psign == 0; psign = -oldsign; end if psign*oldsign < 0 numevals = num_eVals + 1; end oldsign = psign; end function [evalmin,evalmax]= Gerschgorin(c,d) % evaluates the global bounds on the eigenvalues % of A=[c\d\c] % Usage: % [evalmin,evalmax]= Gerschgorin(c,d) n = length(d); evalmin = d(1) - abs(c(1)); evalmax = d(1) + abs(c(1)); for i = 2:n-1 eval = d(i) - abs(c(i)) - abs(c(i-1)); if eval < evalmin; evalmin = eval; end eval = d(i) + abs(c(i)) + abs(c(i-1)); if eval > evalmax; evalmax = eval; end end eval = d(n) − abs(c(n−1)); if eval < evalmin; evalmin = eval; end eval = d(n) + abs(c(n-1)); if eval > evalmax; evalmax = eval; end

ANMF.CH02_4PP.indd 125

3/1/2023 12:54:44 PM

126 • Applied Numerical Methods Using MATLAB function evals = Eigenbrackets(C,D,m) % Computes the smallest m eigenvalues of A = [C\D\C]. % Usage: % evals = Eigenbrackets(C,D,m) % C and D should be delared “global” in the calling program evals = zeros(m,1); % The eigenvalues are bracketed here r = Eigenbrackets(C,D,m); for i=1:m % Use Brent’s method to solve |A − eval*I| for eval evals(i) = Brent(@func,r(i),r(i+1)); end function f = func(eval); % Returns |A - eval*I|; The last element of Sturm sequence global C D p = Strumsequence(C,D,eval); f = p(length(p)); function root = Brent(func,a,b,tol) % Finds a root of f(x) = 0 by combining quadratic % interpolation with Brent’s method % Usage: % root = Brent(func,a,b,tol) % INPUT: % func = Handle of function that returns f(x) % a,b = Limits of the interval containing the root % tol = Error tolerance [default value is 1.0e6*eps] % OUTPUT:

ANMF.CH02_4PP.indd 126

3/1/2023 12:54:44 PM

Linear System of Equations • 127

% root = Zero of f(x) (root = NaN when convergence fails) if nargin < 4; tol =1.0e6*eps; end %Use bisection method x1 = a; f1 = feval(func,x1); if f1 == 0; root = x1; return; end x2 = b; f2 = feval(func,x2); if f2 == 0; root = x2; return; end if f1*f2 > 0.0 error(‘Failed to bracket the root in (a,b)’) end x3 = 0.5*(a + b); % Iterative loop starts here for i = 1:30 f3 = feval(func,x3); if abs(f3) < tol root = x3; return end if f1*f3 < 0.0; b = x3; else; a = x3; end if (b − a) < tol*max(abs(b),1.0) root = 0.5*(a + b);

ANMF.CH02_4PP.indd 127

3/1/2023 12:54:44 PM

128 • Applied Numerical Methods Using MATLAB return end % Quadratic interpolation is tried here den = (f2 − f1)*(f3 - f1)*(f2 − f3); num = x3*(f1 − f2)*(f2 − f3 + f1)... + f2*x1*(f2 − f3) + f1*x2*(f3 − f1); % Check for division by zero if den == 0; dx = b − a; else; dx = f3*num/den; end x = x3 + dx; if (b − x)*(x − a) < 0.0 dx = 0.5*(b − a); x = a + dx; end if x < x3 x2 = x3; f2 = f3; else x1 = x3; f1 = f3; end x3 = x; end root = NaN; function [eval,evec] = Invpowerthree(c,d,s,maxiter,tol) % Finds the eigenvalue of A =[c\d\c] closest to s and % the associated eigenvector by using the inverse power method % Usage: % [eval,evec] = Invpowerthree(c,d,s,maxiter,tol)

ANMF.CH02_4PP.indd 128

3/1/2023 12:54:44 PM

Linear System of Equations • 129

% maxiter = Limit on number of iterations (default number is 50) % tol = Error tolerance (default value is 1.0e-6) if nargin < 5; tol = 1.0e-6; end if nargin < 4; maxiter = 50; end n = length(d); e = c; d = d - s; [c,d,e] = LUdec3(c,d,e); x = rand(n,1); % Normalization of x xmag = sqrt(dot(x,x)); x = x/xmag; for i = 1:maxiter xold = x; x = LUsol3(c,d,e,x); xmag = sqrt(dot(x,x)); x = x/xmag; % Normalization of x xSign = sign(dot(xold,x)); x = x*xSign; % Check for convergence if sqrt(dot(xold - x,xold - x)) < tol eval = s + xSign/xmag; evec = x; return end end error(‘Excessive iterations’)

ANMF.CH02_4PP.indd 129

3/1/2023 12:54:44 PM

130 • Applied Numerical Methods Using MATLAB function [evals,evecs] = Sorteigenvalues(evals,evecs) % Sorts eigenvalues and eigenvectors in an ascending % order of eigenvalues. % Usage: % [evals,evecs] = Sorteigenvalues(evals,evecs) n = length(evals); for i = 1:n−1 index = i; val = evals(i); for j = i+1:n if evals(j) < val index = j; val = evals(j); end end if index ~= i evals = Swaprows(evals,i,index); evecs = Swapcols(evecs,i,index); end end function v = Swapcols(v,i,j) % Swap the columns i and j of the given vector or matrix v temp = v(:,i); v(:,i) = v(:,j); v(:,j) = temp; function v = Swaprows(v,i,j) % Swap the rows i and j of the given vector or matrix v % Usage: v = Swaprows(v,i,j)

ANMF.CH02_4PP.indd 130

3/1/2023 12:54:44 PM

Linear System of Equations • 131

temp = v(i,:); v(i,:) = v(j,:); v(j,:) = temp; function [H,T] = Standform(A,B) % Transforms A*x = lambda*B*x to H*z = lambda*z % and finds the transformation matrix T in x = T*z % Usage: % [H,T] = Standform(A,B) n = size(A,1); % chol is a MATLAB built-in function L = chol(B); Linv = invert(L); H = Linv*(A*Linv′); T = Linv′; function Linv = invert(L) % Inverts the lower triangular matrix L n = size(L,1); for j = 1:n-1 L(j,j) = 1/L(j,j); for i = j+1:n L(i,j) = -dot(L(i,j:i-1), L(j:i-1,j)/L(i,i)); end end L(n,n) = 1/L(n,n); Linv = L; function [eval,evec] = Invpower(A,s,maxiter,tol) % Inverse power method for finding the eigenvalues of A % and the correstponding eigenvectors % Usage: % [eval,evec] = Invpower(A,s,maxiter,tol)

ANMF.CH02_4PP.indd 131

3/1/2023 12:54:44 PM

132 • Applied Numerical Methods Using MATLAB % maxiter = Limit on number of iterations (default is 60) % tol = Error tolerance (default value is 1.0e-6) if nargin < 4; tol = 1.0e-6; end if nargin < 3; maxiter = 60; end n = size(A,1); %A* = A - sI A = A - eye(n)*s; % Decomposition of A* A = LUdec(A); x = rand(n,1); % Normalization of x xmag = sqrt(dot(x,x)); x = x/xmag; for i = 1:maxiter xold = x; % Solve A*x = xold x = LUsol(A,x); % Normalization of x xmag = sqrt(dot(x,x)); x = x/xmag; xsign = sign(dot(xold,x)); x = x*xsign; % Check for convergence if sqrt(dot(xold − x,xold − x)) < tol eval = s + xsign/xmag; evec = x; return end end error(‘Excessive iterations’)

ANMF.CH02_4PP.indd 132

3/1/2023 12:54:44 PM

Linear System of Equations • 133

function x = LUsol(A,b) % Solves L*U*b = x, where A contains L and U % A has the form [L\U] % Usage: % x = LUsol(A,b) if size(b,2) > 1; b = b′; end n = length(b); for k = 2:n b(k) = b(k) - A(k,1:k-1)*b(1:k-1); end for k = n:-1:1 b(k) = (b(k) - A(k,k+1:n)*b(k+1:n))/A(k,k); end x = b; 2.12.3  Householder Reduction to Tridiagonal Form Householder’s method or procedure uses the Householder’s matrix given by uu T Q  I  (2.74) H where u is vector and = H

1 T 1 = u u | u |2 (2.75) 2 2

Noting that Q is symmetric (QT = Q), we can write  uu T   uu T Q T Q  QQ   I  I  H  H 

 uu T u(u T u)u T uu T u(2H)u T I I 2 2  I       H H2 H H2 

(2.76)

ANMF.CH02_4PP.indd 133

3/1/2023 12:54:51 PM

134 • Applied Numerical Methods Using MATLAB Equation (2.76) shows that Q is also orthogonal. If we let x be an arbitrary vector and then the transformation matrix is Qx. Let u = x + ke1(2.77) where

k = ±|x|

e1 = [1 0 0 …. 0]T we have  u(x  ke1 )T   uu T  u(u T x  ke1T x) u(x 2  kx 1 ) Qx   I  x  I  x x x        H H H  H    (2.78) Since 2H  (x  ke1 )T (x  ke1 )  | x |2  k(x T e1  e1T x)  k 2 e1T e  k 2  2 kx 1  k 2  2(k 2  kx 1 ) Hence Qx = x − u = −ke1 = [k 0 0 …. 0]T(2.78a) Equation (2.78a) shows that the transformation eliminates all elements of x except the first one. 2.12.3.1  Householder Reduction of a Symmetric Matrix Applying the following transformation to a symmetric n × n matrix A, we get

1 0 T   A11 P1 A    0 Q   x

x T   A11  A   Qx

xT   (2.79) QA  

Here x represents the first column of A with the first element omitted, and A′ is simply A with its first row and column removed. The matrix Q of dimensions (n − 1) × (n − 1) is constructed using Equations (9.36)−(9.38). Referring to Equation (9.39), we see that the transformation reduces the first column of A to  A11    k A  11    Qx    0  (2.79a)    M   0   

ANMF.CH02_4PP.indd 134

3/1/2023 12:55:11 PM

Linear System of Equations • 135

The transformation  A11 (Qx)T  A  P AP   (2.79b)  1 1  Qx QAQ  thus tridiagonalizes the first row as well as the first column of A. Here is a diagram of the transformation for a 4 × 4 matrix. 1 0 0 0 0 0 Q 0

A11 A 21 A 31 A 41

A12

A13

A14

A′

1 0 0 0 A11 0 k = 0 Q 0 0 0

k

0

0

QA Q

The second row and column of A are reduced next by applying the transformation to the 3 × 3 lower-right portion of the matrix. This transformation can be expressed as A ← P2AP2, where now I2 P2   0

0T   (2.79c) Q

In Equation (2.79c), I2 is a 2 × 2 identity matrix and Q is a (n − 2) × (n − 2) matrix constructed by choosing for x the bottom n − 2 elements of the second column of A. It takes a total of n − 2 transformations with I i Pi   0

0T   , Q

i = 1, 2, …., n − 2

to obtain the tridiagonal form. It is not advisable to form Pi and then carry out the matrix multiplication PiAPi. We note that

 uu T  A u T u  A   vu T A Q  A   I    A  H H  

where

ANMF.CH02_4PP.indd 135

v

A u (2.79d) H

3/1/2023 12:55:41 PM

136 • Applied Numerical Methods Using MATLAB

 uu T Therefore, QAQ   I  H 

 A  vu T 

 uu T T T   ( A vu ) A vu (A  vu T )      H 

u(u T A) u(u T v)u T   A  vu T  uv T  2guu T H H

uT v (2.79e) 2H Letting w = v − gu(2.79f) where g =

It can be easily verified that the transformation can be written as QA′Q = A′ − wuT − uwT(2.79g) which gives us the following computational procedure which is to be carried out with i = 1, 2, …., n − 2: 1. Let A′ be the (n − i) × (n − i) lower right-hand portion of A. 2. Let x   A i 1,i

T

A i  2,i  A n ,i  (the column of length n − i just to the

left of A′). 3. Compute |x|. Let k = |x| if x1 > 0 and k = −|x| if x1 < 0 (this choice of sign minimizes the roundoff error). T 4. Let u  k  x 1 x 2 x 3  x n  i  . 5. Compute H = |u|2/2. 6. Compute v = A′u/H. 7. Compute g = uTv/(2H). 8. Compute w = v − gu. 9. Compute the transformation A ← A′ − wTu − uTw. 10. Set Ai,i+1 = Ai+1,i = −k. 2.12.3.2  Accumulated Transformation Matrix Since we used similarity transformations, the eigenvalues of the tridiagonal matrix are the same as those of the original matrix. However, to determine the eigenvectors X of original A we use the following transformation

ANMF.CH02_4PP.indd 136

3/1/2023 12:55:55 PM

Linear System of Equations • 137

X = PXtridiag where P is the accumulation of the individual transformations: P = P1P2 …. Pn−2 We build up the accumulated transformation matrix by initializing P to a n × n identity matrix and then applying the transformation

 P11 P  PPi   P21

P12  I i  P22   0

0 T   P11  Q  P12

P21 Q  (2.80) P22 Q 

with i = 1, 2, …., n − 2. It can be seen that each multiplication affects only the rightmost n − i columns of P (since the first row of P12 contains only zeroes, it can also be omitted in the multiplication). Using the notation P12  P  P22   P12 we have  P22

Q  uu T  Pu T  PQ  P  I   P  u  P  yu T (2.81)   Q H  H 

y

Pu (2.82) H

where

The procedure for carrying out the matrix multiplication in Equation (b) is • Retrieve u (in our triangularization procedure the u’s are stored in the columns of the lower triangular portion of A). • Compute H = |u|2/2. • Compute y = P′u/H. • Compute the transformation P′ ← P′ − yuT. 2.12.4  Gerschgorin’s Circle Theorem Gerschgorin’s circle theorem identifies the region where the eigenvalues of the given matrix are likely to occur. The eigenvalues are readily given by the diagonal elements when the matrix is diagonal. If matrix is diagonally dominant, then

n

aii   aij

for i = 1, 2, 3, …., n

(2.83)

j 1 j 1

ANMF.CH02_4PP.indd 137

3/1/2023 12:56:10 PM

138 • Applied Numerical Methods Using MATLAB and therefore it is reasonable to assume that the eigenvalues will not be much different from the diagonal elements. If {Y} is an eigenvector associated with an eigenvalue λ, then this eigenvector can also be expressed in the form {X}  Hence

X



{Y} (2.84) Y 1

and Ax − lx = 0 This can also be written as n

 a ij x j  x j i = 1, 2, …., n j 1

Since X 1, then



(2.85)

th  1 , the largest element in x is 1. If the k element in x is equal to

n

 a kj x j  x k j 1

n

or (a kk  )x k   (a kj x j ) (2.85a) j 1 j k

Because xk = 1 and |xj| ≤ 1 for j = 1, 2, …, k − 1, k + 1, …., n, we can write n

a kk     a kj (2.85b) j 1 j k

The above equation states that the eigenvalue λ cannot differ from the diagonal element akk by an amount more than the sum of the nondiagonal elements n

in the same row,

ANMF.CH02_4PP.indd 138

a j 1 j k

kj

.

3/1/2023 12:56:24 PM

Linear System of Equations • 139

Gerschgorin’s theorem is stated below: The eigenvalues of an n × n matrix A are contained within a region R, which is made up of the union of all the circles Rk in the complex plane whose centers n

are given by akk and the corresponding radii given by

a j 1 j k

kj

.

Gerschgorin’s theorem is quite useful in finding the global bounds on the eigenvalues of an n × n matrix A. For a symmetric matrix, if λ is an eigenvalue of A, then

ai − ri ≤ λ ≤ ai + ri, i = 1, 2, …., n n

where ai = Aii and ri   A ij (2.85c) j 1 j k

Hence, it follows that the global bounds on the eigenvalues are

 min  min(a i  ri )



 max  max(a i  ri ) (2.85d)

i

i

The Sturm sequence along with the Gerschgorin’s theorem are useful in bracketing each eigenvalue of a symmetric tridiagonal matrix. 2.12.5  Sturm Sequence Sturm sequence property will make it relatively easy to bracket the eigenvalues of a tridiagonal matrix. Consider a symmetric tridiagonal matrix. Its characteristic polynomial can be computed with 3(n − 1) multiplications as described below: 0 0  c1 d1    c 0  d2   c2  1  0  c2 d3   c3 Pn     A  I   0 c3 d4     0        0  0 c n 1  0

ANMF.CH02_4PP.indd 139



P0 ()  1



P1 ()  d 1   Pi ()  (d i  )Pi 1 ()  c 2i 1 Pi  2 (),

0  0   0   0  (2.86)    d n   

i  2, 3,..., n

3/1/2023 12:56:36 PM

140 • Applied Numerical Methods Using MATLAB The polynomials P0(λ), P1(λ), …, Pn(λ) form a Sturm sequence. The number of sign changes in the sequence P0(a), P1(a), …, Pn(a) is equal to the number of roots of Pn(λ) that are smaller than a. If a member Pi(a) of the sequence is zero, its sign is to be taken opposite to that of Pi−1(a). 2.12.6  QR Method In the preceding section, we described the Jacobi method which is a reliable method but it requires a lot of computation time and is valid only for real symmetric matrices. However, the QR method is numerically extremely stable and is applicable to a general matrix. This method also provides a basis for developing a general purpose procedure for determining the eigenvalues and eigenvectors. The method utilizes the fundamental property that a real matrix can be ­written as  A   QU (2.87) where [Q] is orthogonal and [U] is upper triangular. This is contrary to the decomposition in the form [L][U] where [L] is the unit lower matrix and [U] is the upper triangular matrix. The property can be proved as follows. As in Jacobi’s method we introduce here the rotations matrix and denote it by [R.(p, q, θ)] to indicate the rows that contain the nonzero, off-diagonal ­elements. Note that [R] is orthogonal.



1   1     p cos  sin    1   R  p, q,       (2.88) 1   cos   sin   q  1    p q 1 

If any vector {x} is multiplied by [R.(p, q, θ)] such that y = [R]{x} then

y p  rpp x p  rpq x q  cos  x p  sin  x q

and

Yq  rqp x p  rqq x q   sin  x p  cos  x q (2.89)

ANMF.CH02_4PP.indd 140

3/1/2023 12:56:44 PM

Linear System of Equations • 141

i.e., yi = xi if i ≠ p or q. Therefore, it is necessary to select θ such that yp or yq is equal to zero. It should also be noted that if xp = xq = 0 then yp = yq = 0. If we write  A    x (1) x (2) ... x ( n ) (2.90) then

R  p, q,     A    y (1) y (2) ...y ( n ) 

An element in either pth row or qth row is reduced by a proper choice of θ. Thus, all the elements in the first column except the first element can be made zero by suitable values of θ and they remain zero even after multiplying with [R]. Similarly for all other columns elements are made zero for the elements below the diagonal elements by proper choice of θ values. Thus, we obtain an upper triangular matrix by forming the product

u   R  n, n  1, n,n 1  R  n, n  2, n,n 2  ...R  3,1, 3,1  R  2,1, 2,1   A 

 n 1 n     R i, j,ij  j 1 i  j  1





   A  (2.91) 

Since [R] is an orthogonal matrix, the products of [R] are also orthogonal. Hence, we have [U] = [S][A] where [S] is orthogonal an d[U] is upper triangular. Since [S] is orthogonal we have [A] = [S]−1[U] = [S]T[U]. If [Q] = [S]T then [A] = [Q][U]. We can construct a sequence of matrices [A0], [A1][A2]… where    A 0    A   Q0  A 0 , Q k  A k   Q k 1  A k 1  (2.92) for all values of k ≥ 1. That is, we start with [A0] = [A] and put it in the form [Q0][U0] to obtain [Q0] and [U0] and then obtain [A1] which is equal to the product of [U0][Q0] in reverse order. By repeating this procedure we can obtain any number of sequence.

[A] = [A0] = [Q0][U0]

determines [Q0] and [U0]



[A1] = [U0][Q0]

determines [A1]

     = [Q1][U1]

[A2] = [U1][Q1]

     = [Q2][U2]

ANMF.CH02_4PP.indd 141

determines [Q1] and [U1] determines [A2] 

(2.93)

determines [Q2] and [U2]

3/1/2023 12:56:56 PM

142 • Applied Numerical Methods Using MATLAB [Ak] = [Uk−1][Qk−1]



determines [Ak]

     = [Qk][Uk]

determines [Qk] and [Uk]

From the above sequence we can show that the product Q0Q1Q2 … Qk converges as k → ∞. Then [Ak] converges to an upper triangular matrix with the eigenvalues of [A] as its diagonal elements. This can be proved as follows. We have

 A k   Qk Uk   Uk 1 Qk 1  (2.94)



 A k 1   Qk 1 Uk 1  (2.95)

From which

Uk 1   Qk 1   A k 1  (2.96) 1



Substituting Equation (1.54) in Equation (1.52) we get

 A k   Qk 1   A k 1 Qk 1  1



Hence [Ak] is similar to [Ak−1]. This implies that [Ak] is similar to [A0] = [A]. Therefore, it has the same eigenvalues as [A]. Also

 A k 1   Qk 2   A k 2 Qk 2 

Thus

1 1 1  A k 1   Qk  Qk 1  Q0   A Q0 Q1 ...Qk 

1

  Pk 

1



 A Pk  (2.97)

Pk   Q0 Q1 ...Qk 

where

If k → ∞[Pk] exists and we denote it by [P]. Then



lim Q k  lim  Pk 1 

k 

k 

1

Pk    klim Pk 1  

1

  limP   P

1

k 

k

 P   I

Here we have two limiting conditions 1.  A k    Pk 1 

1



ANMF.CH02_4PP.indd 142

 A Pk 1 

which means



lim  A k   lim  Pk 1 

k 

1

k 

A lim P   P

1

k 

k 1

 A P (2.98)

3/1/2023 12:57:13 PM

Linear System of Equations • 143

Therefore, in the limit [Ak] is similar to [A] and hence has the same eigenvalues as [A]. 2.  A k   Q k Uk 

that is



here we have





lim  A k   lim Q k  lim Uk 

k 

k 

k 



Q k   I klim 

which results in

 A k   klim Uk  (2.99) klim   Since every [Uk] is an upper triangular matrix, the limit of [Ak] is also an upper triangular matrix. The accuracy of this method mainly depends on the effectiveness of the algorithm used for decompositions of [Ak] into [Qk][Uk]. The limit k → ∞ can exist for large size problems. EXAMPLE 2.24 Use the Jacobi method to determine the eigenvalues and eigenvectors of the following matrix 8  11 2   2 10  A   2  9 10 5  Solution: A solution is guaranteed for all real symmetric matrices when the Jacobi method is used. This limitation is not severe since many practical problems of applied mathematics and engineering involve symmetric matrices. From a theoretical viewpoint, the method embodies techniques that are found in more sophisticated algorithms. For instructive purposes, it is worthwhile to investigate the details of the Jacobi method.

ANMF.CH02_4PP.indd 143

3/1/2023 12:57:23 PM

144 • Applied Numerical Methods Using MATLAB Start with the real symmetric matrix  A. Then construct the sequence of orthogonal matrices R1,R2,R3,….Rn  as follows: D0 = A T j

and Dj=R DjRj for j=1,2,...  . It is possible to construct the sequence {Rj} so that lim D j  D  diag(1 ,  2 ,....,  n ). j

In practice we will stop when the off-diagonal elements are close to zero. Then we will have Dm ≈ D. The complete MATLAB program is shown below: A=[11 2 8;2 2 −10;9 −10 5]; %Output - V is the n x n matrix of eigenvectors % - D is the diagonal n x n matrix of eigenvalues D=A; [n, n]=size(A); V=eye(n); %Calculate row p and column q of the off-diagonal element %of greatest magnitude in A [m1 p]=max(abs(D-diag(diag(D)))); [m2 q]=max(m1); p=p(q); i=1; while(i> A=[11 2 8;2 2 −10;9 −10 5]; >> [Q,D]=eig(A) Q = 0.3319 −0.6460

−0.6277



−0.6599 0.3380 −0.6966



−0.6741

−0.6845 0.3475

D =

−9.2213 0

0

0 18.4308 0

ANMF.CH02_4PP.indd 145

0

0

8.7905

3/1/2023 12:57:24 PM

146 • Applied Numerical Methods Using MATLAB EXAMPLE 2.25 Use the Jacobi method to determine the eigenvalues and eigenvectors of matrix  4 1 2   A    1 3 3   2 3 1 

Solution:

The complete computer program is given below: A=[4 −1 −2;−1 3 3;−2 3 1]; %Output − V is the n x n matrix of eigenvectors % − D is the diagonal n x n matrix of eigenvalues D=A; [n, n]=size(A); V=eye(n); %Calculate row p and column q of the off-diagonal element %of greatest magnitude in A [m1 p]=max(abs(D-diag(diag(D)))); [m2 q]=max(m1); p=p(q); i=1; while(i> A=[4 −1 −2;−1 3 3;−2 3 1]; >> [Q,D]=eig(A) Q= 0.2114

0.7636

−0.6102

−0.5184 0.6168 0.5923 0.8286

0.1911

0.5262

D= −1.3872 0

0

0 2.6916 0 0

ANMF.CH02_4PP.indd 147

0 6.6956

3/1/2023 12:57:26 PM

148 • Applied Numerical Methods Using MATLAB  6 2 1 1   2 4 2 1    Find the eigenvalues and eigenvectors of [A] =  1 2 4 2  using the Jacobi method.    1 1 2 4  EXAMPLE 2.26

Solution: The following MALTAB program is used for this: A=[6 −2 1 −1;−2 4 −2 1;1 −2 4 −2;−1 1 −2 4]; %Output − V is the n x n matrix of eigenvectors % − D is the diagonal n x n matrix of eigenvalues D=A; [n, n]=size(A); V=eye(n); %Calculate row p and column q of the off-diagonal element %of greatest magnitude in A [m1 p]=max(abs(D-diag(diag(D)))); [m2 q]=max(m1); p=p(q); i=1; while(i> A=[6 −2 1 −1;−2 4 −2 1;1 −2 4 −2;−1 1 −2 4]; >> [Q,D]=eig(A) Q=

ANMF.CH02_4PP.indd 149

−0.1788

−0.2778 0.7250

−0.6043

−0.5421

−0.6744 0.0252

0.5006

−0.7046

0.1976

−0.4915

−0.4721

−0.4215

0.6550

0.4818

0.4016

3/1/2023 12:57:30 PM

150 • Applied Numerical Methods Using MATLAB D= 1.5186

0

0

2.7910

0

0

0

0

0

0

4.5880

0

0

0

0

9.1025

EXAMPLE 2.27 Transform the matrix

4 3 A 2  1

3 4 1 2

2 1 4 3

1 2  3  4

into tridiagonal form using Householder reduction and also determine the transformation matrix. Solution: Suppose that A is a symmetric n × n matrix.   Start with A0=A Construct the sequence  P1, P2, ..., Pn−1 of Householder matrices, so that Ak = PkAk−1Pk for k = 1, 2, …, n − 2, where Ak has zeros below the sub diagonal in columns 1,2,…,k.  Then An-2 is a symmetric tridiagonal matrix that is similar to A.  This process is called Householder’s method. %Input - A is an n x n symmetric matrix A=[4 3 2 1;3 4 1 2;2 1 4 3;1 2 3 4]; %Output − T is a tridiagonal matrix [n, n]=size(A); for k=1:n−2  s=norm(A(k+1:n,k));   if (A(k+1,k)> disp(V) V= 0

0.7071 −0.4082 0

0

0.7071 0.4082

0

−7.6079 0 0.8165 0 −10.7820 0

0 1.0000

2.12.7  Power Method The power method is generally used to find the largest eigenvalue largest in magnitude, (sometimes called first eigenvalue) of a real matrix A. Let X0 be an arbitrary vector and we use X0 as the initial approximation to an eigenvalue of the given matrix A. We assume here that A has n eigenvalues, labeled as λ1, λ2, …., λn, such that the corresponding eigenvectors {v1, v2, …., vn] from a linearly independent set. Further, it is assumed that A has only one eigenvalue which is largest in magnitude, say λ1, and |λ1| > |λ2| ≥ |λ3| ≥ …. ≥ |λn|. We proceed by normalizing the vector X0 and compute the sequences: AX0 = λ1x1 AX1 = λ2x2 AX2 = λ3x3 ….   …. AXi−1 = λixi

ANMF.CH02_4PP.indd 157

(2.100)

3/1/2023 12:57:45 PM

158 • Applied Numerical Methods Using MATLAB In the sequence given by Equation (2.100), all the vectors X1, X2, …., are to be normalized and the iteration process converges to give a solution of the form AX = λX(2.101) where X = lim xi, λ = lim λi. When the eigenvalue of the matrix A is real and unrepeated and the above process [Equation (2.100)] will converge to give us the largest eigenvalue of the given matrix A. If the power method is implemented successfully, it gives an estimate of the dominant eigenvalue, λ1. One drawback of the power method is that there is no way of knowing, in advance, whether a given matrix indeed has dominant eigenvalues, and if its multiplicity is one. Summarizing, given a vector u = [u1, u2, …., un]T, where umax represents the component of largest magnitude among all ui(i = 1, 2, …, n), form a new 1 vector u max = [u1, u2, …., un]T. Then, the component of largest magnitude u max of unew = 1. Iterative Procedure: Given a square matrix A, assume that λ is its dominant eigenvalue and that v is the corresponding normalized eigenvector. Step 1: Set v0 = [1 1 ….]T Step 2: Using the recursion formulas

uk = Avk(2.102)



vk 1 



generates sequences of vectors {u0, u1, …., un} and {v0, v1, …., vn}.

1 uk k = 0,1,2,… ( uk )max

In Equation (2.102), (uk)max denotes the component of uk with the largest magnitude. The sequence of vectors {v0, v1, …., vn} will converge to v, and the sequence of scalars {(u0)max, (u1)max, ….,} will converge to the dominant eigenvalue λ. Hence vk → v and (uk)max → λ as k → ∞.

ANMF.CH02_4PP.indd 158

3/1/2023 12:57:51 PM

Linear System of Equations • 159

The power method converges to the eigenvalue farthest from zero and the associated eigenvector. EXAMPLE 2.33 Apply power method to find the dominant eigenvalue of the matrix A. 1 6 1    A  1 2 0  0 0 3  initial eigenvector = [1 0 0]T. Solution: Given

1 6 1    A  1 2 0  0 0 3 

and

1    v 0  0  0 

initial approximation of the eigenvector. 1 6 1  1  1    Av 0  1 2 0  0   1  0 0 3  0  0  which implies that the first approximation to the eigenvalue is 3 and the corresponding eigenvector v1 = [1 1 0]T Therefore

1 6 1  1  7  2.3       Av 1  1 2 0  1   3   3  1  0 0 3  0  0   0 

from which, we have v2 = [2.3 1 0]T and an approximate eigenvalue is 3.

ANMF.CH02_4PP.indd 159

3/1/2023 12:58:01 PM

160 • Applied Numerical Methods Using MATLAB Repeating the above process, we obtain 2.1   4 1.1  ;  0 



2.2    4 1.1  ;  0 

2    4 1  ; 0 

2    4 1  ; 0 

2    4 1  0 

Hence, the largest eigenvalue is 4 and the corresponding eigenvector is [2 1 0]T. EXAMPLE 2.34 Apply power method to find the dominant eigenvalue of the following 3 × 3 symmetric real matrix A.  2 1 1    A   1 3 2   1 2 3  Solution: Let us assume the initial vector as v0 = [1 1 1]T. We apply the recursion formulas, Equation (2.102), to generate two sequences. Given

 2 1 1    A   1 3 2   1 2 3 

and

1   v 0  1 1



 2 1 1  1 2   0.3333       1  u 0  Av 0   1 3 2  1   4   0.6667  6  1 2 3  1 6   1 

Now  2 1 1   0.3333   0.9999   0.2143         u1  Av1  6  1 3 2  0.6667   6  3.6668   4.6667 0.7857  , etc.  1 2 3   1   4.6667   1 

ANMF.CH02_4PP.indd 160

3/1/2023 12:58:22 PM

Linear System of Equations • 161

TABLE 2.2 v1

v2

v3

v4

v5

v6

v7

v8

v9

0.3333

0.2143

0.1343

0.0828

0.0505

0.0306

0.0185

0.0111

0.0067

0.6667

0.7857

0.8657

0.9172

0.9495

0.9694

0.9815

0.9889

0.9933

1.0000

1

1

1

1

1

1

1

1

4.6667

4.7857

4.8657

4.9172

4.9495

4.9694

4.9815

4.9889

(vk)max 6

From Table 2.2, we observe (uk)max → = 5 and vk → = [0 1 1]T. 2.12.8  Inverse Power Method The inverse power method is an iterative procedure for obtaining the smallest eigenvalue λ1 and the corresponding eigenvector x1 of Ax = λ(2.103) The eigenvectors of xi of Equation (2.103) are orthonormal and hence they can be used as a basis for any n-dimensional vector. Let v be an approximation to x1, then we solve Az = v

(2.104)

for the vector z. We let v = z/|z| and repeat steps using Equations (2.103) to (2.104) until the change in v is negligible. n

Here

v   vi xi

and

z   z i x i (2.105)

i 1 n

i 1

v and z are the components with respect to the eigenvector xi. From Equations (2.104) and (2.105), we have

ANMF.CH02_4PP.indd 161

n

n

i 1

i 1

A  z i x i   v i x i  0 (2.106)

3/1/2023 12:58:27 PM

162 • Applied Numerical Methods Using MATLAB Since Axi = λixi, we have n



 (z 

Therefore,

zi =

i 1

i

i

 v i )x i  0 (2.107)

vi xi

From Equation (2.105), we obtain n

z i 1

vi 1 xi  i i

n

v i 1

i

 1   1  x i   v 1 x 1  v 2 1 x 2  v 3 1 x 3  .... (2.108) i 1  2 3 

λ1 < 1 (i ≠ 1), we notice that the coefficient of x1 has become more λi significant in z than it was in v. Therefore, z is a better approximation to x1. Since

In the next cycles, we let v =

z and repeat the steps. |z|

Finally, the process converges to z 

1 1 v 1 x 1  x 1 (2.109) 1 1

For nonstandard eigenvalue problems, the inverse power method also works with Ax = λBx 

(2.110)

and we replace Equation (2.104) with Az = Bv

(2.111)

The rate of convergence of the inverse power method can be improved by the technique of eigenvalue shifting. Let λ = λ* + s

(2.112)

where s is a predetermined shift. Now Equation (2.103) can be written as Ax = (λ* + s)x

ANMF.CH02_4PP.indd 162

3/1/2023 12:58:39 PM

Linear System of Equations • 163

or A*x = λ*x(2.113) where

A* = A − sI

(2.114)

Equation (2.113) can be solved by the inverse power method. The results are λ*1 and x1, where λ*1 is the smallest eigenvalue of A*. Hence, the respective eigenvalue of the original problem is given by λ = λ*1 + s

(2.115)

This eigenvalue is the closest to s. Eigenvalue shifting can be used to speed up convergence. When the eigenvalues are known, the inverse power method with eigenvalue shifting can be used very effectively to find the eigenvectors.

2.12 SUMMARY A matrix is a rectangular array of elements in rows and columns. The elements of a matrix can be numbers, coefficients, terms, or variables. This chapter provided the relevant and useful elements of matrix analysis for the solution of linear simultaneous algebraic equations. Topics covered include matrix definitions, matrix operations, determinants, matrix inversion, trace, transpose, and system of algebraic equations and solution. The solution of n linear simultaneous algebraic equations in n unknowns is presented. There are two classes of methods of solving a system of linear algebraic equations: direct and iterative methods. Direct methods transform the original equation into equivalent equations that can be solved more easily. Iterative or indirect methods start with a guess of the solution x, and then repeatedly refine the solution until a certain convergence criterion is reached. Six direct methods (matrix inversion method, Gauss elimination method, Gauss-Jordan method, Cholesky’s triangularization method, Crout’s method, and the Thomas algorithm for tridiagonal system) are presented. Two indirect or iterative methods (Jacobi’s iteration method and the Gauss-Seidel iteration method) are presented. The LU decomposition method is closely related to the Gauss elimination method. LU decomposition is computationally very effective if the coefficient matrix remains the same but the right-hand side vector changes. Cholesky’s decomposition method can be used when the coefficient matrix A is symmetric and positive definite. Gauss-Jordan method is a very stable method for solving linear algebraic equations. The Gauss-Seidel iterative substitution technique is very suitable for predominantly diagonal systems. It requires a guess of the solution.

ANMF.CH02_4PP.indd 163

3/1/2023 12:58:40 PM

164 • Applied Numerical Methods Using MATLAB

EXERCISES NOTE

In addition to solving the following exercise problems through analytical solutions or long-hand numerical computations, try to solve them using userdefined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 2.1

Determine the inverse of the following matrices:  1 1 2    (a) A   3 1 1   1 3 4  1 2 0    (b) A  3 1 2  1 0 3  10 3 10    A   8 2 9  c)  8 1 10  1 2 3    d) A   4 5 6  7 0 0  1 1 1    e) A  1 2 2  1 0 3  1 0 3    f) A  2 1 1 1 1 1 

ANMF.CH02_4PP.indd 164

3/1/2023 12:58:58 PM

Linear System of Equations • 165

2.2

Solve the following set of simultaneous linear equations by the matrix inverse method. (a) 2x + 3y − z = −10

−x + 4y + 2z = −4



2x − 2y + 5z = 35

(b) 10x + 3y + 10z = 5

8x − 2y + 9z = 2



8x + y − 10z = 35

(c) 2x + 3y − z = 1

−x + 2y + z = 8



x − 3y − 2z = −13

(d) 2x − y + 3z = 4

x + 9y − 2z = −8



4x − 8y + 11z = 15

(e) x1 − x2 + 3x3 − x4 = 1

x2 − 3x3 + 5x4 = 2



x1 − x3 + x4 = 0



x1 + 2x2 − x4 = −5

(f) x1 + 2x2 + 3x3 + 4x4 = 8

ANMF.CH02_4PP.indd 165



2x1 − 2x2 − x3 − x4 = −3



x1 − 3x2 + 4x3 − 4x4 = 8



2x1 + 2x2 − 3x3 + 4x4 = −2

3/1/2023 12:58:58 PM

166 • Applied Numerical Methods Using MATLAB 2.3

Solve the following set of simultaneous linear equations using the method of Gaussian elimination. (a) 2x + y − 3z = 11

4x − 2y + 3z = 8



−2x + 2y − z = −6

(b) 6x + 3y + 6z = 30

2x + 3y + 3z = 17



x + 2y + 2z = 11

(c) 2x1 + x2 + x3 = 4

3x2 − 3x3 = 0



−x2 + 2x3 = 1

(d) x1 + 2x2 + 3x3 + 4x4 = 8

2x1 − 2x2 − x3 − x4 = −3



x1 − 3x2 + 4x3 − 4x4 = 8



2x1 + 2x2 − 3x3 + 4x4 = −2

(e) 2x1 + x2 + x3 − x4 = 10

x1 + 5x2 − 5x3 + 6x4 = 25



−7x1 + 3x2 − 7x3 − 5x4 = 5



x1 − 5x2 + 2x3 + 7x4 = 11

(f) x1 + x2 + x3 + x4 = 3

ANMF.CH02_4PP.indd 166



2x1 − x2 + 3x3 = 3



2x2 + 3x4 = 1



−x1 + 2x3 + x4 = 0

3/1/2023 12:58:58 PM

Linear System of Equations • 167

2.4

Solve the following set of simultaneous linear equations by the GaussJordan method. (a) 4x − 3y + 5z = 34

2x − y − z = 6



x + y + 4z = 15

(b) 2x − y + z = −1

3x + 3y + 9z = 0



3x + 3y + 5z = 4

(c) x + y − z = 1

x + 2y − 2z = 0



−2x + y + z = 1

(d) x − y = 2

−2x + 2y − z = −1



y − 2z = 6

(e) x + y + z = 3

2x + 3y + z = 6



x − y − z = −3

(f) 4x1 − 2x2 − 3x3 + 6x4 = 12

2.5



−5x1 + 7x2 + 6.5x3 − 6x4 = −6.5



x1 + 7.5x2 + 6.25x3 + 5.5x4 = 16



−12x1 + 22x2 + 15.5x3 − x4 = 17

Solve the following set of simultaneous linear equations by the Cholesky’s factorization method. (a) 2x − y = 3

ANMF.CH02_4PP.indd 167



−x + 2y − z = −3



−y + z = 2

3/1/2023 12:58:58 PM

168 • Applied Numerical Methods Using MATLAB (b) x + y + z = 7

3x + 3y + 4z = 23



2x + y + z = 10

(c) x + 0.5y = 1

0.5x + y + 0.5z = 2



0.5y + z = 3

(d) 2x + 3y + z = 9

x + 2y + 3z = 6



3x + y + 2z = 8

(e) x − 2y + z = 2

5x + y − 3z = 0



3x + 4y + z = 9

(f) 12x1 − 6x2 − 6x3 + 1.5x4 = 1

2.6



−6x1 + 4x2 + 3x3 + 0.5x4 = 2



−6x1 + 3x2 + 6x3 + 1.5x4 = 3



−1.5x1 + 0.5x2 + 1.5x3 + x4 = 4

Solve the following set of simultaneous linear equations using the Crout’s method. (a) 2x + y = 7

x + 2y = 5

(b) 3x + 2y + 7z = 4

2x + 3y + z = 5



3x − 4y + z = 7

(c) x + y + z = 9

ANMF.CH02_4PP.indd 168



2x − 3y + 4z = 13



3x + y + 5z = 40

3/1/2023 12:58:59 PM

Linear System of Equations • 169

(d) 3x + y = −1

2x + 4y + z = 7



2y + 5z = 9

(e) 2x + y − z = 6

x − 3y + 5z = 11



−x + 5y + 4z = 13

(f) 2x1 − x2 = 1

2.7



−x1 + 2x2 − x3 = 0



−x2 + 2x3 − x4 = 0



−x3 + 2x4 = 1

Solve the following tridiagonal system of equations using the Thomas algorithm. (a) 2x1 + x2 = 3

−x1 + 2x2 + x3 = 6



3x2 + 2x3 = 12

(b) 2x1 + x2 = 4

3x1 + 2x2+ x3 = 8



x2 + 2x3+ 2x4 = 8



x3 + 4x4 = 9

(c) 3x1 − x2 = 2

ANMF.CH02_4PP.indd 169



2x1 − 3x2 + 2x3 = 1



x2 + 2x3 + 5x4 = 13



x3 − x4 = −1

3/1/2023 12:58:59 PM

170 • Applied Numerical Methods Using MATLAB (d) 2x1 + x2 = 3

x1 + 3x2 + x3 = 3



x2 + x3 + 2x4 = 4



2x3 + 3x4 = 4

(e) 2x1 + x2 = 1

3x1 + 2x2 + x3 = 2



x2 + 2x3 + 2x4 = −1



x3 + 4x4 = −3

(f) 2x1 − x2 = 1

2.8



x1 + 3x2 + x3 = 3



x2 + x3 + 2x4 = 0



2x3 + 3x4 = −1

Solve the following set of simultaneous linear equations using the Jacobi’s method. (a) 2x − y + 5z = 15

2x + y + z = 7



x + 3y + z = 10

(b) 20x + y − 2z = 17

3x + 20y − z = −18



2x − 3y + 20z = 25

(c) 5x + 2y + z = 12

x + 4y + 2z = 15



x + 2y + 5z = 20

(d) 10x − y + 2z = 6

ANMF.CH02_4PP.indd 170



−x + 11y + z = 22



2x − y + 10z = −10

3/1/2023 12:58:59 PM

Linear System of Equations • 171

(e) 8x + 2y − 2z = 8

x − 8y + 3z = −4



2x + y + 9z = 12

(f) 10x1 + x2 + 2x3 = 6

2.9



−x1 + 11x2 − x3 + 3x4 = 25



2x1 − x2 + 10x3 − x4 = −1



3x2 − x3 + 8x4 = 15

Solve the following system of simultaneous linear equations using the Gauss-Seidel method. (a) 4x − 3y + 5z = 34

2x − y − z = 6



z + y + 4z = 15

(b) 2x − y + 5z = 15

2x + y + z = 7



x + 3y + z = 10

(c) 15x + 3y − 2z = 85

2x + 10y + z = 51



x − 2y + 8z = 5

(d) 10x1 − 2x2 − x3 − x4 = 3

ANMF.CH02_4PP.indd 171



−2x1 + 10x2 − x3 − x4 = 15



−x1 − x2 + 10x3 − 2x4 = 27



−x1 − x2 − 2x3 + 10x4 = −9

3/1/2023 12:58:59 PM

172 • Applied Numerical Methods Using MATLAB (e) 4x1 + 2x2 = 4

2x1 + 8x2 + 2x3 = 0



2x2 + 8x3 + 2x3 = 0



2x3 + 4x4 = 0

(f) 4x1 + 2x2 = 4

2x1 + 8x2 + 2x3 = 0



2x2 + 8x3 + 2x3 = 0



2x3 + 4x4 = 14

2.10 Find the eigenvalues and eigenvectors of A using the Jacobi method. 1 1  4 2  2 4 2 1   A  1 2 4 2    1 2 4  1 2.11 Use Jacobi method to compute the eigenvalues and eigenvectors of a symmetric matrix A.  1 2  A   2 2 2.12 Use Jacobi method to find the eigenvalues of the following matrix A. 4 2 0   A  2 3 1   0 1 2  2.13 Use Jacobi method to determine the eigenvalues and eigenvectors of A.  4 1 2    A   1 3 3   2 3 1 

ANMF.CH02_4PP.indd 172

3/1/2023 12:59:06 PM

Linear System of Equations • 173

2.14 Use the Jacobi method to compute the eigenvalues and eigenvectors of the matrix. 11 2  3 A 1 4   2

2 3 1 4 2 9 3 5 2 1  3 15 4 3 2   5 4 12 4 3  2 3 4 17 5   1 2 3 5 8 

2.15 Use Householder’s method to convert the matrix A to upper-­ Hessenberg form.  1 2 3    A   2 4 1   3 1 2  2.16 Convert the eigenvalue problem Ax = λBx, where



 4 1 0    A   1 4 1  0 1 4 

and

 2 1 0    B   1 2 1  0 1 1 

2.17 Use Householder’s method to transform the matrix A into tridiagonal form.  7 2 3 1 2 8 5 1  A  3 5 12 9     1 1 9 7  2.18 Use MATLAB Householder to tridiagonalize the matrix in Exercise 2.17. Determine the transformation matrix P.

ANMF.CH02_4PP.indd 173

3/1/2023 12:59:20 PM

174 • Applied Numerical Methods Using MATLAB 2.19 Use Householder’s reduction method to transform the matrix A into tridiagonal form.  4 1 2    A   1 3 3  2 3 1 2.20 Show that the matrix A has one eigenvalue in the interval (2, 4) using the strum sequence. 0 0  5 2  2 4 1 0   A  0 1 4 2    0 2 5  0 2.21 The stress matrix at a point is

 150 60 0    S   60 120 0  MPa  0 0 80  Compute the principal stress (eigenvalues of S).

2.22 Use Gerschgorin’s theorem to locate the region of the eigenvalues of the following matrix.  4 8 0    A   5 13 0   1 0 2  2.23 Locate the region of the eigenvalues of the following matrices using the Gerschgorin’s theorem.  1 24 0    (a) A   3 8 0   1 0 2   4 14 0    (b) A   5 13 0   1 0 2 

ANMF.CH02_4PP.indd 174

3/1/2023 12:59:35 PM

Linear System of Equations • 175

2.24 Use the QR method to evaluate the eigenvalues of the matrix A which describes the system of equations. 1 2 3    A  1 4 9  1 8 27  2.25. Use the Householder’s method to compute the eigenvalues and the corresponding eigenvectors for the eigenvalue problem. [[A] − λ[I]]{x} = 0 where



4 3 [A]   2  1

3 4 1 2

2 1 4 3

1 2  3  4

2.26 The three-dimensional state of stress at a point inside a loaded structure is given by

 45 30 25    S   30 24 68   25 68 80  Determine the principal stresses at a point (eigenvalues of the stress matrix). Use the QR factorization method.

2.27 Determine eigenvalues and eigenvectors of the following matrix using the Jacobi’s method. 2 8 11   A 2 2 10   9 10 5  2.28 Use the power method to evaluate the eigenvalues of the matrix A.  10 4 0    A   6 9 3   0 6 6 

ANMF.CH02_4PP.indd 175

3/1/2023 12:59:48 PM

176 • Applied Numerical Methods Using MATLAB 2.29 The stress matrix describing the state of the stress at a point is 20   30 10   S   10 40 50  MPa.  20 50 10 

Determine the largest principal stress by the power method.

2.30 Find the largest eigenvalue and the corresponding eigenvector of the following matrices. 1 6 1    (a) A  1 2 0  0 0 3   1 3 2   (b) A   1 0 2   3 4 5   10 4 1   (c) A   4 2 3   1 3 1  2 1 0    (d)  1 2 1  0 1 2  2.31 Evaluate the smallest eigenvalue of the following system.



 4 14 0    A   5 13 0   1 0 2  Use the inverse power method.

2.32 Find the smallest eigenvalue and the corresponding eigenvector of the matrix A in Exercise 2.10. Use the inverse power method. 2.33 Use the MATLAB program for the Exercise 2.29.

ANMF.CH02_4PP.indd 176

3/1/2023 1:00:05 PM

CHAPTER

3

Solution of Algebraic and Transcendental Equations 3.1 INTRODUCTION One of the most common problems encountered in engineering analysis is that given a function f(x), find the values of x for which f(x) = 0. The solution (values of x) are known as the roots of the equation f(x) = 0, or the zeroes of the function f(x). The roots of equations may be real or complex. In general, an equation may have any number of (real) roots or no roots at all. For example, sin x − x = 0 has a single root, namely, x = 0, whereas tan x − x = 0 has infinite number of roots (x = 0, ±4.493, ±7.725, …). There are two types of methods available to find the roots of algebraic and transcendental equations of the form f(x) = 0. 1. Direct Methods: Direct methods give the exact value of the roots in a finite number of steps. We assume that there are no round-off errors. Direct methods determine all the roots at the same time. 2. Indirect or Iterative Methods: Indirect or iterative methods are based on the concept of successive approximations. The general procedure is to start with one or more initial approximation to the root and obtain a sequence of iterates (xk) which in the limit converges to the actual or true solution to the root. Indirect or iterative methods determine one or two roots at a time. The indirect or iterative methods are further divided into two categories: bracketing and open methods. The bracketing methods require the limits between which the root lies, whereas the open methods require the initial

ANMF.CH03_4PP.indd 177

3/1/2023 1:00:49 PM

178 • Applied Numerical Methods Using MATLAB estimation of the solution. Bisection and false position methods are two known examples of the bracketing methods. Among the open methods, the Newton-Raphson and the method of successive approximation is most commonly used. The most popular method for solving a nonlinear equation is the Newton-Raphson method and this method has a high rate of convergence to a solution. In this chapter, we present the following indirect or iterative methods with illustrative examples: • bisection method • method of false position (regula falsi method) • Newton-Raphson method (Newton’s method) • successive approximation method

3.2 BISECTION METHOD After a root of f(x) = 0 has been bracketed in the interval (a, b), bisection method can be used to close in on it. The bisection method accomplishes this by successfully halving the interval until it becomes sufficiently small. The bisection method is also known as the interval halving method. The bisection method is not the fastest method available for finding roots of a function, but it is the most reliable method. Once a has been bracketed, the bisection method will always close in on it. We assume that f(x) is a function that is real-valued and that x is a real variable. Suppose that f(x) is continuous on an interval a ≤ x ≤ b and that f(a) f(b)  0 and f (b) < 0 or f (a) < 0 and f (b) > 0. In other words, if there is a solution between x = a, and x = b, then f (a) f (b) < 0.

FIGURE 3.1  Solution of f(x) = 0 between x = a and x = b.

ANMF.CH03_4PP.indd 178

3/1/2023 1:00:49 PM

Solution of Algebraic and Transcendental Equations • 179

The method of finding a solution with the bisection method is illustrated in Figure 3.2. It starts by finding points a and b that define an interval where a solution exists. The midpoint of the interval x s1 is then taken as the first estimate for the numerical solution. The true solution is either in the portion between points a and x s1 or in the portion between points x s1 and b. If the solution obtained is not accurate enough, a new interval that contains the true solution is defined. The new interval selected is the half of the original interval that contains the true solution, and its midpoint is taken as the new (second) estimate of the numerical solution. The procedure is repeated until the numerical solution is accurate enough according to a certain criterion that is selected.

FIGURE 3.2  Bisection method.

The procedure or algorithm for finding a numerical solution with the bisection method is given below: Algorithm for the bisection method 1. Compute the first estimate of the numerical solution x s1 by x s1 

ANMF.CH03_4PP.indd 179

ab 2

3/1/2023 1:00:55 PM

180 • Applied Numerical Methods Using MATLAB 2. Determine whether the true solution is between a and x s1 or between x s1 and b by checking the sign of the product f (a) f( x s1):

If f (a) f ( x s1) < 0, the true solution is between a and x s1.



If f(a)f( x s1) > 0, the true solution is between x s1 and b.



If b − c ≤ ∈, then accept c as the root and stop. ∈ is the error tolerance, ∈ > 0.

3. Choose the subinterval that contains the true solution (a to x s1 or x s1 to b) as the new interval (a, b), and go back to step 1.

Steps 1 through 3 are repeated until a specified tolerance or error bound is attained.

3.2.1 Error Bounds Let an, bn, and cn denote the nth computed values of a, b, and x s1 respectively. Then, we have 1 bn+1 − an+1 = (bn − an) n ≥ 1 (3.1) 2 1 also bn − an = n−1 (b − a) n ≥ 1 (3.2) 2 where (b − a) denotes the length of the original interval with which we started. Since the root xs is in either the interval (an, cn) or (cn, bn), we know that 1 (bn − an)(3.3) 2 This is the error bound for cn that is used in step 2 of the algorithm described earlier. |xs − cn| ≤ cn − an = bn − cn =

From Equations (3.2) and ( 3.3), we obtain the further bound | x s  c n | 

1 (b  a) (3.4) 2n

Equation (3.4) shows that the iterate cn converges to xs as n → ∞. To find out how many iterations will be necessary, suppose we want to have |xs − cn| ≤ ∈

ANMF.CH03_4PP.indd 180

3/1/2023 1:01:02 PM

Solution of Algebraic and Transcendental Equations • 181

This will be satisfied if

1 (b  a)  (3.5) 2n

Taking logarithms of both sides of Equation (3.5), and simplifying the resulting expression, we obtain ba log      n  log 2 

(3.6)

There are several advantages to the bisection method. The method is guaranteed to converge, and always converges to an answer, provided a root was bracketed in the interval (a, b) to start with. In addition, the error bound, given in Equation (3.4), is guaranteed to decrease by one half with each iteration. The method may fail when the function is tangent to the axis and does not cross the x-axis at f(x) = 0. The disadvantage of the bisection method is that it generally converges more slowly than most other methods. For functions f(x) that have a continuous derivative, other methods are usually faster. These methods may not always converge. When these methods do converge, they are almost always much faster than the bisection method. EXAMPLE 3.1 Use the bisection method to find a root of the equation x3 − 4x − 8.95 = 0 accurate to three decimal places using the bisection method. Solution: Here, f(x) = x3 − 4x − 8.95 = 0

f(2) = 23 − 4(2) − 8.95 = −8.95 < 0



f(3) = 33 − 4(3) − 8.95 = 6.05 > 0

Hence, a root lies between 2 and 3. f1(x) = 3x2 − 4 > 0 for x in the interval (2, 3). Hence, we have a = 2 and b = 3. The results of the algorithm for bisection method are shown in Table 3.1.

ANMF.CH03_4PP.indd 181

3/1/2023 1:01:06 PM

182 • Applied Numerical Methods Using MATLAB

TABLE 3.1  Bisection method results. n

a

b

x s1

b − x s1

f( x s1 )

 1

2

3

2.5

0.5

−3.25

 2

2.5

3

2.75

0.25

0.84688

 3

2.5

2.75

2.625

0.125

−1.36211

 4

2.75

2.625

2.6875

−0.0625

−0.28911

 5

2.75

2.6875

2.71875

−0.03125

0.27092

 6

2.6875

2.71875

2.70313

0.01563

−0.01108

 7

2.71875

2.70313

2.71094

−0.00781

0.12942

 8

2.71875

2.71094

2.71484

−0.00391

0.20005

 9

2.71094

2.71484

2.71289

0.00195

0.16470

10

2.71094

2.71289

2.71191

0.00098

0.14706

11

2.71094

2.71191

2.71143

0.00049

0.13824

Hence, the root is 2.711 accurate to three decimal places. MATLAB Solution: Provide the user-defined function y = f(x) here. function y = fex3_1(x) y = x^3-4*x−8.95; The following program (script file) uses the user-defined root = Bisection (Func,x1,x2,filter,tol) function to find the root of the equation: >> a = 2.0;b = 3.0;dx = 0.01; >> root = Bisection(@fex3_1,a,b,dx) When the script is executed, the following solution is displayed in the command window: root = 2.7037 MATLAB Bisection Program: function root = Bisection(Func,x1,x2,filter,tol) % Finds a bracketed root of f(x) by bisection method

ANMF.CH03_4PP.indd 182

3/1/2023 1:01:07 PM

Solution of Algebraic and Transcendental Equations • 183

% USAGE: Root = Bisection(Func,x1,x2,filter,tol) % INPUT: % Func = Handle of Function that returns f(x) % x1,x2 = Interval containing the root % filter = Singularity filter: 0 = off (default),1 = on % tol = Error tolerance (default is 1.0e4*eps) % OUTPUT: % root = Zero of f(x), or NaN [if singularity] % MATLAB Program if nargin < 5; tol = 1.0e5*eps; end if nargin < 4; filter = 0; end f1 = feval(Func,x1); if f1 = = 0.0; root = x1; return; end f2 = feval(Func,x2); if f2 = = 0.0; root = x2; return; end if f1*f2 > 0;   error(‘The root is not bracketed in the interval(x1,x2)’) end n = ceil(log(abs(x2 − x1)/tol)/log(2.0)); for i = 1:n  x3 = 0.5*(x1 + x2);  f3 = feval(Func,x3);  if(filter = = 1) & (abs(f3) > abs(f1))& (abs(f3) > abs(f2))   root = NaN; return  end   if f3 == 0.0   root = x3; return

ANMF.CH03_4PP.indd 183

3/1/2023 1:01:07 PM

184 • Applied Numerical Methods Using MATLAB  end   if f2*f3 < 0.0   x1 = x3; f1 = f3;  else   x2 = x3; f2 = f3;  end end root = (x1 + x2)/2; EXAMPLE 3.2 Find one root of ex − 3x = 0 correct to two decimal places using the method of bisection. Solution: Here, f(x) = ex − 3x

f(1.5) = e1.5 − 3(1.5) = −0.01831



f(1.6) = e1.6 − 3(1.6) = 0.15303

f1(x) = ex − 3 > 0 for x in the interval (1.5, 1.6). Hence, a root lies in the interval (1.5, 1.6). Therefore, here we have a = 1.5 and b = 1.6. The results of the algorithm for bisection method are shown in Table 3.2. TABLE 3.2  Bisection method results. n

a

b

x s1

b − x s1

f( x s1 )

1

1.5

1.6

1.55

0.05

0.06147

2

1.5

1.55

1.525

0.025

0.02014

3

1.5

1.525

1.5125

0.0125

0.00056

4

1.5

1.5125

1.50625

0.00625

−0.00896

5

1.50625

1.5125

1.50938

0.00313

−0.00422

6

1.50938

1.5125

1.51094

0.00156

−0.00184

Hence the root of f(x) = 0 is x = 1.51 accurate up to two decimal places.

ANMF.CH03_4PP.indd 184

3/1/2023 1:01:07 PM

Solution of Algebraic and Transcendental Equations • 185

MATLAB Solution: Provide the user-defined function y = f(x) here. function y = fex3_2(x) y = exp(x)−3.0*x; The following program (script file) uses the user-defined root = Bisection (Func,x1,x2,filter,tol) function to find the root of the equation: >> a = 1.5;b = 1.6;dx = 0.01; >> root=Bisection(@fex3_2,a,b,dx) When the script is executed, the following solution is displayed in the command window: root = 1.5121 EXAMPLE 3.3 Determine the largest root of f(x) = x6 − x − 1 = 0 accurate to within ∈ = 0.001. Use the bisection method. Solution: Here f(x) = x6 − x − 1 = 0

f(1) = 16 − 1 − 1 = −1



f(2) = 26 − 2 − 1 = 61

Since f(1)f(2) < 0, f(x) = 0 has at least one root on the interval. The results of the algorithm for the bisection method are shown in Table 3.3. TABLE 3.3  Bisection method results. n

ANMF.CH03_4PP.indd 185

a

b

x s1

b − x s1

f( x s1 )

 1

1

2

1.5

0.5

8.89063

 2

1

1.5

1.25

0.25

1.56470

 3

1

1.25

1.25

0.125

−0.09771

 4

1.125

1.25

1.1875

0.0625

0.61665

 5

1.125

1.1875

1.15625

0.03125

0.23327

3/1/2023 1:01:07 PM

186 • Applied Numerical Methods Using MATLAB

b

b − x s1

n

a

 6

1.125

1.15625

1.14063

0.01563

0.06158

 7

1.125

1.14063

1.13281

0.00781

−0.01958

 8

1.13281

1.14063

1.13672

0.00391

0.02062

 9

1.13281

1.13672

1.13477

0.00195

0.00043

10

1.13281

1.13477

1.13379

0.00098

−0.00960

x s1

f( x s1 )

MATLAB Solution: Provide the user-defined function y = f(x) here. function y = fex3_3(x) y = x^6−x−1; The following program (script file) uses the user-defined root = Bisection(Func,x1,x2,filter,tol)function to find the root of the equation: >> a = 1;b = 2;dx = 0.001; >> root = Bisection(@fex3_3,a,b,dx) When the script is executed, the following solution is displayed in the command window: root = 1.1347

3.3 METHOD OF FALSE POSITION The method of false position (also called the regula falsi method, and the linear interpolation method) is another well-known bracketing method. It is very similar to the bisection method with the exception that it uses a different strategy to conclude its new root estimate. Rather than bisecting the interval (a, b), it locates the root by joining f(a1) and f(b1) with a straight line. The intersection of this line with the x-axis represents an improved estimate of the root.

ANMF.CH03_4PP.indd 186

3/1/2023 1:01:07 PM

Solution of Algebraic and Transcendental Equations • 187

Here again, we assume that within a given interval (a, b), f(x) is c­ ontinuous and the equation has a solution. As shown in Figure 3.3, the method starts by finding an initial interval (a1, b1) that brackets the solution. f(a1) and f(b1) are the values of the function at the end points a1 and b1. These end points are connected by a straight line, and the first estimate of the numerical solution, x s1, is the point where the straight line crosses the axis. For the second iteration, a new interval (a2, b2) is defined. The new interval is either (a1, x s1) where a1 is assigned to a2 and x s1 to b2 or ( x s1, b1) where x s1 is assigned to a2 and b1 to b2. The end points of the second interval are connected with a straight line, and the point where this new line crosses the x-axis is the second estimate of the solution, x s1. A new subinterval (a3, b3) is selected for the third iteration and the iterations will be continued until the numerical solution is accurate enough.

FIGURE 3.3  Method of false position.

The equation of a straight line that connects points (b, f(b)) to point (a, f(a)) is given by

y

f (b)  f (a) (x  b)  f (b) (3.7) ba

The points xs where the line intersects the x-axis is determined by substituting y = 0 in Equation (3.7) and solving the equation for x.

ANMF.CH03_4PP.indd 187

3/1/2023 1:01:08 PM

188 • Applied Numerical Methods Using MATLAB

Hence

xs 

a f (b)  b f (a) (3.8) f (b)  f (a)

The procedure (or algorithm) for finding a solution with the method of false position is given below: Algorithm for the method of false position 1. Define the first interval (a, b) such that a solution exists between them. Check f(a)f(b) < 0. 2. Compute the first estimate of the numerical solution xs using Equation (3.8). 3. Find out whether the actual solution is between a and x s1 or between x s1 and b. This is accomplished by checking the sign of the product f(a)f( x s1).

If f (a) f ( x s1) < 0, the solution is between a and x s1.



If f(a)f( x s1) > 0, the solution is between x s1 and b.

4. Select the subinterval that contains the solution (a to x s1 , or x s1 to b) which is the new interval (a, b) and go back to step 2. Steps 2 through 4 are repeated until a specified tolerance or error bound is attained. The method of false position always converges to an answer, provided a root is initially bracketed in the interval (a, b). 3.3.1 MATLAB Program for the False Position Method function [root,ea,iter]=Falsep(Func,xl,xu,es,maxit,varargin) % False position method to find the root of a given function % Input: % Func = Function name % xl, xu = Lower and upper guesses % es = Desired relative error (default = 0.00001%) % maxit = Maximum allowable iterations (default = 60) % p1,p2,... = Additional parameters used by function % Output: % root = Real root obtained

ANMF.CH03_4PP.indd 188

3/1/2023 1:01:11 PM

Solution of Algebraic and Transcendental Equations • 189

% ea = Approximate relative error as a percentage % iter = Number of iterations if nargin0,error(‘No sign change’),end if nargin> Func = inline (‘exp(x)−3*x^2’); >> xl = 0.5;xu = 1.0;es = 1.0e−4;maxit = 50; >> [root,ea,iter] = Falsep(Func,xl,xu,es,maxit)

ANMF.CH03_4PP.indd 190

3/1/2023 1:01:15 PM

Solution of Algebraic and Transcendental Equations • 191

When the script is executed, the following solution is displayed in the ­command window: root = 0.9100 ea = 1.8762e−005 iter = 6 EXAMPLE 3.5 Find a real root of cos x − 3x + 5 = 0, correct to four decimal places using the method of false position method. Solution: Here f(x) = cos x − 3x + 5 = 0 f(0) = cos 0 − 3(0) + 5 = 5 > 0 cos  3    3   5  50 f(π/2) = 2 2 2



Therefore, a root of f(x) = 0 lies between 0 and π/2. We apply the method of false position with a = 0 and b = π/2. Equation (3.8) is xs 

a f (b)  b f (a) f (b)  f (a)

The calculations based on the method of false position are shown in Table 3.5. TABLE 3.5 n

ANMF.CH03_4PP.indd 191

a

f(a)

b

f(b)

x s1

ξ

f( x s1 )

1

0

1.5708

6

0.28761

1.64988

−0.02866



2

1.64988

1.5708

−0.02866

0.28761

1.64272

−0.00001

−0.00436

3

1.64272

1.5708

−0.00001

0.28761

1.64271

0

−1.97337 × 10−6

3/1/2023 1:01:20 PM

192 • Applied Numerical Methods Using MATLAB The relative error after the third step is

ξ

=

1.64271 − 1.64272 = −1.97337 × 10−6 1.64271

The root is 1.6427 accurate to four decimal places. MATLAB Solution: The following program (script file) uses the user-defined [root,ea,iter] = False p(Func,xl,xu,es,maxit,varargin) function to find the root of the equation: Provide the user-defined function y = f(x) here. >> Func = inline (‘cos(x)−3*x+5’); >> xl = 0;xu = 2;es = 1.0e−4;maxit = 50; >> [root,ea,iter] = Falsep(Func,xl,xu,es,maxit) When the script is executed, the following solution is displayed in the ­command window: root = 1.6427 ea = 2.3985e−006 iter = 4 EXAMPLE 3.6 Using the method of false position, find a real root of the equation x4 − 11x + 8 = 0 accurate to four decimal places. Solution: Here f(x) = x4 − 11x + 8 = 0

f(1) = 14 − 11(1) + 8 = −2 < 0



f(2) = 24 − 11(2) + 8 = 4 > 0

ANMF.CH03_4PP.indd 192

3/1/2023 1:01:24 PM

Solution of Algebraic and Transcendental Equations • 193

Therefore, a root of f(x) = 0 lies between 1 and 2. We apply the method of false position with a = 1 and b = 2. Equation (3.8) is xs 

a f (b)  b f (a) f (b)  f (a)

The calculations based on the method of false position are summarized in Table 3.6. TABLE 3.6 n 1

a 1

f(a)

b −2

2

f(b) 2

x s1 1.5

ξ

f( x s1 ) −3.4375



2

1.5

2

−3.4375

2

1.81609

−1.9895

0.17405

3

1.81609

2

−1.09895

2

1.88131

−0.16758

3.4666 × 10−2

4

1.88131

2

−0.16758

2

1.89049

−0.02232

4.85383 × 10−3

5

1.89049

2

−0.02232

2

1.89169

−0.00292

6.3902 × 10−4

6

1.89169

2

−0.00292

2

1.89185

−0.00038

8.34227 × 10−5

7

1.89185

2

−0.00038

2

1.89187

−0.00005

1.08786 × 10−5

The relative error after the seventh step is ξ

=

1.89187 − 1.89185 = 1.08786 × 10−5 1.89187

Hence, the root is 1.8918 accurate to four decimal places. MATLAB Solution: The following program (script file) uses the user-defined [root,ea,iter] = Falsep (Func,xl,xu,es,maxit,varargin) function to find the root of the equation: Provide the user-defined function y = f(x) here. >> Func = inline (‘x^4−11*x+8’); >> xl = 1;xu = 2;es = 1.0e−4;maxit = 50; >> [root,ea,iter] = Falsep(Func,xl,xu,es,maxit)

ANMF.CH03_4PP.indd 193

3/1/2023 1:01:25 PM

194 • Applied Numerical Methods Using MATLAB When the script is executed, the following solution is displayed in the ­command window: root = 1.8919 ea = 1.8494e−005 iter = 9

3.4 NEWTON-RAPHSON METHOD The Newton-Raphson method is the best-known method of finding roots of a function f(x). The method is simple and fast. One drawback of this method is that it uses the derivative f′(x) of the function as well as the function f(x). Hence, the Newton-Raphson method is usable only in problems where f′(x) can be readily computed. The Newton-Raphson method is also called Newton’s method. Here, again we assume that f(x) is continuous and differentiable and the equation is known to have a solution near a given point. Figure 3.4 illustrates the procedure used in Newton-Raphson method. The solution process starts by selecting point x1 as the first estimate of the solution. The second estimate x2 is found by drawing the tangent line to f(x) at the point (x1, f(x1)) and determining the intersection point of the tangent line with the x-axis. The next estimate x3 is the intersection of the tangent line to f(x) at the point (x2, f(x2)) with the x-axis, and so on. The slope, f′(x1), of the tangent at point (x1, f(x1)) is written as

f (x 1 ) 

f (x 1 )  0 (3.9) x1  x 2

Rewriting Equation (3.9) for x2 gives

ANMF.CH03_4PP.indd 194

x 2  x1 

f (x 1 ) (3.10) f (x 1 )

3/1/2023 1:01:31 PM

Solution of Algebraic and Transcendental Equations • 195

Equation (3.10) can be generalized for determining the next solution xi+1 from the current solution xi as x i 1  x i 

f (x i ) (3.11) f (x i )

FIGURE 3.4  The Newton-Raphson method.

The solution is obtained by repeated application of the iteration formula given by Equation (3.11) for each successive value of i. Algorithm for the Newton-Raphson Method: 1. Select a point x1 as an initial guess of the solution. 2. For i = 1, 2, …, until the error is smaller than a specified value, compute xi+1 by using Equation (3.11). Two error estimates that are generally used in Newton-Raphson method are given below: x  xi The iterations are stopped when the estimated relative error i 1 xi is smaller than a specified value ∈.

ANMF.CH03_4PP.indd 195

x i 1  x i  (3.12) xi

3/1/2023 1:01:38 PM

196 • Applied Numerical Methods Using MATLAB The iterations are stopped when the absolute value of f(xi) is smaller than some ­number δ: |f(xi)| ≤ δ(3.13) The Newton-Raphson method, when successful, works well and converges fast. Convergence problems occur when the value of f′(x) is close to zero in the vicinity of the solution, where f(x) = 0. The Newton-Raphson method generally converges when f(x), f′(x) and f″(x) are all continuous, if f′(x) is not zero at the solution and if the starting value x1 is near the actual solution. 3.4.1 Convergence of the Newton-Raphson Method The Newton-Raphson iteration formula is given by x i 1  x n 



f (x i )  (x n ) (3.13a) f (x i )

The general form of Equation (3.13a) is given by x = ϕ(x)(3.13b) The Newton-Raphson iteration method given by Equation (3.13b) converges if |ϕ′(x)| < 1. Here   (x)  x 

f (x i ) f (x i )

 [f (x)]2  f (x)f (x)  f (x)f (x) Hence  (x)  1     [f (x)]2 [f (x)]2   or

| (x) | 

f (x)f (x) [f (x)]2

Hence, Newton-Raphson’s method converges if f (x)f (x) 1 [f (x)]2 or |f(x)f″(x)| < [f′(x)]2(3.13c)

ANMF.CH03_4PP.indd 196

3/1/2023 1:01:52 PM

Solution of Algebraic and Transcendental Equations • 197

If α denotes the actual root of f(x) = 0, then we can select a small interval in which f(x), f′(x) and f″(x) are all continuous and the condition given by Equation (3.13c) is satisfied. Therefore, the Newton-Raphson method always converges provided the initial approximation x0 is taken very close to the actual root α. 3.4.2 Rate of Convergence of the Newton-Raphson Method Let α denote he exact value of the root of f(x) = 0, and let xi, xi+1, be two successive approximations to the actual root α. If εi and εi+1 are the corresponding errors, we have xi = α + εi and xi+1 = α + εi+1 by Newton-Raphson’s iterative formula

   i 1     i 



 i 1   i  

or 



 i 1

f (   i ) f (   i )

f (   i ) f (   i )

 2  2 f ()   i f ()   i  f ()  ... ()  i f ()  ... f  i 2 2  i   i  f ()   i f ()  .... f ()   i f ()  ... (since f (α) = 0)     i f ()  i f ()  ...  2i f () 2  1  i     f ()   i f ()  ... 2  f ()   i f ()  ...      2   i f () 1    i 1  f () (3.13d)    2f () 2 f ()    f ()  1   i f ()   ...     

Equation (3.13d) shows that the error at each stage is proportional to the sequence of the error in the previous stage. Hence, Newton-Raphson method has a quadratic convergence.

ANMF.CH03_4PP.indd 197

3/1/2023 1:02:05 PM

198 • Applied Numerical Methods Using MATLAB 3.4.3 MATLAB Program for the Newton Raphson Method function root = NewRap(func,dfunc,a,b,tol) % Newton-Raphson method for finding a root of f(x) = 0 % USAGE: root = NewRap(func,dfunc,a,b,tol) % INPUT % func = handle of function that returns f(x) % dfunc = handle of function that returns f′(x) % a,b = brackets (limits) of the root % tol = error tolerance (default is 1.0e6*eps) % OUTPUT % root = zero of f(x) (root = NaN if there is no convergence) if nargin < 5; tol = 1.0e6*eps; end fa = feval(func,a); fb = feval(func,b); if fa = = 0; root = a; return; end if fb = = 0; root = b; return; end if fa*fb > 0.0   error(‘Root was not bracketed in the interval(a,b)’) end x = (a + b)/2.0; for i = 1:30  fx = feval(func,x);   if abs(fx) < tol; root = x; return; end   % Tighten brackets   if fa*fx < 0.0; b = x;   else; a = x;  end

ANMF.CH03_4PP.indd 198

3/1/2023 1:02:05 PM

Solution of Algebraic and Transcendental Equations • 199

  % Newton-Raphson method  dfx = feval(dfunc,x);   if abs(dfx) = = 0; dx = b − a;   else; dx = −fx/dfx;  end  x = x + dx;   % If x not in the bracket, then use the bisection method   if (b − x)*(x − a) < 0.0   dx = (b − a)/2.0;   x = a + dx;  end   % Convergence check   if abs(dx) < tol*max(b,1.0)   root = x; return  end end root = NaN EXAMPLE 3.7 Use the Newton-Raphson method to find the real root near 2 of the equation x4 − 11x + 8 = 0 accurate to five decimal places. Solution: Here f(x) = x4 − 11x + 8 f′(x) = 4x3 − 11 x0 = 2 and f(x0) = f(2) = 24 − 11(2) + 8 = 2 f′(x0) = f′(2) = 4(2)3 − 11 = 21

ANMF.CH03_4PP.indd 199

3/1/2023 1:02:06 PM

200 • Applied Numerical Methods Using MATLAB

f (x 0 ) 2  2  = 1.90476 f (x 0 ) 21

Therefore, x 1  x 0 

x 2  x1 

f (x 1 ) (1.90476)4  11(1.90476)  8  1.90476  = 1.89209 f (x 1 ) 4(1.90476)3  11



x3  x2 

f (x 2 ) (1.89209)4  11(1.89209)  8  1.89209  = 1.89188 f (x 2 ) 4(1.89209)3  11



x4  x3 

f (x 3 ) (1.89188)4  11(1.89188)  8  1.89188  = 1.89188 f (x 3 ) 4(1.89188)3  11

Hence the root of the equation is 1.89188. MATLAB Solution: The following program (script file) uses the user-defined root = NewRap (func,dfunc,a,b,tol) function to find the root of the equation: Provide the user-defined function y = f(x) here. >> func=inline (‘x^4−11*x+8’); >> dfunc=inline(‘4*x^3−11’); >> root=NewRap(func,dfunc,1.8,2,1.0E05) When the script is executed, the following solution is displayed in the command window: root = 1.9000 MATLAB Solution Using MATLAB Built-in Function: fzero Provide the user-defined function y = f(x) here. function y = fex3_7(x) y = x^4−11*x+8; >> format long >> root=fzero(@fex3_7,x0)

ANMF.CH03_4PP.indd 200

3/1/2023 1:02:14 PM

Solution of Algebraic and Transcendental Equations • 201

When the script is executed, the following solution is displayed in the command window: root = 1.891876036561945 EXAMPLE 3.8 Using the Newton-Raphson method, find a root of the function f(x) = ex − 3x2 to an accuracy of 5 digits. The root is known to lie between 0.5 and 1.0. Take the starting value of x as x0 = 1.0. Solution: Start at x0 = 1.0 and prepare a table as shown in Table 3.7, where f(x) = ex − 3x2 and f′(x) = ex − 6x. The relative error 

x i 1  x i xi  1

The Newton-Raphson iteration method is given by x i 1  x i 

f (x i ) f (x i )

TABLE 3.7 i 0 1 2 3

xi 1.0 0.91416 0.91002 0.91001

f(xi) −0.28172 −0.01237 −0.00003 0

f′(xi) −3.28172 −2.99026 −2.97574 −2.97570

xi+1 0.91416 0.91002 0.91001 0.91001

ξ 0.09391 0.00455 0.00001 6.613 × 10− 11

MATLAB Solution using MATLAB Built-in Function: fzero Provide the user-defined function y = f(x) here. function y = fex3_8(x) y = exp(x)−3*x^2; >> x0=1.0; >>format long >> root=fzero(@fex3_8,x0)

ANMF.CH03_4PP.indd 201

3/1/2023 1:02:19 PM

202 • Applied Numerical Methods Using MATLAB When the script is executed, the following solution is displayed in the command window: root = 0.910007572488709 EXAMPLE 3.9 Evaluate

29 to five decimal places by Newton-Raphson iterative method.

Solution: Let x = 29 then x2 − 29 = 0. We consider f(x) = x2 − 29 = 0 and f′(x) = 2x The Newton-Raphson iteration formula gives x i 1  x i 



f (x i ) x 2  29 1  29   xi  i   x i   (E.1) 2x i 2 f (x i ) xi 

Now f(5) = 25 − 29 = −4 < 0 and f(6) = 36 − 29 = 7 > 0. Hence, a root of f(x = 0) lies between 5 and 6. Taking x0 = 3.3, Equation (E.1) gives

1 29  x 1   5.3   = 5.38585 2 5.3  1 29  x 2   5.38585   = 5.38516 2 5.38585  1 29  x 3   5.38516   = 5.38516 2 5.38516 

Since x2 = x3 up to five decimal places, 29 = 5.38516. MATLAB Solution: Provide the user-defined function y = f(x) here. function y = fex3_9(x) y = x^2−29;

ANMF.CH03_4PP.indd 202

3/1/2023 1:02:33 PM

Solution of Algebraic and Transcendental Equations • 203

The following program (script file) uses the user-defined root = NewRap (func,dfunc,a,b,tol) function to find the root of the equation: >> format long >> x0=5.0;x1=6.0; >> root=fzero(@fex3_9,[x0 x1]) When the script is executed, the following solution is displayed in the command window: root = 5.385164807134504 3.4.4 Modified Newton-Raphson Method Here, the iteration scheme is written as

x i 1  x i 

f (x i )  (x i ) (say) f (x i  a(x i )f (x i ))

or

(x)  x 

f (x ) f (x  a(x)f (x))

(3.13e)

where a(x) is a smooth function. Consider

f (x) f (x)f (x  a(x)f (x))(1  a (x)f (x)  a(x)f (x)) (3.13f)  f (x  a(x)f (x)) [f (x  a(x)f (x))]2 and f (x) f (x)f (x  a(x)f (x))[1  a (x)f (x)  a(x)f (x)] (x)  2 f (x  a(x)f (x)) [f (x  a(x)f (x))]2 (x)  1 



2 



ANMF.CH03_4PP.indd 203

f (x)[f (x  a(x)f (x))]2 [1  a (x)f (x)  a(x)f (x)]2 [f (x  a(x)ff (x))]2

f (x)f (x  a(x)f (x))[1  a (x)f (x)  a(x)f (x)]2 [f (x  a(x)f (x))]2

3/1/2023 1:02:52 PM

204 • Applied Numerical Methods Using MATLAB



[f (x)]2 f (x  a(x)f (x))a (x) [f (x  a(x)f (x))]2



f (x)f (x  a(x)f (x))[2a (x)f (x)  a(x)f (x)] (3.13g) [f (x  a(x)f (x))]2



If ξ is the root of the equation f(x) = 0, then f(ξ) = 0 and therefore ϕ(ξ) = ξ and ϕ′(ξ) = 0. Now, from Equation (3.13g) ()  

f () 2f ()f ()[1  a()f ()] f ()   [1  2a()f ()] (3.13h) f () [f ()]2 f ()

If

a()  

1 then ϕ″(ξ) = 0. 2f ()

Therefore, the iteration scheme in modified Newton-Raphson method is given by

x i 1  x i 

where

a(x i )  

f (x i ) (3.13i)  f [x i  a(x i )  f (x i )

1 2f (x i )

Equation (3.13i) can also be written as

x i 1  x i 

f (x i )f (x i ) (3.13j) [f (x i )]2  f (x i )f (x i )

In addition, we have

ϕ(ξ) = ξ, ϕ′(ξ) = 0 and ϕ′(ξ) = 0

(3.13k)

3.4.5 Rate of Convergence of Modified Newton-Raphson Method Let ξ be the root of the equation f(x) = 0. In addition, let ∈i = xi − ξ

ANMF.CH03_4PP.indd 204

3/1/2023 1:03:06 PM

Solution of Algebraic and Transcendental Equations • 205

Hence

xi+1 = ϕ(xi) = ϕ(∈i + ξ)

or i 1    ()  i ()  or i 1 

2i 3 ()  i ()  .... 2! 3!

3i ()  O(4i ) (3.13l) 3!

If we neglect the terms ∈4i and higher powers of ∈4i , Equation (3.13l) reduces to i 1  A 3i in which A 

1 () (3.13m) 3!

Equation (3.13m) shows that the rate of convergence of the modified NewtonRaphson method is cubic. EXAMPLE 3.10 Repeat Example 3.7 using a modified Newton-Raphson method. Solution:  f(x) = x4 − 11x + 8  f′(x) = 4x3 − 11

f″(x) = 12x2

The modified Newton-Raphson’s formula is x i 1  x i 

f (x i )f (x i ) [f (x i )]2  f (x i )f (x i )

The calculations are shown in Table 3.8. TABLE 3.8 n 0 1 2 3

xi 2 1.878261 1.891624 1.891876

f(xi) 2 −0.21505 −0.00405 −1.4 × 10−6

f′(xi)

f ″(xi)

21 15.50499 16.07476 16.08557

48 42.33437 42.93891 42.95034

xi+1 1.878261 1.891624 1.891876 1.891876

Hence, the root is 1.891876.

ANMF.CH03_4PP.indd 205

3/1/2023 1:03:19 PM

206 • Applied Numerical Methods Using MATLAB

3.5 SUCCESSIVE APPROXIMATION METHOD Suppose we are given an equation f(x) = 0 whose roots are to be determined. The equation can be written as x = f(x)

(3.14)

Let x = x0 be an initial approximation to the desired root α. Then, the first approximation x1 is given by x1 = ϕ(x0) The second approximation x2 = ϕ(x1). The successive approximations are then given by x3 = ϕ(x2), x4 = ϕ(x3), …, xn = ϕ(xn−1). The sequence of approximations of x1, x2, …, xn always converge to the root of x = ϕ(x) and it can be shown that if |ϕ′(x)| < 1, when x is sufficiently close to the exact value c of the root and xn → c as n → ∞. The convergence of xi+1 = ϕ(xn), for |ϕ′(x)| < 1 is shown in Figure 3.5. The following theorem presents the convergence criteria for the iterative sequence of solution for the successive approximation method. Theorem 3.5: Let α be a root of f(x) = 0 which is equivalent to x = ϕ(x), ϕ(x) is continuously differentiable function in an interval I containing the root x = α, if |ϕ′(x)| < 1, then the sequence of approximations x0, x1, x2, …, xn will converge to the root α provided the initial approximation x0 ∈ I.

FIGURE 3.5  Converge of xi+1 = ϕ(xn), for |ϕ′(x)| < 1.

ANMF.CH03_4PP.indd 206

3/1/2023 1:03:19 PM

Solution of Algebraic and Transcendental Equations • 207

Proof: Let α be the actual root of x = ϕ(x), then we can write α = ϕ(α)(3.15)



Let x = x0 be an initial approximation to the root, then x1 = ϕ(x0)(3.16) From Equations (3.15) and (3.16), we obtain α − x1 = ϕ(α) − ϕ(x0) By using the Lagrange’s mean value theorem, we can write

α − x1 = (α − x0)ϕ′(ξ0)

for x0 < ξ0 < α

Similarly,

α − x2 = (α − x1)ϕ′(ξ1)

for x1 < ξ1 < α



α − x3 = (α − x2)ϕ′(ξ2)

for x2 < ξ2 < α

α − xn = (α − xn−1)ϕ′(ξn−1)

for xn−1 < ξn−1 < α

and so on, or

Multiplying the above equations, we obtain α − xn = (α − x0)ϕ′(ξ0)ϕ′(ξ1)ϕ′(ξ2), …, ϕ′(ξn−1) If |ϕ′(xi)| ≤ κ < 1, for all I, then |xn − α| ≤ |x0 − α||ϕ′(ξ0)|ϕ′(ξ1)…|ϕ′(ξn−1)| ≤ κ, κ….κ|x0 − α| ≤ κn|x0 − α| As κ < 1 therefore kn → 0, as n → ∞, and thus we have xn → α, provided x0 ∈ I. Hence, the theorem is proved. 3.5.1 Error Estimate in the Successive Approximation Method n 1 / n )  () Let ∈n = xn − ξ, the error estimate at the nth iteration, then nlim( 

is satisfied. We know that |xn+1 − ξ| = |ϕ(xn) − ϕ(ξ)| = |ϕ′(ξn)||xn − ξ|, ξn∈(xn, ξ) by the mean value theorem. Hence n 1 n | (n ) |  lim(n 1 / n )  () n 

ANMF.CH03_4PP.indd 207

3/1/2023 1:03:22 PM

208 • Applied Numerical Methods Using MATLAB Therefore, we obtain the order of convergence as linear. But if ϕ′(ξ) = 0 and ϕ″(ξ) ≠ 0, then the Taylor series expansion of ϕ in a neighborhood of ξ is given by (x n )  ()  (x n  )() 

(x n  )2 ()  ... 2

which shows that n 1 n () 

2n 3 ()  n ()  .... 2 6

on using xn+1 = ϕ(xn) and ∈n = |xn+1 − ξ|. 2n () , on neglecting the terms containing cubes and higher 2 power of ∈n. This is a quadratic convergence. Hence, n 1  

EXAMPLE 3.11 Find a real root of x3 − 2x − 3 = 0, correct to three decimal places using the Successive Approximation method. Solution: Here f(x) = x3 − 2x − 3 = 0

(E.1)

3

Also f(1) = 1 − 2(1) − 3 = −4 < 0 and f(2) = 23 − 2(2) − 3 = 1 > 0 Therefore, root of Equation (E.1) lies between 1 and 2. Since f(1) < f(2), we can take the initial approximation x0 = 1. Now, Equation (E.1) can be rewritten as

x3 = 2x + 3

or x = (2x + 3)1/3 = ϕ(x) The successive approximations of the root are given by

x1 = ϕ(x0) = (2x0 + 3)1/3 = [2(1) + 3]1/3 = 1.25992



x2 = ϕ(x1) = (2x1 + 3)1/3 = [2(1.25992) + 3]1/3 = 1.31229

ANMF.CH03_4PP.indd 208

3/1/2023 1:03:31 PM

Solution of Algebraic and Transcendental Equations • 209



x3 = ϕ(x2) = (2x2 + 3)1/3 = [2(1.31229) + 3]1/3 = 1.32235



x4 = ϕ(x3) = (2x3 + 3)1/3 = [2(1.32235) + 3]1/3 = 1.32427



x5 = ϕ(x4) = (2x4 + 3)1/3 = [2(1.32427) + 3]1/3 = 1.32463

Hence, the real roots of f(x) = 0 is 1.324 correct to three decimal places. EXAMPLE 3.12 Find a real root of 2x − log10 x − 9 using the successive approximation method. Solution: Here f(x) = 2x − log10 x − 9 

f(4) = 2(4) − log10(4) − 9 = 8 − 0.60206 − 9 = −1.60206



f(5) = 2(5) − log10(5) − 9 = 10 − 0.69897 − 9 = 0.30103

(E.1)

Therefore, a root of Equation (E.1) lies between 4 and 5. Rewriting Equation (E.1) as 1 x  (log10 x  9) = ϕ(x) 2 We start with x0 = 4.

x1 = ϕ(x0) =



x2 = ϕ(x1) =



x3 = ϕ(x2) =



x4 = ϕ(x3) =



x5 = ϕ(x4) =

1 (log104 + 9) = 4.80103 2 1 (log104.80103 + 9) = 4.84067 2 1 (log104.84067 + 9) = 4.84245 2 1 (log104.84245 + 9) = 4.84253 2 1 (log104.84253 + 9) = 4.84254 2

Hence, x = 4.8425 is the root of Equation (E.1) correct to four decimal places.

ANMF.CH03_4PP.indd 209

3/1/2023 1:03:37 PM

210 • Applied Numerical Methods Using MATLAB EXAMPLE 3.13A Find a real root of cos x − 3x + 5 = 0. Correct to four decimal places using the successive approximation method. Solution: Here, we have

f(x) = cos x − 3x + 5 = 0



f(0) = cos(0) − 3(0) + 5 = 5 > 0



f(π/2) = cos(π/2) − 3(π/2) + 5 = −3π/2 + 5 < 0

(E.1)

Also f(0)f(π/2) < 0 Hence, a root of f(x) = 0 lies between 0 and π/2. The given Equation (E.1) can be written as Here

1 x  [5  cos x] 3 1 sin x (x)  [5  cos x] and (x)   3 3 | (x) |

sin x < 1 in (0, π/2) 3

Hence, the successive approximation method applies. Let x0 = 0

x1 = ϕ(x0) =

1 [5 + cos 0] = 2 3



x2 = ϕ(x1) =

1 [5 + cos(2)] = 1.52795 3



x3 = ϕ(x2) =



ANMF.CH03_4PP.indd 210

1 [5 + cos(1.52795)] = 1.68094 3 1 x4 = ϕ(x3) = [5 + cos(1.68094)] = 1.63002 3 1 x5 = ϕ(x4) = [5 + cos(1.63002)] = 1.64694 3

3/1/2023 1:03:52 PM

Solution of Algebraic and Transcendental Equations • 211



x6 = ϕ(x5)  =



x7 = ϕ(x6)  =



x8 = ϕ(x7)  =



x9 = ϕ(x8)  =



x10 = ϕ(x9) =

1 [5 + cos(1.64694)] = 1.64131 3 1 [5 + cos(1.64131)] = 1.64318 3 1 [5 + cos(1.64318)] = 1.64256 3 1 [5 + cos(1.64256)] = 1.64277 3 1 [5 + cos(1.64277)] = 1.64270 3

Hence, the root of the equation is 1.6427 correct to four decimal places.

3.6 SECANT METHOD The secant method is very similar to the Newton-Raphson method. The main disadvantage of the Newton-Raphson method is that the method requires the determination of the derivatives of the function at several points. Often, the calculation of these derivatives takes too much time. In some cases, a closedform expression for f′(x) may be difficult to obtain or may not be available. To remove this drawback of the Newton-Raphson method, the derivatives of the function being approximated by finite differences instead of being calculated analytically. In particular, the derivative f′(x) is approximated by the backward difference f (x i ) 

f ( x i )  f ( x i 1 ) (3.17) x i  x i 1

where xi and xi−1 are two approximations to the root but does not require the condition f(xi). f(xi−1) < 0. Now, from the Newton-Raphson method, we have

ANMF.CH03_4PP.indd 211

x i 1  x i 

f (x i ) f (x )(x  x i 1 )  xi  i i (3.18) f (x i ) f ( x i )  f ( x i 1 )

3/1/2023 1:04:00 PM

212 • Applied Numerical Methods Using MATLAB It should be noted here that from Equation (3.18) this method requires two initial guess values x0 and x1 for the root. The secant method is illustrated geometrically as shown in Figure 3.6, where a secant is drawn connecting f(xi−1) and f(xi). The point where it intersects the x-axis is xi+1. Another secant is drawn connecting f(xi) and f(xi+1) to obtain xi+2 and the process continues.

FIGURE 3.6  The secant method.

3.6.1 Convergence of the Secant Method The formula for the secant method can be written as x n 1  x n   

( x n  x n 1 ) (3.19) f ( x n )  f ( x n 1 )

Let ξ be the exact root of the equation f(x) = 0 and f(ξ) = 0. The error at the nth iteration is given by     εn = xn − ξ(3.20) Now Equation (3.19) becomes

ANMF.CH03_4PP.indd 212

 n 1   n   n 

( n   n 1 )f ( n  ) f ( n  )  f ( n 1  ) ( n   n 1 )[f ()   n f ()  (2n / 2)f ()  .....] 1 ( n   n 1 )f ()  ( 2n   2n 1 )f ()  ..... 2

3/1/2023 1:04:05 PM

Solution of Algebraic and Transcendental Equations • 213

1



  1   2 f () f ()   n   n  n  .... 1  ( n   n 1 )  ....   f f   2 ( ) 2 ( )   



1 f ()   n  n 1  O( 2n  n 1   n 2n 1 ) (3.21) 2 f ()

Equation (3.21) can be expressed as where

εn+1 = cεnεn−1(3.22) c

1 f () (3.23) 2 f ()

Equation (3.23) is a nonlinear difference equation which can be solved by letting  n 1  A np or  n  A np 1 and that gives

 n 1  1n/ p A 1 / p (3.24)

Hence A np  c n 1n/ p A 1 / p or

 np  cA (11 / p) 1n1 / p (3.25)

Now by equating the power of εn both sides of Equation (3.25), we obtain

p 1

or

p

1 p





1 1  5 (3.26) 2

Therefore taking the positive sign in Equation (3.26), we get

p = 1.618

and    n 1  A1n.618 (3.27) Hence, the rate of convergence of the secant method is 1.618, which is lesser than the Newton-Raphson method. The second method evaluates the function only once in each iteration, whereas the Newton-Raphson method evaluates two functions f and f ′ in each iteration. Therefore, the second method is more efficient than the Newton-Raphson method.

ANMF.CH03_4PP.indd 213

3/1/2023 1:04:29 PM

214 • Applied Numerical Methods Using MATLAB 3.6.2 MATLAB Program to Search for a Root of the Function f(x) in the Interval (a,b) Write up p. 144 kialass here The MATLAB program is listed below: function [x1,x2] = Rootsearch(func,a,b,dx) % Successive root search method for a root of f(x) % USAGE: [x1,x2] = Rootsearch(func,a,b,dx) % INPUT: % func = Handle of function % a,b = Limits of search % dx = Search increment % Output: % x1,x2 = Limits of the smallest root in (a,b) % NaN if no root was detected x1 = a; f1 = feval(func,x1); x2 = a + dx; f2 = feval(func,x2); while f1*f2 > 0.0 if x1 >= b x1 = NaN; x2 = NaN; return end x1 = x2;  f1 = f2; x2 = x1 + dx; f2 = feval(func,x2); end 3.6.3 MATLAB Program for Secant Method function root = Secant(func,xold,x,es,maxiter) % Secant method to find the root of a function % input: % func = Name of function

ANMF.CH03_4PP.indd 214

3/1/2023 1:04:29 PM

Solution of Algebraic and Transcendental Equations • 215

% xold, x = Initial guesses % es = Stopping criterion % maxiter = Maximum allowable iterations % output: % root = Real root if nargin func = inline (‘(x^3−8*x−5)’); >> a = 3.0;b = 3.5;dx = 0.01 >>[x1,x2]= Rootsearch(func,a,b,dx)

ANMF.CH03_4PP.indd 215

3/1/2023 1:04:29 PM

216 • Applied Numerical Methods Using MATLAB When the script is executed, the following solution is displayed in the ­command window: x1 = 3.1000 x2 = 3.1100 Hence, we conclude that the smallest positive root lies between x = 3.1 and x = 3.11. EXAMPLE 3.13C Find a root of the equation x3 − 8x − 5 = 0 using the secant method. Solution:

f(x) = x3 − 8x − 5 = 0



f(3) = 33 − 8(3) − 5 = −2



f(4) = 3.53 − 8(3.5) − 5 = 9.875

Therefore, one root lies between 3 and 4. Let the initial approximations be x0 = 3, and x1 = 3.5. Then, x2 is given by x2 

x 0 f (x 1 )  x 1 f (x 0 ) f (x 1 )  f (x 0 )

The calculations are summarized in Table 3.9. TABLE 3.9  Secant method. x0 3 3.5 3.08421 3.08788 3.10045

f(x0) −2 9.875 −0.33558 −0.05320 0.00039

x1 3.5 3.08421 3.09788 3.10045 3.10043

f(x1) 9.875 −0.33558 −0.05320 0.00039 0

x2 3.08421 3.09788 3.10045 3.10043 3.10043

f(x2) −0.33558 −0.05320 0.00039 0 0

Hence, a root is 3.1004 correct up to five significant figures.

ANMF.CH03_4PP.indd 216

3/1/2023 1:04:30 PM

Solution of Algebraic and Transcendental Equations • 217

MATLAB Solution: The following program (script file) uses the user-defined root = Secant(func, xold,x,es,maxiter) function to find the root of the equation: Provide the user-defined function y = f(x) here. function y = fex3_9(x) y = x^3−8*x−5; a = 3.0;b = 3.5; >> Root = Secant(@fex3_13a,a,b) When the script is executed, the following solution is displayed in the command window: Root = 3.1004 EXAMPLE 3.14 Determine a root of the equation sin x + 3 cos x − 2 = 0 using the secant method. The initial approximations x0 and x1 are 2 and 1.5. Solution: The formula for x2 is given by x2 

x 0 f (x 1 )  x 1 f (x 0 ) f (x 1 )  f (x 0 )

The calculations are summarized in Table 3.10. TABLE 3.10  Secant method x0 2 1.5 1.24488 1.21122 1.20788

f(x0) −2.33914 −0.79029 −0.09210 −0.00833 −0.00012

x1 1.5 1.24488 1.21122 1.20788 1.20783

f(x1) −0.79029 −0.09210 −0.00833 −0.00012 0

x2 1.24488 1.21122 1.20788 1.20783 1.20783

f(x2) −0.09210 −0.00833 −0.00012 0 0

Hence, a root is 1.2078 correct up to five significant figures.

ANMF.CH03_4PP.indd 217

3/1/2023 1:04:31 PM

218 • Applied Numerical Methods Using MATLAB MATLAB Solution: Provide the user-defined function y = f(x) here. function y = fex3_15(x) y = sin(x)−3*cos(x)−2; >> a = 0.0;b = 1.5; >> Root = Secant(@fex3_15,a,b) When the script is executed, the following solution is displayed in the ­command window: Root = 1.9338 EXAMPLE 3.15 Repeat Example 3.14 with initial approximations of x0 = −2 and x1 = −1.5. Solution: x2 is given by x2 

x 0 f (x 1 )  x 1 f (x 0 ) f (x 1 )  f (x 0 )

The calculations are summarized in Table 3.11. TABLE 3.11  Secant method. x0 −2 −1.5 −0.48529 −0.54918 −0.56485 −0.56432

f(x0) −4.15774 −2.78528 0.18715 0.03687 −0.00129 0.00001

x1 −1.5 −0.48529 −0.54918 −0.56485 −0.56432 −0.56433

f(x1) −2.78528 0.18715 0.03687 −0.00129 0.00001 0

x2 −0.48529 −0.54918 −0.56488 −0.56432 −0.56433 −0.56433

f(x2) 0.18715 0.03687 −0.00129 0.00001 0 0

Hence, a root is −0.5643 correct up to five significant figures.

ANMF.CH03_4PP.indd 218

3/1/2023 1:04:31 PM

Solution of Algebraic and Transcendental Equations • 219

MATLAB Solution: The following program (script file) uses the user-defined root = Secant(func, xold,x,es,maxiter) function to find the root of the equation: Provide the user-defined function y = f(x) here. When the script is executed, the following solution is displayed in the command window:

3.7 MULLER’S METHOD Muller’s method generalizes the secant method but uses quadratic interpolation among three points instead of linear interpolation between two. Muller’s method is an iterative method and free from the evaluation of derivative as in the Newton-Raphson method. It requires three starting points (xn−2, fn−2), (xn−1, fn−1), and (xn, fn). A parabola is constructed that passes through these points then the quadratic formula is employed to find a root of the quadratic for the next approximation. In other words, we assume that xn is the best approximation to the root, and consider the parabola through the three starting values as shown in Figure 3.7. We denote f(xn−2) = fn−2, f(xn−1) = fn−1 and f(xn) = fn.

FIGURE 3.7  Muller’s method.

Let the quadratic polynomial be   f(x) = ax2 + bx + c

ANMF.CH03_4PP.indd 219

(3.28)

3/1/2023 1:04:31 PM

220 • Applied Numerical Methods Using MATLAB If Equation (3.28) passes through the points (xn−2, fn−2), (xn−1, fn−1) and (xn, fn), then ax 2n  2  bx n  2  c  f n  2 ax n 1  bx n 1  c  f n 1 (3.29) 2

ax 2n  bx n  c  f n Eliminating a, b, c from Equation (3.29), we obtain the following determinant f (x ) x 2 f n  2 x 2n  2 f n 1 x 2n 1 fn x 2n

x x n 2 x n 1 xn

1 1  0 (3.30) 1 1

By expanding this determinant in Equation (3.30) the function f(x) can be written as

f (x ) 





(x  x n 1 )(x  x n ) (x  x n  2 )(x  x n ) fn 2  f n 1 (x n  2  x n 1 )(x n  2  x n ) (x n 1  x n  2 )(x n 1  x n )

(x  x n  2 )(x  x n 1 ) f n (3.31) (x n  x n  2 )(x n  x n 1 )

Equation (3.31) is a quadratic polynomial passing through the three given points. Let h = x − xn, hn = xn − xn−1 and hn−1 = xn−1 − xn−2. Now, Equation (3.31) becomes h(h  h n ) h(h  h n  h n 1 ) (h  h n )(h  h n  h n 1 ) fn 2  f n 1  f n  0 (3.32) h n 1 (h n 1  h n ) h n h n 1 h n (h n  h n 1 ) Noting f(x) = 0. Let

ANMF.CH03_4PP.indd 220



h h ,  n  n and n  1   n . hn h n 1

3/1/2023 1:04:47 PM

Solution of Algebraic and Transcendental Equations • 221

The Equation (3.32) now reduces to the following form:  2 (f n 1  2n f n 1  n n  f n  n )n1   f n  2  2n  f n  2 2n  f n ( n  n ) n1  f n  0 (3.33) or

λ2cn + λgn + δnfn = 0

where

gn   f



cn = λn(λnfn−2 − δnfn−1 + fn)

2 n n 2

 f

2 n n 1

(3.34)

 ( n  n )f n

Equation (3.34) can be written as

 1  g n f n  2   n  c n  0 (3.35)   

Solving Equation (3.35) for 1/λ, we obtain



2 n f n g n  g 2n  4n f n c n

(3.36)

The sign in the denominator of Equation (3.36) is ± according as gn > 0 or gn < 0. Hence  

x  xn or x = xn + (xn − xn−1)λ(3.37) x n  x n 1

Now, replacing x on left hand side by xn+1 in Equation (3.37), we obtain xn+1 = xn + (xn − xn−1)λ(3.38) Equation (3.38) is the Muller’s formula for the root and the procedure for finding a root is summarized in Table 3.12. TABLE 3.12  Muller’s method. hn = xn − xn−1,  n  h n , δn = 1 + λn h n2 g n   2n f n2  2n f n1  ( n  n )f n cn = λn(λnfn−2 − δnfn−1 + fn) 

2 n f n g n  g 2n  4n f n c n

xn+1 = xn + (xn − hnλ)

ANMF.CH03_4PP.indd 221

3/1/2023 1:05:12 PM

222 • Applied Numerical Methods Using MATLAB 3.7.1 MATLAB Program for Muller’s Method function [x, k] = Muller (x0, x1, x2, tol, maxiter, f, dm) % Usage: [x, k] = Muller (x0, x1, x2, tol, maxiter, f) % Muller method to find a root of f(x) = 0 % Input:  x0 = First initial guess % x1 = Second initial guess (x1 x0) % x2 = Third initial guess (x2 x0, x2 x1) toll = Error tolerance to terminate the search

%

% (tol >= 0) % maxiter = Maximum number of iterations (maxiter >= 1) % f = User-supplied function y = f(x) % dm = optional display mode. If input, intermediate results are displayed % Output: x = Estimated root of f(x) = 0 % k = Number of iterations performed. If k < maxiter, then |f(x)| < eps

%

% Initialization if (abs(x1−x0) < eps) | (abs(x2−x0) < eps) | (abs(x2−x1) < eps)   fprintf (‘Secant requires that x2 x1 x0.\n’);  return end display = nargin > 6; f0 = feval (f, x0); f1 = feval (f, x1); f2 = feval (f, x2); if display

ANMF.CH03_4PP.indd 222

3/1/2023 1:05:12 PM

Solution of Algebraic and Transcendental Equations • 223

end % Search for root k = 1; y = eps + 1; while (y > eps) & (k abs (ca)  ca = cb;  end  c = 2*c0;  cb = c/ca;  x3 = x2 − cb;  x0 = x1;  f0 = f1;  x1 = x2;

ANMF.CH03_4PP.indd 223

3/1/2023 1:05:12 PM

224 • Applied Numerical Methods Using MATLAB  f1 = f2;  x2 = x3;  f2 = feval (f, x3);  y = abs (f2);   if display  end  k = k + 1; end % Final results x = x2; k = k−1; EXAMPLE 3.16 Find a root of the equation x3 − 3x − 7 = 0 using the Muller’s method where the root lies between 2 and 3. Solution: Let x0 = 2, x1 = 2.5 and x2 = 3. The calculations are shown in Table 3.13(a) and 3.13(b). TABLE 3.13(a)  Muller’s method. n

xn−2

xn−1

xn

hn

λn

hn−1

δn

2

2

2.5

3

0.5

0.5

1

2

3

2.5

3

2.4272

−0.5728

0.5

−1.14559

−0.14559

4

3

2.4272

2.42599

−0.00122

−0.5728

0.00213

1.00213

5

2.4272

2.42599

2.42599

0

−0.00122

−0.0029

0.99710

TABLE 3.13(b)  Muller’s method. n

fn−2

fn−1

fn

gn

cn

λ

xn+1

2

−5

1.125

11

23.5

3.75

−1.14559

2.42720

3

1.125

11

0.01781

1.22026

−0.37867

0.00213

2.42599

4

11

0.01781

−0.00005

−0.01789

0.00001

−0.0029

2.42599

5

0.01781

−0.0005

0

0.00005

0

−0.00005

2.42599

Hence one root is 2.42599 correct up to five decimal places.

ANMF.CH03_4PP.indd 224

3/1/2023 1:05:13 PM

Solution of Algebraic and Transcendental Equations • 225

MATLAB Solution: The following program (script file) uses the user-defined [x, k] = Mullert(x0, x1, x2, tol, maxiter, f, dm) function to find the root of the equation: Provide the user-defined function y = f(x) here. >> [x, k] =Muller(x0, x1, x2, tol, maxiter, f, 1) When the script is executed, the following solution is displayed in the command window: x= 2.425988757361622 k= 4

3.8 CHEBYSHEV METHOD Consider the equation to be solved as f(x) = 0. The function f(x) can be expanded by Taylor’s series in the neighborhood of xn as 0 = f(x) = f(xn) + x − xn)f′(xn) + ….

(3.39)

Equation (3.39) gives

x  xn 

f (x n ) (3.40) f (x n )

Equation (3.40) gives the (n + 1)th approximation to the root. Hence

x n 1  x n 

f (x n ) (3.41) f (x n )

Once again, we expand f(x) by Taylor’s series and retain up to the second order term, we obtain

ANMF.CH03_4PP.indd 225

0  f (x)  f (x n )  (x  x n )f (x n ) 

(x  x n )2 f (x n ) (3.42) 2

3/1/2023 1:05:20 PM

226 • Applied Numerical Methods Using MATLAB

Hence f (x n 1 )  f (x n )  (x n 1  x n )f (x n ) 

(x n 1  x n )2 f (x n )  0 (3.43) 2

Substituting the value of xn+1 − xn from Equation (3.41) to the last term and we obtain f (x n )  (x n 1  x n )f (x n ) 



Hence x n 1  x n 

1 [f (x n )]2 f (x n )  0 (3.44) 2 [f (x n )]2

f (x n ) 1 [f (x n )]2  f (x n ) (3.45) f (x n ) 2 [f (x n )]3

Equation (3.45) can be recognized as the extended form of the NewtonRaphson formula and it is called the Chebyshev’s formula. The rate of convergence of this method can be shown to be cubic.

3.9 AITKEN’S Δ2 METHOD Suppose we have an equation f(x) = 0

(3.46)

whose roots are to be determined. Let I be an interval containing the point x = α. Now, Equation (3.46) can be written as x = ϕ(x) such that ϕ(x) and ϕ′(x) are continuous in I and |ϕ′(x)| < 1 for all x in I. Denoting xi−1, xi and xi+1 as the three successive approximations to the desired root α, we can write and

α − xi = λ(α − xi−1)(3.47) α − xi+1 = λ(α − xi)(3.48)

where λ is a constant so that |ϕ′(x)| ≤ λ ≤ 1 for all i. Dividing Equation (3.47) with Equation (3.48), we obtain

ANMF.CH03_4PP.indd 226

  xi   x i 1  (3.49)   x i 1   xi

3/1/2023 1:05:30 PM

Solution of Algebraic and Transcendental Equations • 227

Equation (3.49) gives   x i 1 



(x i 1  x i )2 (3.50) ( x i  1  2 x i  x i 1 )

Now

Δxi = xi+1 − xi

and

Δ2xi−1 = (E − 1)2xi−1 = (E2 − 2E + 1)xi−1 = xi+1 − 2xi + xi−1(3.51)

Using Equation (3.51), Equation (3.50) can be written as

  x i 1 

(x i )2 (3.52)  2i 1

Equation (3.52) gives the successive approximation to the root α and method is known as the Aitken’s Δ2 method. Δ

Δ2

xi−1 Δxi−1 Δ2xi−1

xi Δxi xi+1

EXAMPLE 3.17

 1  cos x  Find the root of the function x    correct to four decimal places 3  using Aitken’s iteration method.  Solution: >> fplot (‘1+cos(x)−3*x’, [0, 1]) >> xlabel (‘x’); ylabel (‘f(x)’) The plot of the function shown in Figure 3.8 shows that the function has one solution between 0 and 1.0.

ANMF.CH03_4PP.indd 227

3/1/2023 1:05:36 PM

228 • Applied Numerical Methods Using MATLAB

FIGURE 3.8



f(x) = cos x − 3x + 1



f(0) = 1



f(π/2) = cos(π/2) − 3(π/2) + 1 = −3.005

(E.1)

Hence f(0) > 0 and f(π/2) < 0 Also

f(0) f(π/2) = 1(−3.005) = −3.005 < 0

Therefore, a root exists between 0 and π/2. Equation (E.1) can be written as

 1  cos x  x    (x) 3  

Now

(x) 

 sin x  sin x    (x)   1  x   0,  (E.2) 3 3  2

Equation (E.2) signifies that Aitken’s method can be employed.

ANMF.CH03_4PP.indd 228

3/1/2023 1:05:41 PM

Solution of Algebraic and Transcendental Equations • 229

Let x0 = 0 be an initial approximation to the root of (E.1).

 1  cos 0  x 1  (x 0 )     0.66667 3  



x 2  (x 1 ) 

1  cos(0.66667)  0.59530 3



x 3  (x 2 ) 

1  cos(0.59530)  0.60933 3

We can now construct the table as shown in Table 3.14. Table 3.14  Aitken’s method. x x1 = 0.66667

Δx 0.07137 x 1

0.08540 ∆ 2 x1

x2 = 0.59530

x3 = 0.60933

Therefore x 4  x 3 

Δ2

0.01403 ∆x 2

(x 2 )2 (0.01403)2  0 . 60933   0.60703. ( 2 x 1 ) (0.08540)

Hence, the root is 0.6070 correct to four decimal places.

3.10 BRENT’S METHOD Brent’s method is an efficient technique for finding the roots of equation. The method is based on the integration of bisection and quadratic interpolation. The method is most suitable for problems where the derivative of the function is difficult to compute. Brent’s method starts with the assumption that a root of f(x) = 0 has been bracketed in the interval (x1, x2). The root-finding procedure begins with a bisection step, namely, halving the interval to (x1, x3) or (x3, x2) as shown in Figure 3.9. Here x3 = (x1 + x3)/2. The three points f1 = f(x1), f2 = f(x2), and

ANMF.CH03_4PP.indd 229

3/1/2023 1:06:00 PM

230 • Applied Numerical Methods Using MATLAB f3 = f(x3) would facilitate the next iteration of the root by the inverse quadratic interpolation (see Chapter 5). If the result obtained falls inside the latest bracket (Figure 3.9), the procedure is stopped. Otherwise, another round of bisection procedure is applied.

FIGURE 3.9  Inverse quadratic iteration.

The next step is to relabel the new interval as (x1, < x3 < x2) as shown in Figure 3.10. The interval containing the root has been reduced in this first iteration cycle. The next cycle another inverse quadratic interpolation is repeated and the process continued until the convergence criterion |x − x3| < ε is fulfilled. Here, ∈ is a prescribed tolerance.

FIGURE 3.10  Relabeling points after iteration.

ANMF.CH03_4PP.indd 230

3/1/2023 1:06:00 PM

Solution of Algebraic and Transcendental Equations • 231

Lagrange’s three-point interpolation (described in Chapter 5) is used for the inverse quadratic interpolation. Hence, we can write x(f ) 

(f  f2 )(f  f3 ) (f  f1 )(f  f 3 ) (f  f1 )(f  f2 ) x1  x2  x 3 (3.53) (f1  f2 )(f1  f3 ) (f2  f1 )(f 2  f3 ) (f3  f1 )(f3  f2 )

Substituting f = 0 and simplifying, we obtain for the estimate of the root as x  x(0) 

f2 f3 f1 (f2  f3 )  f3 f1 f2 (f3  f1 )  f1 f2 f3 (f1  f2 ) (3.54) (f1  f2 )(f2  f3 )(f3  f1 )

The change in the root is given by x  x  x 3  f3

x 3 (f1  f2 )(f2  f3  f1 )  f2 x 1 (f2  f3 )  f1 x 2 (f3  f1 ) (3.55) (f2  f1 )(f3  f1 )(f2  f3 )

3.10.1  MATLAB Program for Brent’s Method function root = Brent (func, a, b, tol) % Finds a root of f(x) = 0 by combining quadratic interpolation with Brent’s method % Usage: % root = Brent (func, a, b, tol) % INPUT: % func = Handle of function that returns f(x) % a, b = Limits of the interval containing the root % tol = Error tolerance [default value is 1.0e6*eps] % OUTPUT: % root = Zero of f(x) (root = NaN when convergence fails) if nargin < 4; tol =1.0e6*eps; end

ANMF.CH03_4PP.indd 231

3/1/2023 1:06:07 PM

232 • Applied Numerical Methods Using MATLAB %Use bisection method x1 = a; f1 = feval (func, x1); if f1 = = 0; root = x1; return; end x2 = b; f2 = feval (func, x2); if f2 = = 0; root = x2; return; end if f1*f2 > 0.0 error (‘Failed to bracket the root in (a, b)’) end x3 = 0.5*(a + b); % Iterative loop starts here for i = 1:30 f3 = feval (func, x3); if abs (f3) < tol root = x3; return end if f1*f3 < 0.0; b = x3; else; a = x3; end if (b − a) < tol*max (abs (b), 1.0) root = 0.5*(a + b); return end

ANMF.CH03_4PP.indd 232

3/1/2023 1:06:07 PM

Solution of Algebraic and Transcendental Equations • 233

% Quadratic interpolation den = (f2 − f1)*(f3 − f1)*(f2 − f3); num = x3*(f1 − f2)*(f2 − f3 + f1)... + f2*x1*(f2 − f3) + f1*x2*(f3 − f1); if den == 0; dx = b − a; else; dx = f3*num/den; end x = x3 + dx; if (b − x)*(x − a) < 0.0 dx = 0.5*(b − a); x = a + dx; end if x < x3 x2 = x3; f2 = f3; else x1 = x3; f1 = f3; end x3 = x; end root = NaN; EXAMPLE 3.18 Compute the zero of f(x) = ex + x − 2 that lies in the interval (0, 0.5) with Brent’s method. MATLAB Solution: The plot of the function is obtained by typing >> fplot (‘exp(x) +x−2’, [0, 1]) >> xlabel (‘x’); ylabel (‘f(x)’)

ANMF.CH03_4PP.indd 233

3/1/2023 1:06:07 PM

234 • Applied Numerical Methods Using MATLAB The plot of the function shown in Figure 3.11 shows that the function has one solution between 0 and 0.5.

FIGURE 3.11

The following program (script file) uses the user-defined root = Secant (func, xold, x, es, maxiter) function to find the root of the equation: Provide the user-defined function y = f(x) here. function y = fex3_19(x) y = exp(x) + x−2; >>format long >> Brent (@fex3_19, 0.0, 0.5) When the script is executed, the following solution is displayed in the ­command window: ans = 0.442854401004286

ANMF.CH03_4PP.indd 234

3/1/2023 1:06:07 PM

Solution of Algebraic and Transcendental Equations • 235

function root = Newton2 (func, x, tol) % Newton’s method for finding a root of simultaneous % equations fi(x1, x2,..., xn) = 0, i = 1,2,...,n % Usage: root = Newton2(func,x,tol) % INPUT: % func = Handle of function that returns[f1,f2,...,fn] % x = Initial solution vector [x1,x2,...,xn] % tol = Error tolerance (default value is 1.0e4) % OUTPUT: % root = Solution vector if nargin = = 2; tol = 1.0e4*eps; end if size(x,1) = = 1; x = x’; end for i = 1:30 [jac,f0]=jacobian(func,x); if sqrt(dot(f0,f0)/length(x)) < tol  root = x;  return end dx = jac\(−f0); x = x + dx; if sqrt(dot(dx,dx)/length(x)) < tol*max(abs(x),1.0)  root = x;  return end end error(‘Execessive iterations’)

ANMF.CH03_4PP.indd 235

3/1/2023 1:06:07 PM

236 • Applied Numerical Methods Using MATLAB function [jac,f0] = jacobian(func,x) h = 1.0e−4; n = length(x); jac = zeros(n); f0 = feval(func,x); for i = 1:n temp = x(i); x(i) = temp + h; f1 = feval(func,x); x(i) = temp; jac(:,i) = (f1 − f0)/h; end

3.11 NEWTON METHOD FOR A SYSTEM OF NONLINEAR EQUATIONS We note that the methods mentioned so far can handle only one scalar equation with respect to one variable. In order to obtain a solution for a system of nonlinear equations numerically, we rewrite the two equations

f1(x1, x2) = 0

(3.56)



f2(x1, x2) = 0

(3.57)

Taking the Taylor series expansion up to first order about some estimate point (x1k, x2k) as f ( x 1 , x 2 )  f1 ( x 1 k , x 2 k ) 

f (x 1 , x 2 )  f 2 (x 1 k , x 2 k ) 

ANMF.CH03_4PP.indd 236

f1 x 1 f2 x 1

(x 1  x 1 k )  ( x1 k , x 2 k )

(x 1  x 1 k )  ( x1 k , x 2 k )

f1 f2 f2 f2

(x 2  x 2 k )  0 ( x1 k , x 2 k )

(x 2  x 2 k )  0 (3.58) ( x1 k , x 2 k )

3/1/2023 1:06:11 PM

Solution of Algebraic and Transcendental Equations • 237

Equation (3.58) can be rearranged into a matrix-vector form as  f1 f ( x , x ) f ( x , x )  1 1 2   1 1 k 2 k   xx 1 f (x , x )  f (x , x )   f  2 1 2   2 1k 2 k   2  x  1

1

f1  x 2   f2  x 2  ( x

 x1  x1k  0   x  x   0  (3.59) 2k     2 1 k ,x2 k )

We solve Equation (3.59) for (x1, x2) to obtain the updated vector estimate  f1  x x  1,k 1   1 k  x 1 x    x    f 2 , k  1    2k   2  x  1

f1  x 2   f2  x 2  ( x

where the Jacobian

 f1 ( x 1 k , x 2 k )  f (x , x ) (3.60)  2 1k 2 k  1 k ,x2 k )

x k 1  x k  Jk 1 f (x k ) (3.61)

 f  Jk (m, n)   m     f n  x k Equation (3.61) is very similar to the Newton’s iteration formula for a single variable. The MATLAB program to implement this method is given below: MATLAB Program EXAMPLE 3.19 Newton’s method to solve the following system of equations in the vicinity of the point (1, 1). sin x + 3 cos x − 3 = 0 cos x − sin y + 0.3 = 0

ANMF.CH03_4PP.indd 237

3/1/2023 1:06:21 PM

238 • Applied Numerical Methods Using MATLAB Solution: The following program (script file) uses the user-defined Newton2 function to find the root of the equation: Provide the user-defined function y = f(x) here. function y = fex3_20(x) y = [sin(x(1)) + 3*cos(x(2)) − 3;cos(x(1)) − sin(x(2)) + 0.3]; >> Newton2(@fex3_20,[1;1]) When the script is executed, the following solution is displayed in the command window: ans = 1.1406 0.7995

3.12 COMPARISON OF ITERATIVE METHODS The bisection method and the method of false position always converge to an answer, provided a root is bracketed in the interval (a, b) to start with. Since the root lies in the interval (a, b), on every iteration the width of the interval is reduced until the solution is obtained. The Newton-Raphson method and the method of successive approximations require only one initial guess and on every iteration it approaches to the true solution or the exact root. The bisection method is guaranteed to converge. The bisection method may fail when the function is tangent to the axis and does not cross the x-axis at f(x) = 0. The bisection method, the method of false position, and the method of successive approximations converge linearly while the Newton-Raphson method converges quadratically. Newton-Raphson method requires a lesser number of iterations than the other three methods. One disadvantage with Newton-Raphson method is that when the derivative f′(xi) is zero, a new starting or initial value of x must be selected to continue with the iterative procedure. The successive approximation method converges only when the condition |ϕ′(x)| < 1 is satisfied. Table 3.15 gives a summary of the comparison of the methods presented in this chapter.

ANMF.CH03_4PP.indd 238

3/1/2023 1:06:21 PM

Solution of Algebraic and Transcendental Equations • 239

TABLE 3.15  Comparison of the methods.

No

Method

1.

Bisection

2.

False position

3.

Newton-Raphson

4.

Modified Newton-Raphson

5.

Successive approximation

6.

Secant

7.

Muller

8.

Chebyshev

Order of convergence

Formula

Functional evaluations at each step

x s1 

ab 2

One bit/ iteration gain

1

xs 

af (b)  bf (a) f (b)  f (a)

1

1

x i 1  x i 

f (x i ) f (x i )

2

2

x i 1  x n 

fn f   x n  12 f n / f n 

3

3

1

1

1.62

1

1.84

1

3

3

x1 = ϕ(x0) x i 1  x i 

f (x i )(x i  x i 1 ) f (x i )  f (x i 1 )

xn+1 = xn + (xn − xn−1)λ x n1  x n 

f n 1 f n2  f n f n 2 f n3

3.13 MATLAB BUILT-IN FUNCTION: FZERO In MATLAB, a zero of a function can be determined with the command (built-in function) fzero that has the form: x = fzero (function, x0), where x is the solution, and function is the function to be solved. It can be entered in several different ways: enter the mathematical expression as a string; the function is first created as a user-defined function in a function file and then the function handle is entered; the function is first created as an anonymous (which is the name of the handle) is entered. The function has to be written in a standard form. When a function is entered as an expression (string), it cannot include predefined variables. x0 = a value of x near to where the function crosses the x axis. x0 can be a scalar or a two-element vector. If it is entered as a scalar, it has to be a value of x near the point where the function crosses the x axis. If x0 is entered as a vector, the two elements have to be points on opposite sides of the solution

ANMF.CH03_4PP.indd 239

3/1/2023 1:06:36 PM

240 • Applied Numerical Methods Using MATLAB that f(x0(1)) has a different sign than f(x0(2)). When a function has more than one solution, each solution can be determined separately by using the fzero function and entering values for x0 that are near each of the solutions. EXAMPLE 3.20 Find a root of the equation x3 − 4x − 8.95 = 0 accurate to three decimal places using the MATLAB built in command fzero. Solution: The plot of the function is obtained by typing >> fplot(‘x^3−4*x−8.95’,[0,10]) The plot of the function shown in Figure 3.12 below, shows that the function has one solution between 2 and 3.

FIGURE 3.12

With a guess value of 0.2 for x, we use the built-in function fzero as shown below: >> x = fzero(‘x^3−4*x−8.95’,0.2)

ANMF.CH03_4PP.indd 240

3/1/2023 1:06:36 PM

Solution of Algebraic and Transcendental Equations • 241

x= 2.7037 Hence, the solution is x = 2.7037. EXAMPLE 3.21 Find one root of ex − 3x = 0 correct to two decimal places using the MATLAB command fzero. Solution: The plot of the function is obtained by typing >> fplot(‘exp(x)−3*x’,[0,5]) The plot of the function shown in Figure 3.13 below, shows that the function has one solution between 0.5 and 0.7.

FIGURE 3.13

ANMF.CH03_4PP.indd 241

3/1/2023 1:06:36 PM

242 • Applied Numerical Methods Using MATLAB With a guess value of 0.0 for x, we use the built-in function fzero as shown below: >> x1=fzero(‘exp(x)−3*x’,0.0) x1 = 0.6191 Hence, the solution is x = 0.6191. EXAMPLE 3.22 Find the roots of xe-x − 0.3 = 0 correct to three decimal places using the MATLAB command fzero. Solution: The plot of the function is obtained by typing >> fplot(‘x*exp(x)−0.3’,[0,10]) The plot of the function shown in Figure 3.14 below, shows that the function has two solutions: the first solution is between 0 and 0.5, and the second solution is between 1 and 2.

FIGURE 3.14

ANMF.CH03_4PP.indd 242

3/1/2023 1:06:36 PM

Solution of Algebraic and Transcendental Equations • 243

With a guess value of 0.0 for x, we use the built-in function fzero as shown below: >> x1 = fzero(‘x*exp(−x)−0.3’,0.0) x1 = 0.4894 Hence, the first solution is x = 0.4894. >> % Using the name of the anonymous function in fzero >> F = @(x) x*exp(−x)−0.3 F= @(x)x*exp(−x)−0.3 >> fzero (F, 2.0) ans = 1.7813 The second solution is x = 1.7813.

3.14 SUMMARY In this chapter, the techniques for the numerical solution of algebraic and transcendental equations have been presented. Numerical methods involving iterative solution of nonlinear equations are more powerful. These methods can be divided into two categories: Direct methods and indirect (or iterative) methods. The indirect or iterative methods are further divided into two categories: bracketing and open method. The bracketing methods require the limits between which the root lies, whereas the open methods require the initial estimation of the solution. Bisection and false position methods are two known examples of the bracketing methods. For functions that are smooth near a root, the methods known for respectively as false position (or regula falsi) and secant method generally converge faster than bisection. In both of these methods the function is assumed to be approximately linear in the local region of interest, and the next improvement in the root is taken as the point where the approximating line crosses the axis. Among the open methods, the Newton-Raphson and the method of successive approximation are most commonly used. We have also presented the secant method, Muller’s

ANMF.CH03_4PP.indd 243

3/1/2023 1:06:36 PM

244 • Applied Numerical Methods Using MATLAB method, Chebyshev’s method, and Aitkin’s Δ2 method. The most popular method for solving a nonlinear equation is the Newton-Raphson method, and this method has a quadratic rate of convergence. Brent’s method combines root bracketing, bisection, and inverse quadratic interpolation to converge from the neighborhood of a zero crossing. Brent’s method is a good choice for general one-dimensional root finding where a function’s values only (and not its derivative or functional form) are available. In addition, Newton’s method for solving a system of nonlinear equations is presented. All these methods have been illustrated with examples.

EXERCISES NOTE

In addition to solving the following exercises through analytical solutions or long-hand numerical computations, try to solve them using user-defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 3.1 3.2

Use the bisection method to find a solution accurate to four decimal places for x = tan x in the interval (4.4, 4.6). 9 Determine the solution of the equation 8 − (x − sin x) = 0 by using 2 the bisection method accurate to five decimal places in the interval (2, 3).

3.3

Use the bisection method to compute the root of ex − 3x = 0 correct to three decimal places in the interval (1.5, 1.6).

3.4

Find the root of log x = cos x and correct to two decimal places using bisection method.

3.5

Use the bisection method to find a root of the equation x3 − 4x − 9 = 0 in the interval (2, 3), accurate to four decimal places.

3.6

Use the bisection method to determine a root correct to three decimal places of the equation x long10 x = 1.2. Interval (2, 3).

3.7

Use the bisection method to find a root of the equation 4.905t2 − 15t + 5 = 0 in the interval (0.3, 0.4) with an accuracy of 4 digits.

3.8

Use bisection method to find the root of f(x) = x3 − 10x2 + 5 = 0 that lies in the interval (0.6, 0.8) correct within four decimal places.

ANMF.CH03_4PP.indd 244

3/1/2023 1:06:37 PM

Solution of Algebraic and Transcendental Equations • 245

Use bisection method to find the root of f(x) = x − tan x in the interval (7, 8) correct to four decimal places. 1 3.10 Use bisection method to find the smallest positive root of cos x = + 2 sin x in the interval (0.41, 043). Use an error tolerance of ∈ = 0.0001. 3.9

3.11 Use the method of false position to find solution accurate to within 10−4 for the function f(x) = x − cos x in the interval (0, π/2). 3.12 Use the method of false position to find solution accurate to within 10−4 for the function f(x) = x − 0.8 − 0.2 sin x = 0 in the interval (0, π/2). 3.13 Repeat Problem 4.6 correct to four decimal places using the false position method. 3.14 Repeat Problem 4.7 correct to four decimal places using the false position method. 3.15 Use the method of false position to solve the equation x tan x + 1= 0 accurate to three decimal places starting with 2.5 and 3.0 as the initial approximations to the root. 3.16 Use method of false position to solve the equation x log x − 1 = 0 correct to three significant figures. 3.17 Use the method of false position to solve the equation xex − cos x = 0 correct to four decimal places in the interval (0, 1). 3.18 Use the method of false position to find a root correct to three decimal places for the function tan x − 4x = 0. 3.19 Use the method of false position to find a root of f(x) = ex − 2x2 = 0 with an accuracy of four digits. The root lies between 1 and 1.5. 3.20 Use the method of false position to find a root correct to three decimal places of the function x3 − 4x − 9 = 0. 3.21 A root of f(x) = x3 − 10x2 + 5 = 0 lies close to x = 0.7. Determine this root with the Newton-Raphson method to five decimal accuracy. 3.22 A root of f(x) = ex − 2x2 lies in the interval (1, 2). Determine this root with the Newton-Raphson method to five decimal accuracy. 3.23 A root of f(x) = x3 − x2 − 5 = 0 lies in the interval (2, 3). Determine this root with the Newton-Raphson method for four decimal places.

ANMF.CH03_4PP.indd 245

3/1/2023 1:06:40 PM

246 • Applied Numerical Methods Using MATLAB 3.24 Use Newton-Raphson method to find solution accurate to within 10−4 for the function f(x) = x − cos x in the interval (0, π/2). 3.25 Use Newton-Raphson method to find solution accurate to within 10−4 for the function f(x) = x − 0.8 − 0.2 sin x = 0 in the interval (0, π/2). x 2 x 3 0.3 x  e lies in the inter2 6 val (2, 3). Use Newton-Raphson method to find this root accurate to five decimal places.

3.26 A positive root of the equation e x  1  x 

3.27 Use Newton-Raphson method to find the smallest positive root of the equation tan x = x accurate to four decimal places. 3.28 Determine the positive root of the equation x = 2 sin x accurate to three decimal places. 3.29 Use the Newton-Raphson method to estimate the root of f(x) = e−x − x with an initial guess of x0 = 0 accurate to five decimal places. x2 x3 x4    ...  0 has one root in the 4 36 576 interval (0, 1). Determine this root correct to five decimal places.

3.30 The equation f (x)  0.1  x 

3.31 Use the successive approximation method to find correct to four significant figures a real root of cos x − 3x + 1 = 0. 3.32 Use the successive approximation method to find correct to four significant figures a real root of e-x − 10x = 0. 3.33 Use the successive approximation method to find correct to four decimal places a real root of 2x− log10 x − 7 = 0. 3.34 Use the successive approximation method to find correct to four significant figures a real root of the function ex tan x − 1 = 0. 3.35 Find the real root of the equation x − sin x − 0.25 = 0 to three significant digits using the successive approximation method. 3.36 Use the method of successive approximation to find a root of the equation ex − 3x = 0 in the interval (0, 1) accurate to four decimal places. 3.37 Use the method of successive approximation to find a real root of ex − x2 = 0 correct to four significant figures.

ANMF.CH03_4PP.indd 246

3/1/2023 1:06:45 PM

Solution of Algebraic and Transcendental Equations • 247

3.38 Use the method of successive approximation to determine a solution accurate to within 10−2 for x4 − 3x2 − 3 = 0 on [1, 2]. Use x0 = 1. 3.39 Find a root of the equation x3 − 3x2 + 4 = 0 using the modified NewtonRaphson method, starting with x0 = 1.8. 3.40 Find a root of the following function with an accuracy of 4 digits using modified Newton-Raphson method, starting with x0 = 1.4. f(x) = ex − 2x2 = 0. 3.41 Find a root of the equation x3 − 8x − 4 = 0 using the modified NewtonRaphson method starting with x0 = 2.8 up to four significant figures. 3.42 Find a root of the equation x3 − 3x − 5 = 0 using the modified NewtonRaphson method correct up to four decimal places starting with x0 = 2.0. 3.43 Find a root of the equation x3 − x − 1 = 0 using the modified NewtonRaphson method correct up to four decimal places starting with x0 = −1.5. 3.44 Find a root of the equation x6 − x − 1 = 0 using the secant method approximations: x0 = 2 and x1 = 1.0. 3.45 Find a root of the equation x3 − 75 = 0 using the secant method with the initial approximations of x0 = 4 and x1 = 5. 3.46 Find a root of the equation tan x − tan hx = 0 using the secant method with initial approximations: x0 = 7 and x1 = 7.5. 3.47 Find a root of the equation cos x cosh x − 1 = 0 using the secant method with initial approximations: x0 = 4.5 and x1 = 5.0. 3.48 Find a root of the equation sin x − 0.1x = 0 using the secant method with initial approximations: x0 = 2 and x1 = 3. 3.49 Repeat Problem P3.39 using Muller’s method given that a root is near 1.0. 3.50 Repeat Problem P3.40 using Muller’s method given that a root is near 4.0. 3.51 Repeat Problem P3.41 using Muller’s method given that a root is near 7.0.

ANMF.CH03_4PP.indd 247

3/1/2023 1:06:45 PM

248 • Applied Numerical Methods Using MATLAB 3.52 Repeat Problem P3.42 using Muller’s method given that a root is near 4.6. 3.53 Repeat Problem P3.43 using Muller’s method given that a root is near 2.8. 3.54 Find a root of the equation cos x − xex = 0 using Aitken’s Δ2 method. 3.55 Find the root of the equation x3 − 5x − 11 = 0 correct to three decimal places using Aitken’s method. 3.56 Find the root of 0.5 + sin x − x = 0 and x0 = 1 using Aitken’s method. 3.57 Use Aitken’s method to find a root of the equation 3x − log10x − 16 = 0. 3.58 Use Aitken’s method to find a root of the equation ex − 3x = 0 lying between 0 and 1. 3.59 Use Aitken’s method to find a root of the equation x3 + x − 1 = 0. 3.60 Use Aitken’s method to find a root of the equation 5x3 − 20x + 3 = 0 in the interval (0, 1). 3.61 Use Aitken’s method to find a root of the equation x3 + 2x − 2 = 0 up to three decimal places. 3.62 Use Aitken’s method to find a root of the equation x3 − 3x2 + 4 = 0. 3.63 Use Brent’s method to find a root of the equation f(x) = x|cos x| − 1 that lies I n the interval (0, 4). 3.64 Use Brent’s method to find the smallest positive, nonzero root of cosh x cos x − 1 = 0 that lies in the interval (4, 5). 3.65 A root of the equation tan x − tanh x = 0 lies in (7, 7.4). Find the root by the Brent’s method. 3.66 A root of the equation f(x) = x3 − 10x2 + 5 = 0 lies in the interval (0.6, 0.8). Find the root by the Brent’s method. 3.67 Use MATLAB to draw a plot of f(x) = cosh x cos x − 1 in the range 4 ≤ x ≤ 8. (a) Check from the plot that the smallest positive, nonzero root of f(x) = 0 lies in the interval (4, 5). (b) Find the root by Brent’s method that lies in the interval (4, 5).

ANMF.CH03_4PP.indd 248

3/1/2023 1:06:45 PM

Solution of Algebraic and Transcendental Equations • 249

3.68 Solve the following system of equations with (x0, y0) = [1 0.5]. (a) x 12  4 x 22  5

2 x 12  2 x 1  3 x 2  2.5

(b) x2 + y2 = 3

xy = 1

(c) sin x + y2 + ln z − 7 = 0

3x + 2y − z3 + 1 = 0



x+y+z−5=0

(d) sin x + 3 cos x − 2 = 0

cos x − sin y + 0.2 = 0

(e) tan x − y = 1

ANMF.CH03_4PP.indd 249

cos x − 3 sin y = 0

3/1/2023 1:06:49 PM

ANMF.CH03_4PP.indd 250

3/1/2023 1:06:49 PM

CHAPTER

4

Numerical Differentiation 4.1 INTRODUCTION Numerical differentiation deals with the following problem: given the function y = f(x) find one of its derivatives at the point x = xk. Here, the term given implies that we either have an algorithm for computing the function, or possesses a set of discrete data points (xi, yi), i = 1, 2, n. In other words, we have a finite number of (x, y) data points or pairs from which we can compute the derivative. Numerical differentiation is a method to compute the derivatives of a function at some values of independent variable x, when the function f(x) is explicitly unknown, however it is known only for a set of arguments. Like the numerical interpolation discussed in Chapter 5, a number of formulae for differentiation are derived in this chapter. They are: a) Derivatives based on Newton’s forward interpolation formula. This formula is used to find the derivative for some given x lying near the beginning of the data table. b) Derivatives based on Newton’s backward interpolation formula. This formula is suitable to find the derivative for a point near the end of the data table. c) Derivatives based on Stirling’s interpolation formula. This formula is used to find the derivative for some point lying near the middle of the tabulated value. A method to find the maxima and minima of a given function is also discussed in this chapter.

ANMF.CH04_4PP.indd 251

3/1/2023 2:04:48 PM

252 • Applied Numerical Methods Using MATLAB

4.2 DERIVATIVES BASED ON NEWTON’S FORWARD INTEGRATION FORMULA Suppose the function y = f(x) is known at (n + 1) equispaced points x0, x1, …., xn and they are y0, y1, …., yn respectively, i.e., yi = f(xi), i = 0, 1, …., n. x  xi Let xi = x0 + ih and u  , where h is the spacing. h Referring to Chatper 5, the Newton’s forward interpolation formula is u(u − 1) 2 u(u − 1)....(u − n − 1) n y = f (x) = y 0 + u∆y 0 + ∆ y 0 + .... + ∆ y0 2! n! u2  u 2 u 3  3u 2  2u 3 u 4  6u 3  11u 2  6u 4  y 0  uy 0   y0   y0   y0 2! 3! 4!



u 5  10u 4  35u 3  50u 2  24u 5!

 5 y 0  .... (4.1)

Differentiating Equation (4.1) w.r.t. x, we get 1 2u  1 2 3u 2  6u  2 3 4u 3  18u 2  22u  6 4  y0   y0   y0 f ( x )   y 0  h 2! 3! 4! 5u 4 − 40u 3 + 105u 2 − 100u + 24 5 + ∆ y0 5! 

(4.2)

Note here that du = 1 . dx

h

Differentiating Equation (4.2) w.r.t. x, we obtain  2 6u − 6 3 12u 2 − 36u + 22 4 ∆ y + ∆ y + ∆ y0 0 0 1  3! 4!  ′′ f (x) = 2 h  20u 3 − 120u 2 + 210u − 100 5 ∆ y 0 + .... + 5!  

(4.3)

and so on.

ANMF.CH04_4PP.indd 252

3/1/2023 2:04:55 PM

Numerical Differentiation • 253

Equations (4.2) and (4.3) give the approximate derivatives of f(x) at arbitrary point x = x0 + uh. When x = x0, u = 0, Equations (4.2) and (4.3) become 

f ′(x 0 ) =

1 1 1 1 1  ∆y 0 − ∆ 2 y 0 + ∆ 3 y 0 − ∆ 4 y 0 + ∆ 5 y 0 − ....  h 2 3 4 5 

(4.4)

and

f ′′(x 0 ) =

1 h2

(4.5)

11 4 5 5  2  3  ∆ y 0 − ∆ y 0 + 12 ∆ y 0 − 6 ∆ y 0 + ....   

and so on. 4.2.1 MATLAB Program for Derivatives Based on Newton’s Forward Integration Formula—Equally Spaced Points function [dydx, d2ydx2] = Diffeq(x, y) n = length(x); dx = x(2) - x(1); for i = 1:n if i == 1 dydx(i) = (-y(i + 2) + 4∗y(i + 1) - 3∗y(i))/dx/2; d2ydx2(i) = (-y(i + 3) + 4∗y(i + 2) -5∗y(i + 1)+2∗y(i))/dx^2; elseif i == n dydx(i) = (3∗y(i) - 4∗y(i-1) + y(i - 2))/dx/2; d2ydx2(i) = (2∗y(i) - 5∗y(i - 1) + 4∗y(i - 2) - y(i - 3))/dx^2; else dydx(i) = (y(i + 1) - y(i - 1))/dx/2; d2ydx2(i) = (y(i + 1) - 2∗y(i)+y(i - 1))/dx^2; end end subplot(2,1,1);plot(x,dydx);grid; title('First derivatives')

ANMF.CH04_4PP.indd 253

3/1/2023 2:04:55 PM

254 • Applied Numerical Methods Using MATLAB xlabel('x');ylabel('dy/dx') subplot(2,1,2);plot(x,d2ydx2);grid; title('Second derivatives') xlabel('x');ylabel('d2y/dx2') EXAMPLE 4.1 From the following table find the value of

dy d2 y and at the point x = 1.0. dx dx 2

x

1

1.1

1.2

1.3

1.4

1.5

y

5.4680

5.6665

5.9264

6.2551

6.6601

7.1488

Solution: The forward difference table is x

y

1.0

5.4680

1.1

5.6665

1.2

5.9264

∆y

∆2y

∆3y

0.1985 0.0614 0.2599

0.0074 0.0688

0.3287 1.3

6.2551

0.0074 0.0763

0.4050 1.4

6.6601

0.0074 0.0837

0.4887 1.5

7.1488

Here x0 = 1.0 and h = 0.1. Then u = 0 and hence

dy 1 1 1  = y′(1.0) = ∆y 0 − ∆ 2 y 0 + ∆ 3 y 0 − ....  dx h 2 3 



1  1 1  0.1985  (0.0614)  (0.0074)   1.7020  2 3 0.1  

d2 y 1 1  = = 2  ∆y 0 − ∆ 3 y 0 + .... = y′′(1.0) [0.0614 − 0.0074=] 5.4040 2 h (0.1) 2 dx

ANMF.CH04_4PP.indd 254

3/1/2023 2:04:56 PM

Numerical Differentiation • 255

MATLAB Solution: Trial>> x = [1.0, 1.1,1.2,1.3,1.4,1.5]; Trial>> y = [5.4680,5.6665,5.9264,6.2551,6.6601,7.1488]; Trial>> [dydx,d2ydx2] = Diffeq(x, y) dydx = 1.6780

2.2920

2.9430

3.6685

4.4685

5.3055

6.1400

6.8800

7.6300

8.3700

9.1100

d2ydx2 = 5.4000

FIGURE 4.1 

ANMF.CH04_4PP.indd 255

3/1/2023 2:04:56 PM

256 • Applied Numerical Methods Using MATLAB EXAMPLE 4.2 Obtain the first and second derivatives of the function tabulated below at the points x = 1.1 and x = 1.2. x :

1

1.2

1.4

1.6

  1.8

y :

0

0.128

0.544

1.298

2.440

2.0 4.02

Solution: We first construct the forward difference table as shown below. x 1.0



0



∆2y

∆y

y

0.128→

∆3y

1.2 → → 0.128→ →

0.288→

1.4

0.338→ →

0.416→ →

0.544 1.298



0.388 1.142

1.8

2.440

0

0.05→

0.754 1.6

0.05→

∆4y

0

0.05 0.438

1.580 2.0

4.02

Since x = 1.1 is a nontabulated point near the beginning of the table, we take x0 = 1.0 and compute    

p

x  x 0 1.1  1.0   0.5 h 0.2

2p  1 2 3p 2  6p  2 3  dy 1   y0   y0    y 0  2 6 dx h    1  3(0.5) 2  6(0.5)  2 0 128 0    . (0.05)   0.62958  0.2  6  Hence

d2 y 1 1 [0.288  (0.5  1)0.05]  6.5575  2   2 y 0  (p  1) 3 y 0   2 (0.2) 2 dx h

ANMF.CH04_4PP.indd 256

3/1/2023 2:04:58 PM

Numerical Differentiation • 257

Now, x = 1.2 is a tabulated point near the beginning of the table. For x = x0 = 1.2, p = 0 and 1 1 1 1 dy 1   1   0.416  (0.338)  (0.05)   1.31833   y 0   2 y 0   3 y 0    2 3 2 3 dx h   0.2   d2 y 1 2 1  2 [ y0  3 y0 ]  [0.338  0.05]  7.2 2 (0.2) 2 dx h MATLAB Solution: Trial>> x = [1 1.2 1.4 1.6 1.8 2.0]; Trial>> y = [0 0.1 0.5 1.25 2.4 3.9]; Trial>> [dydx,d2ydx2] = Diffeq(x, y) dydx = -0.2500

1.2500

2.8750

4.7500

6.6250

8.3750

7.5000

8.7500 10.0000

8.7500

7.5000

d2ydx2 = 6.2500

FIGURE 4.2 

ANMF.CH04_4PP.indd 257

3/1/2023 2:05:00 PM

258 • Applied Numerical Methods Using MATLAB EXAMPLE 4.3 Find the first and second derivatives of the functions tabulated below at the point x = 1.1 and x = 1.2. x

1

1.2

1.4

1.6

1.8

2.0

y

0

0.1

0.5

1.25

2.4

3.9

Solution: First, we construct the forward difference table: x 1.0 →

0→

1.2→ → 0.1→



1.4

0.5

1.6

1.25

1.8

2.40

∆2y

∆y

y

0.1 → 0.4→



0.75

0.3 → 0.35→ →

∆3y

0.05



0

0.05→



0.40 1.15

∆4y

0

0.05 0.45

1.5 2.0

3.90

Here x = 1.1 is a nontabulated point near the beginning of the table. For x0 = 1.0, p



x  x 0 1.1  1.0   0.5 h 0.2

2p  1 2 3p 2  6p  2 3  dy 1   y0   y0    y 0  2 6 dx h  

Hence





 1  3(0.5) 2  6(0.5)  2 0 1 0   . (0.05)   0.48958  0.2  6 

d2 y 1 1 [0.3  (0.5  1)0.05]  6.875    2 y 0  (p  1) 3 y 0   (0.2) 2 dx 2 h 2 

ANMF.CH04_4PP.indd 258

3/1/2023 2:05:02 PM

Numerical Differentiation • 259

For x = 1.2, it is a tabulated point near the beginning of the table. Let

x = x0 = 1.2, p = 0

dy 1  1 1 1 1  1     y 0   2 y 0   3 y 0   0.4  (0.35)  (0.05)   1.208  dx h  2 3 2 3  0.2   d 2 y 1 2 1  2 [ y0  3 y0 ]  [0.35  0.05]  7.5 2 (0.2) 2 dx h MATLAB Solution: Trial>> x = [1 1.2 1.4 1.6 1.8 2.0]; Trial>> y = [0 0.1 0.5 1.25 2.4 3.9]; Trial>> [dydx, d2ydx2] = Diffeq(x, y) dydx = -0.2500

1.2500

2.8750

4.7500

6.6250

8.3750

7.5000

8.7500 10.0000

8.7500

7.5000

d2ydx2 = 6.2500

FIGURE 4.3 

ANMF.CH04_4PP.indd 259

3/1/2023 2:05:03 PM

260 • Applied Numerical Methods Using MATLAB

4.3 DERIVATIVES BASED ON NEWTON’S BACKWARD INTERPOLATION FORMULA Here, we assume the function y = f(x) is known at (n + 1) points x0, x1, …., xn, i.e., yi = f(xi), i = 0, 1, 2, …., n are known. Let xi = x0 + ih, i = 0, 1, 2, …., n and x  xn . v h Then, the Newton’s backward interpolation formula from Chapter-5 is given by f ( x )  y n  vy n 

v( v  1) 2 v( v  1)( v  2) 3  yn   yn 2! 3!

v( v  1)( v  2)( v  3) 4 v( v  1)( v  2)( v  3)( v  4) 5   yn   y n  .... (4.6) 4! 5! When the Equation (4.6) is differentiated w.r.t. x successively, we obtain 1  2v  1 2 3v 2  6 v  2 3 4 v3  18v 2  22v  6 4 f ( x )  y n   yn   yn   yn 2! 3! 4! h 4  5v  40 v3  105v 2  100v  24 5  (4.7)   y n  .... 5!  2  2 6v  6 3 12 v  36 v  22 4   yn   yn   yn   1 ! ! 3 4  (4.8)  f ( x )  2  h  20 v3  120 v 2  210 v  100 5   y n  ....   5!   and so on. Equations (4.7) and (4.8) can be used to determine the approximate differentiation of first, second, and so on order at any point x, where x = xn + vh. If x = xn, then v = 0. Equations (4.7) and (4.8) become

ANMF.CH04_4PP.indd 260

f ( x n ) 

1 1 1 1 1  y n   2 y n  3 y n   4 y n  5 y n  .... (4.9)  h 2 3 4 5 

3/1/2023 2:05:08 PM

Numerical Differentiation • 261

and f ( x n ) 

1 h2

11 4 5 5   2 3  y n   y n  12  y n  6  y n  .... (4.10)  

EXAMPLE 4.4 A slider in a machine moves along a fixed straight rod. Its distance x(m) along the rod is given in the following table for various values of the time t (seconds). t(sec.)

1

2

3

4

5

6

x(m)

0.0201

0.0844

0.3444

1.0100

2.3660

4.7719

Find the velocity and acceleration of the slider at time t = 6 sec. Solution: The backward difference table is ∇2x

∇x

∇3x

∇4x

t

x

1.0

0.0201

2.0

0.0844

0.0643

3.0

0.3444

0.2600

0.1957

4.0

1.0100

0.6656

0.4056

0.2100

5.0

2.3660

1.3560

0.6904

0.2847

0.0748

6.0

4.7719

2.4059

1.0499

0.3595

0.0748

∇5x

0.0000

Here h = 1.0 1 1 1 1 dx 1    x   2 x  3 x   4 x  5 x  .... 2 3 4 5 dt h   1 1  1 1 1   2.4059  (1.0499)  (0.3595)  (0.0748)  (0.0)   3.0694  1.0  2 3 4 5  d2 x 1  dt 2 h 2 

ANMF.CH04_4PP.indd 261

11 5   2 3  x   x  12  x  ....  

1 (1.0) 2

5  11  1.0499  0.3595  12 (0.0748)  6 (0)   1.4780  

3/1/2023 2:05:11 PM

262 • Applied Numerical Methods Using MATLAB MATLAB Solution: Trial>> x = [1 2 3 4 5 6]; Trial>> y = [0.0201 0.0844 0.3444 1.0100 2.3660 4.7719]; Trial>> [dydx, d2ydx2] = Diffeq (x, y) dydx = -0.0335

0.1621

0.4628

1.0108

1.8809

2.9308

0.1957

0.4056

0.6904

1.0499

1.4094

d2ydx2 = -0.0142

FIGURE 4.4 

ANMF.CH04_4PP.indd 262

3/1/2023 2:05:11 PM

Numerical Differentiation • 263

4.4 DERIVATIVES BASED ON STIRLING’S INTERPOLATION FORMULA Suppose y±i = f(x±i), i = 0, 1, …., n are given for 2n + 1 equispaced points x0, x±1, x±2, …., x±n, where x±i = x0 ± ih, i = 0, 1, …., n. The Stirling’s interpolation polynomial is given by f ( x )  y0 

u  y 1  y 0  u 2 2 u 3  u   3 y 2   3 y 1    y  1    2! 1!  2 3!  2  

u4  u2 4 u 5  5u 3  4u   5 y 3   5 y 2    y 2     ....  4! 5! 2   where u 

(4.11)

x  x0 . h

When Equation (4.11) is differentiated with respect to x successively, we obtain f ( x ) 

1  y 1  y 0 3u 2  1   3 y 2   3 y 1  2 u y       1 h 2 6  2 

 2u 3  u 4 5u 4  15u 2  4   5 y 3   5 y 2     y    .... 2 (4.12) 12 120 2    and  2  3 y 2   3  1 6u 2  1 4    y 2  y u   1  2 12 1   f ( x )  2  h  2u 3  3u   5 y 3   5 y 2      ....   2 12    

(4.13)

At x = x0, u = 0 and Equations (4.12) and (4.13) become 1  y  y  1   3 y 1   3 y 2  1   5 y 2   5 y 3  1 f ( x 0 )   0     .... (4.14)   h 2 6 2 2  30   

ANMF.CH04_4PP.indd 263

3/1/2023 2:05:16 PM

264 • Applied Numerical Methods Using MATLAB and f ( x 0 ) 

1 h2

1 4   2   y 1  12  y 2  .... (4.15)  

EXAMPLE 4.5 Find

dy d2 y and for x = 0.2 for the data given in the following table. dx dx 2 x

0

0.1

0.2

0.3

0.4

0.5

y

0

0.10017

0.20134

0.30452

0.41076

0.52115

Solution: Construct the following difference table. x 0

y

∆2y

∆y

∆3y

∆4y

0 0.10017

0.1

0.10017

0.001 0.10017

0.2

0.00101

0.20134

0.00201 0.10318

0.3

0.30452

0.00306 0.10624

0.4

0.00004 0.00105 0.00004 0.00109

0.41076

0.00415 0.11039

0.5

0.52115

Here, we use Stirling’s formula. Hence, for x = 0.2, we have dy 1  y 1  y 0 1  3 y 2   3 y 1      dx h  2 6 2  

1  0.10117  0.10318 1   (0.00101  0.00105  1.020033  0.1  2 12 

d2 y 1  2 1 1   2   y 1   4 y 2   2 2 dx h  12  (0.1)

ANMF.CH04_4PP.indd 264

. 1   0.00201  12 (0.00004)   0.200666  

3/1/2023 2:05:19 PM

Numerical Differentiation • 265

MATLAB Program: Trial>> x = [0 0.1 0.2 0.3 0.4 0.5]; Trial>> y = [0 0.10017 0.20134 0.30452 0.41076 0.52115]; Trial>> [dydx,d2ydx2]=Diffeq(x, y) dydx =  0.9967 1.0067 1.0218 1.0471 1.0831 1.1246 d2ydx2 =  -0.0010 0.1000 0.2010 0.3060 0.4150 0.5240

FIGURE 4.5 

ANMF.CH04_4PP.indd 265

3/1/2023 2:05:19 PM

266 • Applied Numerical Methods Using MATLAB EXAMPLE 4.6 Compute the values of f′(3.1) and f ''(3.1) using the following table. x

1

2

3

4

5

f(x)

0

1.4

3.3

5.6

8.1

Solution: The central difference table is x

y = f(x)

x­–2 = 1

0

x­–1 = 2

1.4

x­0 = 3

3.3

x­1 = 4

5.6

x­2 = 5

8.1

∆y

∆2y

∆3y

∆4y

1.4 0.5 1.9

–0.1 0.4

2.3

–0.1 –0.2

0.2 2.5

3.1  3  0.1 1  y 1  y 0 3u 2  1  .3 y 2   3 y 1    u 2 y 1     1 2 6  2   f (3.1)   h  2u 3  u    4 y 2  ....  12  Let x0 = 3, h = 1, u 

 1 1.9  2.3 3(0.1) 2  1  0.1  0.2  2(0.1)3  0.1    0.1(0.4)  (0.1)    6 2 1 2 12   

= [2.1 + 0.04 + 0.02425 + 0.00082] = 2.16507.

   2 y 2   3 y 1  6u 2  1 4 1  f ''(3.1)  2   2 y 1  u   y 2  ....  h  2 12   .  2  1  0.1  0.2  6(0.1)  1  2 0.4  0.1 (0.1)   1  2 12     [0.4  0.015  0.00783]  0.39283

ANMF.CH04_4PP.indd 266

3/1/2023 2:05:24 PM

Numerical Differentiation • 267

MATLAB Solution: Trial>> x = [ 1 2 3 4 5]; Trial>> y = [0 1.4 3.3 5.6 8.1]; Trial>> [dydx, d2ydx2] = Diffeq (x, y) dydx =  1.1500 1.6500 2.1000 2.4000 2.6000 d2ydx2 =  0.6000 0.5000 0.4000 0.2000 -0.0000

FIGURE 4.6 

ANMF.CH04_4PP.indd 267

3/1/2023 2:05:24 PM

268 • Applied Numerical Methods Using MATLAB

4.5 MAXIMA AND MINIMA OF A TABULATED FUNCTION From calculus, we know that if a function is differentiable, then the maximum and minimum value of that function can be determined by equating the first derivative to zero and solving for the variable. This method is extendable for the tabulated function. Now, consider the Newton’s forward difference formula given in Equation (4.1). Differentiating Equation (4.1) w.r.t. u, we obtain dy 2u  1 2 3u 2  3u  2 3  y 0   y0   y 0  ..... (4.16) du 2 6 dy = 0. Neglecting the term after the third differdu ence to obtain a quadratic equation in u. For maximum or minimum,

 u2 u 1  3 1 2  Hence y 0   u    y 0       y 0  0 (4.17) 2   2 2 3 3  y0 2  2 1 1 1    u    y 0   2 y 0  u   y 0   2 y 0   3 y 0   0 or 2 2 2 3     2 or a0u + a1u + a2 = 0 (4.18) which gives the values of u. 1 3 Here a 0   y 0 2 1 a1   2 y 0   3 y 0 2 (4.19) 1 2 1 3 a 2  y 0   y 0   y 0 2 3 The values of x will then be obtained from x = x0 + uh. EXAMPLE 4.7 Find x correct to four decimal places for which y is maximum from the following data given in tabular form and find also the value of y.

ANMF.CH04_4PP.indd 268

x

1

1.2

1.4

1.6

1.8

y

0

0.128

0.544

1.298

2.44

3/1/2023 2:05:28 PM

Numerical Differentiation • 269

Solution: We first construct the forward difference table as shown below: x

y

1.0

0

1.2

0.128

1.4

0.544

1.6

1.298

1.8

2.44

∆2y

∆y

∆3y

0.128 0.288 0.416

0.05 0.338

0.754

0.05 0.388

1.142

Let

x0 = 1.0.

1 = (0.05) 0.025 2 1 a1  0.288  (0.05)  0.2630 2 1 1 a 2  0.128  (0.288)  (0.05)  0.128  0.144  0.0166  0.000666 2 3

Here a0 =

Hence a0u2 + a1u + a2 = 0, which gives the value of u. or

0.025u2 + 0.263u + 0.000666 = 0

0.263  (0.263) 2  4(0.025)(0.000666)  (0, 10.5175) 2(0.025) Hence u = 0 or u = –10.5175. u1, 2 

Therefore, x = 1.0 and x = 1.0 – 10.5175(0.2) = –1.1035 At x = 1.0, y = 0 and at x – 1.1035, we apply the Newton’s forward interpolation formula. u (u  1) 2 u (u  1)(u  2) 3  y0   y 0  .... 2! 3! (10.5175)(11.5175)  0  (10.5175)(0.128)  (0.288) 2 (10.5175)(11.5175)(12.5175)  (0.05) (3)(2)(1) = 3.46132 (maximum value).

ANMF.CH04_4PP.indd 269

y  y 0  uy 0 

3/1/2023 2:05:32 PM

270 • Applied Numerical Methods Using MATLAB

4.6 CUBIC SPLINE METHOD The cubic spline method described in Section 5.7 of Chapter 5 can be used to find the first and second derivatives of a function. The method requires two steps. In the first step, the cubic splines are constructed with suitable intervals. In the second step, the first and second derivatives are determined from the appropriate cubic spline. These steps are illustrated by an example as follows: EXAMPLE 4.8 Given y = f(x) = cos x, 0 ≤ x ≤ p/2. Determine a) the natural cubic spline in the interval 0 ≤ x ≤ p/4 and p/4 ≤ x ≤ p/2 b) the first and second derivatives f′(p/8) and f″(p/8). Solution: Here h = k0 = k2 = 0.

1  π p , y0 = cos 0 = 1, y1 = cos  and y2 = cos = 0. Also, 4 2 2 4

From Equation (5.85) of Section 5.7 of Chapter 5, 6 k i 1  4k i  k i 1  2 [ yi 1  2 yi  yi 1 ], i = 2, 3, …., n – 1  h 6 or k 0  4k1  k 2  2 [ y 0  2 y1  y 2 ] h 96 or 4 k1  2 1  2  24 or k1  2 1  2  1.007247 





(E.1)





Therefore, the cubic spline is given by Equation (5.81) of Chapter 5, f 0, 4 ( x ) f ( x )   (E.2)  f 4 , 2 ( x )  4  x3 1 2   where f 0,  ( x )   k1  1   k1  x   (E.3) 4 6 4 2 96  

ANMF.CH04_4PP.indd 270

3/1/2023 2:05:37 PM

Numerical Differentiation • 271

and

3       x   2  1    4  2    f (E.4) (x)  k1   k1    x   , 4 2  6 96 2    2    

    Hence f     f 0, 4    0.339961 4 8 and

    f     f 0,     0.503623 . 4 8 8

4.7 RICHARDSON EXTRAPOLATION Richardson extrapolation uses two derivatives and estimates to determine a third, more accurate approximation. The estimated and the error with the trapezoidal rule can be represented as

l = l(h) + E(h)

where l = the exact value of the integral. l(h) = the approximation from an n-segment application of the trapezoidal rule with step size h = (b – a)/n, and E(h) = the truncation error. When two estimates with step sizes of h1 and h2 are used, then the exact values for the error is l(h1) + E(h1) = l(h2) + E(h2)(4.20) The error of the trapezoidal rule can be approximated with n = (b – 1)/h as ba 2 h f  (4.21) E   12 Here we assume that f ′′ is constant regardless of step size. Equation (4.21) can now be used to find the ratio of the two errors as

ANMF.CH04_4PP.indd 271

E (h1 ) h12 ≅ (4.22) E (h 2 ) h 22

3/1/2023 2:05:41 PM

272 • Applied Numerical Methods Using MATLAB without prior knowledge of the function’s second derivative. We rewrite Equation (4.22) as 2  h1  E (h1 )  E (h 2 )   (4.23)  h2  Substituting Equation (4.20), we obtain 2

h  l(h1) + E(h2)  1  = l(h2) + E(h2)(4.24)  h2  Solving Equation (4.24) for E(h2) gives E (h 2 ) 

l ( h1 )  l ( h 2 ) (4.25) 1  ( h1 / h 2 ) 2

This is an estimate of the truncation error in terms of the integral estimates and their step sizes. Substituting Equation (4.25) in (4.26) l = l(h2) + E(h2)(4.26) We obtain an improved estimate of the integral: l = l(h2) +

1 [l(h2) – l(h1)](4.27) ( h1 / h 2 ) 2 − 1

It can be shown that the error of this estimate is O(h4). For the special case where the interval is halved (h2 = h1/2), this equation becomes 4 1 l  l (h 2 )  l (h1 ) (4.28) 3 3 Similarly, Equation (4.28) can be written for derivatives as 4 1 D  D(h 2 )  D(h1 ) (4.29) 3 3 For centered difference approximations with O(h2), the application of Equation (4.29) will give a new derivative estimate of O(h4).

ANMF.CH04_4PP.indd 272

3/1/2023 2:05:49 PM

Numerical Differentiation • 273

EXAMPLE 4.9 Estimate the first derivatives of y = cos x at x = p/4 using step sizes of h1 = p/3 and h2 = p/6. Use centered differences of O(h2) for the initial estimates. Use Richardson extrapolation. Solution: The exact value is  sin     0.70711 4 D( / 3) 

0.2589  0.9659  0.5848 2(1.0472)

D( / 6) 

0.2588  0.9660  0.6752 2(0.5236)

4 1 Hence D  (0.6752)  (0.5848)  0.7054 . 3 3

4.8 DIFFERENTIATION OF UNEQUALLY SPACED DATA In Chapter 5, Section 5.4, we have a method to fit a Lagrange’s interpolating polynomial to a set of adjacent points that bracket the location value at which one wants to evaluate the derivative. This polynomial does not need the points be equispaced. Here, the polynomial can be differentiated mathematically to give a formula that can be utilized to estimate the derivative. For instance, a second-order Lagrange polynomial that fits three adjacent points (x0, y0), (x1, y1), and (x2, y2) is given by



ANMF.CH04_4PP.indd 273

f (x)  

( x  x 0 )( x  x 2 ) ( x  x1 )( x  x 2 ) f (x 0 )  f ( x1 ) ( x 0  x1 )( x 0  x 2 ) ( x1  x 0 )( x1  x 2 ) ( x  x 0 )( x  x1 ) f (x 2 ) ( x 2  x 0 )( x 2  x1 )

(4.30)

3/1/2023 2:05:52 PM

274 • Applied Numerical Methods Using MATLAB Differentiating Equation (4.30), we obtain

f ( x ) 

(2x  x 0  x 2 ) 2 x  x1  x 2 f ( x1 ) f (x 0 )  ( x 0  x1 )( x 0  x 2 ) ( x1  x 0 )( x1  x 2 )

( 2 x  x 0  x1 )  f (x 2 ) (4.31) ( x 2  x 0 )( x 2  x1 ) where x is the value at which the estimate of the derivative is needed. Equation (4.31) can provide estimates any where in the range presented by the three points. The points do not have to be equispaced. EXAMPLE 4.10 For the data in Table 4.1, estimate the first derivative at x = 8. TABLE 4.1  x

2

6

9

y = f(x)

2

16

32.8

Solution: Using the Equation (4.31), we have f ( x )  2 x  x1  x 2 f ( x 0 )  (2 x  x 0  x 2 ) f ( x1 ) ( x 0  x1 )( x 0  x 2 ) ( x1  x 0 )( x1  x 2 ) 

( 2 x  x 0  x1 ) f (x 2 ) ( x 2  x 0 )( x 2  x1 )

Hence f ( x )  f (8) 

2(8)  6  9 2(8)  2  9) 2(8)  2  6 ( 2)  (16)  32.8 = 5.90. (2  6)(2  9) (6  2)(6  9) (9  2)(9  6)

4.9 MATLAB BUILT-IN FUNCTIONS: DIFF AND GRADIENT d = dif(y) returns the differences d(i) = y(i + 1) – y(i). Note that length (d) = length(y) – 1. Thus, the diff function returns a vector of length (n – 1) containing the difference between adjacent elements. This function can be used to find the finite-difference approximation of first derivatives.

ANMF.CH04_4PP.indd 274

3/1/2023 2:05:56 PM

Numerical Differentiation • 275

dn = diff(y, n) returns the nth differences; thus d2(i) = d(i + 1) – d(i), d3(i) = d2(i + 1) – d2(i), etc. Here length (dn) = length (y) – n. d = gradient(y, h) returns the finite difference approximation of dy/dx at each point, where h is the spacing between the points.  d2 y  d2 = de 12(y, h) returns the finite difference approximation of  2  4 at  dx  each point, where h is the spacing between the points. EXAMPLE 4.11 Use MATLAB built-in function diff to differentiate the function f(x) = 0.1 + 30x – 300x2 + 700x3 – 1000x4 + 300x5 from x = 0 to 1.0. Compare the results with the exact solution: f′(x) = 30 – 600x + 2100x2 – 4000x3 + 1500x4. MATLAB Solution: The desired solution is obtained by the following MATLAB Program: % Anonymous function, f(x) >> f = @(x) 0.1 + 30∗x - 300∗x.^2 + 700∗x.^3 - 1000∗x.^4 + 300∗x.^5; % Develop a series of equally spaced values of x and y >> x = 0:0.1:1.0; >> y = f(x); % Develop a vector division of the y differences by the x differences >> d = diff(y)./diff(x); >> n = length(x); % Vector holding the x values for the midpoint of each interval >> xm = (x(1:n-1) + x(2:n))./2; >> xa = 0:0.01:1.0; % Compute the x and y values for the exact analytical derivative >> ya = 30 - 600∗xa + 2100∗xa.^2 - 4000∗xa.^3 + 1500∗xa.^4; % Plot of the numerical and analytical estimates >> plot(xm,d,'o',xa,ya) >> xlabel('x');ylabel('df/dx')

ANMF.CH04_4PP.indd 275

3/1/2023 2:05:56 PM

276 • Applied Numerical Methods Using MATLAB The plot obtained is shown in Figure 4.7 and shows a good agreement for the analytical and numerical values.

FIGURE 4.7 

EXAMPLE 4.12 Use the MATLAB built-in function gradient to differentiate the same function in Example 4.11. MATLAB Solution: The desired solution is obtained by the following MATLAB Program: % Anonymous function, f(x) f = @(x) 0.1 + 30∗x - 300∗x.^2 + 700∗x.^3 - 1000∗x.^4 + 300∗x.^5; % Develop a series of equally spaced values of x and y x = 0:0.1:1.0; y = f(x);

ANMF.CH04_4PP.indd 276

3/1/2023 2:05:56 PM

Numerical Differentiation • 277

% Determine the derivatives dy = gradient(y, 0.1); % Compute the x and y values for the exact analytical derivative xa = 0:0.01:1.0; ya = 30 - 600∗xa + 2100∗xa.^2 - 4000∗xa.^3 + 1500∗xa.^4; % Plot of the numerical and analytical estimates plot(x,dy,'o',xa,ya) xlabel('x');ylabel('df/dx') The plot obtained is shown in Figure 4.8 and shows a good agreement for the analytical and numerical values.

FIGURE 4.8 

ANMF.CH04_4PP.indd 277

3/1/2023 2:05:56 PM

278 • Applied Numerical Methods Using MATLAB

4.10 SUMMARY Numerical differentiation is not a particularly accurate process due to a conflict between round off errors and errors inherent in interpolation. Hence, a derivative of a function can never be computed with the same precision as the function itself. In this chapter, we have presented derivatives based on Newton’s forward interpolation formula, Newton’s backward interpolation formula, Stirling’s interpolation formula, the maxima and minima of a tabulated function, the cubic spline method, the Richardson extrapolation method, and a method for differentiation of unequally spaced data.

EXERCISES NOTE

In addition to solving the following exercises through analytical solutions or long-hand numerical computations, try to solve them using user-defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 4.1. From the following table of values, estimate y′(1.0) and y″(1.0): a) x

1

2

3

4

5

6

y

–4

+3

22

59

120

211

b) x

1

1.5

2

2.5

3

3.5

y

5

6.125

9

14.375

23

35.625

c) x

1

2

3

4

5

6

y

6

25

104

309

730

1481

d) x

1

1.5

2

2.5

3

3.5

y

2.7

–5.5188

–27.8

–75.4688

–163.3

–309.5188

e)

ANMF.CH04_4PP.indd 278

x

1

2

3

4

5

6

y

2.9

–26.2

–157.9

–523

–1307.5

–2752.6

3/1/2023 2:05:57 PM

Numerical Differentiation • 279

f) x

1

3

5

7

9

11

y

5.2

28.4

130

367.6

798.8

1481.2

x

1

1.5

2

2.5

3

3.5

y

–48

–41.0068

–24.362

8.1098

64.084

152.7363

g)

4.2. Find the values of y′(3.5) and y″(3.5) from the following table: a) x

1

1.5

2

2.5

3

3.5

y

–49

–46.6250

–42

–34.3750

–23

–7.1250

x

1

1.5

2

2.5

3

3.5

y

5

6.1250

9

14.3750

23

35.6250

b)

c) x

1

1.5

2

2.5

3

3.5

y

0.5470

0.4536

0.2020

–0.3284

–1.2930

–2.8814

d)

4.3.

x

1

1.5

2

2.5

3

3.5

y

0.2377

0.3108

0.4829

0.7941

1.2849

1.9953

Find the values of y′(6.0) and y″(6.0) from the following table: a) x

1

2

3

4

5

6

y

–0.5530

–0.7740

–1.2490

–2.0380

–3.1770

–4.6780

x

1

2

3

4

5

6

y

0.0858

–0.0099

–0.3242

–0.9827

–2.1094

–3.8270

b)

ANMF.CH04_4PP.indd 279

3/1/2023 2:05:57 PM

280 • Applied Numerical Methods Using MATLAB 4.4. A particle is moving along a straight line. The displacement x at some time instance t are given below. Find the velocity and acceleration of the particle at t = 4. t

1

3

5

7

9

11

x

0.1405

0.7676

3.5135

9.9351

21.5892

40.0324

4.5. Find the values of y′(4) and y″(4) from the following table: x

0

1

2

3

4

y

5

8

12

17

26

4.6. Find the values of y′(2) and y″(2) from the following table: x

1.5

1.6

1.7

1.8

1.9

2.0

y

0.3328

0.5312

0.7651

1.0384

1.3552

1.7198

4.7. Compute the values of y′(3) and y″(3) from the following table: a) x

1

2

3

4

5

y

0

1.4

1.65

5.673

8.0978

b) x

1

2

3

4

5

y

0.4

0.65

0.75

0.87

0.98

4.8. Compute the values of y′(2) and y″(2) from the following table: a) x

1

1.5

2

2.5

3

y

0

0.5

1.1

3.2

5.3

b)

ANMF.CH04_4PP.indd 280

x

1

1.5

2

2.5

3

y

–2

2

3.456

5.674

8.4592

3/1/2023 2:05:57 PM

Numerical Differentiation • 281

4.9. Compute the values of y′(1.2) and y″(1.2) from the following table: a) x

1

1.1

1.2

1.3

1.4

y

0.1

0.34

0.42

0.53

0.62

b) x

1

1.1

1.2

1.3

1.4

y

0.0254

0.0437

0.0587

0.0670

0.0780

x

1

1.1

1.2

1.3

1.4

y

0.0012

0.2342

0.5786

0.7693

0.8934

c)

4.10. Find x for which y is maximum and also find the corresponding value of y, from the table given below: a) x

1

2

3

4

5

6

y

–4

+3

22

59

120

211

x

1

1.5

2

2.5

3

3.5

y

5

6.125

9

14.375

23

35.625

b)

c) x

1

2

3

4

5

6

y

6

25

104

309

730

1481

d) x

1

1.5

2

2.5

3

3.5

y

2.7

–5.5188

–27.8

–75.4688

–163.3

–309.5188

e)

ANMF.CH04_4PP.indd 281

x

1

2

3

4

5

6

y

2.9

–26.2

–157.9

–523

–1307.5

–2752.6

3/1/2023 2:05:57 PM

282 • Applied Numerical Methods Using MATLAB f) x

1

3

5

7

9

11

y

5.2

28.4

130

367.6

798.8

1481.2

g) x

1

1.5

2

2.5

3

3.5

y

–48

–41.0068

–24.362

8.1098

64.084

152.7363

4.11. Explore Exercise 5.68 of Chapter 5. 4.12. Explore Exercise 5.71 of Chapter 5. 4.13. Use cubic spline method to find f′(2.0) and f″(2.5) from the following table: x

2

3

5

6

y

13

34

136

229

4.14. Repeat Exercise 4.7(a) using the cubic spline method. 4.15. Repeat Exercise 4.8(a) using the cubic spline method. 4.16. Repeat Exercise 4.9(a) using the cubic spline method. 4.17. Repeat Exercise 4.9(b) using the cubic spline method. 4.18. Use Richardson extrapolation to estimate the first derivative of y = ln x at x = 5 using h1 = 2 and h2 = 1.0. 4.19. For the data in Table 4.2, estimate the first derivative at x = 0. TABLE 4.2 –3

x(10 ) –2

y = f(x)(10 )

0

2

6

12

0

28.7

89.9

192

4.20. For the data in Table 4.20, estimate the first derivative at x = 0. TABLE 4.3 

ANMF.CH04_4PP.indd 282

x

0

1

3

y = f(x)

0.06

0.32

0.6

3/1/2023 2:05:57 PM

Numerical Differentiation • 283

4.21. For the data in Table 4.21, estimate the first derivative at x = 0. TABLE 4.4  x

0

0.01

0.03

y = f(x)

900

480

270

4.22. Show how the MATLAB built-in function diff can be used to differentiate the function f(x) = e2x from x = 0 to 1. Compare your results with the exact solution: f′(x) = 2e2x. 4.23. Use the MATLAB built-in function gradient to differentiate the same function in Exercise 4.22. 4.24. Show how the MATLAB built-in function diff can be used to differentiate the function f(x) = cos 2x from x = 0 to 1. Compare your results with the exact solution: f¢(x) = −2 sin 2x. 4.25. Use the MATLAB built-in function gradient to differentiate the same function in Exercise 4.24. 4.26. Show how the MATLAB built-in function diff can be used to differentiate the function f(x) = cos x − 4x from x = 0 to 1. Compare your results with the exact solution: f′(x) = − sin x – 4. 4.27. Use the MATLAB built-in function gradient to differentiate the same function in Exercise 4.26. HINT

ANMF.CH04_4PP.indd 283

For solutions to Exercises 4.22 to 4.27 follow Examples 4.11 and 4.12.

3/1/2023 2:05:57 PM

ANMF.CH04_4PP.indd 284

3/1/2023 2:05:57 PM

CHAPTER

5

Finite Differences and Interpolation 5.1 INTRODUCTION Interpolation is the technique of estimating the value of a function for any intermediate value of the independent variable. The process of computing or finding the value of a function for any value of the independent variable outside the given range is called extrapolation. Here, interpolation denotes the method of computing the value of the function y = f(x) for any given value of the independent variable x when a set of values of y = f(x) for certain values of x are known or given. Hence, if (xi, yi), i = 0, 1, 2, …., are the set of (n + 1) given data points of the function y = f(x), then the process of finding the value of y corresponding to any value of x = xi between x0 and xn, is called interpolation. There are several definitions available for the term interpolation. Hiral defines interpolation as the estimation of a most likely estimate in given conditions. It is the technique of estimating a past figure. Theile’s definition of interpolation is “Interpolation is the art of reading between the lines of a table” while Harper’s definition is “Interpolation consists in reading a value which lies between two extreme points.” If the function f(x) is known explicitly, then the value of y corresponding to any value of x can easily be obtained. On the other hand, if the function f(x) is not known, then it is very hard to find the exact form of f(x) with the tabulated values (xi, yi). In such cases, the function f(x) can be replaced by a

ANMF.CH05_4PP.indd 285

3/1/2023 2:06:51 PM

286 • Applied Numerical Methods Using MATLAB simpler, function, say, ϕ(x), that has the same values as f(x) for x0, x1, x2, …., xn. The function ϕ(x) is called the interpolating or smoothing function and any other value can be computed from ϕ(x). If ϕ(x) is a polynomial, then ϕ(x) is called the interpolating polynomial and the process of computing the intermediate values of y = f(x) is called the polynomial interpolation. In the study of interpolation, we make the following assumptions: a) there are no sudden jumps in the values of the dependent variable for the period under consideration b) the rate of change of figures from one period to another is uniform. In this chapter, we present the study of interpolation based on the calculus of finite differences. The following important interpolation formulae obtained or derived based on forward, backward and central differences of a function are presented. a) Newton’s binomial expansion formula for equal intervals b) Newton’s forward interpolation formula for equal intervals c) Newton’s backward interpolation formula for equal intervals d) Lagrange’s formula for unequal intervals e) Lagrange’s formula for inverse interpolation f) Gauss’s forward interpolation formula g) Gauss’s backward interpolation formula h) Bessel’s formula i) Stirling’s formula j) Laplace-Everett’s formula

5.2 FINITE DIFFERENCE OPERATORS Consider a function y = f(x) defined on (a, b). x and y are the independent and dependent variables respectively. If the points x0, x1, …., xn are taken at equidistance, i.e., xi = x0 + ih, i = 0, 1, 2, …., n, then the value of y, when x = xi, is denoted as yi, where yi = f(xi). Here, the values of x are called arguments and the values of y are known as entries. The interval h is called the difference

ANMF.CH05_4PP.indd 286

3/1/2023 2:06:51 PM

Finite Differences and Interpolation • 287

interval. The differences y1 − y0, y2 − y1, …., yn − yn−1 are called the first differences of the function y. They are denoted by Δy0, Δy1, …., etc., That is

Δy0 = y1 − y0



Δy1 = y2 − y1



M Δyn = yn − yn−1



(5.1)

The symbol Δ in Equation (5.1) is called the difference operator. 5.2.1 Forward Differences The forward difference or simply difference operator is denoted by Δ and may be defined as Δf(x) = f(x + h) − f(x)

(5.2)

or writing in terms of y, at x = xi, Equation (5.2) becomes

Df(xi) = f(xi + h) − f(xi)

or

Dyi = yi+1 − yi

(5.3) i = 0, 1, 2, …., n − 1

The differences of the first differences are called the second differences and they are denoted by D2y0, D2y1, …., D2yn. Hence D2y0 = Dy1 − Dy0 = (y2 − y1) − (y1 − y0) = y2 − 2y1 + y0

D2y1 = Dy2 − Dy1 = (y3 − y2) − (y2 − y1) = y3 − 2y2 + y1



D3y0 = D2y1 − D2y0 = (y3 − 2y2 + y1) − (y2 − 2y1 + y0) = y3 − 3y2 + 3y1 − y0

etc.

D3y1 = y4 − 3y3 +3 y2− y1

Generalizing, we have

ANMF.CH05_4PP.indd 287

Dn+1f(x) = D[Dnf(x)], i.e., Dn+1yi = D[Dnyi], n = 0, 1, 2, ….

(5.4)

3/1/2023 2:06:51 PM

288 • Applied Numerical Methods Using MATLAB Also,

Dn+1f(x) = Dn[f(x + h) − f(x)] = Dnf(x + h) − Dnf(x)

and

Dn+1yi = Dnyi+1 − Dnyi, n = 0, 1, 2, ….

(5.5)

where D0 ≡ identity operator, i.e., D0f(x) = f(x) and D1 = D. TABLE 5.1  Forward difference table. x

y

x0

y0

Δy

Δ2y

Δ3y

Δ4y

Δ5y

Δy0 x1

Δ2y0

y1

Δ3y0

Δy1 x2

Δ2y1

y2 Δy2

x3

Δ2y2

y3

Δ5y0

Δ y1 Δ4y1 Δ3y2

Δy3 x4

Δ4y0 3

Δ2y3

y4 Δy4

x5

y5

The forward differences for the arguments x0, x1, …., x5 are shown in Table 5.1. Table 5.1 is called a diagonal difference table or forward difference table. The first term in Table 5.1 is y0 and is called the leading term. The differences Dy0, D2y0, D3y0, …., are called the leading differences. Similarly, the differences with fixed subscript are called forward differences. 5.2.2 Backward Differences The backward difference operator is denoted by ∇ and it is defined as ∇f(x) = f(x) − f(x − h)

(5.6)

Equation (5.6) can be written as

∇yi = yi − yi−1,

i = n, n − 1, …., 1.

or

∇y1 = y1 − y0, ∇y2 = y2 − y1, …., ∇yn = yn − yn−1

(5.7) (5.8)

The differences in Equation (5.8) are called first differences. The second ­differences are denoted by ∇2y2, ∇2y3, …., ∇2yn.

ANMF.CH05_4PP.indd 288

3/1/2023 2:06:51 PM

Finite Differences and Interpolation • 289

Hence

∇2y2 = ∇(∇y2) = ∇(y2 − y1) = ∇y2 − ∇y1 = (y2 − y1) − (y1 − y0)  = y2 − 2y1 + y0.

Similarly,  ∇2y3 = y3 − 2y2 + y1, ∇2y4 = y4 − 2y3 + y2, and so on. Generalizing, we have where

∇kyi = ∇k−1yi − ∇k−1yi−1,   i = n, n − 1, …., k 0

(5.9)

1

∇ yi = yi, ∇ yi = ∇yi.

The backward differences written in a tabular form is shown in Table 5.2. In Table 5.2, the differences ∇ny with a fixed subscript i lie along the diagonal upward sloping. TABLE 5.2  Backward difference table. x

y

x0

y0

x1

y1

∇y

∇2y

∇3y

∇4y

∇y1 ∇2y2 ∇3y3

∇y2 x2

∇2y3

y2

∇3y4

∇y3 x3

∇4y4

2

∇ y4

y3 ∇y4

x4

y4

Table 5.2 is called the backward difference or horizontal table. 5.2.3 Central Differences The central difference operator is denoted by the symbol d and is defined by δf(x) = f(x + h/2) − f(x − h/2) where h is the interval of differencing. In terms of y, the first central difference is written as δy1 = yi+1/2 − yi−1/2

ANMF.CH05_4PP.indd 289

(5.10)

3/1/2023 2:06:52 PM

290 • Applied Numerical Methods Using MATLAB where yi+1/2 = f(xi + h/2) and yi−1/2 = f(xi − h/2). Hence δy1/2 = y1 − y0, δy3/2 = y2 − y1, …., δyn−1/2 = yn − yn−1. The second central differences are given by

δ2yi = δyi+1/2 − δyi−1/2 = (yi+1 − yi) − (yi − yi−1) = yi+1 − 2yi + yi−1.

Generalizing δnyi = δn−1yi+1/2 − δn−1yi−1/2.

(5.11)

The central difference table for the seven arguments x0, x1, …., x4 is shown in Table 5.3. TABLE 5.3  Central difference table. x

y

x0

y0

δ2

δ

δ3

δ4

δ5

δ6

δy1/2 x1

δ2y1

y1

δ3y3/2

δy3/2 x2

2

δ4y2

δ y2

y2

δ3y5/2

δy5/2 x3

2

δ y3

y3

δ3y7/2 2

δ y4 3

δy9/2 x5

y5

x6

y6

δ5y7/2 4

δ y4

y4

d6y3

δ y3

δy7/2 x4

δ5y5/2 4

δ y9/2 δ2y5

δy11/2

It is noted in Table 5.3 that all odd differences have fraction suffices and all the even differences are with integral suffices. EXAMPLE 5.1 (a) Construct the forward difference table and the horizontal table for the following data:

ANMF.CH05_4PP.indd 290

x

1

2

3

4

5

y = f(x)

4

6

9

12

17

3/1/2023 2:06:52 PM

Finite Differences and Interpolation • 291

(b) Construct a forward difference table for the following data x

0

10

20

30

y

0

0.174

0.347

0.518

(c) Construct a difference table for y = f(x) = x3 + 2x + 1 for x = 1, 2, 3, 4, 5. (d) Obtain the backward differences for the function f(x) = x3 from x=1 to 1.05 to two decimals chopped. Solution: (a) The forward and the horizontal or backward difference tables are shown in Tables 5.4 (a) and 5.4 (b) respectively. TABLE 5.4(a)  Forward difference table. x

f(x)

1

4

2

6

Δf(x)

Δ2f(x)

Δ3f(x)

Δ4f(x)

2 1 3 3

9

4

12

1 0

3

3

2 2

5 5

17

TABLE 5.4(b)  Horizontal or backward difference table.

ANMF.CH05_4PP.indd 291

Δf(x)

Δ2f(x)

Δ3f(x)

x

f(x)

1

4

2

6

2

3

9

3

1

4

12

3

0

1

5

17

5

2

2

Δ4f(x)

3

3/1/2023 2:06:52 PM

292 • Applied Numerical Methods Using MATLAB (b) Table 5.4(c) shows the forward difference operations. TABLE 5.4(c) x

y

0

0

10

0.174

20

0.347

30

0.518

Δ2y

Δy

Δ3y

0.174 −0.001 −0.001

0.173 −0.002 0.171

(c) Table 5.4(d) shows the forward difference table. TABLE 5.4(d) x

x = f(x)

1

4

Δ2y

Δy

Δ3y

9 2

13

12 21

3

6

34

18 39

4

6

73

24 63

5

136

(d) The following Table 5.4(e) shows the backward differences. TABLE 5.4(e) x 1.00

y = f(x) 1

1.01

1.030

∇y

∇2y

∇3y

∇4y

0.030 0.001 −0.001

0.031 1.02

1.061

0.000 0.031

1.03

1.092

−0.001

0.001 0.032

1.04

0.002 0.001

1.124

0.000 0.001

0.033 1.05

1.157

Note the typical oscillations in sign and growth of the entries.

ANMF.CH05_4PP.indd 292

3/1/2023 2:06:52 PM

Finite Differences and Interpolation • 293

5.2.4 Error Propagation in a Difference Table Let y0, y1, y2, …., yn be the true values of a function and suppose the value y4 to be affected with an error ∈, so that its erroneous value is y4 + ∈. Then the successive differences of the y are as shown in Table 5. 5. TABLE 5.5  Error propagation in a difference table. y

Δ2y

Δy

Δ3y

y0 Δy0 Δ2y0

y1

Δ3y0

Δy1 2

Δ y0

y2

Δ3y1

Δy2 2

Δ y1

y3

Δ3y2 + ∈

Δy3 2

Δ y3 + ∈

y4

D3y3 − 3∈

Δy4 + ∈ 2

y5 + ∈

Δ y4 − 2∈ Δ3y4 + 3∈

Dy5 - ∈ 2

Δ y5 + ∈

y6

Δ3y5 − ∈

Δy6 2

Δ y6

y7

Δ3y6

Dy7 2

Δ y7

y8 Δy8 y9

Table 5.5 shows that the effect of an error increases with the successive differences, that the coefficients of the ∈’s are the binomial coefficients with alternating signs, and that the algebraic sum of the errors in any difference column is zero. The same effect is also true for the horizontal difference Table 5.2. EXAMPLE 5.2 Table 5.6(a) gives the values of a polynomial of degree five. It is given that f(4) is in error. Correct the value of f(4).

ANMF.CH05_4PP.indd 293

3/1/2023 2:06:52 PM

294 • Applied Numerical Methods Using MATLAB

TABLE 5.6(a) x

1

2

3

4

5

6

7

y = f(x)

0.975

−0.6083

−3.5250

−5.5250

−6.3583

4.2250

36.4750

Solution: It is given that y = f(x) is a polynomial of degree five. Hence Δ5y must be a constant and f(4) is in error. Let −5.5250 + ∈ be the true or correct value. The difference table is shown in Table 5.6(b). TABLE 5.6(b) x

y

1

0.975

∆y

∆2y

∆3y

∆4y

∆5y

−1.5833 2

−0.6083

−1.3333 −2.9167

3

−3.5250

4

−5.5250 + ∈

2.25 + ∈ 0.9167 + ∈

−2 + ∈

0.25 − 3∈ 1.1667 − 2∈

−0.8333 − ∈ 5

−6.3583

11.4667 + ∈

4.2250

12 + 10∈ 10 + 6∈

10.25 + 3∈

−10 − 10∈ 0 − 4∈

10.25 − ∈

10.5833 6

−2 − 4∈

21.6667 32.2500

7

36.4750

Since the fifth differences of y are constant, we have 12 + 10∈ = −10 − 10∈ or 20∈ = −22 or

∈ = −1.1

Hence f(4) = −5.5250 + ∈ = −5.5250 − 1.1 or f(4) = −6.6250

ANMF.CH05_4PP.indd 294

3/1/2023 2:06:52 PM

Finite Differences and Interpolation • 295

EXAMPLE 5.3 The following is a table of values of a polynomial of degree 5. It is given that f (3) is in error. Correct the error. TABLE 5.7(a) x

0

1

2

3

4

5

6

y

1

2

33

254

1054

3126

7777

Solution: It is given that y = f(x) is a polynomial of degree 5. Hence Δ5y must be constant; f(3) is in error. Let 254 + ε be the true value, now we form the difference table as shown in Table 5.7(b). TABLE 5.7(b) x

y

0

1

1

2

∆y

∆2y

∆3y

∆4y

∆5y

1 30 160 + ε

31 2

190 + ε

33 221 + ε

3

254 + ε

550 − 2ε 1771 − ε

4

420 + 6ε

1330 + ε

1054 3126

220 + 10ε

1780 + 3ε

20 − 10ε 440 − 4ε

1220 − ε

2101 5

200 − 4ε 360 − 3ε

12550 4651

6

7777

Since the fifth differences of y are constant 220 + 10ε = 20 − 10ε

ANMF.CH05_4PP.indd 295



⇒ 20ε = −220



⇒ ε = −10

3/1/2023 2:06:52 PM

296 • Applied Numerical Methods Using MATLAB f(3) = 254 + ε

Hence

⇒ f (3) = 244

EXAMPLE 5.4 Table 5.8 below shows a difference table. Find the location of error. TABLE 5.8 x

y=x



∆2

∆3

∆4

3

5

125

7.651

0.306

0.006

0

5.1

132.651

7.957

0.312

0.006

0

5.2

140.608

8.269

0.318

0.006

−0.027

5.3

148.877

8.587

0.324

−0.021

0.081

5.4

157.464

8.911

0.303

0.060

−0.081

5.5

166.375

9.214

0.363

−0.021

0.027

5.6

175.616

9.577

0.342

0.006

0

5.7

185.193

9.919

0.348

0.006

5.8

195.112

10.267

0.354

5.9

205.379

10.621

6

216

Solution: The location of an error is obviously centered on 0.060 in the third difference. Instead of 9.214, one has wrongly entered it as 9.241. 5.2.5 Properties of the Operator Δ 1. If c is a constant then Δc = 0. Proof: Let

f(x) = c

Hence f(x + h) = c, where h is the interval of differencing.

ANMF.CH05_4PP.indd 296

Hence

Df(x) = f(x + h) − f(x) = c − c = 0

or

Dc = 0

3/1/2023 2:06:53 PM

Finite Differences and Interpolation • 297

2. Δ is distributive, i.e., Δ[f(x) ± g(x)] = Δf(x) ± Δg(x). Proof:

Δ[f(x) + g(x)] = [f(x + h) + g(x + h)] − [f(x) + g(x)]



= f(x + h) − f(x) + g(x + h) − g(x)



= Df(x) + Dg(x).

Similarly, we have D[f(x) − g(x)] = Df(x) − Dg(x) 3. If c is a constant then D[cf(x)] = cDf(x). From properties 2 and 3 above, it is observed that D is a linear operator. Proof: D[cf(x)] = cf(x + h) − cf(x) = c[f(x + h) − f(x)] = cDf(x)



Hence  D[cf(x)] = cDf(x). 4. If m and n are positive integers then DmDnf(x) = Dm+nf(x). Proof:

DmDnf(x) = (D × D × D …. m times) (D × D …. n times) f(x)

   = (DDD … (m + n) times) f(x)    = Dm+nf(x). In a similar manner, we can prove the following properties: 5. D[f1(x) + f2(x) + …. + fn(x)] = Df1(x) + Df2(x) + …. + Dfn(x). 6. D[f(x)g(x)] = f(x) Dg(x) + g(x) Df(x).  f (x)  g(x)f (x)  f (x)g(x)   7.  g(x)  g(x)g(x  h)

ANMF.CH05_4PP.indd 297

3/1/2023 2:06:56 PM

298 • Applied Numerical Methods Using MATLAB 5.2.6 Difference Operators a) Shift operator, E The shift operator is defined as Ef(x) = f(x + h) or

(5.12)

Eyi = yi+1(5.13)

Hence, shift operator sifts the function value yi to the next higher value yi+1. The second shift operator gives

E2f(x) = E[Ef(x)] = E[f(x + h)] = f(x + 2h)

(5.14)

E is linear and obeys the law of indices. Generalizing,

Enf(x) = f(x + nh) or Enyi = yi+nh(5.15)

The inverse shift operator E−1 is defined as E−1f(x) = f(x − h)

(5.16)

In a similar manner, second and higher inverse operators are given by

E−2f(x) = f(x − 2h) and E−nf(x) = f(x − nh)

The more general form of E operator is given by Erf(x) = f(x + rh)

(5.17)

where r is positive as well as negative rationals. b) Average operator, μ The average operator m is defined as

ANMF.CH05_4PP.indd 298



1 f (x)  [f (x  h /2)  f (x  h /2)] 2

i.e.,

1 y i  [y i 1 / 2  y i 1 / 2 ] (5.18) 2

3/1/2023 2:06:59 PM

Finite Differences and Interpolation • 299

c) Differential operator, D The differential operator is usually denoted by D, where d f (x)  f (x) dx



Df (x) 



D2 f (x) 

d2 f (x)  f (x) (5.19) dx 2

5.2.7 Relation Among the Operators In order to develop approximations to differential equations, following summary of operators is useful. TABLE 5.9 Operator

Definition

Forward difference operator Δ

Δf(x) = f(x+h) − f(x)

Backward difference operator ∇

∇f(x) = f(x) − f(x−h)

Central difference operator δ

δf(x) = f(x+h/2) − f(x−h/2)

Shift operator E

Ef(x) = f(x+h)

Average operator μ

μf(x) = 0.5[f(x+h/2) − f(x−h/2)]

Differential operator D

Df(x) = f′(x)

Here h is the difference interval. For linking different operators with differential operator D we consider Taylor’s formula: f(x+h) = f(x) + hf′(x) +

1 2 h f″(x) +…… 2!

In operator notation, we can write it as: 1   Ef(x) = 1  hD  (hD)2  .... f(x) 2!   This series in brackets is the expression for the exponential and hence we can write hD

E=e

This relation can be used by symbolic programs such as Maple or Mathematica to analyze the accuracy of finite difference scheme.

ANMF.CH05_4PP.indd 299

3/1/2023 2:07:09 PM

300 • Applied Numerical Methods Using MATLAB From the definition of Δ, we know that Δf(x) = f(x + h) − f(x) where h is the interval of differencing. Using the operator E we can write Δf(x) = Ef(x) − f(x) ⇒ Δf(x) = (E − 1) f(x).



The above relation can be expressed as an identity

Δ=E−1

i.e., E = 1 + Δ Proof: EΔf(x) = E(f(x + h) − f(x))

= Ef(x + h) − Ef(x)



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



= Δf(x + h)



= ΔEf(x)

Hence EΔ = ΔE. EXAMPLE 5.5

 f (x)  Show that Δlog f(x) = log 1  . f (x)   Solution: Let h be the interval of differencing f(x +h) = Ef(x) = (Δ + 1) f(x) = Δ f(x) + f(x)



f (x  h) f (x) 1 , = f (x ) f (x )

Taking logarithms on both sides we get  f (x)   f (x + h)  log  = log 1   f (x)    f (x) 

ANMF.CH05_4PP.indd 300

3/1/2023 2:07:18 PM

Finite Differences and Interpolation • 301

 f (x)  ⇒ log f(x + h) − log f(x) = log 1  f (x)    f (x)  ⇒ Δlog f(x) = log 1  . f (x)   EXAMPLE 5.6  2  Evaluate   x 3 . E Solution: Let h be the interval of differencing  2  3 2 −1 3   x = (D E ) x E = (E − 1)2 E− 1 x3



= (E2 − 2E + 1) E− 1 x3



= (E − 2 + E− 1)x3



= Ex3 − 2x3 + E− 1 x3



= (x + h)3 − 2x3 + (x − h)3



= 6xh.

 2 Note: If h = 1, then  E

 3  x = 6x. 

EXAMPLE 5.7 Prove that ex =

D 2 x Ee x e . 2 x , the interval of differencing being h. E De

Solution: We know that

Ef(x) = f(x + h)

Hence   E ex = ex+h,

ANMF.CH05_4PP.indd 301

3/1/2023 2:07:22 PM

302 • Applied Numerical Methods Using MATLAB Again

Δex = ex+h − ex = ex(eh − 1) ⇒ Δ2ex = ex.(eh − 1)2



 2  Hence,   e x = (Δ2E− 1) ex = D2ex− h E   = e− h(Δ2ex) = e− hex(eh − 1)2 Therefore, the right-hand side = e− hex (eh − 1)

ex  h = ex. x h e (e  1)

Relation between E and ∇:

∇f(x) = f(x) − f(x − h) = f(x) − E− 1f(x)

⇒ ∇ = 1 − E−1 E−1   ∇ = . E

EXAMPLE 5.8 Prove the following (a) (1 + Δ) (1 − Δ) = 1 (b) Δ∇ = Δ − ∇. Solution: (a) (1 + Δ) (1 − ∇) f(x) = EE− 1f(x) = E f(x − h) = f(x) = 1.f(x).

\ (1 + Δ)(1 − ∇) = 1 (b) ∇Δf(x) = (E − 1)(1 − E− 1)f(x)

  = (E − 1) [f(x) − f(x − h)] Proofs for the Relations among the Operators: 1. D = E − 1 Since

Df(x) = f(x + h) − f(x)

or

Df(x) = E[f(x)] − f(x) = (E − 1)f(x)



Since f(x) is arbitrary, so ignoring it, we have



ANMF.CH05_4PP.indd 302

D = E − 1 or E = 1 + D

3/1/2023 2:07:31 PM

Finite Differences and Interpolation • 303

2. ∇ = 1 − E−1

We have

Hence

∇f(x) = f(x) − f(x − h) = f(x) − E−1[f(x)] = (1 − E−1)f(x) ∇ = 1 − E−1

3. δ = E1/2 − E−1/2

We have

Hence

δ[f(x)] = f(x + h/2) − f(x − h/2) = E1/2.[f(x)] − E−1/2.[f(x)] = (E1/2 − E−1/2)f(x) d = E1/2 − E−1/2

4. Δ = E∇ = ∇E = δE1/2

We have

E∇[f(x)] = E[f(x) − f(x − h)] = E[f(x)] − E[f(x − h)] = f(x + h) − f(x) = Δf(x)

Hence

E∇ = Δ

Again,

∇E[f(x)]= = ∇f(x + h) = f(x + h) − f(x) = Δf(x)

Hence

∇E = Δ



δE1/2.[f(x)] = δ[f(x + h/2)] = f(x + h) − f(x) = Δf(x)

Also,

Hence

δE1/2 = Δ

5. E = ehD

d dx



where   D =



We know  E[f (x)]  f (x  h)  f (x)  hf (x)   f (x)  hDf (x)  

  1  hD  

Hence

h2 f (x)  .... , by Taylor’s series 2!

h2 2 D f (x)  .... 2!

 h 2D2  ....  f (x)  ehD .f (x) 2! 

E = ehD.

1 1/2 1 / 2 6. u  (E  E ) 2 1 Since u[f (x)]  [f (x  h / 2)  f (x  h / 2) 2 1 1/2 1 1/2 1 / 2 1 / 2    [E f (x)  E f (x)]  [E  E ]f (x) 2 2 1 1/2 1 / 2 Hence u  (E  E ) . 2

ANMF.CH05_4PP.indd 303

3/1/2023 2:07:57 PM

304 • Applied Numerical Methods Using MATLAB 7. Δ∇ = Δ∇ = δ2

 Δ∇f(x) = Δ(f(x) − f(x − h)) = Δf(x) − Δf(x − h)

Since



= [f(x + h) − f(x)] − [f(x) − f(x − h)]



= δ.f(x + h/2) − δf(x − h/2) = δ2f(x)



Hence    D∇ = d2



Also  ∇ Df(x) = ∇ (f(x+h) − f(x)) = ∇f(x+h) − ∇f(x)



= [f(x + h) − f(x)] − [f(x) − f(x − h)]



= d.f(x + h/2) − df(x − h/2) = d2f(x)

Hence

∇ D = d2

8. (1 + D)(1 − D) = 1 L.H.S. = E.E−1 = E1−1 = Eº = 1 = R.H.S. Hence the result. The relationships among the various operators are shown in Table 5.10. TABLE 5.10  Relationship among the operators. E

Δ



δ −1

E

E

Δ+1

(1 − ∇)

Δ

E−1

Δ

(1 − ∇)−1 − 1



1 − E−1

1 − (1 + D)−1



δ

E1/2 − E−1/2

D(1 + D)−1/2

∇(1 − ∇)−1/2

δ

u

1 1/ 2 (E  E1/ 2 ) 2

1   1/ 2  1    (1  ) 2  

1   1 / 2  1    (1  ) 2  

1 2  1    4  

1 1   1  2    1  2  2 4   1 2 1       1  2  2 4   1 1    2    1  2  2 4  

5.2.8 Representation of a Polynomial using Factorial Notation A polynomial of degree n can be expressed as a fractional polynomial of the same degree. Let f(x) be a polynomial of degree which is to be expressed in factorial notation and let f(x) = a0 + a1x1 + a2x2 + …. + anxn(5.20)

ANMF.CH05_4PP.indd 304

3/1/2023 2:08:16 PM

Finite Differences and Interpolation • 305

where a0, a1, …., an are constants and a0 ≠ 0 then Δf(x) = Δ[a0 + a1x1 + …. + anxn]



⇒ Δf(x) = a1 + 2a2x1 + …. + ranx(n−1)

Hence Δ2f(x) = Δ[a1 + 2a2x1 + …. + ranx(n−1)] or Δ2f(x) = 2a2 + 2 × 3a3x1 + …. + n(n − 1)x(n−2) ….  r f (x)  a rn (n  1)....2  1x (0)  a rn! . Substituting x = 0 in the above, we obtain f (0)  a 0 ,

f (0)  2 f (0)  n f (0)  a1 ,  a 2 ,....,  an 1! 2! n!

Putting the values of a0, a1, a2, …., an in Equation (5.20), we get f (x)  f (0) 

f (0) 1  2 f (0) 2  n f (0) n x  x  ....  x . 1! 2! n!

EXAMPLE 5.9  2 Evaluate (a)  E

 2 x  (b) Δ sin x (c) Δ log x (d) Δ tan−1 x. Solution:

 2 (a)  E

 2  (E  1)2  2  E2  2E  1  2 x x   x  E     E  −1 2 2 = (E − 2 + E ) x2 = Ex − 2x + E−1x2 = (x + 1)2 − 2x2 + (x + 1)2 = 2.

(b) Δsin x = sin(x+h) − sin x xhx xhx = 2 cos   sin   2 2    

ANMF.CH05_4PP.indd 305

3/1/2023 2:08:31 PM

306 • Applied Numerical Methods Using MATLAB

h h  = 2 cos  x   sin 2 2  h h     Hence Δ sin x = 2 cos  x   sin . 2 2  (c) Δ log x = log(x + h) − log x x+h  h    = log = log 1   x x   h    Hence Δ log x = log 1   x  −1 −1 Δtan = tan (x + h) − tan−1 h  xhx    = tan−1  = tan−1  . 2    1  hx  x   1  (x  h)x 

EXAMPLE 5.10 Find a)  D2ex

b) D log x

Solution: a) D2ex = D(Dex) = D[ex+h − ex]

= D[ex(eh − 1)] = (eh − 1) Dex



= (eh − 1)(ex+h − ex) = eh − 1)ex

Hence D2ex = (eh− 1)2ex EXAMPLE 5.11 Evaluate

 2  E

 3 x . 

Solution: Let h = interval of differencing.

ANMF.CH05_4PP.indd 306

3/1/2023 2:08:51 PM

Finite Differences and Interpolation • 307

 2  3 2 1 3 2 1 3   x  ( E )x  (E  1) E x E      = (E2 − 2E + 1)E−1x3 = (E − 2 + E−1)x3 = Ex3 − 2x3 + E−1x3

   = (x + h)3 − 2x3 + (x − h)3 = 6xh EXAMPLE 5.12 Given u0 = 1, u1 = 11, u2 = 21, u3 = 28, u4 = 30, find Δ4u0. Solution:

Δ4u0 = (E − 1)4u0 = (E4 − 4c1E3 + 4c2E2 − 4c3E + 1)u0

  = E4y0 − 4E3u0 + 6E2u0 − 4Eu0 + u0 = u4 − 4u3 + 6u2 − 4u1 + u0   = 30 − 112 + 126 − 44 + 1 = 1. EXAMPLE 5.13 Estimate the missing term in the following table. x

0

1

2

3

4

y = f(x)

4

3

4

?

12

Solution: We are given four values, so the third differences are constant and the fourth differences are zero. Hence Δ4f(x) = 0 for all values of x. That is (E − 1)4f(x) = 0

(E4 − 4E3 + 6E2 − 4E + 1)f(x) = 0



E4f(x) − 4E3f(x) + 6E2f(x) − 4Ef(x) + f(x) = 0



f(x + 4) − 4f(x + 3) + 6f(x + 2) − 4f(x + 1) + f(x) = 0

where the interval of differencing is 1.

ANMF.CH05_4PP.indd 307

3/1/2023 2:08:55 PM

308 • Applied Numerical Methods Using MATLAB Now substituting x = 0, we obtain

f(4) + 4f(3) + 6f(2) − 4f(1) + f(0) = 0



12 + 4f(3) + 6(4) − 4(3) + 4 = 0

or f(3) = 7. EXAMPLE 5.14 Find

Δ3(1 − 3x) (1 − 2x) (1 − x).

Solution: Let f(x) = (1 − 3x)(1 − 2x)(1 − x) = −6x3 + 11x2 − 6x + 1 Here, f(x) is a polynomial of degree three and the coefficient of x3 is (−6). Hence D3f(x) = (−6)3! = −36. EXAMPLE 5.15 Evaluate   Δ(eax log bx). Solution: Let  f(x) = eax and g(x) = log bx. Hence Δf(x) = ea(x+h) − eax = eax(eah − 1) Also

h  Δg(x) = log b(x + h) − log bx = log  1   x  D(f(x).g(x)) = f(x + h) Dg(x) + g(x). Df(x)



= ea(x+h) log(1 + h/x) + log bx.eax(eah − 1)



= eax.[eah log(1 + h/x) + (eah − 1)log bx].

EXAMPLE 5.16 If m is a positive integer and the interval of differencing is 1, show that

ANMF.CH05_4PP.indd 308

D2x(−m) = m(m + 1)x(−m−2)

3/1/2023 2:08:58 PM

Finite Differences and Interpolation • 309

Solution: x (  m) 



[x (  m) ] 

1 , (x  1)(x  2)....(x  m) 1 1  (x  2)(x  1)....(x  m  1) (x  1)....(x  m) 1 1 1     (x  2)....(x  m)  (x  m  1) (x  1) 







m



(1)  (m)x (  m 1) (x  1)(x  2)....(x  m  1)

D2(x(−m)) = (−m)(−m − 1)x(−m−2) = m(m + 1)x(−m−2).

EXAMPLE 5.17 Express f(x) = 3x3 + x2 + x + 1, in the factorial notation, interval of differencing being unity. Solution: Here f(x) is a polynomial of degree 3. Hence, we can write

f(x) = f(0) +

Df (0) 1 D 2 f (0) 2 D 3 f (0) 3 x + x + x . 2! 1! 3!

The interval of differencing is unit and finding the values of the function at x = 0, 1, 2, 3, we get f(0) = 1, f(1) = 6, f(2) = 31, f(3) = 94. The difference table (Table 5.11) for the above values is given below: TABLE 5.11 x 0

f(x) 1

1

6

Δf(x)

Δ2f(x)

Δ3f(x)

5 20 25 2

31

3

94

16 38

63

ANMF.CH05_4PP.indd 309

3/1/2023 2:09:14 PM

310 • Applied Numerical Methods Using MATLAB From the table we have f(0) = 1, Df (0) = 5, D2f(0) = 20, D3f(0) = 18. Substituting the above values in f(x), we get f(x) = 1+ 5x1 + Hence

20 2 18 3 x + x, 2! 3!

f(x) = 3x3 + 10x2 + 5x + 1.

5.3 INTERPOLATION WITH EQUAL INTERVALS Here, we assume that for function y = f(x), the set of (n + 1) functional values y0, y1, …., yn are given corresponding to the set of (n + 1) equally spaced values of the independent variable, xi = x0 + ih, i = 0, 1, ..., n, where h is the spacing. 5.3.1 Missing Values Let a function y = f(x) is given for equally spaced values x0, x1, x2, …., xn of the argument and y0, y1, y2, …., yn denote the corresponding values of the function. If one or more values of y = f(x) are missing, we can determine the missing values by employing the relationship between the operators E and D. 5.3.2 Newton’s Binomial Expansion Formula Suppose y0, y1, y2, …., yn denote the values of the function y = f(x) corresponding to the values x0, x0 + h, x0 + 2h, …., x0 + nh of x. Let one of the values of y is missing since n values of the functions are known. Therefore, we have Dny0 = 0 or (E − 1)ny0 = 0

(5.21)

Expanding Equation (5.21), we have

[En − nC1En−1 + nC2En−2 + …. + (−1)n]y0 = 0

n(n − 1) n−2 E y0 + …. + (−1)ny0 = 0 2! n(n − 1) or yn − nyn−1 + yn−2 + …. + (−1)ny0 = 0 2

(5.22)

or Eny0 − nEn−1y0 +

ANMF.CH05_4PP.indd 310

(5.23)

3/1/2023 2:09:26 PM

Finite Differences and Interpolation • 311

Equation (5.23) is quite useful in determining the missing values without actually constructing the difference table. EXAMPLE 5.18 Determine the missing entry in the following table. x

0

y = f(x)

1

1 4

2

3

4

17



97

Solution: Let y0 = 1, y1 = 4, y2 = 17 and y4 = 97. We are given four values of y. Let y be a polynomial of degree 3. Hence D4y0 = 0 or (E − 1)4y0 = 0

(E4 − 4E3 + 6E2 − 4E + 1)y0 = 0



E4y0 − 4E3y0 + 6E2y0 − 4Ey0 + y0 = 0

or y4 − 4y3 + 6y2 − 4y1 + y0 = 0 That is 97 − 4(y3) + 6(17) − 4(4) + 1 = 0 or y3 = 46. EXAMPLE 5.19 Find the missing entry in the following table. x

0

1

2

3

4

5

y = f(x)

1

3

11



189

491

Solution: Here, we are given y0 = 1, y1 = 3, y2 = 11, y4 = 189 and y5 = 491. Since five values are given, we assume that y is a polynomial of degree 4. Hence D5y0 = 0 or (E − 1)5y0 = 0

5

4

(E.1) 3

2

(E − 5E + 10E − 10E + 5E − 1)y0 = 0

or y5 − 5y4 + 10y3 − 10y2 + 5y1 − y0 = 0

ANMF.CH05_4PP.indd 311

(E.2)

3/1/2023 2:09:26 PM

312 • Applied Numerical Methods Using MATLAB Substituting the given values for y0, y1, …., y5 in Equation (E.2), we get

491 − 5(189) + 10y3 − 10(11) + 5(3) − 1 = 0

or 10y3 = 550 or y3 = 55. EXAMPLE 5.20 Find the missing entries in the following table. x

0

1

2

3

4

5

y = f(x)

1



11

28



116

Solution: Here, we are given y0 = 1, y2 = 11, y3 = 28, and y5 = 116. Since three values are known, we assume y = f(x) as a polynomial of degree three. Hence D4y0 = 0 or (E − 1)4y0 = 0 That is (E4 − 4E3 + 6E2 − 4E + 1)y0 = 0 or y4 − 4y3 + 6y2 − 4y1 + y0 = 0

y4 − 4(28) + 6(11) − 4y1 + 1 = 0



y4 − 4y1 = 45

and

D5y0 = 0

(E.1)

or (E − 1)5y0 = 0 or (E5 − 5E4 + 10E3 − 10E2 + 5E − 1)y0 = 0

y5 − 5y4 + 10y3 − 10y2 + 5y1 − y0 = 0



116 − 5y4 + 10(28) − 10(11) + 5y1 − 1 = 0

or

−5y4 + 5y1 = −285(E.2)

Solving Equations (E.1) and (E.2), we obtain y1 = 4 and y4 = 61.

ANMF.CH05_4PP.indd 312

3/1/2023 2:09:26 PM

Finite Differences and Interpolation • 313

5.3.3 Newton’s Forward Interpolation Formula Let y = f(x), which takes the values y0, y1, y2, …., yn, that is the set of (n + 1) functional values y0, y1, y2, …., yn are given corresponding to the set of (n + 1) equally spaced values of the independent variable, xi = x0 + ih, i = 0, 1, 2, …., n where h is the spacing. Let f(x) be a polynomial of the nth degree in x taking the same values as y corresponding to x = x0, x1, …., xn. Then, ϕ(x) represents the continuous function y = f(x) such that f(xi) = ϕ(xi) for i = 0, 1, 2, …., n and at all other points f(x) = ϕ(x) + R(x) where R(x) is called the error term (reminder term) of the interpolation formula. Let

ϕ(x) = a0 + a1(x − x0) + a2(x − x0)(x − x1) + a3(x − x0)(x − x1)(x − x2) + …. + an(x − x0)(x − x1)(x − x2) …. (x − xn−1)(5.24)

and

ϕ(xi) = yi i = 0, 1, 2, …., n

(5.25)

The constants a0, a1, a2, …., can be determined as follows: Substituting x = x0, x1, x2, …., xn successively in Equation (5.24), we get

a0 = y0 



y1 = a0 + a1(x1 − x0)

(5.26)

or y1 = y0 + a1(x1 − x0)

[using Equation (5.26)]

y 1  y 0 y 0   x1  x 0 h



a1 



y2 = a0 + a1(x2 − x0) + a2(x2 − x0)(x2 − x1)

(5.27)

or y2 − y0 − a1(x2 − x0) = a2(x2 − x0)(x2 − x1) (y  y 0 ) or (y 2  y 0 )  1 (x 2  x 0 )  a 2 (x 2  x 0 )(x 2  x 1 ) (x 1  x 0 ) (y  y 0 )2 h or (y 2  y 0 )  1  a 2 2 hh h or

a2 

y 2  2y 1  y 0 2 y 0   2h2 2! h2

(5.28)

Similarly, we obtain a3 

ANMF.CH05_4PP.indd 313

3 y 0 n y 0 ,...., a  n 3! h3 n! hn

3/1/2023 2:09:40 PM

314 • Applied Numerical Methods Using MATLAB Hence, from Equation (5.24), we have (x)  y 0 

y 0 2 y 0 n y 0 (x  x 0 )  ( x  x )( x  x )  (x  x 0 )(x  x 1 )....(x  x n 1 )  (5.29) 0 1 h 2! h2 n! hn

Let x = x0 + uh or x − x0 = uh and x − x1 = (x − x0) − (x1 − x0) = uh − h = (u − 1)h

(5.30)

x − x2 = (x − x1) − (x2 − x1) = (u − 1)h − h = (u − 2)h, etc.



Using the values from Equation (5.30), Equation (5.29) reduces to u(u  1) 2 u(u  1)(u  2) 3  y0   y 0  .... 2! 3! u(u  1)....(u  (n  1)) n   y0 (5.31) n!

(x)  y 0  uy 0 

The formula given in Equation (5.31) is called the Newton’s forward interpolation formula. This formula is used to interpolate the values of y near the beginning of a set of equally spaced tabular values. This formula can also be used for extrapolating the values of y a little backward of y0. 5.3.4 MATLAB M-file: Newtonint An M-file for Newton interpolation is given below: function yint = Newtonint(x, y, xx) % Newton’s interpolation method based on n data points of (x, y) % Input: % x = Independent variable % y = Dependent variable % xx = Independent variable value at which the interpolated value is % determined % Output: % yint = Interpolated value of y

ANMF.CH05_4PP.indd 314

3/1/2023 2:09:45 PM

Finite Differences and Interpolation • 315

n = length(x); b = zeros (n, n); b (:, 1) = y(:); for j = 2:n   for i = 1:n − j + 1   b(i, j) = (b(i + 1, j − 1) − b(i, j − 1))/(x(i + j − 1) − x(i));  end end xt = 1; yint = b(1,1); for j = 1:n − 1  xt = xt*(xx − x(j));  yint = yint + b(1, j + 1)*xt; end EXAMPLE 5.21 Given that

15500 = 124.4990,

15510 = 124.5392,

15530 = 124.6194, find the value of

15520 = 124.5793 and

15516 .

Solution: The difference table is given below: x

y= x

15500 x0

124.4990 y0

Δy

Δ2y

0.0402 15510

124.5392

15520

124.5793

15530

124.6194

0 Δ2y0 0.0401 0 0.0401

ANMF.CH05_4PP.indd 315

3/1/2023 2:09:54 PM

316 • Applied Numerical Methods Using MATLAB Here x0 = 15500, h = 10 and x = 15516

u

x  x 0 15516  15500   1.6 h 10

Newton’s forward difference formula is

u(u  1) 2  y 0  .... 2!

f (x)  y 0  uy 0 

or f(15516) = 124.4990 + 1.6(0.0402) + 0 = 124.56323 EXAMPLE 5.22 A second degree polynomial passes through the points (1, −1), (2, −2), (3, −1), and (4, 2). Find the polynomial. Solution: The difference table is constructed with the given values of x and y as shown below: Δy

x

y

1

−1

Δ2y

Δ3y

−1 2

−2

2 1

3

−1

4

2

0 2

3

Here x0 = 1, h = 1, y0 = −1, Δy0 = −1 and Δ2y0 = 2

u

x  x0  (x  1) h

From the Newton’s forward interpolation formula, we have or

ANMF.CH05_4PP.indd 316

y  f (x)  y 0  uy 0 

u(u  1) 2  y 0  .... 2!

f (x)  1  (x  1)(1) 

(x  1)(x  1  1) .2  x 2  4 x  2 2

3/1/2023 2:10:03 PM

Finite Differences and Interpolation • 317

EXAMPLE 5.23 Find y = e3x for x = 0.05 using the following table. x

0

0.1

0.2

0.3

0.4

e3x

1

1.3499

1.8221

2.4596

3.3201

Solution: The difference table is shown in below: x

y = e3x

0.00

1.0000

Δy

Δ2y

Δ3y

Δ4y

0.3499 0.10

1.3409

0.1224 0.4723

0.20

1.8221

0.0428 0.1652

0.6375 0.30

2.4596

0.0150 0.0578

0.2230 0.8605

0.40

3.3201

We have x0 = 0.00, x = 0.05, h = 0.1 Hence u 

x  x 0 0.05  0.00   0.5 h 0.1

Using Newton’s forward formula u(u  1) 2 u(u  1)(u  2) 3  y0   y0 2! 3! u(u  1)(u  2)(u  3) 4   y 0  .... 4!

f (x)  y 0  uy 0   

f(0.05) = 1.0 + 0.5(0.3499) + 0.5(0.5  1) (0.1224)  (0.5)(0.5  1)(0.5  2) (0.0428) 2 6 0.5(0.5  1)(0.5  2)(0.5  3)  (0.0150) 24     f(0.05) = 1.16172.

ANMF.CH05_4PP.indd 317

3/1/2023 2:10:14 PM

318 • Applied Numerical Methods Using MATLAB MATLAB Solution: >> x = [0 0.1 0.2 0.3 0.4]; >> y = exp(3*x); >> y = Newtonint(x,y,0.05) y= 1.1617 EXAMPLE 5.24 The values of sin x are given below for different values of x. Find the value of sin 42º. x

40

45

50

55

60

y = f(x) = sin x

0.6428

0.7071

0.7660

0.8192

0.8660

Solution: x = 42º is near the starting value x0 = 40º. Hence, we use Newton’s forward interpolation formula. x

y = sin x

40º

0.6428

Δy

Δ2y

Δ3y

Δ4y

0.0643 45º

−0.0054

0.7071

−0.0004

0.0589 50º

−0.0058

0.7660

55º

0 −0.0004

0.0531 −0.0062

0.8192 0.0469

60º

0.8660

u

x  x 0 42 º  40 º   0.4 h 5

We have y0 = 0.6428, Δy0 = 0.0643, Δ2y0 = −0.0054, Δ3y0 = −0.0004

ANMF.CH05_4PP.indd 318

3/1/2023 2:10:15 PM

Finite Differences and Interpolation • 319

Putting these values in Newton’s forward interpolation formula we get

f (x)  y 0  uy 0 

u(u  1) 2 u(u  1)(u  2) 3  y0   y 0  .... 2! 3!

f (42)  0.6428  0.4(0.0643)  



0.4(0.4  1) (0.0054) 2

0.4  (0.4  1)(0.4  2) (0.0004)  0.66913. 6

MATLAB Solution: >> x = [40 45 50 55 60]; >> y = sin(x*pi/180); >> y = Newtonint(x,y,42) y= 0.6691 EXAMPLE 5.25a The profits of a company (in thousands of rupees) are given below: Year (x)

1990

1993

1996

1999

2002

Profit y = f(x)

120

100

111

108

99

Calculate the total profits in 1991. Solution: The forward difference table is constructed as shown below: x 1990

y 120

1993

100

Δy0

Δ2y0

Δ3y0

Δ4y0

−20 31 −45

11 1996

−14

111 −3

1999

53 8

−6

108 −9

2002

ANMF.CH05_4PP.indd 319

99

3/1/2023 2:10:18 PM

320 • Applied Numerical Methods Using MATLAB To calculate profits for 1991:

x  x0 = 0.33 h Using Newton’s forward interpolation formula, we obtain Let x0 = 1990, x = 1991, h = 3, p 

u(u  1) 2 u(u  1)(u  2) 3  y0 . y 0  2! 3! u(u  1)(u  2)(u  3) 4   y0 4!

y (1991)  y 0  uy 0 

 120  0.33(20)  

or

0.33(0.33  1) 0.33(0.33  1)(0.33  2) (31)  (45) 2 6

0.33(0.33  1)(0.33  2)(0.33  3) (53)  104.93 24

104.93(1000) rupees.

EXAMPLE 5.25b Consider the difference table (Table 5.12) of f(x) = sin x for x = 0° to 50°: find sin(45). TABLE 5.12 x(deg)

f(x) = sin x

0

0

10

0.1736

20

0.3420

Δ

Δ2

Δ3

Δ4

Δ5

0.0004

0

0.1736 −0.0052 −0.0052

0.1684 −0.0104

−0.0048

0.1580 30

−0.0152

0.5000

40

0.0004 −0.0044

0.1428 −0.0196

0.6425 0.1232

50

0.766

Solution: Since the fourth order differences are constant, we conclude that a quartic approximation is appropriate. In order to determine sin 5° from the table, we

ANMF.CH05_4PP.indd 320

3/1/2023 2:10:30 PM

Finite Differences and Interpolation • 321

use Newton’s forward difference formula (to fourth order); thus, taking xj = 0, 5−0 we find a = = ½ . Hence. 10 sin 5o = sin 0o + ½ (0.1736) + (½)(½)(−½) (−0.0052) + (1/6) (½)(−½)(−3/2)(−0.0052) + (1/24) (½)(−½)(−3/2)(−5/2)(0.0004)

= 0 + 0.0868 + 0.0006(5) − 0.0003(3) − 0.0000(2) = 0.0871.

In order to determine sin 45° from the table, we use Newton’s backward dif45  40 1  ference formula (to fourth order); thus, taking xj= 40, we find b = 10 2 and  sin 45o = sin 40o + 1 (0.1428) + 1 1 3 (−0.0152) + 1 1 3 5 (−0.0048) 2 2 2 2 6 2 2 2 1 1 3 5 7 + (0.0004) 24 2 2 2 2

= 0.6428 + 0.0714 − 0.0057 − 0.0015 + 0.00001 = 0.7071

EXAMPLE 5.26 If f(x) is known at the following data points  xi

0

1

2

3

4

fi

1

7

23

55

109

Find f(0.5) using Newton’s forward difference formula. Solution: Forward difference table is prepared as shown in Table 5.13. TABLE 5.13 x

f

0

 1

Δf

Δ2f

Δ3f

Δ4f

6 1

 7

10 16

2

 23

6 16

32 3

 55

0 6

22 54

4

ANMF.CH05_4PP.indd 321

 109

3/1/2023 2:10:50 PM

322 • Applied Numerical Methods Using MATLAB By Newton’s forward difference formula a(a  1) 2 a(a  1)(a  2) 3  f(x0 + a h) = f0  af0   f0   f0   2! 3!  To find f(0.5): At x = 0.5, a = (x − x0) / h = (0.5 − 0)/1 = 0.5



0.5(0.5  1) 0.5(0.5  1)(0.5  2)   10  6 Hence f(0.5) = 1  0.5  6  2! 3!   = 1 + 3 + 2.5 × (−0.5) + (−0.25)(−1.5) = 3.125



MATLAB Solution: >> x = [0 1 2 3 4]; >> y = [1 7 23 55 109]; >> yint = Newtonint(x,y,0.5) yint = 3.1250 5.3.5 Newton’s Backward Interpolation Formula Newton’s forward interpolation formula is not suitable for interpolation values of y near the end of a table of values. Let y = f(x) be a function that takes the values y0, y1, y2, …., yn corresponding to the values x0, x1, x2, …., xn of the independent variable x. Let the values of x be equally spaced with h as the interval of differencing. That is xi = x0 + ih,

i = 0, 1, 2, …., n th

Let ϕ(x) be a polynomial of the n degree in x taking the same values of y corresponding to x = x0, x1, …., xn. That is, ϕ(x) represents y = f(x) such that f(xi) = ϕ(xi), i = 0, 1, 2, …. Hence, we can write ϕ(x) as

ϕ(xi) = yi, i = n, n − 1, …., 1, 0

and xn−i = xn−ih,

ANMF.CH05_4PP.indd 322

i = 1, 2, …., n

3/1/2023 2:10:55 PM

Finite Differences and Interpolation • 323

Let f(x) = a0 + a1(x − xn) + a2(x − xn)(x − xn−1) + …. +     an(x − xn)(x − xn−1) …. (x − x0)(5.32) Substituting x = xn, xn−1, …., x1, x0 successively, we obtain   a0 = yn(5.33)

yn−1 = a0 + a1(xn−1 − xn)

y  y n y n a 1  n 1  or   (5.34) x n 1  x n h Similarly, we obtain a 2 

2 y n n y n ,...., a  (5.35) n 2! h2 n! hn

Substituting the values from Equations (5.33), (5.34), and (5.35) in Equation (5.32), we get y n 2 y n (x  x n )  (x  x n )(x  x n 1 )  .... h 2! h2 n y n (x  x n )(x  x n 1 )....(x  x 0 )  (5.36) n! hn

(x)  y n 



Now, setting x = xn + vh, we obtain   x − xn = vh

x − xn−1 = (v + 1)h

…..   x − x0 = (v + n − 1)h Hence, Equation (5.36) reduces to

(x)  y n 

where  v 

ANMF.CH05_4PP.indd 323

v(v  1) 2 (v  n  1) n  y n  ....  v(v  1)....  y n (5.37) 2! n!

x  xn . h

3/1/2023 2:11:05 PM

324 • Applied Numerical Methods Using MATLAB The formula given in Equation (5.37) is called the Newton’s backward ­interpolation formula. This formula is used for interpolating values of y near the end of the tabulated values and also is used for extrapolating values of y a little backward of yn. EXAMPLE 5.27 Find f(0.15) using Newton backward difference formula from Table 5.14. TABLE 5.14 x

f(x)

0.1

0.09983

∇2f

∇f

∇3f

∇4f

0.09884 0.2

−0.00199

0.19867

−0.00156

0.09685 0.3

−0.00355

0.29552

0.4

0.00121 −0.00035

0.0939 −0.0039

0.38942 0.09

0.5

0.97943

Solution: Using Newton-Gregory’s formula: b(b  1) 2 b(b  1)(b  2) 3 b(b  1)(b  2)(b  3) 4    fn   fn   fn  f(x) = f n  nf n  2! 3! 4!   where in present case: h = 0.1, n = 5 b = (x − xn ) / h = (0.15 − 0.5)/0.1 = −3.5 Hence, f(0.15) = 0.97943 + 3.5(3.5  1) 3.5(3.5  1)(3.5  2) (0.0039)  2! 3! 3.5(3.5  1)(3.5  2)(3.5  3) (0.00035)  (0.00121) 4!

3.5  0.09 

=  0.97943 − 0.315 − 0.01706 + 0.000765625 + 0.00033086 = 0.14847

ANMF.CH05_4PP.indd 324

3/1/2023 2:11:15 PM

Finite Differences and Interpolation • 325

EXAMPLE 5.28 Calculate the value of f(84) for the data given in the table below: x

40

50

60

70

80

90

f(x)

204

224

246

270

296

324

Solution: The value of 84 is near the end of Table 5.15. Hence, we use the Newton’s backward interpolation formula. The difference table is shown below. TABLE 5.15 x

f(x)

40

204

50

224

60

246

70

270

80

296

90

324



∇2

∇3

∇4

∇5

20 2 22

0 2

24

0 0

2 26

0 0

0 2

28

We have xn = 90, x = 84, h = 10, tn = yn = 324, ∇tn = ∇yn = 28,

∇2yn = 2 and f h = f h. ∇3yn = ∇4yn = ∇5yn = 0, u

x  x n 84  90   0.6 h 10

From Newton’s backward formula f (84)  t n  ut n 

u(u  1) 2  t n  .... 2

f (84)  324  0.6  28 

ANMF.CH05_4PP.indd 325

(0.6)(0.6  1) 2  324  16.8  0.24  306.96. 2

3/1/2023 2:11:22 PM

326 • Applied Numerical Methods Using MATLAB MATLAB Solution: >> x = [40 50 60 70 80 90]; >> y = [204 224 246 270 296 324]; >> yint = Newtonint(x,y,84) yint = 306.9600 EXAMPLE 5.29 Use Gauss forward formula to find y at x = 30 given the following table of values: x

21

25

29

33

37

y

18.4708

17.8144

17.1070

16.3432

15.5154

Solution: We construct the following difference Table 5.16: TABLE 5.16 x

y

x0 − 2h = 21

18.4708

x0 − h = 25

17.8144

x0 = 29

17.1070

x + h + 33

16.3432

x0 + 2h = 37

15.5154

Δy

Δ2y

Δ3 y

Δ4y

−0.6564 −0.0510 −0.7074

−0.0054 −0.0564

−0.7638

−0.002 −0.0076

−0.0640 −0.8278

30  29 1  = 0.25. Here h = 4, u = 4 4 u = 0.25 lies between 0 and 1. Hence, the Gauss’s forward formula is suitable. Substituting in the Gauss’s interpolation formula u(u  1) 2 (u  1)u(u  1) 3 y = y0 + uDy0 +  y 1 +  y 1 2! 3! (u  1)u(u  1)(u  2) 4  y 2 + …. + 4!

ANMF.CH05_4PP.indd 326

3/1/2023 2:11:32 PM

Finite Differences and Interpolation • 327

We get

(0.25)(0.75)  (0.0564) 2 (1.25)(0.25)(−0.75)(−1.75) (1.25)(0.25)(0.75) +  (0.0076) + (−0.0022) 24 6 = 16.9216. y0.25 = f(0.25) = 17.1070 + (0.25)(−0.7638) +

MATLAB Solution: >> x = [21 25 29 33 37]; >> y = [18.4708 17.8144 17.1070 16.3432 15.5154]; >> yint = Newtonint(x,y,30) yint = 16.9216 EXAMPLE 5.30 From the following table estimate the number of students who obtained marks in computer programming between 75 and 80. Marks

: 35−45 45−55 55−65 65−75 75−85

No. of students :

20

40

60

60

20

Solution: The cumulative frequency table is shown in Table 5.17. TABLE 5.17

ANMF.CH05_4PP.indd 327

Marks less than (x)

No. of students (y)

45

20

55

60

40

65

120

60

75

180

60

0

−20

85

200

20

−40

−40

∇y

∇2y

∇3y

∇4y

20 −20

3/1/2023 2:11:35 PM

328 • Applied Numerical Methods Using MATLAB To find the number of students with marks less than 80 x  xn  0.5 Let xn = 85, x = 80, h = 10, p  h Then using Newton’s backward interpolation formula, we obtain y  y n  py n 

v(v  1) 2 v(v  1)(v  2) 3 v(v  1)(v  2)(v  3) 4  yn   yn   yn 2! 3! 4!

0.5(0.5  1) 0.5(0.5  1)(0.5  2)  200  (0.5)(20)  (40)  (40) 2 6 

0.5(0.5  1)(0.5  2)(0.5  3) (20)  198.2813 24

So, the number of students getting marks in computer programming between 75 and 80

= 198 − 180 = 18.

5.3.6 Error in the Interpolation Formula Let ϕ(x) denote the interpolating polynomial. Also, let the function f(x) be continuous and possess continuous derivatives within the interval (x0, xn). Now, defining the auxiliary function F(t) as

F(t)  f (t)  (t)  {f (x)  (x)}

(t  x 0 )(t  x 1 )....(t  x n ) (5.38) (x  x 0 )(x  x 1 )....(x  x n )

The expression (t − x0) (t − x1)…. (t − xn) is a polynomial of degree (n + 1) in t and the coefficient of t = 1. Hence, the (n + 1)th derivative f polynomial is (n + 1)!. That is

F n 1 ()  f n 1 ()  {f (x)  (x)}

or

f (x)  (x) 

ANMF.CH05_4PP.indd 328

(n  1)!  0 (5.39) (x  x 0 )(x  x 1 )....(x  x n )

f n 1 () (x  x 0 )(x  x 1 )....(x  x n ) (5.40) (n  1)!

3/1/2023 2:11:48 PM

Finite Differences and Interpolation • 329

Let R(x) denotes the error in the formula. Then R(x) = f(x) − f(x) Hence R(x) 

f n 1 () (x  x 0 )(x  x 1 )....(x  x n ) (n  1)!

Since x − x0 = uh or x − x1 = (u − 1)h, …. (x − xh) = (u − n)h where h is the interval of differencing, we have Error

R( x ) 

h n 1 f n 1 () u(u  1)(u  2)....(u  n) (n  1)!

Now, employing the relation

D

1  h

we have Dn 1  or

1

 n 1 h n 1  n  1 f (x 0 ) f n 1 ()  (5.41) n 1

The error in the forward interpolation formula is given by

R( x ) 

 n 1 y 0 u(u  1)(u  2)....(u  n) (5.42) (n  1)!

In a similar manner, by taking the auxiliary function F(t) in the form F(t)  f (t)  (t)  {f (x)  (x)}

(t  x n )(t  x n 1 )....(t  x 0 ) (x  x n )(x  x n 1 )....(x  x 0 )

and proceeding as above, we obtain the error in the Newton’s backward interpolation formula as



ANMF.CH05_4PP.indd 329

R( x ) 

 n 1 y n u(u  1)....(u  n) (5.43) (n  1)!

3/1/2023 2:12:03 PM

330 • Applied Numerical Methods Using MATLAB

where

u

x  xn . h

EXAMPLE 5.31a Using Newton’s forward interpolation formula find the value of cos 52º from the following data and estimate the error. x y = cos x

45º

50º

55º

60º

0.7071

0.6428

0.5736

0.5

Solution: The difference table is given below: x

y = cos x

45º

0.7071

50º

0.6428

55º

0.5736

60º

0.5

Δy

Δ2y

Δ3y

−0.0643 −0.0049 −0.0692

0.0005 −0.0044

−0.0736

Here x0 = 45º, x1 = 52º, y0 = 0.7071, Dy0 = −0.0643, D2y0 = −0.0049 and D3y0 = 0.0005. x  x 0 52 º 45 º u   1.4  5º h From Newton’s forward interpolation formula

y  u 0  uy 0 

u(u  1) 2 u(u  1)(u  2) 3  y0   y 0 .... 2! 3!

Hence, y  f (52)  0.7071  1.4(0.0643) 

1.4(1.4  1) (0.0049) 2

(1.4)(1.4  1)(1.4  2) (0.0005)  0.615680 6 u(u  1)(u  2)....(u  n) n 1  y0 Error   where n = 2. n 1 

ANMF.CH05_4PP.indd 330

3/1/2023 2:12:11 PM

Finite Differences and Interpolation • 331

  

u(u  1)(u  2) 3 1.4(1.4  1)(1.4  2)  y0  (0.0005) 3! 6

Error   = −0.000028. MATLAB Solution: >> x = [45 50 55 60]; >> y = cos(x*pi/180); >> yint = Newtonint(x,y,52) yint = 0.6157

5.4 INTERPOLATION WITH UNEQUAL INTERVALS 5.4.1 Lagrange’s Interpolating Polynomial for Equal Intervals Let us formulate a linear interpolating polynomial as the weighted average of the two values that we are connecting by a straight line (See Figure 5.1(a)):    f(x) = L1f(x1) + L2f(x2)(5.43a) where the L’s are the weighting coefficients. The first weighting coefficient is the straight line that is equal to 1 at x1 and 0 at x2: L1 

x  x2 x1  x 2

Similarly, the second coefficient is the straight line that is equal to 1 at x2 and 0 at x1: L2 

x  x1 x 2  x1

Substituting these coefficients into Equation (5.43a) yields the straight line that connects the points (see Figure 5.1(a)):

ANMF.CH05_4PP.indd 331

f1 ( x ) 

x  x2 x  x1 f (x 1 )  f (x 2 ) (5.43b) x1  x 2 x 2  x1

3/1/2023 2:12:21 PM

332 • Applied Numerical Methods Using MATLAB where the nomenclature f1(x) designates that this is a first-order ­polynomial. Equation (5.43b) is referred to as the linear Lagrange interpolating polynomial.

FIGURE 5.1(a)

The same strategy can be used to fit a parabola through three points. In this case three parabolas would be used with each one passing through one of the points and equaling zero at the other two. Their sum would then represent the unique parabola that connects the three points. Such a second-order Lagrange interpolating polynomial can be written as follows: f 2 (x ) 

(x  x 2 )(x  x 3 ) (x  x 1 )(x  x 3 ) (x  x 1 )(x  x 2 ) f (x 1 )  f (x 2 )  f (x 3 ) (x 1  x 2 )(x 1  x 3 ) (x 2  x 1 )(x 2  x 3 ) (x 3  x 1 )(x 3  x 2 )

(5.43c) Notice how the first term is equal to f(x1) at x1 and is equal to zero at x2 and x3. Both the first- and second-order versions as well as higher-order Lagrange polynomials can be written as n



f n 1 (x)   L i (x)f (x i ) (5.43d) i 1

n

where

x  xj

x i 1 j1

i

 xj

(5.43e)

where n = the number of data points and ∏ designates the product of.

ANMF.CH05_4PP.indd 332

3/1/2023 2:12:25 PM

Finite Differences and Interpolation • 333

An M-file to implement Lagrange’s interpolation is given below: 5.4.2 function yint = Lagrangeint (x,y,xx) % Lagrange’s interpolation method based on n data points of (x, y) % Input: % x = Independent variable % y = Dependent variable % xx = Independent variable value at which the interpolated value is % determined % Output: % yint = Interpolated value of y n = length(x); s = 0; for i = 1:n product=y(i); for j=1:n    if i~=j    product=product*(xx − x(j))/(x(i) − x(j));   end  end  s = s + product; end yint = s; The Newton’s forward and backward interpolation formulae are applicable only when the values of n are given at equal intervals. In this section, we ­present Lagrange’s formula for unequal intervals.

ANMF.CH05_4PP.indd 333

3/1/2023 2:12:25 PM

334 • Applied Numerical Methods Using MATLAB EXAMPLE 5.31b Use a Lagrange interpolating polynomial of the first and second order to ­evaluate the value of y at x = 25 based on the data in Table 5.18. TABLE 5.18 x

10

30

50

y

7.7

1.6

0.424

Solution: The first-order polynomial can be used to obtain the estimate at x = 25. (x  x 2 ) (x  x 1 ) (25  10) (25  30) f1 ( x )  f (x 1 )  f (x 2 )  7.70  1.6  3.1250 (30  10) (x 1  x 2 ) (x 2  x 1 ) (10  30) The second order polynomial can now be used. (x  x 2 )(x  x 3 ) (x  x 1 )(x  x 3 ) (x  x 1 )(x  x 2 ) f 2 (x )  f (x 1 )  f (x 2 )  f (x 3 ) (x 2  x 1 )(x 2  x 3 ) (x 1  x 2 )(x 1  x 3 ) (x 3  x 1 )(x 3  x 2 ) f 2 (x ) 

(25  30) (25  50) (25  10)(25  50) 1.6 7.70  (10  30) (10  50) (30  10)(30  50) (25  10)(25  30)  0.424  2.6634 (50  10)(50  30)

MATLAB Solution: >> x = [10 30 50]; >> y = [7.7000 1.6000 0.4240]; >> yint = Lagrangeint(x,y,25) yint = 2.6634 5.4.3 Lagrange’s Formula for Unequal Intervals Let y = f(x) be a real valued continuous function defined in an interval [a, b]. Let x0, x1, x2, …., xn be (n + 1) distinct points that are not necessarily equally

ANMF.CH05_4PP.indd 334

3/1/2023 2:12:32 PM

Finite Differences and Interpolation • 335

spaced and the corresponding values of the function are y0, y1, …., yn. Since (n + 1) values of the function are given corresponding to the (n + 1) values of the independent variable x, we can represent the function y = f(x) is a polynomial in x of degree n. Let the polynomial is represented by f(x) = a0(x − x1)(x − x2)….(x − xn) + a1(x − x0)(x − x2)….(x − xn) + a2(x − x0)(x − x1)(x − x3)….(x − xn) + …. + an(x − x0)(x − x1)….(x − xn−1)(5.44)



Each term in Equation (5.44) being a product of n factors in x of degree n, putting x = x0 in Equation (5.44) we obtain f(x) = a0(x0 − x1)(x0 − x2)….(x0 − xn) f (x 0 ) a0  (x 0  x 1 )(x 0  x 2 )....(x 0  x n )

or

Putting x = x2 in Equation (5.44) we obtain

f(x1) = a1(x1 − x0)(x1 − x2)….(x1 − xn)

or

a1 

f (x 1 ) (x 1  x 0 )(x 1  x 2 )....(x 1  x n )

Similarly putting x = x2, x = x3, x = xn in Equation (5.44) we obtain a2 



f (x 2 ) (x 2  x 0 )(x 2  x 1 )....(x 2  x n ) M

and

an 

M

f (x n ) (x n  x 0 )(x n  x 1 )....(x n  x n 1 )

Substituting the values of a0, a1, …., an in Equation (5.44) we get (x  x 0 )(x  x 2 )....(x  x n ) (x  x 1 )(x  x 2 )....(x  x n ) y  f (x )  f (x 0 )  f (x 1 )  .... (x 1  x 0 )(x 1  x 2 )....(x 1  x n ) (x 0  x 1 )(x 0  x 2 )....(x 0  x n )

ANMF.CH05_4PP.indd 335



(x  x 0 )(x  x 1 )....(x  x n 1 ) f (x n ) (5.45) (x n  x 0 )(x n  x 1 )....(x n  x n 1 )

3/1/2023 2:12:46 PM

336 • Applied Numerical Methods Using MATLAB The formula given by Equation (5.45) is known as the Lagrange’s ­interpolation formula. EXAMPLE 5.32 Apply Lagrange’s interpolation formula to find a polynomial that passes through the points (0, −20), (1, −12), (3, −20), and (4, −24). Solution: We have x0 = 0, x1 = 1, x2 = 3, x3 = 4, y0 = f(x0) = −20, y1 = f(x1) = −12, y2 = f(x2) = −20 and y3 = f(x3) = −24. The Lagrange’s interpolation formula is f (x ) 

(x  x 1 )(x  x 2 )(x  x 3 ) (x  x 0 )(x  x 2 )(x  x 3 ) f (x 0 )  f (x 1 ) (x 0  x 1 )(x 0  x 2 )(x 0  x 3 ) (x 1  x 0 )(x 1  x 2 )(x 1  x 3 )





(x  x 0 )(x  x 1 )(x  x 3 ) (x  x 0 )(x  x 1 )(x  x 2 ) f (x 3 ) . f (x 2 )  (x 2  x 0 )(x 2  x 1 )....(x 2  x 3 ) (x 3  x 0 )(x 3  x 1 )(x 3  x 2 )

Hence f (x) 

(x  1)(x  3)(x  4) (x  0)(x  3)(x  4) (20)  (12) (1  0)(1  3)(1  4) (0  1)(0  3)(0  4) 

(x  0)(x  1)(x  4) (x  0)(x  1)(x  3) (24) (20)  (3  0)(3  1)(3  4) (4  0)(4  1)(4  3)

or f(x) = x3 − 8x2 + 15x + 20 is the required polynomial. EXAMPLE 5.33 Using Lagrange’s interpolation formula find a polynomial that passes the points (0, −12), (1,0), (3,6), (4,12). Solution: We have x0 = 0, x1 = 1, x2 = 3, x3 = 4, y0 = f(x0) = −12, y1 = f(x1) = 0, y2 = f(x2) = 6, y3 = f(x3) = 12.

ANMF.CH05_4PP.indd 336

3/1/2023 2:12:51 PM

Finite Differences and Interpolation • 337

Using Lagrange’s interpolation formula, we can write f(x) =

(x − x 1 )(x − x 2 )(x − x 3 ) (x − x 0 )(x − x 2 )(x − x 3 ) f(x0) + f(x1) (x 0 − x 1 )(x 0 − x 2 )(x 0 − x 3 ) (x 1 − x 0 )(x 1 − x 2 )(x 1 − x 3 ) (x − x 0 )(x − x 1 )(x − x 3 ) (x − x 0 )(x − x 1 )(x − x 2 ) f(x2) + f(x3) (x 2 − x 0 )(x 2 − x 1 )(x 2 − x 3 ) (x 3 − x 0 )(x 3 − x 1 )(x 3 − x 2 )

+

Substituting the values, we get: f(x) = 

(x  1)(x  3)(x  4) (x  0)(x  3)(x  4)  12  0 12 6

(x  0)(x  1)(x  4) (x  0)(x  1)(x  3)  12 6 + 12 6 = −(x − 1)(x − 3)(x − 4) + −(x − 0)(x − 1)(x − 4) + (x − 0)(x − 1)(x − 3) +

EXAMPLE 5.34 Using Lagrange’s interpolation formula, find the value of y corresponding to x = 10 from the following data. x y = f(x)

5

6

9

11

380

−2

196

508

Solution: The Lagrange’s interpolation formula is y  f (x ) 



(x  x 1 )(x  x 2 )(x  x 3 ) (x  x 0 )(x  x 2 )(x  x 3 ) y0  y1 (x 0  x 1 )(x 0  x 2 )(x 0  x 3 ) (x 1  x 0 )(x 1  x 2 )(x 1  x 3 ) (x  x 0 )(x  x 1 )(x  x 3 ) (x  x 0 )(x  x 1 )(x  x 2 ) y2  y 3 (E.1) (x 2  x 0 )(x 2  x 1 )(x 2  x 3 ) (x 3  x 0 )(x 3  x 1 )(x 3  x 2 )

Here, we have x0 = 5, x1 = 6, x2 = 9, x3 = 11, y0 = 380, y1 = −2, y2 = 196 and y3 = 508. Substituting these values in Equation (E.1), we get f (10) 

ANMF.CH05_4PP.indd 337

(10  6)(10  9)(10  11) (10  5)(10  9)(10  11)  (380)   (2) (5  6)(5  9)(5  11) (6  5)(6  9)(6  11)

3/1/2023 2:13:27 PM

338 • Applied Numerical Methods Using MATLAB





(10  5)(10  6)(10  11) (10  5)(10  6)(10  9)  (196)   (508) (11  5)(11  6)(11  9) (9  5)(9  6)(9  11)

or f(10) = 330. 5.4.4 Hermite’s Interpolation Formula Hermite’s interpolation formula provides an expression for a polynomial passing through given points with given slopes. The Hermite interpolation accounts for the derivatives of a given function. Let xi, fi, fi′, (for i = 0,1,2,…n) be given. The polynomial f(x) of degree (2n + 1) for which f(xi) = fi and f′(xi) = f′i is given by: n

 f(x) =  h j (x)f i + j 0

where  hj(x) = 1 

q n (x j ) q n (x j )

n

 h (x)f  j 0

j

i

(x  x j ) [Lj(x)]2



h j (x)  (x  x j )[L j (x)]2



qn(x) = (x − x0) (x − x1)….(x − xn)



Lj(x) =

q n (x ) (x  x j )q n (x j )

It is used to write the interpolation formulae in finite element analysis. Famous cubic polynomials are derived from two points with their slopes. It is used to represent bending motion of a beam. For example, in the case of a beam finite element, suppose we need to obtain cubic polynomials that satisfy the following cases: 1. Consider: y = ax3 + bx2 + cx + d in [0, 1]. 2. Apply conditions @ x = 0

ANMF.CH05_4PP.indd 338

@x=1

Case 1:

y = 1, y′ = 0

y = y′ = 0

Case 2:

y = 0, y′ = 1

y = y′ = 0

Case 3:

y = 0, y′ = 0

y = 1, y′ = 0

Case 4:

y = 0, y′ = 0

y = 0, y′ = 1

3/1/2023 2:13:39 PM

Finite Differences and Interpolation • 339

3. Solve each case for a, b, c, d. Then we obtain: y (x)  1  0 x  1x 2  2 x 2 (x  1)  2 x 3  3 x 2  1 y (x)  0  1x  1x 2  1x 2 (x  1)  x 3  2 x 2  x y (x)  0  0 x  1x 2  2 x 2 (x  1)  2 x 3  3 x 2 y (x)  0  0 x  0 x 2  1x 2 (x  1)  x 3  x 2

These polynomials are plotted in Figure 5.1(b). For cases involved with higher order derivatives, the principle is same. When y(n)(xi) is used, all lower derivatives and y(xi) itself must be included in the constraints. For example, you cannot have y′(xi) as a constraint but not y(xi), nor y(2)(xi) but not y′(xi) and y(xi).

FIGURE 5.1(b)  Hermite interpolation.

ANMF.CH05_4PP.indd 339

3/1/2023 2:13:49 PM

340 • Applied Numerical Methods Using MATLAB EXAMPLE 5.35 Construct displacements in a beam element from Hermite polynomials. Solution: Consider the beam of length L. The Hermite polynomials are: 3

2

x x N1 (x)  2    3    1   L L x3 x2 N2 ( x )  2  2  x L   L 3

x x N3 (x)  2    3   L L  

2

x3 x2 N4 (x )  2  L   L These polynomial interpolation functions may be thought of as the ­fundamental modes of deflection. The deflection w(x) of any statically loaded beam can be written in terms of these modes as w (x)  N1 W1  N2 1  N3 W2  N4 2 where the subscripts associate quantities with positions (or nodes) 1 and 2 on the beam and Wi, θi, i = 1,2, are the deflection and slope, respectively, at each node. 5.4.5 Inverse Interpolation In interpolation, we estimate the missing value of the function y = f(x) corresponding to a value x intermediate between two given values. In inverse interpolation, we interpolate the argument x corresponding to an intermediate value y of the entry. 5.4.6 Lagrange’s Formula for Inverse Interpolation In the Lagrange interpolation formula y is expressed as a function of x as (x  x 0 )(x  x 2 )....(x  x n ) (x  x 1 )(x  x 2 )....(x  x n ) y  f (x )  y0  y 1  .... (x 0  x 1 )(x 0  x 2 )....(x 0  x n ) (x 1  x 0 )(x 1  x 2 )....(x 1  x n )    

ANMF.CH05_4PP.indd 340

(x  x 0 )(x  x 1 )....(x  x n 1 ) y n (5.46) (x n  x 0 )(x n  x 1 )....(x n  x n 1 )

3/1/2023 2:14:05 PM

Finite Differences and Interpolation • 341

By interchanging x and y in Equation (5.46) we can express x as a function of y as follows: x

(y  y 1 )(y  y 2 )....(y  y n ) (y  y 0 )(y  y 2 )....(y  y n ) x0  x 1  .... (y 0  y 1 )(y 0  y 2 )....(y 0  y n ) (y 1  y 0 )(y 1  y 2 )....(y 1  y n )

  

(y  y 0 )(y  y 1 )....(y  y n 1 ) x n (5.47) (y n  y 0 )(y n  y 1 )....(y n  y n 1 )

Equation (5.47) can be used for inverse interpolation. EXAMPLE 5.36 The following table gives the values of y corresponding to certain values of x. Find the value of x when y = 167.59789 by applying Lagrange’s inverse interpolation formula. x

1

2

5

7

y = f(x)

1

12

117

317

Solution: Here x0 = 1, x1 = 2, x2 = 5, x3 = 7, y0 = 1, y1 = 12, y2 = 117, y3 = 317 and y = 167.59789. The Lagrange’s inverse interpolation formula is given by

x

(y  y 1 )(y  y 2 )(y  y 3 ) (y  y 0 )(y  y 2 )(y  y 3 ) x0  x1 (y 0  y 1 )(y 0  y 2 )(y 0  y 3 ) (y 1  y 0 )(y 1  y 2 )(y 1  y 3 )

    Hence, x 

(167.59789  12)(167.59789  117)(167.59789  317) (1) (1  12)(1  117))(1  317)

   

ANMF.CH05_4PP.indd 341

(y  y 0 )(y  y 1 )(y  y 3 ) (y  y 0 )(y  y 1 )(y  y 2 ) x2  x3 (y 2  y 0 )(y 2  y 1 )(y 2  y 3 ) (y 3  y 0 )(y 3  y 1 )(y 3  y 2 )

(167.59789  1)(167.59789  117)(167.59789  317) (12) (12  1)(12  117)(12  317)

3/1/2023 2:14:20 PM

342 • Applied Numerical Methods Using MATLAB

   

(167.59789  1)(167.59789  12)(167.59789  317) (117) (117  1)(117  117))(117  317)

   

(167.59789  1)(167.59789  12)(167.59789  117) (317) (317  1)(317  12)(317  117)

or x = 5.65238.

5.5 CENTRAL DIFFERENCE INTERPOLATION FORMULAE In this section, we derive some important interpolation formulae by means of central differences of a function that are quite frequently employed in engineering and scientific computations. In particular, we develop central difference formulae which are best suited for interpolation near the middle of a tabulated data set. The following central difference formulae are presented: 1. Gauss’s forward interpolation formula 2. Gauss’s backward interpolation formula 3. Bessel’s formula 4. Stirling’s formula 5. Laplace-Everett formula Let the function y = yx = f(x) be given for (2n + 1) equispaced values of argument x0, x0 ± h, x0 ± 2h, …., x0, xh. The corresponding values of y be yi (i = 0, ±1, ±2, …., ±n). Also, let y = y0 denote the central ordinate corresponding to x = x0. We can then form the difference table as shown in Table 5.19. Table 5.20 shows the same Table 5.19 written using the Sheppard’s operatord, in which the relation d = DE−1/2 was used. Tables 5.17 and 5.18 are known as central difference tables. TABLE 5.19  Central difference table. x

y

x0 − 3h

y−3

x0 − 2h

y−2

Δy

Δ2y

Δ3y

Δ4y

Δ5y

Δ6y

Dy−3 D2y−3 D3y−3

Dy−2 x0 − h

ANMF.CH05_4PP.indd 342

y−1

2

D y−2

D4y−3

3/1/2023 6:47:45 PM

Finite Differences and Interpolation • 343

x

y

Δy

Δ2y 2

x0 + h

y2

x0 + 3h

y3

D y−1 3

Dy1 x0 + 2h

D5y−2 4

D y0

y1

D6y−3

D y−2

2

Δ6y

D5y−3

D3y−1

Dy0

Δ5y

4

D y−1

y0

Δ4y

D3y−2

Dy−1 x0

Δ3y

D y0 D2y1

Dy2

TABLE 5.20  Central differences written in terms of Sheppard’s operator δ. x

y

x0 − 3h

y−3

δy

δ2y

δ3y

δ4y

δ5y

δ6y

dy−5/2 .x0 − 2h

d2y−2

y−2

d3y−3/2

dy−3/2 x0 − h

d2y−1

y−1 dy−1/2

x0

d2y0

y0

d6y0 5

d y1/2

d y1/2 4

d y1

d y1 d3y3/2

dy3/2 x0 + 2h

d4y0 3

2

y1

d5y−1/2

d y−1/2

dy1/2 x0 + h

d4y−1 3

d2y2

y2 dy5/2

x0 + 3h

y3

5.5.1 Gauss’s Forward Interpolation Formula The Newton’s forward interpolation formula is

where

ANMF.CH05_4PP.indd 343

y  f (x)  y 0  uy 0  u

u(u  1) 2 u(u  1)(u  2) 3  y0   y 0 ...., (5.48) 2! 3!

x  x0 and x = x0 is the origin. h

3/1/2023 2:14:26 PM

344 • Applied Numerical Methods Using MATLAB In deriving the Gauss’s forward interpolation formula, we assume the ­differences lie on the bottom solid lines in Table 5.21 and they are of the form

yp = y0 + G1Dy0 + G2D2y−1 + G3D3y−1 + G4D4y−2 + ….

(5.49)

TABLE 5.21  Gauss’s forward and backward interpolation formulae. x

y

x−4

y−4

x−3

y−3

Δy

Δ2y

Δ3y

Δ4y

Δ5y

Δ6y

Dy−4 D2y−4 D3y−4

Dy−3 x−2

2

D3y−3

Dy−2 x−1

2

D3y−2

x1

D3y−1

x2

y2

x3

y3

x4

y4

D5y−2 4

D y0 2

D y1

D6y−2

D y−1 D3y0

Dy1

D6y−3

D y−2

2

y1

D5y−3 4

D y−1 Dy0

D6y−4

D y−3

2

y0

D5y−4 4

D y−2

y−1 Dy−1

x0

D4y−4

D y−3

y−2

D5y−1 4

D y0

Dy2 D2y2 Dy3

where G1, G2, …., Gn are coefficients to be determined. From the Newton’s forward interpolation formula [Equation (5.48)], we have u(u  1) 2 u(u  1)(u  2) 3  y2   y 0  .... 2! 3! (5.50) y p  Ep y 0  (1  )p y 0  y 0  uy 0 

Now

D2y−1 = D2E−1y0 = D2(1 + D)−1y0 = D2(1 − D + D2 − D3 + ….)y0 = D2y0 − D3y0 + D4y0 − D5y0 + ….



D3y−1 = D3y0 − D4y0 + D5y0 − D6y0 + ….

D4y−2 = D4E−2y0 = D4(1 − D)−2y0 = D4(1 − 2D + 3D2 − 4D3 + ….)y0 = D4y0 − 2D5y0 + 3D6y0 − 4D7y0 + …. and so on.

ANMF.CH05_4PP.indd 344

3/1/2023 2:14:29 PM

Finite Differences and Interpolation • 345

Hence Equation (5.49) becomes yp = y0 + G1Dy0 + G2(D2y0 − D3y0 + D4y0 − D5y0 + ….) + G3(D3y0 − D4y0 + D5y0 − D6y0 + ….) + G4(D4y0 − 2D5y0 + 3D6y0 − 4D7y0) + …. (5.51) Comparing Equations (5.50) and (5.51), we have G1 = u u(u  1) G2  2!

(u  1)u(u  1) 3! (u  1)(u)(u  1)(u  2) G4  , etc. 4! G3 

(5.52)

Hence, the Gauss’s forward interpolation formula can be written as u(u − 1) 2 (u + 1)u(u − 1) 3 ∆ y0 + ∆ y0 2! 3! (5.53) (u + 1)u(u − 1)(u − 2) 4 + ∆ y 0 + .... 4!

y p = y 0 + u∆y 0 +

This formula given by Equation (5.53) can be used to interpolate the values of y for u (0 < u < 1) measured forwardly from the origin. Equation (5.53) can also be written as

y  f (x)  y 0  uy 0 





u(u  1) 2 u(u  1)(u  2) 3 ( y 1   3 y 1 )  ( y 1   4 y 1 ) 2! 3!

u(u  1)(u  2)(u  3) 4 ( y 1   5 y 1 )  .... (5.54) 4!

or   y 4  f (x)  y 0  uy 0 



u(u  1) 2 (u  1)u(u  1) 3  y 1   y 1 2! 3!

(u  1)u(u  1)(u  2) 4  y 2  .... (5.55) 4!

Equation (5.55) is known as the Gauss’s forward interpolation formula. The Gauss’s forward interpolation formula employs odd differences above the ­central line through y0 and even differences on the central line. Gauss’s ­forward formula is used to interpolate the values of the function for the value of u such that 0 < u < 1.

ANMF.CH05_4PP.indd 345

3/1/2023 2:14:41 PM

346 • Applied Numerical Methods Using MATLAB 5.5.2 Gauss Backward Interpolation Formula The Gauss backward interpolation formula uses the differences that lie on the upper dashed line in Table 5.3 and can be assumed of the form 2 3 4   y p  y 0  G1 y 1  G2  y 1  G3  y 2  G4  y 2  .... (5.56)

where G1′ , G′2 , G′3 ,...., G′n are coefficients to be determined. Now following the procedure described in Section 5.5.1 and comparing with the Newton’s backward interpolation formula, we find

G′1 = u u(u  1) G2  2!

(u  2)(u  1)(u  1)u 3! (u  1)(u)(u  1)(u  2) G4  , etc. 4! u u(u  1) 2 2 ( y 1   3 y 1 )  .... (5.57) Hence y  f (x)  y 0  (y 1   y 1 )  1! 2!

G3 

u u(u  1) 2 (u  1)u(u  1) 3 or y p  y 0  y 1   y 1   y 2 1! 2! 3! (5.58) (u  2)(u  1)u(u  1) 4   y 2  .... 4! Equation (5.58) is called the Gauss’s backward interpolation formula. Gauss’s backward interpolation formula employs odd differences below the central line through y0 and even differences on the central line as shown in Table 5.3. Gauss’s backward interpolation formula is used to interpolate line value of the function for a negative value of u that lies between −1 and 0 (−1 < u < 0). EXAMPLE 5.37 Use Gauss’s forward interpolation formula to find y for x = 20 given that

ANMF.CH05_4PP.indd 346

x

11

15

19

23

27

y

19.5673

18.8243

18.2173

17.1236

16.6162

3/1/2023 2:14:53 PM

Finite Differences and Interpolation • 347

Solution: The difference table constructed is shown below: x

y

11

19.5673

15

18.8243

19

18.2173

23

17.1236

27

16.6162

Δ2y

Δy

Δ3y

Δ4y

−0.743 0.1360 −0.607

−0.6227 −0.4867

1.69570

−1.0937

1.0730 0.5863

−0.5074

Here h = 4, u 

x  x 0 20  19   0.25 h 4

The Gauss’s forward interpolation formula is u(u − 1) 2 (u + 1)u(u − 1) 3 ∆ y −1 + ∆ y −1 2! 3! (u + 1)(u)(u − 1)(u − 2) 4 + ∆ y −2 4! 0.25(0.25 − 1) = 18.21730 + 0.25(−1.09370) + (−0.48670) 2 (0.25 + 1)(0.25)(0.25 − 1) + (1.07300) 6 (0.25 + 1)(0.25)(0.25 − 1)(0.25 − 2) + (1.69570) 24 y = y 0 + u∆y 0 +

y20 = 17.97657. EXAMPLE 5.38

Use Gauss’s backward interpolation formula to find the sales for the year 1986 from the following data:

ANMF.CH05_4PP.indd 347

Year

1951

1961

1971

1981

1991

2001

Sales (in thousands)

13

17

22

28

41

53

3/1/2023 2:14:57 PM

348 • Applied Numerical Methods Using MATLAB Solution: Here h = 10, x = 1986 and x0 = 1991.

u

x  x 0 1986  1991   0.5 h 10 x

y

−4

13

−3

17

−2

22

−1

28

0

41

1

53

Δy

Δ2y

Δ3y

Δ4y

Δ5y

4 1 5

0 1

6

6 −20

6 −14

7 −8

13 −1 12

Gauss’s backward interpolation formula is u(u  1) 2 (u  1)(u)(u  1) 3  y 1   y 2 3! 2! (u  1)(u)(u  1)(u  2) 4   y 2  .... 4!

y  y 0  uy 1   

or  y  41  (0.5)(13) 

(0.5)(0.5  1) (0.5  1)(0.5)(0.5  1) (1.0)  (8) 2 6

(0.5  1)(0.5)(0.5  1)(0.5  2) (14)  33.79688. 24

5.5.3 Bessel’s Formula Bessel’s formula uses the differences as shown in Table 5.22 in which brackets mean that the average has to be taken.

ANMF.CH05_4PP.indd 348

3/1/2023 2:15:04 PM

Finite Differences and Interpolation • 349

TABLE 5.22 M

M

x−1

y−1

M

M

M

M

M

M

x0 x1

 y0     y1 

Dy0

  2 y 1   2    y0 

D3y−1

  4 y 2   4    y 1 

D5y−2

  6 y 3   6    y 2 

M

M

M

M

M

M

M

M

Hence, the Bessel’s formula assumes the form   4 y 2   4 y 1    2 y 1   2 y 0  y  y1 3 yp  0  A1 y 0  A 2 y 0   A y  A    ....  3 1 4  2 2 2       4 y 2   4 y 1    2 y 1   2 y 0  1  3  y 0   A1   y 0  A 2   A y  A    ....  1 3 4  2 2 2      (5.59) The Newton’s forward difference interpolation formula is given by u(u − 1) 2 u(u − 1)(u − 2) 3 ∆ y0 + ∆ y0 2! 3! (5.60) u(u − 1)(u − 2)(u − 3) 4 + ∆ y 0 + .... 4!

y p = y 0 + u∆y 0 +

Now, comparing Equations (5.59) and (5.60) and after simplifying the differences, we get

1   A1    u 2 



A2 



ANMF.CH05_4PP.indd 349

u(u  1) 2!

1  u(u  1)  u   2  A3  3! A4 

(u  1)(u)(u  1)(u  2) etc.(5.61) 4!

3/1/2023 2:15:27 PM

350 • Applied Numerical Methods Using MATLAB Hence, the Bessel’s formula in Equation (5.59) becomes 1  u(u  1)  u   u(u  1)   y 1   y 0  2 3   y 1 y p  y 0  uy 0    2!  2 3!  2

   

2

(u  1)(u)(u  1)(u  2)   4 y 2   4 y 1     .... (5.62) 4! 2  

Using the central differences notation, Equation (5.62) can be written as 1   u   u(u  1) u(u  1) 2 2 y p  y 0  uy 1 / 2  3 y 1 / 2  y 1 / 2   2! 3! (u  1)u(u  1)(u  2) 4  y 1 / 2  ....  (5.63) 4! where

2 y 1 / 2 

1 2   y 1   2 y 0  2



4 y 1 / 2 

1 4   y 2   4 y 1  , etc. 2

(5.64)

EXAMPLE 5.39 Apply Bessel’s interpolation formula to obtain y25, given that y20 = 2860, y24 = 3167, y28 = 3555, and y32 = 4112. Solution: The difference table is shown below: x−1

x

yx

20

2860

Δy

Δ2y

Δ3y

307 x0

24

3167

81 388

x1

28

3555

88 169

557 x2

ANMF.CH05_4PP.indd 350

32

4112

3/1/2023 2:15:38 PM

Finite Differences and Interpolation • 351

Here x0 = 24, h = 4 and u 

x  x 0 25  24   0.25 h 4

The Bessel’s formula is y  y 0  uy 0 



u(u  1)   2 y 1   2 y 0  u(u  1)(u  0.5) 3  y 1   2  2 6 

0.25(0.25  1)  81  169    2 2 0.25(0.25  1)(0.225  0.5)  (88)  3252.96875 6

 3167  0.25(388) 



5.5.4 Stirling’s Formula Consider the mean of the Gauss’s forward and backward interpolation f­ ormula given by Equations (5.54) and (5.57), we get 2 u(u 2  1)   3 y 1   3 y 2   y  y 0  u 2   y p  y 0  u  1 y    1  2 2 3!  2   



   

u 2 (u 2  1) 4 u(u 2  1)(u 2  4) 5   y 2   5 y 3  (5.65)  y 2  4! 5!

Equation (5.65) is known as the Stirling’s formula. In the central differences notation, Stirling’s formula given by Equation (5.65) becomes



y p  y 0  uy 0 

u 2 2  u(u 2  1)  3 u 2 (u 2  12 ) 4  y  y 0  ..... (5.66)  y0   0  2! 4!  3! 

where

1 1 y 0  y 0  y 1   y 1 / 2  y 1 / 2  2 2

and

3 y 0 

1 3 1   y 1   3 y 2   3 y 1 / 2  3 y 1 / 2  (5.67) 2 2

Stirling formula gives the most accurate result for −0.25 ≤ u ≤ 0.25. Hence, x0 should be selected such that u satisfies this inequality.

ANMF.CH05_4PP.indd 351

3/1/2023 2:15:57 PM

352 • Applied Numerical Methods Using MATLAB EXAMPLE 5.40 Use Stirling’s interpolation formula to find y28, given that y20 = 48234, y25 = 47354, y30 = 46267, y35 = 44978 and y40 = 43389. Solution: Here x = 30 as origin and h = 5. Therefore u  table is shown below: x 20

u=

x − 30 5 −2

Δyu

yu

28  30  0.4 . The difference 5

Δ2yu

Δ3yu

Δ4yu

48234 −880

25

−1

−207

47354 −1087

30

0

46267

35

1

44978

40

2

43389

5 −202

−1289

−103 −98

−300 −1589

The Stirling’s interpolation formula is   2 y 0   2 y 1  u 2  2 y 1 u(u 2  1)   3 y 1   3 y 2  yu  y0  u      2 2 6 2     2 2 u (u  1) 4   y 2  .... 24 2 (0.4)(0.4 2  1)  5  98   1087  1289  (0.4)  ( 202 )   46267  (0.4)    2  2 2 6  (0.4)2 (0.4 2  1) (103)  46724.0128 .    24

5.5.5 Laplace-Everett’s Formula Eliminating odd differences in Gauss’s forward formula [Equation (5.54)] by using the relation Dy0 = y1 − y0

ANMF.CH05_4PP.indd 352

3/1/2023 2:16:09 PM

Finite Differences and Interpolation • 353

We have D3y−1 = D2y0 − D2y−1

D5y−2 = D4y−1 − D4y−2 ….,

Hence u u(u  1) 2 (u  1)u(u  1) 2 (y 1  y 0 )  ( y 0   2 y 1 )  y 1  1! 2! 3! (u  1)u(u  1)(u  2) 4 (u  2)(u  1)u(u  1)(u  2) 4   y 2  ( y 1   4 y 2 )  .... 4! 5! u 1  2 (u  1)u(u  1) 2  1  (1  u)y 0  uy 1  u(u  1)    y 1   y0  3! 1  2 1  2  3 

y  f (x )y 0 

1 u  2 2  (u  1)u(u  1)(u  2)    y 2 5  1  2  3  4 (u  2)(u  1)u(u  1)(u  2) 4   y 1  .... 5! uy u(u  1)(u  2) 2 (u  1)u(u  1) 2  (1  u)y 0  1   y 1   y0 1! 3! 3! (u  1)u(u  1)(u  2)(u  3) 4 (u  2)(u  1)u(u  1)(u  2) 4   y 2   y 1  .... 3! 5! (5.68) Writing v = 1 − u, i.e., u = 1 − v and changing the terms (5.68) with a negative sign we get y  vy 0 

u (v  1)v(v  1) 2 (u  1)u(u  1) 2 y1   y 1   y0 1! 3! 3!

(v  2)(v  1)v(v  1)(v  2) 2 (u  2)(u  1)u(u  1)(u  2) 4  y 2   y 1  .... 5! 5! (5.69) Equation (5.69) can be written as   

y 4  f (x)  vy 0     

v(v 2  12 ) 2 v(v 2  12 )(u 2  2 2 ) 4  y 1   y 2  ....  uy 1 3! 5!

u(u 2  12 ) 2 u(u 2  12 )(u 2  2 2 ) 2  y0   y 1  .... (5.70) 3! 5!

Equation (5.70) is known as Laplace-Everett’s formula. Equation (5.71) uses only even differences of the function.

ANMF.CH05_4PP.indd 353

3/1/2023 2:16:30 PM

354 • Applied Numerical Methods Using MATLAB EXAMPLE 5.41 Use Everett’s interpolation formula to find the value of y when x = 1.60 from the following table. x y = f(x)

1.0

1.25

1.50

1.75

2.0

2.25

1.0543

1.1281

1.2247

1.3219

1.4243

1.4987

Solution: The difference table is shown below: i

xi

yi

−2

1.00

1.0543

Δyi

Δ2yi

Δ3yi

Δ4yi

0.0738 −1

1.25

1.1281

0.0228 −0.0222

0.0966 0

1.50

1.2247

0.006

0.0268

0.0972 1

1.75

1.3219

0.0046

2

2.0

−0.0378

0.00520 −0.0332

0.1024 −0.0280

1.4243 0.0744

3

2.25

1.4987

x0 = 1.50 and h = 0.25. x  x 0 1.60  1.50 v   0.4 Therefore h 0.25 and u = 1 − v = 1 − 0.4 = 0.6 Here

The Everett’s interpolation formula is  v(v 2  12 ) 2 v(v 2  12 )(v 2  2 2 ) 4  y   vy 1   y0   y 1  3! 5!  

 u(u 2  12 ) 2 u(u 2  12 )(u 2  2 2 ) 4   y 1   y 2     uy 0  3! 5!   0.4(0.16  1) 0.4(0.16  1)(0.16  4)    0.4(1.3219)  (0.00520)  ( 0.03780) 6 120  

ANMF.CH05_4PP.indd 354

3/1/2023 2:16:35 PM

Finite Differences and Interpolation • 355

0.6(0.36 − 1) 0.6(0.36 − 1)(0.36 − 4)   + 0.6(1.2247) + (0.0006) + (0.02680) 6 120   = 1.26316.   5.5.6 Selection of an Interpolation Formula In general, the selection of an interpolation formula depends to a great extent on the position of the interpolated value in the given data. a) Use Newton’s forward interpolation formula to find a tabulated value near the beginning of the table. b) Use Newton’s backward interpolation formula to find a value near the end of the table. c) Use either Stirling or Bessel’s or Laplace-Everett’s formula to find an interpolated value near the center of the table. The coefficients in the central difference formulae are smaller and converge faster than those in Newton’s forward or Newton’s backward interpolation formulae. Also, after a few terms, the coefficients in the Stirling’s formula decrease more rapidly than those of the Bessel’s formula. Similarly, the coefficients of Bessel’s formula decrease more rapidly than those of Newton’s forward or backward formula. Hence, wherever possible, central difference formula are preferred than the Newton’s formulae. However, as described in (a), (b), and (c) above, the right selection of an interpolation formula greatly depends on the position of the interpolated value in the given tabular data set.

5.6 DIVIDED DIFFERENCES Let the function y = f(x) be given at the point x0, x1, x2, …, xn (that need not be equally spaced) f(x0), f(x1), f(x2), …, f(xn), denote the (n + 1) values the function at the points x0, x1, x2, …, xn. Then the first divided differences of f(x) for the arguments x0, x1 is defined as f (x 0 ) − f (x 1 ) . x 0 − x1

ANMF.CH05_4PP.indd 355

3/1/2023 2:16:37 PM

356 • Applied Numerical Methods Using MATLAB It is denoted by f(x0, x1) or by [x0, x1] Likewise, f(x1, x2) =

f (x 1 ) − f (x 2 ) , x1 − x 2

f(x2, x3) =

f (x 2 ) − f (x 3 ) , etc. x2 − x3

The second divided difference for the arguments x0, x1, x2 is defined as f(x0, x1, x2) =

f (x 0 , x 1 ) − f (x 1 , x 2 ) , x0 − x2

similarly, the third differences for the arguments x0, x1, x2, x3 is defined as f(x0, x1, x2, x3) =

f (x 0 , x 1 , x 2 ) − f (x 1 , x 2 , x 3 ) x0 − x3

The first divided differences are called the divided differences of order one, the second divided differences are called the divided differences of order two and so on. The divided difference table (Table 5.23) is given below: TABLE 5.23 Argument, x

Entry

x0

f(x0)

∀f(x)

∀2f(x)

∀3f(x)

f(x0, x1) x1

f(x1)

f(x0, x1, x2) f(x1, x2)

x2

f(x2)

f(x0, x1, x2, x3) f(x1, x2, x3)

f(x2, x3) x3

f(x3)

EXAMPLE 5.42 1 If f(x) = , then find the divided differences f(a, b) and f(a, b, c) x Solution: Given f(x) =

ANMF.CH05_4PP.indd 356

1 , x

3/1/2023 2:16:50 PM

Finite Differences and Interpolation • 357

1 1  f (a)  f (b) a b ba 1 ⇒ f(a, b) =    ab (a  b) ab(a  b) ab f (a, b) − f (b, c) and f(a, b, c) = a−c 1  1    1 ab  bc  1  c  a  1   =     ac b  ac  a  c abc Hence  f(a, b, c) =

1 . abc

EXAMPLE 5.43 Prepare the divided difference table for the following data TABLE 5.24(a) x

1

3

4

6

10

f(x)

0

18

58

190

920

Solution: Table 5.24(b) shows the divided differences. TABLE 5.24(b) x

f(x)

∀f(x)

∀2f(x)

∀3f(x)

∀4f(x) 0.207672

1

0

9

10.33333

−0.33333

3

18

40

8.666667

1.535714

19.41667

4

58

66

6

190

182.5

10

920

5.6.1 Newton’s Divided Difference Interpolation Formula A function f(x) is written in terms of divided differences as follows: f(x) = f(x0) + (x − x0)f(x0, x1) + (x − x0), (x − x1) f(x0, x1, x2) + (x − x0) (x − x1) (x − x2) f(x0, x1, x2, x3) + (x − x0) (x − x1) (x − x2) (x − x3) f(x0, x1, x2, x3, x4) + ….

ANMF.CH05_4PP.indd 357

3/1/2023 2:16:59 PM

358 • Applied Numerical Methods Using MATLAB EXAMPLE 5.44 Find the form of the function f(x) under suitable assumption from the ­following data. x f(x)

0 2

1 3

2 12

5 147

Solution: The divided difference table (Table 5.25(a)) is given as under: TABLE 5.25(a) x 0

f(x) 2

1

3

∀2f

∀f

∀3f

1 4 9 2

12

5

147

1 9

45

We have x0 = 0, f(x0) = 2, f(x0, x1) = 1, f(x0, x1, x2) = 4, f(x0, x1, x2, x3) = 1. The Newton’s divided difference interpolation formula for this case is: f(x) = f(x0) + (x − x0) f(x0, x1) + (x − x0)(x − x1) f(x0, x1, x2) + (x − x0) (x − x1) (x − x2) f(x0, x1, x2, x3). Substituting all constants, we get: f(x) = 2 + (x − 0)1 + (x − 0)(x − 1)4 +(x − 0)(x − 1)(x − 2)1 Hence, f(x) = x3 + x2 − x + 2. EXAMPLE 5.45 Derive the equation of the interpolating polynomial for the data given in Table 5.26(a) below: TABLE 5.26(a)

ANMF.CH05_4PP.indd 358

x(deg)

f(x)

0

3

1

2

2

7

3

24

4

59

5

118

3/1/2023 2:16:59 PM

Finite Differences and Interpolation • 359

Solution: First form the divided difference table as shown in Table 5.26(b): TABLE 5.26(b) x

f(x)

∀f

∀2f

∀3f

∀4f

0

3

−1

3

1

0

1

2

5

6

1

0

2

7

17

9

1

3

24

35

12

4

59

59

5

118

Using Newton’s divided difference formula, the interpolating polynomial is: f(x) = f(x0) + (x − x0) f(x0, x1) + (x − x0)(x − x1) f(x0, x1, x2) + (x − x0) (x − x1) (x − x2) f(x0, x1, x2, x3).   =3 − x + 3x(x − 1) + x(x − 1)(x − 2) Figure 5.2 shows the variation of the function with actual values and those obtained from polynomial.

FIGURE 5.2  Newton’s polynomial.

ANMF.CH05_4PP.indd 359

3/1/2023 2:16:59 PM

360 • Applied Numerical Methods Using MATLAB Main advantage of divided difference table approach is that it has less ­computational operations. We do not need to write the polynomial and then use the C0 condition to calculate the constants. Secondly, it is much easier to incorporate in a computer code. It is important to realize that both the Lagrange and Newton polynomials are C0 continuous and each would generate the same result.

5.7 CUBIC SPLINE INTERPOLATION Generally, we use only one polynomial to describe the data over the entire range. Here, we will use different continuous polynomials to describe the function in each interval of known points. This type of approximation is called the piecewise polynomial approximation. Therefore, for n + 1 set of data, there will be n piecewise polynomials formed. Splines of different degree are available in the literature. However, cubic splines are most widely used. Cubic spline interpolation method interpolates a function between a given set of data points by means of piecewise smooth polynomials. Here, the curve passes through the given set of data points. The slope and its curvature are continuous at each point. The advantage of cubic spline interpolation method is that these polynomials are of a lower degree and less oscillatory, therefore describing the given data more accurately. Cubic spline interpolation method is very powerful and widely used. It has several applications in numerical differentiation, integration, solution of boundary value problems, plotting a two and three dimensional graph. With a cubic spline, an expression for the second derivative can be obtained and will describe the behavior of the data most accurately within each interval.

FIGURE 5.3  Cubic spline.

ANMF.CH05_4PP.indd 360

3/1/2023 2:16:59 PM

Finite Differences and Interpolation • 361

The second derivatives of the spline is zero at the end points. Since these end conditions occur naturally in a beam model (in strength of materials), the resulting curve is known as the natural cubic spline. The pins, i.e., the data points, are called the knots of the spline in a beam model. Figure 5.3 shows a cubic spline that spans n knots. Let us denote fi,i+1(x) be the cubic polynomial that spans the segment between knots i and i + 1. In Figure 5.3, we note that the spline is a piecewise cubic curve, assembled together form the n − 1 cubics f1,2(x), f2,3(x), …., fn−1,n(x), all of which have different coefficients. Denoting the second derivative of the spline at knot i by ki, the continuity of second derivatives requires that f i1,i (x i )  f i,i 1 (x i )  k i (5.71) In Equation (5.71), ki is unknown, except for    k1 = kn = 0

(5.72)

We know that the expression for f i,i 1 (x) is linear and the starting point for obtaining the coefficients of fi,i+1(x) is f i,i 1 (x) . Hence, we can write using Lagrange’s two-point interpolation,

f i,i 1 (x)  k i  i (x)  k i 1  i 1 (x) (5.73)

where

 i (x ) 

and

 i  1 (x ) 

x  xi (5.74) x i 1  x i

Hence f i,i 1 (x) 

k i ( x  x i 1 )  k i  1 ( x  x i ) (5.75) x i  x i 1

x  x i 1 x i  x i 1

Integrating Equation (5.75) twice with respect to x, we get f i,i 1 (x)  or

ANMF.CH05_4PP.indd 361

k i (x  x i 1 )3  k i 1 (x  x i )3  A(x  x i 1 )  B(x  x i ) (5.76) 6(x i  x i 1 ) k (x  x i 1 )3  x i 1 (x  x i )3  i  Cx  D (5.77) 6(x i  x i 1 )

3/1/2023 2:17:15 PM

362 • Applied Numerical Methods Using MATLAB where A and B are constants of integration, C = A − B and D = −Axi+1 + Bxi. Now applying the condition fi,i+1(xi) = yi, Equation (5.76) becomes

k i (x i  x i 1 )3  A(x i  x i 1 )  y i (5.78) 6(x i  x i 1 )

Hence, A 

yi k  i (x i  x i 1 ) (5.79) x i  x i 1 6

Similarly, applying the condition fi,i+1(xi+1) = yi+1, gives

B

y i 1 k  i 1 (x i  x i 1 ) (5.80) x i  x i 1 6

From Equations (5.79) and (5.80), we obtain = f i,i +1 (x)



 k i  (x − x i +1 )3 − (x − x i +1 )(x i − x i +1 )  6  x i − x i +1  (5.81) 3  y i (x − x i +1 ) − y i +1 (x − x i ) k i +1  (x − x i ) − − (x − x i )(x i − x i +1 ) +  6  x i − x i +1 x i − x i +1 

We note here that the second derivatives ki of the spline at the interior knots are found from the slope continuity conditions

f i1 (x i )  f i,i 1 (x i ) , i = 1, 2, 3, …., n − 1

(5.82)

Applying the conditions given by Equation (5.82) in Equation (5.81) and after some mathematical manipulations or simplifications, we obtain the simultaneous equations: ki−1(xi−1 − xi) + 2ki(xi−1 − xi+1) + ki+1(xi − xi+1)

 y i 1  y i y i  y i  1   = 6 ,  x i 1  x i x i  x i  1 

i = 2, 3, …., n − 1

(5.83)

If the data points are equally spaced at intervals h, then, we have h = xi−1 − xi = xi − xi+1(5.84)

ANMF.CH05_4PP.indd 362

3/1/2023 2:17:27 PM

Finite Differences and Interpolation • 363

and Equation (5.83) become k i 1  4 k i  k i  1 

6 y i 1  2 y i  y i 1  , i = 2, 3, …., n − 1 h2 

(5.85)

There are two boundary conditions normally used. They are 1. Natural boundary condition:

The second derivatives of the data at the end points x0 and xn are arbitrarily assumed to be zero. This condition is known as the free or natural boundary condition. The polynomials resulting from this condition are referred to as natural or free cubic splines. They may not provide very accurate values close to the boundaries, but they are accurate enough in the interior region.

2. Clamped boundary condition:

When the first derivative of the data is known at the end point x0 and xn, the corresponding boundary conditions are known. This condition is known as the clamped boundary condition.

EXAMPLE 5.46 Given the data points: x

1

2

3

4

5

y

13

15

12

9

13

Find the natural cubic spline interpolation at x = 3.4. Solution: For equally spaced knots, the equations for the curvatures are written as

k i 1  4 k i  k i  1 

6 (y i 1  2 y i  y i 1 ) , i = 2, 3, 4 h2

Here we have k1 = k5 and h = 1. Hence 4k2 + k3 = 6[13 − 2(15) + 12] = −30

k2 + 4k3 + k4 = 6[15 − 2(12) + 9] = 0

k3 + 4k4 = 6[12 − 2(9) + 13] = 42

ANMF.CH05_4PP.indd 363

(E.1)

3/1/2023 2:17:33 PM

364 • Applied Numerical Methods Using MATLAB Solving Equation (E.1), we obtain k2 = −7.286 k3 = −0.857 k4 = 10.714



The interpolant between knots 2 and 3 is given by f 3 , 4 (x )        

 k 4  (x  x 3 )3  k 3  (x  x 4 )3  ( x  x )( x  x )  (x  x 3 )(x 3  x 4 )  4 3 4   6  x3  x4  6  x3  x4  y 3 (x  x 4 )  y 4 (x  x 3 ) x3  x4

Hence, the natural cubic spline interpolation at x = 3.4 is f3,4 (3.4) 

 10.714  (3.4  3)3  0.857  (3.4  4)3   ( 3 . 4  4 )( 3  4 )  (3.4  3)(3  4)    6  34 6  34  

  

12(3.4  4)  9(3.4  3)  0.054848  0.599984  10.8  10.2552 . 34

EXAMPLE 5.47 Find the natural spline that passes through the points given below: i

1

2

3

xi

0

1

2

yi

0

2

1

Find the first and second derivatives at x = 1 noting that the interpolant consists of two cubics, one valid in 0 ≤ x ≤ 1, the other in 1 ≤ x ≤ 2. Solution: For natural spline, we have k1 = k3 = 0. The equation for k1 is given by 6 k 1  4 k 2  k 3  2 (y 1  2 y 2  y 3 ) 6h Hence, 0  4 k 2  0  2 [0  2(2)  1] 1 or k2 = −4.5

The interplant in 0 ≤ x ≤ 1 is given by

ANMF.CH05_4PP.indd 364

3/1/2023 2:17:48 PM

Finite Differences and Interpolation • 365

f1 , 2 ( x )  

 y (x  x 2 )  y 2 (x  x 1 ) k 2  (x  x 1 )3  (x  x 1 )(x 1  x 2 )  1  6  x1  x 2 x1  x 2 





 0  2(x  0) 4.5  (x  0)3  (x  0)(0  1)    0.75 x 3  2.75 x   6  0 1 0 1 

The interpolant in 1 ≤ x ≤ 2 is given b y  y (x − x 3 ) − y 3 (x − x 2 ) k 2  (x − x 3 )3 − (x − x 3 )(x 2 − x 3 ) + 2  6  x2 − x3 x2 − x3   2(x − 2) − (x − 1) 4.5  (x − 2)3 − − (x − 2)(1 − 2)  +  6  1−2 1−2 

− f2,3 (x) =

= −0.75(x − 2)3 − 1.75x + 4.5 Now

f1,2 (x)  3(0.75)x 2  2.75  2.25 x 2  2.75



f2,3 (x)  3(0.75)(x  2)2  1.75  2.25(x  2)2  1.75



f1,2 (1)  2.25(1)2  2.75  0.5



f2,3 (1)  2.25(1  2)2  1.75  0.5



f1,2 (1)  2.25(2)  4.5



f2,3 (1)  2.25(2)(1  2)  4.5

Hence f1,2 (1)  f2,3 (1)  0.5 and

f1,2 (1)  f2,3 (1)  4.5

EXAMPLE 5.48 Use the end conditions with a cubic spline that has constant second derivatives within its first and last segments (noting the end segments are parabolic). The end conditions for this spline are given as k1 = k2 and kn−1 = kn. The data points are given below:

ANMF.CH05_4PP.indd 365

i

1

2

3

4

x

0

1

2

3

y

1

1

0.5

0

3/1/2023 2:18:15 PM

366 • Applied Numerical Methods Using MATLAB Solution: With evenly spaced knots, the equations for the curvatures are given by k i 1  4 k i  k i  1 



6 (y i 1  2 y i  y i 1 ) , i = 2, 3 h2

(E.1)

With k1 = k2, k4 = k3 and h = 1, Equation (E.1) become

5k2 + k3 = 6(1 − 2(1) + 0.5) = −3 



k2 + 5k3 = 6[1 − 2(0.5) + 0] = 0

(E.2)

Solving Equation (E.2), we get k2 = −5/8, k3 = 1/8. The interpolant can now be evaluated from   k  (x  x i )3 k i  (x  x i 1 )3  (x  x i )(x i  x i 1 )  (x  x i 1 )(x i  x i 1 )  i 1   6  x i  x i 1 6  x i  x i 1   y i (x  x i  1 )  y i  1 (x  x i )     (E.3) x i  x i 1

f i , i  1 (x ) 

Substituting xi − xi+1 = −1 and i = 3, Equation (E.3) becomes f 3 , 4 (x ) 

k3 k [(x  x 4 )3  (x  x 4 )]  4 [(x  x 3 )3  (x  x 3 )  y 3 (x  x 4 )  y 4 (x  x 3 )] 6 6

Hence f3,4 (2.6) 

1/8 1/ 8 [(2.6  3)3  (2.6  3)]  [(2.6  2)3  (2.6  2)] 6 6 00.5(2.6  3)  0  0.1853

5.8 GENERALIZED SPLINE METHOD 5.8.1 Splines In general, (n − 1)th order polynomials can be used to interpolate between n data points. Sometimes, these functions can lead to erroneous results due to round off-error and oscillations. To overcome this difficulty, one can apply lower-order polynomials in a piecewise manner to subsets of data points. Such connecting polynomials are called spline functions. In this section we present splines to minimize oscillations by fitting lower-order polynomials to data in a piecewise fashion.

ANMF.CH05_4PP.indd 366

3/1/2023 2:18:25 PM

Finite Differences and Interpolation • 367

5.8.2 Linear Splines

FIGURE 5.4

Figure 5.4 shows the general notation used for splines. There are (n − 1) intervals for n data points (i = 1, 2, …., n) and si(x) is the spline function for the interval i. For linear splines, each function is a straight line connecting two data points at each end of the interval. That is, si(x) = ai + bi(x − xi)(5.86) where ai = intercept or

ai = fi



bi = slope of the straight line connecting the points

or

bi 

(5.87)

f i 1  f i (5.88) x i 1  x i

and fi = f(xi) From Equations (5.86) and (5.87), Equation (5.88) can be written as



ANMF.CH05_4PP.indd 367

s i (x )  f i 

f i 1  f i (x  x i ) (5.89) x i 1  x i

3/1/2023 2:18:31 PM

368 • Applied Numerical Methods Using MATLAB Equations (5.86) to (5.89) can be used to evaluate the function at any point between x1 and xn. EXAMPLE 5.49 Fit the data in Table 5.27 with first-order splines. Determine the function at x = 2. TABLE 5.27 i

xi

fi

1 2 3 4

1 1.5 4 6

5 3.5 5 3

Solution: Equation (5.89) can be used to generate the linear spline functions. For instance, for the second interval from 1.5 to 4, the function is given by s 2 (x )  f i 

f i 1  f i 5  3.5 (x  x i )  3.5  (x  1.5) x i 1  x i 4  1.5

The value at x = 2 is given by



s2 (x)  3.5 

5  3.5 (2  1.5)  3.575 4  1.5

5.8.3 Quadratic Splines In quadratic splines, a second-order polynomial is used for each interval between data points. The second-order polynomial can be written as (see Figure 5.4 for notating) si(x) = ai + bi(x − xi) + ci(x − xi)2(5.90) Note here that there are (n − 1) intervals for the n data points (i = 1, 2, …., n) and hence 3(n − 1) unknown constants (a’s, b’s, and c’s) to be determined. Consequently, 3n equations are required to determine the unknown constants.

ANMF.CH05_4PP.indd 368

3/1/2023 2:18:35 PM

Finite Differences and Interpolation • 369

The procedure for obtaining the 3(n − 1) unknown constants is described below: a) Continuity condition: The function should pass through all the data points. This can be written as    fi = ai + bi(xi − xi) + ci(xi − xi)2(5.91)  or ai = fi(5.92)

Equation (5.92) states that the constant in each quadratic must be equal to the value of the dependent variable at the beginning of the interval.



Hence, Equation (5.91) can be written as

  si(x) = fi + bi(x − xi) + ci(x − xi)2(5.93)

The number of conditions to be determined are now reduced to 2(n − 1).

b) The condition that the function values of adjacent polynomials must be equal at the knots requires that for knot i + 1 as   fi = bi(xi+1 − xi) + ci(xi+1 − xi)2 = fi+1 + bi+1(xi+1 − xi+1) + ci+1(xi+1 − xi+1)2(5.94)

Equation (5.94) can be simplified by defining the width of the ith interval as

hi = xi+1 − xi(5.95)

Hence, Equation (5.94) becomes

fi + bihi + ci h2i = fi+1(5.96)

Equation (5.96) can be written for the nodes, i = 1, 2, …., n − 1 or (n − 1) conditions and the remaining conditions are 2(n − 1) − (n − 1) = n − 1.

c) The first derivative at the interior nodes must be equal in order to join the adjacent splines smoothly.

Now, Equation (5.93) can be differentiated to give

s1i (x) = bi + 2c(x − xi)(5.97) or bi + 2cihi = bi+1(5.98)

ANMF.CH05_4PP.indd 369

Equation (5.98) can be written for the interior nodes giving (n − 2) conditions and there remains (n − 1) − (n − 2) = 1 condition. An arbitrary choice may be made to determine the constants.

3/1/2023 2:18:35 PM

370 • Applied Numerical Methods Using MATLAB d) We assume the second derivative as zero at the first point. From Equation (5.93), this condition can be written as ci = 0

(5.99)

Equation (5.99) implies that the first two points are connected by a straight line. EXAMPLE 5.50 Refer to Example 5.49 and fit a quadratic spline. Determine the value of the function at x = 2. Solution: Here n = 3 intervals. Hence, by applying the continuity condition and the zero second-derivative condition, we find that 2(4 − 1) − 1 = 5 conditions are needed. Now, Equation (5.96) for i = 1, 2, and 3 with c1 = 0 gives

f1 + b1h1 = f2



f2 + b2h2 + c2h22 = f3(E.1)



f3 + b3h3 + c3h32 = f4

Also, Equation (5.98) gives us an additional 3 − 1 = 2 conditions and c1 = 0. Hence, we have b1 = b2 and b2 + 2c2h2 = b3(E.2) The function and interval widths are therefore given by

f1 = 5

h1 = 1.5 − 1 = 0.5



f2 = 3.5

h2 = 4 − 1.5 = 2.5



f3 = 5

h3 = 6 − 4 = 2



f4 = 3

(E.3)

Hence, the conditions in matrix form are 0 0 0.5 0  0 2.5 6.25 0  0 0 2      0  0 0  1 1  0 1 5 1

ANMF.CH05_4PP.indd 370

0  b1  1.5  0  b2   1.5      4  c 2    2  (E.4)  0  b3   0      0  c 3   0 

3/1/2023 2:18:38 PM

Finite Differences and Interpolation • 371

Solving Equation (E.4) using MATLAB, we obtain

b1 = 3, b2 = −3, c2 = 1.44, b3 = 4.2, and c3 = −2.6(E.5)

Hence substituting the values of Equation (E.5) along with the values of a’s of Equation (5.92) in Equation (5.93) we obtain the following quadratic splines for each interval:

si(x) = fi + bi(x − xi) + ci(x − xi)2



s1(x) = 5 − 3(x − 1)



s2(x) = 3.5 − 3(x − 1.5) + 1.44(x − 1.5)2



s3(x) = 5 − 4.2(x − 4) − 2.6(x − 4)2

Since x = 2 lies in the second interval, we use s2 to make the prediction. Therefore, s2(2) = 3.5 − 3(2 − 1.5) + 1.44(2 − 1.5)2 = 2.36. 5.8.4 Cubic Splines Quadratic and higher-order splines are known to exhibit the instabilities inherent in high-order polynomials. Cubic splines are recommended since they exhibit the desired smoothness. In cubic splines, a third-order polynomial for each interval between knots is derived given by si(x) = ai + bi(x − xi) + ci(x − xi)2 + di(x − xi)3(5.100) Hence, there are (n − 1) intervals for the n data points (i = 1, 2, …, n) and 4(n − 1) unknown coefficients to be determined. Also, there are 4(n − 1) conditions required for their determinations. The derivation of cubic splines is presented below: The first condition that the spline must pass through all the data points requires fi = ai + bi(xi − xi) + ci(xi − xi)2 + di(xi − xi)3(5.101) or ai = fi(5.102) Equation (5.102) implies that the constant in each cubic must be equal to the value of the dependent variable at the beginning of the interval. Now, Equation (5.100) becomes si(x) = fi − bi(x − xi) + ci(x − xi)2 + di(x − xi)3(5.103)

ANMF.CH05_4PP.indd 371

3/1/2023 2:18:38 PM

372 • Applied Numerical Methods Using MATLAB The second condition that each of the cubics must joint at the knots for knot i + 1 requires that fi + bihi + ci h2i + di h3i = fi+1(5.104) where hi = xi+1 − xi The first derivatives at the interior nodes must be equal. Thus, Equation (5.101) when differentiated gives s1′(x) = bi + 2ci(x − xi) + 3di(x − xi)2(5.105) or the equivalence of the derivatives at the interior nodes, i + 1 can be written as bi + 2cihi + 3dih2i = bi+1(5.106) The second derivatives at the interior node must be equal. Differentiating Equation (5.105) gives s1′′(x) = 2ci + 6di(x − xi)(5.107) The equivalence of the second derivative at an interior node, i + 1 is written ci + 3dihi = ci+1(5.108) Solving Equation (5.108) for di gives d i 

c i 1  c i (5.109) 3h i

From Equations (5.104) and (5.109), we have

fi  bi h i 

h 2i (2c i  c i 1 )  f i 1 (5.110) 3

Similarly, from Equations (5.106) and (5.109), we have bi+1 = bi + hi(ci + ci+1)(5.111) Solving Equation (5.110) for bi gives

bi 

f i 1  f i h i  (2c i  c i 1 ) (5.112) hi 3

The index of this equation can be reduced by 1. Hence

ANMF.CH05_4PP.indd 372

b i 1 

f i  f i  1 h i 1  (2c i 1  c i ) (5.113) h i 1 3

3/1/2023 2:18:47 PM

Finite Differences and Interpolation • 373

The index of Equation (5.111) can also be reduced by 1. This gives   bi = bi−1 + hi−1(ci−1 + ci)(5.114) Substituting Equations (5.112) and (5.113) in (5.114) gives after simplification

h i 1 c i 1  2(h i 1  h i )c i  h i c i 1  3 f [x i , x j ] 

Also,

f f f i 1  f i  3 i i 1 (5.115) hi h i 1

fi  f j xi  x j

Hence, Equation (5.115) becomes

hi−1ci−1 + 2(hi−1 − hi)ci + hici+1 = 3(f [xi+1, xi] − f[xi, xi−1])(5.116)

Equation (5.116) can be written for the interior knots, i = 2, 3, …, n − 2. This results in n − 3 simultaneous tridiagonal equations with n − 1 unknown coefficients, c1, c2, …., cn−1. Thus, if we have two additional conditions, we can solve for the c’s. Then Equations (5.109) and (5.112) can be used to determine the remaining coefficients, b and d. The second derivative at the first node (Equation (5.107)) can be set to zero or s1(x 1 )  0  2c1  6d n 1 (x 1  x 1 ) (5.117)



Hence, this condition amounts to setting c1 = 0. Similar evaluation can be made at the last node, which results in sn1 ( xn )  0  2 cn1  6 d1 ( x1  x1 ) Now, Equation (5.117) becomes cn−1 + 3dn−1hn−1 = cn = 0 Therefore, to impose a zero second derivative at the last node, we set cn = 0. The final equations can now be written in matrix form as shown below: 1 h  1    

2(h1  h 2 )

h2 M h n 2

2(h n  2

0    c1     c   3(f [x , x ]  f [x , x ])  3 2 2 1    2    M    M      h n 1 ) h n 1  c n 1 3(f [x n , x n 1 ]  f [x n 1 , x n  2 ])     1   c n   0 

(5.118)

ANMF.CH05_4PP.indd 373

3/1/2023 2:19:00 PM

374 • Applied Numerical Methods Using MATLAB EXAMPLE 5.51 Refer to Example 5.49 and fit cubic splines. Determine the value at x = 2. Solution: We have f [x i , x j ]  Hence f (x 2 , x 1 ) 

fi  f j xi  x j

(E.1)

f2  f1 3.5  5 1.5  3   x 2  x 1 1.5  1 0.5



f (x 3 , x 2 ) 

f3  f2 5  3.5 1.5  0.6 (E.2)   x 3  x 2 4  1.5 2.5



f (x 4 , x 3 ) 

f 4  f3 3  5 2  1   x4  x3 6  4 2

where x1 = 1

f1 = 5

h1 = 1.5 − 1 = 0.5

x2 = 1.5

f2 = 3.5

h2 = 4 − 1.5 = 2.5

x3 = 4

f3 = 5

h3 = 6 − 4 = 2.0

x4 = 6

f4 = 3

(E.3)

Substituting the above values in Equation (5.118) gives 1 h  1 0  0

0 0 0  c1   0      2(h1  h 2 ) h2 0 c 2   3(f [x 3 , x 2 ]  f [x 2 , x 1 ])       (E.4) h2 2(h 2  h 3 ) h 3  c 3  3(f [x 4 , x 3 ]  f [x 3 , x 2 ])   0 0 1  c 4   0 Equation (E.4) becomes 0 0 0  c1   0  1 0.5 6 2.5 0  c   10.8     2     (E.5)  0 2.5 9 2  c 3   4.8      0 0 1  c 4   0  0 Solving Equation (E.5) using MATLAB, we get c1 = 0, c2 = 2.2869, c3 = −1.1686, and c4 = 0.

ANMF.CH05_4PP.indd 374

3/1/2023 2:19:16 PM

Finite Differences and Interpolation • 375

We can use Equations (5.109) and (5.112) to compute the values of d’s and b’s.

di 

c i 1  c i 3h i



d1 

c 2  c1 2.2869   1.5246 3 h1 3(0.5)



d2 

c 3  c 2 1.1686  2.2869   0.4607 3h2 3(2.5)



d3 

c 4  c 3 0  1.1686   0.1948 3h3 3(2)



bi 

f i 1  f i h i  (2c i  c i 1 ) hi 3

b1 

f 2  f1 h 1 3.5  5 0.5  (2c1  c 2 )   (0  2.2869)  3.3812 h1 3 0.5 3

b2 

f3  f2 h 2 5  3.5 2.5  (2c 2  c 3 )   [2(2.2869  1.1686]  3.2115 h2 3 2.5 3

b3 

f 4  f3 h 3 35 2  (2c 3  c 4 )   [2(1.1686  0)]  0.8081 h3 3 2 3

Hence Equation (5.100) becomes si(x) = ai + bi(x − xi) + ci(x − xi)2 + di(x − xi)3 where ai = fi

s1(x) = 5 − 3.3812(x − 1) + 1.5246(x − 1)3



s2(x) = 3.5 − 3.2115(x − 1.5) + 2.2869(x − 1.5)2 − 0.4607(x − 1.5)3



s3(x) = 5 + 0.8081(x − 4) + 0.8081(x − 4)2 + 0.1948(x − 4)3

The value at x = 2 falls within the second interval. Hence, s2(2) = 3.5 − 3.2115(2 − 1.5) + 2.2869(2 − 1.5)2 − 0.4607(2 − 1.5)3 = 2.4084.

ANMF.CH05_4PP.indd 375

3/1/2023 2:19:36 PM

376 • Applied Numerical Methods Using MATLAB 5.8.5 End Conditions There exists three end conditions: natural spline, clamped end condition, and not-a-knot end conditions. There end conditions can be applied by using Equation (5.116) for the interior knots (i = 2, 3, …., n − 2) and using the first and last equations (n − 1) as shown in Table 5.28. TABLE 5.28  The first and last equations needed to specify some commonly used end conditions for cubic splines. Condition

First and last equations

1. Natural

c1 = 0, cn = 0

2. Clamped f′1 and f′n are the specified first derivatives at the first and last nodes respectively

2h1c1 + h1c2 = 3f[x2, x1] − 3f′1 hn−1cn−1 + 2hn−1cn = 3f′n − 3f[xn, xn−1]

3. Not-a-knot

2h2c1 − (h1 − h2)c2 + h1c3 = 0 hn−1cn−2 − (hn−2+hn−1)cn+1+ hn−2 cn = 0

5.8.6 MATLAB Built-in Function: spline MATLAB has a built-in function, spline to perform cubic spline interpolation as described earlier in the section. It has the general syntax yy = spline(x, y, xx) where x and y are the vectors containing the values that are to be interpolated. yy = a vector containing the results of the spline interpolation as evaluated at the point in the vector xx. Spline uses the not-a-knot end condition by default. On the other hand, if y contains two or more values than x entries, then the first and last value in y are used as the derivatives at the end points. As a result, this option provides the means to implement the clamped-end condition. EXAMPLE 5.52 1 Given f (x)  . Use MATLAB built-in function, spline to fit nine 1  30 x 2 equally spaced data points sampled from the function in the interval [−1, 1]. Apply a) a not-a-knot spline b) a clamped spline with end slopes of f′1 = 1 and f′n−1 = −5.

ANMF.CH05_4PP.indd 376

3/1/2023 2:19:40 PM

Finite Differences and Interpolation • 377

Solution: a)

>> x = linspace(−1,1, 9);



>> y = 1./(1+30*x.^2);



>> xx = linspace(−1,1);



>> yy = spline(x,y,xx);



>> yr = 1./(1+30*xx.^2);



>> plot(x,y,‘o’,xx,yy,xx,yr,‘--’)

FIGURE 5.5(a)

The not-a-knot spline does not exhibit wild oscillations between the points as seen in Figure 5.5(a). b) Here, we create the clamped condition by defining a new vector yc that has the required first derivatives as its first and last elements. This new vector is used to generate and plot the desired spline fit as shown by the MATLAB program below: MATLAB Program: >> yc = [1 y −5]; >> yyc = spline(x,yc,xx); >> plot(x,y,‘o’,xx,yyc,xx,yr,‘--’)

ANMF.CH05_4PP.indd 377

3/1/2023 2:19:40 PM

378 • Applied Numerical Methods Using MATLAB

FIGURE 5.5(b)

Figure 5.5(b) shows some oscillations due to the artificial slopes that were imposed at the boundaries. 5.8.7 Multidimensional Interpolation The methods of interpolation for one-dimensional problems can be extended to multidimensional interpolation problems. Here, we mention a two-dimensional interpolation method in Cartesian coordinates.

FIGURE 5.6

ANMF.CH05_4PP.indd 378

3/1/2023 2:19:40 PM

Finite Differences and Interpolation • 379

Consider Figure 5.6 where we deal here with two-dimensional interpolation with determining the intermediate values for functions of two variables z = f(xi, yi) we know the values at four points f(x1, y1), f(x2, y1), f(x1, y2) and f(x2, y2).

FIGURE 5.7

Here we estimate the value at an intermediate point (xi, yi). If we use a linear function, the result is a plane connecting the points as in Figure 5.6. Such functions are called bilinear. A simple approach for developing the bilinear function is depicted in Figure 5.7. First, we can hold the y value fixed and apply one-dimensional linear interpolation in the x direction. Using the Lagrange form, the result at (xi, yi) is given by x  x2 x  x1 f (x i , y 1 )  i f (x 1 , y 1 )  i f (x 2 , y 1 ) (5.119) x1  x 2 x2  y1 and at (xi, y2) is

f (x i , y 2 ) 

xi  x2 x  x1 f (x 1 , y 2 )  i f (x 2 , y 2 ) (5.120) x1  x 2 x2  y1

These points can then be used to linearly interpolate along the dimension to yield the final result:

ANMF.CH05_4PP.indd 379

f (x i , y i ) 

yi  y2 y  y1 f (x i , y i )  i f (x i , y 2 ) (5.121) y1  y 2 y 2  y1

3/1/2023 2:19:48 PM

380 • Applied Numerical Methods Using MATLAB A single equation be developed by substituting Equations (5.119) and (5.120) into Equation (5.121) to give f (x i , y i ) 

xi  x2 y i  y 2 x  x1 y i  y 1 f (x 1 , y 1 )  i f (x 2 , y 1 ) x1  x 2 y 1  y 2 x 2  x1 y 2  y 1

  

xi  x2 y i  y1 x  x1 y i  y 1 f (x 1 , y 2 )  i f (x 2 , y 2 ) (5.122) x1  x 2 y 2  y 1 x 2  x1 y 2  y 1

5.8.7.1 Multidimensional Interpolation in MATLAB MATLAB has two built-in functions for two- and three-dimensional piecewise interpolation: interp2 and interp3. These functions operate in a similar way as interp1. A simple representation of the syntax of interp2 is given by zi = interp2(x, y, z, xi, yi, ‘method’) where x and y = matrices containing the coordinates of the points at which the values in the matrix z are given, zi = a matrix containing the results of the interpolation as evaluated at the points in the matrices xi and yi, and method = the desired method. These methods are identical to those used by interp1; that is, linear, nearest, spline and cubic. EXAMPLE 5.53 The stresses at a number of coordinates on the surface of a metal plate are given as follows: S(3,2) = 70 Pa, S(3,7) = 65 Pa, S(10,2) = 67 Pa, and S(10,7) = 80 Pa. Use ­bilinear interpolation to determine the stress at xi = 6.5 and yi = 5.5. Solution: Substituting the given stress values in Equation (5.122), we have f (x i , y i ) 

(x i  x 2 ) (y i  y 2 ) (x  x 1 ) ( y i  y 1 ) f (x 1 , y 1 )  i f (x 2 , y 1 ) (x 1  x 2 ) (y 1  y 2 ) (x 2  x 1 ) ( y 2  y 1 )

  

ANMF.CH05_4PP.indd 380

(x i  x 2 ) (y i  y 1 ) (x  x 1 ) (y i  y 1 ) f (x 1 , y 2 )  i f (x 2 , y 2 ) (x 1  x 2 ) (y 2  y 1 ) (x 2  x 1 ) (y 2  y 1 )

3/1/2023 2:19:59 PM

Finite Differences and Interpolation • 381

Therefore, f (6.5, 5.5) 

(6.5  10) (5.5  7) (6.5  3) (5.5  7) (70)  (67) (3  10) (2  7) (10  3) (2  7)

  

(6.5  10) (5.5  2) (6.5  3) (5.5  2) (65)  (80)  71.3000 (3  10) (7  2) (10  3) (7  2)

EXAMPLE 5.54 Solve Example 5.53 using the MATLAB built-in function interp2. MATLAB Solution: >> x = [3 10]; >> y = [2 7]; >> z = [70 67;65 80]; >> interp2(x,y,z,6.5,5.5) ans = 71.3000 5.8.8 MATLAB Built-in Function: interpl 1-D data interpolation (table lookup) Syntax: yi = interp1(x,Y,xi) yi = interp1(Y,xi) yi = interp1(x,Y,xi,method) yi = interp1(x,Y,xi,method,‘extrap’) yi = interp1(x,Y,xi,method,extrapval) pp = interp1(x,Y,method,‘pp’)

ANMF.CH05_4PP.indd 381

3/1/2023 2:20:06 PM

382 • Applied Numerical Methods Using MATLAB Description: yi = interp1(x,Y,xi) interpolates to find yi, the values of the underlying function Y at the points in the vector or array xi. x must be a vector. Y can be a scalar, a vector, or an array of any dimension, subject to the following conditions: • If Y is a vector, it must have the same length as x. A scalar value for Y is expanded to have the same length as x. xi can be a scalar, a vector, or a multidimensional array, and yi has the same size as xi. • If Y is an array that is not a vector, the size of Y must have the form [n,d1,d2,...,dk], where n is the length of x. The interpolation is performed for each d1-by-d2-by-...-dk value in Y. The sizes of xi and yi are related as follows: • If xi is a scalar or vector, size(yi) equals [length(xi), d1, d2, ..., dk]. • If xi is an array of [m1,m2,...,mj,d1,d2,...,dk].

size

[m1,m2,...,mj],

yi

has

size

yi = interp1(Y, xi) assumes that x = 1: N, where N is the length of Y for vector Y, or size(Y, 1) for matrix Y. yi = interp1(x, Y, xi, method) interpolates using alternative methods: ‘nearest’

Nearest neighbor interpolation

‘linear’

Linear interpolation (default)

‘spline’

Cubic spline interpolation

‘pchip’

Piecewise cubic Hermite interpolation

‘cubic’

(Same as ‘pchip’)

‘v5cubic’ Cubic interpolation used in MATLAB 5. This method does not extrapolate. Also, if x is not equally spaced, ‘spline’ is used/ For the ”nearest,” ”linear,” and “v5cubic” methods, interp1(x, Y, xi, method) returns NaN for any element of xi that is outside the interval spanned by x. For all other methods, interp1 performs extrapolation for out of range values. yi = interp1(x, Y, xi, method,’extrap’) uses the specified method to perform extrapolation for out of range values. yi = interp1(x, Y, xi, method, extrapval) returns the scalar extrapval for out of range values. NaN and 0 are often used for extrapval. pp = interp1(x, Y, method,’pp’) uses the specified method to generate the piecewise polynomial form (ppform) of Y. You can use any of the methods in

ANMF.CH05_4PP.indd 382

3/1/2023 2:20:06 PM

Finite Differences and Interpolation • 383

the preceding table, except for ‘v5cubic’. pp can then be evaluated via ppval. ppval (pp, xi) is the same as interp1(x, Y, xi, method,’extrap’). Interpolation is the same operation as table lookup. Described in table lookup terms, the table is [x, Y] and interp1 looks up the elements of xi in x, and, based upon their locations, returns values yi interpolated within the elements of Y. NOTE

interp1q is quicker than interp1 on nonuniformly spaced data because it does no input checking. For interp1q to work properly x must be a monotonically increasing column vector and Y must be a column vector or matrix with length(X) rows. Type help interp1q at the command line for more information. EXAMPLE 5.55 Generate a coarse sine curve and interpolate over a finer abscissa. x = 0:10; y = sin(x); xi = 0:.25:10; yi = interp1(x, y, xi); plot(x, y,‘o’, xi, yi)

FIGURE 5.8

ANMF.CH05_4PP.indd 383

3/1/2023 2:20:06 PM

384 • Applied Numerical Methods Using MATLAB EXAMPLE 5.56 The following multidimensional example creates 2-by-2 matrices of interpolated function values, one matrix for each of the three functions x2, x3, and x4. x = [1:10]’; y = [x. ^2, x. ^3, x. ^4]; xi = [1.5, 1.75; 7.5, 7.75]; yi = interp1(x, y, xi); The result yi has size 2-by-2-by-3. size (yi) ans = 2 2 3 EXAMPLE 5.57 Here are two vectors representing the census years from 1900 to 1990 and the corresponding United States population in millions of people. t = 1900:10:1990; p = [75.995 91.972 105.711 123.203 131.669...

150.697 179.323 203.212 226.505 249.633];

The expression interp1 (t, p, 1975) interpolates within the census data to estimate the population in 1975. The result is ans = 214.8585 Now interpolate within the data at every year from 1900 to 2000 and plot the result. x = 1900:1:2000; y = interp1 (t, p, x,‘spline’); plot (t, p,‘o’, x, y)

ANMF.CH05_4PP.indd 384

3/1/2023 2:20:06 PM

Finite Differences and Interpolation • 385

FIGURE 5.9

Sometimes it is more convenient to think of interpolation in table lookup terms, where the data are stored in a single table. If a portion of the census data is stored in a single 5-by-2 table, tab = 1950 150.697 1960 179.323 1970 203.212 1980 226.505 1990 249.633 then the population in 1975, obtained by table lookup within the matrix tab, is p = interp1 (tab (:, 1),tab(:, 2),1975) p= 214.8585

ANMF.CH05_4PP.indd 385

3/1/2023 2:20:06 PM

386 • Applied Numerical Methods Using MATLAB EXAMPLE 5.58 The following example uses the “cubic” method to generate the piecewise polynomial form (ppform) of Y, and then evaluates the result using ppval. x = 0:.2:pi; y = sin(x); pp = interp1(x,y,‘cubic’,‘pp’); xi = 0:.1:pi; yi = ppval(pp,xi); plot(x,y,‘ko’), hold on, plot(xi,yi,‘r:’), hold off

FIGURE 5.10

Algorithm The interp1 command is a MATLAB M-file. The “nearest” and “linear” ­methods have straightforward implementations. For the “spline” method, interp1 calls a function spline that uses the functions ppval, mkpp, and unmkpp. These routines form a small suite of functions for working with piecewise

ANMF.CH05_4PP.indd 386

3/1/2023 2:20:06 PM

Finite Differences and Interpolation • 387

polynomials. spline uses them to perform the cubic spline interpolation. For access to more advanced features, see the spline reference page, the M-file help for these functions, and the Spline Toolbox™. For the “pchip” and “cubic” methods, interp1 calls a function pchip that performs piecewise cubic interpolation within the vectors x and y. This method preserves monotonicity and the shape of the data. See the pchip reference page for more information. Interpolating Complex Data For Real x and Complex Y: For interp1(x,Y,...) where x is real and Y is complex, you can use any interp1 method except for “pchip.” The shapepreserving aspect of the “pchip” algorithm involves the signs of the slopes between the data points. Because there is no notion of sign with complex data, it is impossible to talk about whether a function is increasing or decreasing. Consequently, the “pchip” algorithm does not generalize to complex data. The “spline” method is often a good choice because piecewise cubic splines are derived purely from smoothness conditions. The second derivative of the interpolant must be continuous across the interpolating points. This does not involve any notion of sign or shape and so generalizes to complex data. For Complex x: For interp1(x,Y,...) where x is complex and Y is either real or complex, use the two-dimensional interpolation routine interp2(REAL(x), IMAG(x),Y,...) instead. See also interp1q, interpft, interp2, interp3, interpn, pchip, spline. EXAMPLE 5.59 Use MATLAB’s interp1 function to fit the data in Table 5.29 with (a) linear interpolation, (b) nearest neighbor, (c) cubic spline with not-a-knot end conditions, and (d) piecewise cubic Hermite interpolation. Table 5.29

ANMF.CH05_4PP.indd 387

x

0

25

35

45

55

65

75

85

95

105

115

y

0

24

18

23

35

78

75

100

100

126

130

3/1/2023 2:20:06 PM

388 • Applied Numerical Methods Using MATLAB Solution: a) The following MATLAB program fits and plots the linear interpolation: x = [0 25 35 45 55 65 75 85 95 105 115]; y = [0 24 18 23 35 78 75 100 100 126 130];

>> xx = linspace(0,115);



>> y1 = interp1(x,y,xx);



>> plot(x,y,‘o’,xx,y1)



>> xlabel(‘x’);ylabel(‘y’)

FIGURE 5.11(a)

The results in Figure 5.11(a) are not so smooth and do not exhibit any overshoot. b) The commands to obtain and plot the nearest neighbor interpolation are given below:

>> yn = interp1(x,y,xx,‘nearest’);



>> plot(x,y,‘o’,xx,yn)



>> xlabel(‘x’);ylabel(‘y’)

ANMF.CH05_4PP.indd 388

3/1/2023 2:20:06 PM

Finite Differences and Interpolation • 389

FIGURE 5.11(b)

The results in Figure 5. 11(b) show that they are neither smooth nor an accurate representation of the underlying process. c) The commands to implement the cubic spline are given below:

>> ys = interp1(x,y,xx,’spline’);



>> plot(x,y,’o’,xx,ys)



>> xlabel(‘x’);ylabel(‘y’)

FIGURE 5.11(c)

ANMF.CH05_4PP.indd 389

3/1/2023 2:20:06 PM

390 • Applied Numerical Methods Using MATLAB The results in Figure 5.11(c) show that the plot is smooth and severe overshoot occurs at several locations. d) The commands to implement the piecewise cubic Hermite interpolation are given below:

>> yh = interp1(x, y, xx,‘pchip’);



>> plot(x, y,‘o’, xx, yh)



>> xlabel (‘x’); ylabel (‘y’)

FIGURE 5.11(d)

The results in Figure 5. 11(d) show that the transition between points is more gradual and therefore more physically realistic.

5.9 SUMMARY Interpolation is the method of computing the value of the function y = f(x) for any given value of the independent variable x when a set of values of y = f(x) for certain values of x are given. The study of interpolation is based on the assumption that there are no sudden jumps in the values of the dependent variable for the period under consideration. In this chapter, the study of

ANMF.CH05_4PP.indd 390

3/1/2023 2:20:07 PM

Finite Differences and Interpolation • 391

interpolation was presented based on the calculus of finite differences. Some important interpolation formulae by means of forward, backward, and central differences of a function; the cubic spline method, and the generalized cubic spline method, (all of which are frequently used in scientific and engineering calculations), were also presented along with illustrative worked example problems and solutions.

EXERCISES NOTE

In addition to solving the following exercises through analytical solutions or long-hand numerical computations, try to solve them using user-defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 5.1

Show that (a) Δ∇ = Δ − ∇ (b) ∇ = ΔE−1 (c) En = (1 + Δ)n   2  x Ee x x (d) e    e 2 x (h = interval of differencing) E e n a    a    n 2 sin (e) Δ sin (ax + b) =   sin ax  b  n   2  2    (interval of differencing = 1) (f) Δ2 = (1 + Δ)d2 (g) Δ3y2 = ∇3y5 (h) d = Δ(1 + Δ)−1/2 (i) ∇ = 1 − (1 + ∇)−1 (j)

ANMF.CH05_4PP.indd 391

     

3/1/2023 2:20:11 PM

392 • Applied Numerical Methods Using MATLAB 5.2

Find the following: (a) Deax (b) D sin x (c) D tan−1x  5 x  12  (d)   2  x  5 x  6  n1 (e)     2 x 3 (f)   x E

(with interval of differencing = 1)

 (1  x 2 ) (h) D sin (ax + b) (g)

(i) D2(3ex) 5.3

Construct a forward difference table for the following data: (a) x

45

55

65

75

y = f(x)

20

60

120

180

(b) x

40

50

60

70

80

90

y = f(x)

204

224

246

270

296

324

5.4

Construct a difference table for y = x3 + 2x + 3 for x = 1, 2, 3, 4, 5.

5.5

Given u0 = 1, u1 = 5, u2 = 10, u3 = 30, u4 = 30, find D4u0.

5.6

Given u0 = 5, u1 = 24, u2 = 81, u3 = 200, u4 = 100 and u5 = 8, find D5u0.

5.7

Estimate the missing term in the following tables: (a)

ANMF.CH05_4PP.indd 392

x

1

2

3

4

5

y = f(x)

5

14

?

74

137

3/1/2023 2:20:22 PM

Finite Differences and Interpolation • 393

(b) x

1

2

3

4

5

y = f(x)

8

17

38

?

140

x

0

1

2

3

4

y = f(x)

3

2

3

?

11

(c)

5.8

If m is a positive integer and the interval of differencing is 1, show that xm = x(x − 1) … [x − (x − 1)].

5.9

Express the following in the factorial notation. Take interval of differencing as equal to 1. (a) y = f(x) = 3x3 + x2 + x + 1 (b) y = f(x) = x4 − 5x3 + 3x + 4

5.10 Find the missing entry in the following tables: (a) x

0

1

2

3

4

y = f(x)

1

3

13



81

x

0

1

2

3

4

y = f(x)

1

0



28

69

x

0

1

2

3

4

y = f(x)

1

−2

−1



37

x

0

1

2

3

4

y = f(x)

1

4



28

61

(b)

(c)

(d)

(e)

ANMF.CH05_4PP.indd 393

x

0

1

2

3

4

y = f(x)

−6

−3

4



54

3/1/2023 2:20:22 PM

394 • Applied Numerical Methods Using MATLAB 5.11 Find the missing entry in the following tables: (a) x

0

1

2

3

4

y = f(x)

1

3



55

189

x

0

1

2

3

4

y = f(x)

1

−3

−1



165

(b)

(c) x

0

1

2

3

4

y = f(x)

−31

−35



5

133

x

0

1

2

3

4

y = f(x)

−23

−27



13

141

(d)

(e) x

0

1

2

3

4

y = f(x)

2

−2

0



166

5.12 Interpolate the missing entries in the following tables: (a) x

0

1

2

3

4

y = f(x)

1



13



81

(b) x

0

1

2

3

4

y = f(x)

1

−2





37

x

0

1

2

3

4

y = f(x)

1



11



61

(c)

ANMF.CH05_4PP.indd 394

3/1/2023 2:20:22 PM

Finite Differences and Interpolation • 395

(d) x

0

1

2

3

4

y = f(x)

−6



4



54

(e) x

0

1

2

3

4

y = f(x)

−6



12



118

12600 = 112.24972,

5.13 Given that

12610 = 112.29426,

12630 = 112.38327. Find the value of

112.33877,

12620 =

12616 .

5.14 Evaluate y = e2x for x = 0.25 from the data in the following table. x 2x

e

0.2

0.3

0.4

0.5

0.6

1.49182

1.82212

2.22554

2.71828

3.32012

5.15 The values of sin x are given below for different values of x. Find the value of sin 42º. x

40º

45º

50º

55º

60º

y = sin x

0.64279

0.70711

0.76604

0.81915

0.86603

5.16 In an examination the number of students who obtained marks between certain limits was as follows: Marks

30-40

40-50

50-60

60-70

70-80

No. of students

18

40

64

50

28

Find the number of students whose scores lie between 70 and 75. 5.17 From the following table estimate the number of students who obtained marks in the examination between 50 and 55. Marks

35-45

45-5

55-65

65-75

75-85

No. of students

31

42

51

35

31

5.18 A second degree polynomial passes through the points (2, −1), (3, 1), (4, 5) and (5, 11). Find the polynomial. 5.19 A second degree polynomial passes through the points (1, 5), (2, 11), (3, 19) and (4, 29). Find the polynomial.

ANMF.CH05_4PP.indd 395

3/1/2023 2:20:31 PM

396 • Applied Numerical Methods Using MATLAB 5.20 Find a cubic polynomial that takes the following values. x

0

1

2

3

f(x)

1

4

17

46

5.21 Refer to Exercise 5.11. Find f (1.5). 5.22 Refer to Exercise 5.10. Find f (3.5). 5.23 The table below gives the values of f(x) for 0.10 ≤ x ≤ 0.30. Find f (0.12) and f (0.26). x

0.1

0.15

0.2

0.25

0.30

f(x)

0.0998

0.1494

0.1987

0.2474

0.2955

5.24 The population (in thousands) of a small town is given in the following table. Estimate the population in the years 1965 and 1995. Year, x

1961

1971

1981

1991

2001

Population y = f(x) (in thousands)

46

66

81

93

101

5.25 Using Newton’s forward interpolation formula find the value of sin 52º from the following data. Estimate the error. x

40º

45º

50º

55º

60º

y = sin x

0.64279

0.70711

0.76604

0.81915

0.86603

5.26 Find the polynomial of degree three relevant to the following data using Lagrange’s interpolation formula. x

1

2

3

5

f(x)

−12

−14

−20

−20

5.27 Find the polynomial of the least degree that attains the prescribed values at the given point using Lagrange’s interpolation formula. x

1

2

4

5

y = f(x)

−27

−44

−84

−95

5.28 Find the polynomial of degree three relevant to the following data using Lagrange’s interpolation formula.

ANMF.CH05_4PP.indd 396

x

1

3

5

6

y = f(x)

71

115

295

466

3/1/2023 2:20:31 PM

Finite Differences and Interpolation • 397

5.29 Find the polynomial of degree three relevant to the following data using Lagrange’s interpolation formula. x

0

1

2

4

y = f(x)

2

5

12

62

5.30 Using Lagrange’s interpolation formula, find the value of y corresponding to x = 8 from the following table: x

1

3

6

9

y = f(x)

71

115

466

1447

5.31 Using Lagrange’s interpolation formula, find the value of y corresponding to x = 6 from the following table: x

0

3

5

7

y = f(x)

2

29

117

317

5.32 Using Lagrange’s interpolation formula, find the value of y corresponding to x = 4 from the following table: x

0

1

3

5

y = f(x)

−20

−12

−20

−20

5.33 Using Lagrange’s interpolation formula, find the value of y corresponding to x = 9 from the following table: x

5

6

11

13

y = f(x)

380

−2

508

1020

5.34 The following table gives the values of y corresponding to certain values of x. Find the value of x when y = 420.61175 by applying Lagrange’s inverse interpolation formula. x

1

2

5

6

y = f(x)

71

82

295

466

5.35 The following table gives the values of y corresponding to certain values of x. Find the value of x when y = −76.0188 by applying Lagrange’s inverse interpolation formula.

ANMF.CH05_4PP.indd 397

x

1

2

4

5

y = f(x)

−27

−65

−84

−95

3/1/2023 2:20:31 PM

398 • Applied Numerical Methods Using MATLAB 5.36 The following table gives the values of y corresponding to certain values of x. Find the value of x when y = 89.64656 by applying Lagrange’s inverse interpolation formula. x

1

2

5

6

y = f(x)

71

82

295

466

5.37 The following table gives the values of y corresponding to certain values of x. Find the value of x when y = −16.875 by applying Lagrange’s inverse interpolation formula. x

0

1

3

5

y = f(x)

−20

−12

−20

−20

5.38 Apply Gauss’s forward interpolation formula to find the value of f(x) at x = 11 from the following table: x

1

5

9

13

17

f(x)

13

16

18

21

26

5.39 Find the value of f(x) at x = 10 by applying Gauss’s forward interpolation formula from the following data: x

0

4

8

12

16

f(x)

23

28

36

39

45

5.40 Find the value of f(9) by applying Gauss’s forward interpolation formula from the following data: x

0

4

8

12

16

f(x)

15

25

34

37

42

5.41 Apply Gauss’s forward interpolation formula to find the value of f(12.2) from the following data: x

10

11

12

13

14

f(x)

23967

28060

31788

35209

38368

5.42 Find the value of f(9) by applying Gauss’s forward interpolation formula from the following data:

ANMF.CH05_4PP.indd 398

x

0

4

8

12

16

f(x)

17

19

35

38

41

3/1/2023 2:20:31 PM

Finite Differences and Interpolation • 399

5.43 Use Gauss’s forward interpolation formula to find y for x = 10 given that x

0

4

8

12

16

y = f(x)

15

25

34

37

42

5.44 Use Gauss’s backward interpolation formula to find the sales for the year 1966 given the following data: Year

1931

1941

1951

1961

1971

1981

Sales (in millions)

5

7

12

17

23

31

5.45 Apply Gauss’s backward interpolation formula and find the population of a city in 1946 based on the following data: Year

1931

1941

1951

1961

1971

Population (in millions)

16

21

29

41

54

5.46 Use Gauss’s backward interpolation formula to find the sales for the year 1966 based on the following data: Year

1951

1961

1971

1981

1991

Sales (in millions)

23

32

43

52

61

5.47 Apply Gauss’s backward interpolation formula to find the population of a city in 1986 based on the following data: Year

1951

1961

1971

1981

1991

2001

Population (in millions)

15

21

25

29

47

61

5.48 Use Gauss’s backward interpolation formula to find the sales for the year 1986 based on the following data: Year

1951

1961

1971

1981

1991

2001

Sales (in millions)

1

3

6

11

17

23

5.49 Apply Bessel’s interpolation formula to obtain y25, given that y20 = 515, y24 = 438, y28 = 348 and y32 = 249. 5.50 Apply Bessel’s interpolation formula to obtain y16, given that y15 = 0.345, y20 = 0.375, y25 = 0.478 and y30 = 0.653. 5.51 Apply Bessel’s interpolation formula to obtain y1.6, given that y1.5 = 0.345, y2.0 = 0.423, y2.5 = 0.512 and y3.0 = 0.756.

ANMF.CH05_4PP.indd 399

3/1/2023 2:20:31 PM

400 • Applied Numerical Methods Using MATLAB 5.52 Apply Bessel’s interpolation formula to obtain y36, given that y21 = 19, y231 = 29, y41 = 43 and y51 = 54. 5.53 Apply Bessel’s interpolation formula to obtain y1.4, given that y1.25 = 1.0772, y1.5 = 1.1447, y1.75 = 1.2051 and y2.0 = 1.2599. 5.54 Apply Bessel’s interpolation formula to obtain y0.644, given that y0.64 = 1.89648, y0.65 = 1.91554, y0.66 = 1.93479 and y0.67 = 1.95424. 5.55 Use Stirling’s interpolation formula to find y12.2 from the following table. x

10

11

12

13

14

y = f(x)

24765

27876

30879

36543

39879

5.56 Use Stirling’s interpolation formula to find y1.22 from the following table. x

0

0.5

1.0

1.5

2.0

y = f(x)

0

0.1910

0.3410

0.4330

0.4770

5.57 Use Stirling’s interpolation formula to find y22.6 from the following table. x

20

21

22

23

24

y = f(x)

1.2123

1.3546

1.4879

1.5765

1.6987

5.58 Use Stirling’s interpolation formula to find y3.8 from the following table of data. x

1

2

3

4

5

y = f(x)

0.12340

0.34560

0.87650

1.12346

1.34657

5.59 Use Stirling’s interpolation formula to find y3.25 from the following data. x

2

2.5

3.0

3.5

4.0

y = f(x)

49225

48316

47236

45926

44306

5.60 Use Everett’s interpolation formula to find the value of y when x = 3.5 from the following table. x y = f(x)

ANMF.CH05_4PP.indd 400

1

2

3

4

5

6

1.2567

1.4356

1.5678

1.6547

1.7658

1.8345

3/1/2023 2:20:31 PM

Finite Differences and Interpolation • 401

5.61 Use Everett’s interpolation formula to find the value of y when x = 6 from the following table. x

1

3

5

7

9

11

y = f(x)

−0.375

−2.947

−6.063

−2.331

24.857

105.165

5.62 Use Everett’s interpolation formula to find the value of y when x = 0.35 from the following table. x

0.1

0.2

0.3

0.4

0.5

0.6

y = f(x)

1.23900

1.12999

0.95294

0.70785

0.39469

0.01348

5.63 Use Everett’s interpolation formula to find the value of y when x = 0.35 from the following table. x

0.1

0.2

0.3

0.4

0.5

0.6

y = f(x)

2.4780

2.25997

1.90589

1.41569

0.78938

0.02696

5.64 Use Everett’s interpolation formula to find the value of y when x = 0.644 from the following table. x

0.61

0.62

0.63

0.64

0.65

0.66

0.67

y = f(x)

1.850431

1.858928

1.887610

1.906481

1.925541

1.944792

1.964237

5.65 Use Everett’s interpolation formula to find the value of y when x = 1.71 from the following table. x

1.4

1.5

1.6

1.7

1.8

1.9

2.0

y = f(x)

4.055200

4.481689

4.953032

5.473947

6.049647

6.685894

7.389056

5.66 Fit a cubic spline curve that passes through the points as shown below:



x

0

1

2

3

y

0

0.5

2

1.5

The natural end boundary conditions are: y″(0) = y ″(3) = 0.

5.67 Apply natural cubic spline interpolation method to find y at x = 1.5. The data points are given below:

ANMF.CH05_4PP.indd 401

x

1

2

3

4

5

y

0

1

0

1

0

3/1/2023 2:20:32 PM

402 • Applied Numerical Methods Using MATLAB 5.68 Develop a natural cubic spline for the following data:



x

3

4

5

6

7

y

3.7

3.9

3.9

4.2

5.7

Find f ′(3.4), f ′(5.2) and f ′(5.6).

5.69 Find the zero of the function y(x) from the following data:



x

1.0

0.8

0.6

0.4

0.2

y

−1.049

−0.0266

0.377

0.855

1.15

Use inverse interpolation with the natural cubic spline.

5.70 Fit a cubic spline curve for the following data with end conditions y ′(0) = 0.2 and y ′(3) = −1. x

0

1

2

3

y

0

0.5

3.5

5

5.71 Construct a clamped cubic spline for the following data given that the slope of 0.2 at x0 and a slope of 0.6 at xn.



n

0

1

2

3

4

x

3

4

5

6

7

y

3.7

3.9

3.9

4.2

5.7

Find f ″(3.4), f ′(5.2) and f (5.6).

5.72 Fit the data in Table 5.30 with cubic spline and find the value at x = 5. TABLE 5.30 i

1

2

3

4

x

3

4.5

7

9

y

2.5

1.0

2.5

0.5

5.73 Determine the cubic spline interpolation at x = 2.6 based on the data points given below:



ANMF.CH05_4PP.indd 402

x

0

1

2

3

y

1

1

0.5

0

Given the end conditions as f1,2 (0)  0 (zero slope).

3/1/2023 2:20:34 PM

Finite Differences and Interpolation • 403

5.74 Fit the data in Table 5.31 a cubic spline with not-a-knot end condition, and (b) piecewise Hermite in interpolation. TABLE 5.31 i

xi

fi

1 2 3 4

3.0 4.0 7.0 9.0

2 1 3 0.5

5.75 The following data in Table 5.32 is sampled from the step function. TABLE 5.32



x

−1

−0.6

−0.19

−0.19

0.5

1

f(x)

0

0

0

1

1

1

Fit the data with (a) cubic spline with not-a-knot end conditions, (b) cubic spline with zero-slope clamped end conditions, and (c) piecewise cubic Hermite interpolation.



In each case, create a plot comparing the fit with the step function.

5.76 (a) Use MATLAB to fit a cubic spline to the following data:



x

0

2

4

7

10

12

y

20

19.5

11.9

7

5.9

5.9

Determine the value of y at x = 1.5 (b) Repeat (a), but with zero first derivatives at the end knots.

5.77 Give the function

ANMF.CH05_4PP.indd 403

1 1  30 x 2 Generate five equidistantly spaced values of this function over the range [1, 1]. Fit the data with (a) fourth-order polynomial, (b) a linear spline, and (c) a cubic spline. Present the results graphically. f (x ) 

3/1/2023 2:20:35 PM

404 • Applied Numerical Methods Using MATLAB 5.78 For the data in Table 5.33, estimate f(2.1) using cubic splines. TABLE 5.33 x

1.9

2

2.3

2.5

2.6

f(x)

0.6

0.58

0.57

0.54

0.50

5.79 For the data given in Table 5.34, fit this data using MATLAB with a (a) cubic spline with not-a-knot end conditions, (b) cubic spline with zero-slope clamped end conditions, (c) piecewise cubic Hermite interpolation.

In each case, create a plot comparing the data with the fit. TABLE 5.34 x

−1

−0.62

−0.19

0.22

0.58

1

y

0

0

0

1

1

1

5.80 (a) Use MATLAB to fit a cubic spline to the data given in Table 5.35. Determine the value of y at x = 1.6. (b) Repeat (a), but zero first derivatives at the end knots. TABLE 5.35 x

0

2

4

7

10

12

y

19

19

11

6

7

7

5.81 Use MATLAB to generate eight points from the function y = cos2 q from q = 0 to 2p. Fit this data using (a) cubic spline with not-a-knot end conditions, (b) cubic spline with derivative end conditions, (c) piecewise cubic Hermite interpolation.

ANMF.CH05_4PP.indd 404

Generate plots of each fit as well as plots of the absolute error, where absolute error = (approximation − true) for each case.

3/1/2023 2:20:35 PM

CHAPTER

6

Curve Fitting, Regression, and Correlation In real-life engineering practice, often a relationship is found to exist between two (or more) variables. For example, the experimental data for force (N) and velocity (m/s) from a wind-tunnel experiment. A mechanical element/component is suspended in a wind tunnel and the force measured for various levels of wind velocity. This relationship can be visualized by plotting force versus velocity. It is frequently desirable to express this relationship in mathematical/ analytical form by establishing an equation connecting the variables. In order to determine an equation connecting the variables, it is often necessary to collect the data depicting the values of the variables under consideration. For example, if x and y denote respectively the velocity and force from the wind-tunnel experiment, then a sample of n individual would give the velocities x1, x2, …, xn and the corresponding forces y1, y2, …, yn. When these points (x1, y1), (x2, y2), …, (xn,yn) are plotted on a rectangular coordinate system, the resulting set of points on the plot is called the scatter diagram. From such a scatter diagram, one can visualize a smooth curve approximating the given data points. Such a curve is known as an approximating curve. Figure 6.1(a) shows that the data appears to be approximated by a straight line, and it clearly exhibits a linear relationship between the two variables. However, Figure 6.1(b) shows a relationship that is not linear and in fact it is a nonlinear relationship between the variables. Curve fitting is the general problem of finding equations of approximating curves which best fit the given set of data.

ANMF.CH06_4PP.indd 405

3/1/2023 2:21:13 PM

406 • Applied Numerical Methods Using MATLAB

y

y

x

x (a) Linear Relationship

(b) Nonlinear Relationship

FIGURE 6.1  Linear and nonlinear relationship.

APPROXIMATING CURVES Table 6.1 lists a few common approximating curves used in practice and their equations. The variables x and y are called the independent and dependent variables, respectively. The scatter diagrams of the variables or transformed variables will help determine the curve to be used. TABLE 6.1 

ANMF.CH06_4PP.indd 406

No.

Equation

1.

y = a + bx

Description of the curve Straight line 2

2.

y = a + bx + cx

3.

y = a + bx + cx2 + dx3 2

Parabola or quadratic curve 3

Cubic curve 4

4.

y = a + bx + cx + dx + ex

Quartic curve

5.

y = a0 + a1x + a2x2 + … + anxn

nth degree curve

6.

y=

7.

y = abx or log y = log a + x(log b) = a0 + b0x

Exponential curve

8.

y = axb or log y = log a + b(log x)

Geometric curve (power function)

1 1 or = c + mx y c + mx

Hyperbola

x

9.

y = ab + c

10

y = axb + c bx

Modified exponential curve Modified geometric curve x

11.

y = pq or log y = log p + b log q = abx + q

12.

y = pq b + h

13.

y=

x

1 1 or = abx + q ab x + q y

Gompertz curve Modified Gompertz curve Logistic curve

3/1/2023 2:21:14 PM

Curve Fitting, Regression, and Correlation • 407

No.

Equation mx

Description of the curve mx

14.

y = be or y = b 10

Exponential function

15.

1 y= mx + b

Reciprocal function

16.

y= α

x β+x

Saturation-growth-rate equation

In Table 6.1, a, b, c, d, e, a0, a1, a2, …, an, b0, p, q, h, m, α and β are all constant coefficients.

LINEAR REGRESSION Linear regression and correlation are two commonly used methods for examining the relationship between quantitative variables and for making predictions. In this chapter, we review linear equations with one independent variable and explain how to find the regression equation—the equation of the line that best fits a set of data points. We also examine the coefficient of determination that is a descriptive measure of the utility of the regression equation for making predictions. In addition, we discuss the linear correlation coefficient, which provides a descriptive measure of the strength of the linear relationship between the two quantitative variables.

6.1 LINEAR EQUATION The general form of a linear equation with one independent variable can be written as

y = a + bx

where a and b are constants (fixed numbers), x is the independent variable, and y is the dependent variable. The graph of a linear equation with one independent variable is a straight line, or simply a line. Also, any nonvertical line can be represented by such an equation. Linear equations with one independent variable occur frequently in applications of mathematics in many different fields; including the social sciences, engineering, and management, as well as physical and mathematical sciences. For a linear equation y = a + bx, the number a is the y-value of the point of intersection of the line and the y-axis. The number b measures the steepness of

ANMF.CH06_4PP.indd 407

3/1/2023 2:21:14 PM

408 • Applied Numerical Methods Using MATLAB

y y = a + bx

1 unit increase

0,a

b units up

a x FIGURE 6.2  (a) Graph of y = a + bx. y

y y = a + bx

y y = a + bx

y = a + bx

O

O

O

b>0

b 0, slopes downward if b < 0, and is horizontal if b = 0, as shown in Figure 6.2(b).

6.2 CURVE FITTING WITH A LINEAR EQUATION Curve fitting is a procedure in which a mathematical formula (equation) is used to best fit a given set of data points. The objective is to find a function that fits the data overall. Curve fitting is used when the values of the data points have some error, or scatter and require a curve fit to the points. Curve fitting

ANMF.CH06_4PP.indd 408

3/1/2023 2:21:15 PM

Curve Fitting, Regression, and Correlation • 409

can be accomplished with many types of functions and with ­polynomials of various orders. Curve fitting using a linear equation (first degree polynomial) is the process by which an equation of the form

y = a + bx

(6.1)

is used to best fit the given data points. This can be accomplished by finding the constants a and b, that give the smallest error when the data points are substituted in Equation (6.1). If the data points consist of only two points, the constants can be obtained such that Equation (6.1) gives the exact values at the points. Figure 6.3 shows the straight line corresponding to the Equation (6.1) and passing through the two points. When the data has more than two points, the constants a and b are determined such that the line has the best fit overall as shown in Figure 6.4. The procedure for obtaining the constants a and b that give the best fit requires a definition of best fit and an analytical procedure for deriving the constants y

O

x

FIGURE 6.3  Straight line connecting two points.

y

O

x

FIGURE 6.4  A straight line passing through many data points.

ANMF.CH06_4PP.indd 409

3/1/2023 2:21:15 PM

410 • Applied Numerical Methods Using MATLAB

(xn,yn) en f(xn) y = a + bx e2 f(x1)

e1 (x1,y1)

f(x3)

(x3,y3) x

O FIGURE 6.5  Curve-fitting points with a linear equation y = a + bx.

a and b. The fitting between the given data points and an approximating linear function is obtained by first computing the error, also called the residual, which is the difference between a data point and the value of the approximating function, at each point. Figure 6.5 shows a linear function (straight line) that is used for curve fitting n points. Thus, the residual, e is the discrepancy between the true value of y and the approximating value, a + bx, predicted by the linear equation.

6.3 CRITERIA FOR A BEST FIT A criterion that measures how well the approximating function fits the given data can be determined by computing a total error E in terms of the residuals as

n

E=∑ = ei i=1

n

∑[y i=1

i

− (a + bx i )] (6.2)

where n = total number of points. However, this is an inadequate criterion, as illustrated in Figure 6.6, which shows that E is zero since e1 = –e4 and e2 = –e3.

ANMF.CH06_4PP.indd 410

3/1/2023 2:21:15 PM

Curve Fitting, Regression, and Correlation • 411

y e4 e3 e2 e1

O FIGURE 6.6  Straight line fit with E = 0.

One way to remove the effect of the signs might be to minimize the sum of the absolute values of the discrepancies: = E



n

n

e | ∑| y ∑| =

i =i 1 =i 1

i

− a − bx i | (6.3)

Figure 6.7 shows why this criterion is also inadequate. our points show, for the same set of points there can be several functions that give the same total error. E is the same for the two approximating lines in Figure 6.7. A third strategy for fitting a best line is the minmax criterion. In this technique, the straight line is chosen that minimizes the maximum distance that

y

e4 e3

e1 e2

O

x

FIGURE 6.7  Two straight line fits with the same total error as per Equation (6.3).

ANMF.CH06_4PP.indd 411

3/1/2023 2:21:16 PM

412 • Applied Numerical Methods Using MATLAB

y

outlier x

O

FIGURE 6.8  Minimization of the maximum error of any individual point.

an individual point falls from the line. As shown in Figure 6.8, this technique gives undue influence to an outlier (a single point with a large error). A method that overcomes the shortcomings of the aforementioned approaches to minimize the sum of the squares of the residuals:

n 2 i =i 1 =i 1

= Sr

n

e ∑ (y ∑=

i

− a − bx i )2 (6.4)

Equation (6.4) always gives a positive number of Sr (total error) and positive and negative residuals do not cancel each other. This criterion (Equation 6.4) is called the least squares and has many advantages, including that it gives a unique line for a given set of data. Equation (6.4) can be used to determine the coefficients a and b in the linear function y = a + bx that yield the smallest total error. This is accomplished by using a procedure called linear least-squares regression, which is presented in the next section.

6.4 LINEAR LEAST-SQUARES REGRESSION Linear least-squares regression is a method in which the coefficients a and b of a linear function y = a + bx are determined such that the function has the best fit to a given set of data points. The best fit is defined as the smallest possible total error that is computed by adding the squares of the residuals according to Equation (6.4).

ANMF.CH06_4PP.indd 412

3/1/2023 2:21:16 PM

Curve Fitting, Regression, and Correlation • 413

For a given set of n data points (xi, yi), the overall error calculated by Equation (6.4) is

S= r

n

∑[y i =1

i

− (a + bx i )]2 (6.5)

Since the values of xi and yi are known, Sr in Equation (6.5) is a nonlinear function of two variables a and b. This function Sr has a minimum at the values of a and b, where the partial derivatives of Sr with respect to each variable is equal to zero. Taking the partial derivatives and setting then equal to zero gives

n ∂Sr =−2∑ (y i − a − bx i ) =0 (6.6) ∂a i =1



n ∂Sr =−2∑ [(y i − a − bx i )x i ] =0 (6.7) ∂b i =1

Equations (6.6) and (6.7) are a system of two linear equations for the two unknowns a and b, and can be rewritten in the form

n  n  na +  ∑ x i  b = y i (6.8) ∑ = i 1  i 1= 

n  n   n 2 x a x b x i y i (6.9) + = ∑ ∑ i  ∑ i  = i 1 i 1=   i 1=  Equations (6.8) and (6.9) are called the normal equations and can be solved simultaneously for





or

ANMF.CH06_4PP.indd 413

n  n  n  n ∑ xi yi −  ∑ x i   ∑ y i   i 1=   i 1  (6.10) b= = i1 = 2 n n   2 n∑ x i − ∑ x i  =i 1 = i 1  n   n  x ∑ i  ∑ y i  n = i 1  x i y i −  i 1= ∑ n i =1 b= (6.11) 2 n   ∑ x i  n 2 x i −  i =1  ∑ n i =1

3/1/2023 2:21:16 PM

414 • Applied Numerical Methods Using MATLAB

and

 n 2 n   n  n  ∑ x i  ∑ y i  − ∑ x i y i  ∑ x i  = i 1 =  i 1 =   i 1  (6.12) a =  i 1= 2 n n   n ∑ x 2i −  ∑ x i  =i 1 = i 1 

or

a= y − bx

Since Equations (6.10) to (6.12) contain summations that are the same for a set of n points, they can also be written as,

SSxx =Σ(x i − x)2 =Σx 2i − (Σx i )2 /n (6.13)



SSxy =Σ(x i − x)(y i − y) =Σx i y i − (Σx i )(Σy i )/n (6.14)



SSyy =Σ(y i − y)2 =Σy 2i − (Σy i )2 /n (6.15)

The regression equation for a set of n data points is

yˆ = a + bx

where

b=

and

a=

SSxy SSxx

(6.16)

1 (Σy i − bΣx i ) = y − bx (6.17) n

6.5 LINEAR REGRESSION ANALYSIS A regression model is a mathematical equation that describes the relationship between two or more variables. A single regression model includes only two variables: one independent and one dependent. The relationship between two variables in a regression analysis is expressed by a mathematical equation called a regression equation or model. A regression equation that gives a straight-line relationship between two variables is called a linear regression model; otherwise, it is called a nonlinear regression model. Figures 6.9(a) and (b) show a linear and a nonlinear relationship between independent variable and the dependent variable.

ANMF.CH06_4PP.indd 414

3/1/2023 2:21:17 PM

O

Dependent variable

Dependent variable

Curve Fitting, Regression, and Correlation • 415

Linear

O

Independent variable (a)

Nonlinear

Independent variable (b)

FIGURE 6.9   (a) Linear relationship. (b) Nonlinear relationship.

The equation of a linear relationship between two variables x and y is written as

y = a + bx

(6.13)

where a gives the y-intercept and b represents the slope of the line. In a regression model, x is the independent variable and y is the dependent variable. The simple linear regression model for population is written as

y = A + Bx

(6.14)

Equation (6.14) is called a deterministic model. It gives an exact relationship between x and y. However, in many instances the relationship between the variables is not exact. Therefore, the complete regression model is then written as

y   A + Bx + ∈ (6.15)

where ∈ is called the random error term. This regression model (Equation 6.5) is called a probabilistic model (or a statistical relationship). The random error term ∈ is included in the model to take into consideration of the following two phenomena: a) Missing or omitted variables: The random error term ∈ is included to capture the effect of all the missing or omitted variables that were not included in the model. b) Random variation: The random error term ∈ is included to capture the random variation.

ANMF.CH06_4PP.indd 415

3/1/2023 2:21:17 PM

416 • Applied Numerical Methods Using MATLAB In Equation (6.15), A and B are the population parameters. The regression line obtained from Equation (6.15) by using the population data is called the population regression line. The values of A and B combined is the population regression line is called the true values of the y-intercept and slope. However, most often the population data is difficult to obtain. As a consequence, we almost always use the sample data and use the model given by Equation (6.15). The values of the y-intercept and slope calculated from sample data on x and y are called the estimated values of A and B and are denoted by a and b. The estimated regression model is then written as yˆ = a + bx (6.16)



where yˆ is the estimated or predicted value of y for a given value of x. Equation (6.16) is called the estimated regression model. It gives the regression of y on x. A plot of paired observation is called a scatter diagram as shown in Figure 6.10. To find the line that best fits the scatter of points, we minimize the error sum of squares, denoted by SSE, which is given by ˆ 2 (6.17) SSE = Σe 2 = Σ(y − y)

where

e = y – yˆ .

The least-squares method gives the values of a and b such that the sum of squared errors (SSE) is minimum. y

x

O FIGURE 6.10  Scatter diagram.

ANMF.CH06_4PP.indd 416

3/1/2023 2:21:18 PM

Curve Fitting, Regression, and Correlation • 417

For the least-squares regression line yˆ = a + bx from Equations (6.10) to (6.17), we have SSxy



b=

where

SSxy = Σxy −

(Σx)(Σy) n



SSxx = Σx 2 −

(Σx)2 n



SSyy =Σ(y − y) =Σy 2 − (Σy)2 / n (6.19)

and a= y − bx (6.18)

SSxx

The least-squares regression line yˆ = a + bx is also called the regression of y on x. EXAMPLE 6.1 Table 6.2 gives experimental data for force (N) and velocity (m/s) for an object suspended in a wind tunnel. TABLE 6.2  Velocity, v(m/s)

10

20

30

40

50

60

70

80

Force F(N)

24

68

378

552

608

1218

831

1452

a) Use the linear least-squares regression to determine the coefficients a and b in the function yˆ = a + bx that best fits the data. b) Estimate the force when the velocity is 55 m/s. Solution: Here n = 8.

ANMF.CH06_4PP.indd 417

n

x

y

x2

xy

1 2 3 4 5 6 7 8

10 20 30 40 50 60 70 80

24 68 378 552 608 1218 831 1452

100 400 900 1600 2500 3600 4900 6400

240 1360 11340 22080 30400 73080 58170 116160



360

5131

20400

312830

3/1/2023 2:21:18 PM

418 • Applied Numerical Methods Using MATLAB



Σx 360 = = 45 n 8 Σy 5131 = y = = 641.375 n 8 = x

From Equation (6.16), we have SSxy



b=

where

SSxy = Σxy −

(Σx)(Σy) (360)(5131) = 312830 − = 81935 n 8

and

Σx 2 − SSxx =

(Σx)2 (360)2 = = 20400 − 4200 n 8



b=

SSxx

SSxy SSxx



==

81935 = 19.5083 4200

From Equation (6.17), we have

a= y − bx = 641.375 − (19.5083)(45) = −236.50

Hence

yˆ = –236.50 + 19.5083x

6.5.1 MATLAB built-in function: polyfit >> x= [10 20 30 40 50 60 70 80]; >> y= [24 68 378 552 608 1218 831 1452]; >> coefficients=polyfit(x, y, 1) coefficients =   19.5083 -236.5000 >> y=polyval (coefficients, 55) y=  836.4583 Because a straight line is a first-order polynomial, polyfit(x,y,1) will return the slope and the intercept of the best-fit straight line. Thus, the slope is 19.5083 and the intercept is -236.5.

ANMF.CH06_4PP.indd 418

3/1/2023 2:21:19 PM

Curve Fitting, Regression, and Correlation • 419

Hence yˆ = –236.50 + 19.5083x b) The estimated value of the force when the velocity is 55 m/s is given by yˆ = a + bx = –236.50 + 19.5083(55) = 836.4583(N).



6.5.2 MATLAB built-in function: polyval Another function, polyval, can then be used to compute a value using the coefficients. It has the general format: >>y = polyval (p, x) Where p = the polynomial coefficients, and y = the best-fit value at x. Here, we have >> x= [10 20 30 40 50 60 70 80]; >> y= [24 68 378 552 608 1218 831 1452]; >> coefficients=polyfit(x, y, 1) coefficients =   19.5083 -236.5000 >> y=polyval (coefficients, 55) y=  836.4583 The estimated value of the force when the velocity is 55 m/s is = 836.4583(N). The estimated values of the force are shown in Table 6.3. TABLE 6.3  Velocity, v(m/s) 10

20

30

40

50

60

Force F(N)

68

378

552

608

1218 831

24

Estimated Force -41.4167 153.6667 348.75 F(N)

543.8333 738.9167 934

70

80 1452

1129.0333 1324.1667

A graph of the numerical and analytical solutions was generated as shown in Figure 6.11 with the following commands in MATLAB:

ANMF.CH06_4PP.indd 419

3/1/2023 2:21:19 PM

420 • Applied Numerical Methods Using MATLAB

Linear Least squares straight line

1600

analytical numerical

1400 1200

Force F(N)

1000 800 600 400 200 0 -200 10

20

30

40 50 Velocity v (m/s

60

70

80

FIGURE 6.11  Plot of least-squares fit (line).

>> x= [10 20 30 40 50 60 70 80]; >> y= [24 68 378 552 608 1218 831 1452]; >> yp= [-41.4167 153.6667 348.75 543.8333 738.9167 934 1129.0333 1324.1667]; >> plot(x, y,’o’, x, yp) >>grid on >> title (‘Linear Least squares straight line ‘) >> xlabel (‘Velocity v (m/s’), ylabel (‘Force F (N)’) >> legend (‘analytical’,’numerical’)

6.6 INTERPRETATION OF A AND B When b is positive, an increment in x will lead to an increase in y and a decrease in x will lead to a decrease in y. That is, when b is positive, the movements in x and y are in the same direction. Such a relationship between x and y is called a positive linear relationship. The regression line slopes upward from left to right. Similarly, if the value of b is negative, an increase in x will cause a decrease in y and a decrease in x will cause an increase in y. The changes in x and y are in opposite directions. Such a relationship between x and y is called a negative linear relationship. The regression line slopes downward from left to right. Figure 6.12 shows these two relationships.

ANMF.CH06_4PP.indd 420

3/1/2023 2:21:19 PM

Curve Fitting, Regression, and Correlation • 421

y

y

b>0

b>0

x O (a) Positive linear relationship (b > 0)

x O (b) Negative linear relationship (b < 0)

FIGURE 6.12  Positive and negative relationship between x and y.

Assumptions in the Regression Model The linear regression analysis is based on the following assumptions: 1. The random error term ∈ has a mean equal to zero for each x. 2. The errors associated with different observations are independent. 3. For any given x, the distribution of errors is normal. 4. The distribution of population errors for each x has the same (constant) standard deviation, which is denoted by σe as shown in Figures 6.13(a) and (b).

6.7 STANDARD DEVIATION OF RANDOM ERRORS The standard deviation se measures the spread of the errors around the regression line as shown in Figure 6.13(b). The standard deviation of errors is calculated using se =

where or where

ANMF.CH06_4PP.indd 421

SSE n−2

ˆ 2 SSE = Σ(y − y) se =

SSyy − bSSxy n−2

Σ(y − y)2 = Σy 2 − SSyy = Σxy − SSxy =

(6.20) (Σy)2 n

(Σx)(Σy) (6.21) n

3/1/2023 2:21:19 PM

422 • Applied Numerical Methods Using MATLAB

Normal distribution with (constant) standard deviation e

E() = 0 (a) y

Population regression line

x (b) FIGURE 6.13  (a) Assumptions in the regression model. (b) Distribution on the regression line.

In Equation (6.20), (n – 2) represents the degrees of freedom for the regression model. The reason for df = n – 2 is that we lose one degree of freedom to calculate x and one for y .

6.8 COEFFICIENT OF DETERMINATION The coefficient of determination, denoted by r2, represents the proportion of the total sum of squares that is explained by the use of the regression model. The computational formula for r2 is given by

r2 = b

SSxy SSyy

  0 ≤ r2 ≤ 1

(6.22)

The total sum of squares denoted by SST is the total variation in the observed values of the response variable

SST = SSyy = Σ(y − y)2 = Σy 2 −

(Σy)2 (6.23) n

which is the same formula we use for SSyy.

ANMF.CH06_4PP.indd 422

3/2/2023 4:57:41 PM

Curve Fitting, Regression, and Correlation • 423

The regression sum of squares, denoted by SSR, is the variation in the observed values of the response variable explained by the regression: ˆ 2     SSE = Σ(y − y)      SSR = SST – SSE or    SST = SSR + SSE

(6.24)

The ratio of SSR to SST gives the coefficient of determination. That is,

r2=

SSR SST − SSE SSE = = 1− (6.25) SST SST SST

This formula shows that we can also interpret the coefficient of determination as the percentage reduction obtained in the total squared error by using the regression equation instead of the mean, y , to predict the observed values of the response variable. The coefficient of determination, denoted by r2, represents the portion of SST that is explained by the use of the regression model. Hence, r2 =



b SSxy SSyy

(6.26)

and 0 ≤ r2 ≤ 1 The value of r2 is the proportion of the variation in y that is explained by the linear relationship between x and y. The coefficient of determination is the amount of the variation in y that is explained by the regression line. It is computed as r2 =

Explained variation Σ(yˆ − y)2 = Total variation Σ(y − y)2

The coefficient of determination, r2, always lies between 0 and 1. A value of r2 near 0 suggests that the regression equation is not very useful for making predictions, whereas a value of r2 near 1 suggests that the regression equation is quite useful for making predictions.

ANMF.CH06_4PP.indd 423

3/1/2023 2:21:20 PM

424 • Applied Numerical Methods Using MATLAB EXAMPLE 6.2 For the data of Example 6.1, calculate the a) standard deviation of errors, se b) error sum of squares, SSE c) total sum of squares, SST d) regression sum of squares, SSR e) the coefficient of determination, r2 Solution: Referring to Table 6.4, we have

n = 8, ∑x = 360, ∑y = 5131, ∑x2 = 20400, ∑xy = 312830, ∑y2 = 5104841



= x

Σx 360 = = 45 n 8



= y

Σy 5131 = = 641.375 n 8



SSxy = Σxy −

(Σx)(Σy) (360)(5131) = 312830 − = 81935 n 8



Σx 2 − SSxx =

(Σx)2 (360)2 = = 20400 − 4200 n 8



SSyy = Σy 2 −

(Σy)2 (5131)2 = 5104841 − = 1813945.875 n 8



SS xy 81935 = b = = 19.5083 SS 4200 xx a =y − bx =641.375 − 19.5083x a) The standard deviation of errors, se

= se

ANMF.CH06_4PP.indd 424

SSyy − bSSxy = n−2

1813945.875 − 19.5083(81935) = 189.5304 8−2

3/1/2023 2:21:21 PM

Curve Fitting, Regression, and Correlation • 425

b) The error sum of squares, SSE

ˆ 2= SSE = Σ(y − y) 215530.5833 c) Total sum of squares, SST



SST = SSyy = 1813945.875 d) The regression sum of squares, SSR



SSR = SST – SSE = 1813946.875 – 215530.5833 = 1598415.2917 e) The coefficient of determination, r2



= r2

b SSxy (19.5083)(81935) = = 0.8812 SSyy 1813945.875

6.9 LINEAR CORRELATION Linear correlation coefficient is a measure of the relationship between two variables. Linear correlation coefficient measures how closely the points in a scatter diagram are spread around the regression line. The correlation coefficient calculated for the population is denoted by r and the one calculated for sample data is denoted by r. The linear correlation coefficient r measures the strength of the linear relationship between the paired x- and y- quantitative values in a sample. The linear correlation coefficient is sometimes referred to as the Pearson product moment correlation coefficient in honor of Karl Pearson (1857–1936), who originally developed it. The square of the correlation coefficient is equal to the coefficient of determination. The value of the correlation coefficient always lies in the range –1 to 1. Hence –1 ≤ ρ ≤ 1 and –1 ≤ r ≤ 1. If r = 1, it refers to a case of perfect positive linear correlation and all points in the scatter diagram lie on a straight line that slopes upward from left to right, as shown in Figure 6.14. If r = –1, the correlation is said to be perfect negative linear correlation and all points in the scatter diagram fall on a straight line that slopes downward from left to right, as shown in Figure 6.14(b). When there is no linear correlation between the two variables and r is close to  0. Also, in this case, all the points are scattered all over the diagram as shown in Figure 6.14(c).

ANMF.CH06_4PP.indd 425

3/1/2023 2:21:21 PM

426 • Applied Numerical Methods Using MATLAB Two variables are said to have a strong positive linear correlation when the correlation is positive and close to 1. If the correlation between the two variables is positive but close to zero, then the variables have a weak positive linear correlation. Similarly, when the correlation between two variables is negative and close to –1, then the variables are said to have a strong negative linear correlation. A weak negative linear correlation exists when the correlation between the variables is negative but close to zero. The four cases are shown in Figures 6.15(a) to 6.15(d). Figure 6.16 shows the various degrees of linear correlation. y

y

y

r=1

x

O

x

O

(a)

x

O

(b)

(c)

FIGURE 6.14  Linear correlation between two variables. (a) Perfect positive linear correlation, r = 1 (b) Perfect negative linear correlation, r = –1 (c) No linear correlation, r = 0 y

y

x

x (a) Strong positive correlation (r close to 1)

(b) Weak positive linear correlation (r is positive but close to zero)

y

y

x

x

(c) Strong negative linear correlation (r close to –1)

(d) Weak negative linear correlation (r is negative and close to zero)

FIGURE 6.15  Linear correlation between variables.

ANMF.CH06_4PP.indd 426

3/1/2023 2:21:21 PM

Curve Fitting, Regression, and Correlation • 427

y

y

y

x

x

(a) Perfect positive linear correlation r=1

x

(b) Strong positive linear correlation r = 0.92

y

(c) Weak positive linear correlation r = 0.42

y

y

x

x

(d) Perfect negative linear correlation r=1

(e) Strong negative linear correlation r = 0.92

x (f) Weak negative linear correlation r = 0.42

y

x (g) No linear correlation (linearly uncorrelated) r=0

FIGURE 6.16  Various degrees of linear correlation.

The simple linear correlation, denoted by r, measures the strength of the linear relationship between two variables for a sample and is calculated as

r=

SSxy SSxx SSyy

(6.27)

It should be noted here that r and b calculated for the same sample will always have the same sign. Properties of the Linear Correlation Coefficient r 1. The value of r is always between -1 and +1 inclusive. That is, –1 ≤ r ≤ 1. 2. The values of r do not change if all values of either variable are converted to a different scale. 3. The value of r is not affected by the choice of x or y. Interchange all x- and y-values, and the value of r will not change.

ANMF.CH06_4PP.indd 427

3/1/2023 2:21:22 PM

428 • Applied Numerical Methods Using MATLAB 4. r measures the strength of a linear relationship. The magnitude of r indicates the strength of the linear relationship. A value of r close to -1 or to 1 indicates a strong linear relationship between the variables and that the variable x is a good linear predictor of the variable y. That is, the regression equation is extremely useful for making predictions. A value of r near 0 indicates at most a weak linear relationship between the variables and that the variable x is a poor linear predictor of the variable y. That is, the regression equation is either useless or not very useful for making predictions. It is not designed to measure the strength of a relationship that is not linear. 5. The sign of r suggests the type of linear relationship. A positive value of r suggests that the variables are positively correlated, meaning that y tends to increase linearly as x increases, with the tendency being greater the closer that r is to 1. A negative value of r suggests that the variables are negatively linearly correlated, meaning that y tends to decrease linearly as x increases, with the tendency being greater the closer that r is to -1. 6. r reflects the slope of the scatter plot. The linear correlation coefficient is positive when the scatter plot shows a positive slope and is negative when the scatter plot shows a negative slope. 7. The sign of r and the sign of the slope of the regression line are identical. If r is positive, so is the slope of the regression line. That is, the regression line slopes upward. If r is negative, so are the slope of the regression line and the regression line slopes downward. Explained and Unexplained Variation The total variation is defined as Σ(y − y)2 , that is, the sum of the squares of the deviations of the values of y from the mean y . This can be written as

ˆ 2 + Σ(yˆ − y)2 (6.28) Σ(y − y)2 = Σ(y − y)

where yˆ is the value of y for given values of x as estimated from yˆ = a + bx , a measure of the scatter about the regression line of y on x. The first term on the right side of Equation (6.28) is called the unexplained variation while the second term is called the explained variation. The deviations yˆ − y have a definite pattern while the deviations y − yˆ behave in a random or unpredictable manner. Similar results hold true for the variable x.

ANMF.CH06_4PP.indd 428

3/1/2023 2:21:22 PM

Curve Fitting, Regression, and Correlation • 429

The ratio of the explained variation to the total variation is called the coefficient of determination. If there is zero explained variation, that is, the total variation is all unexplained, then this ratio is zero. If there is zero unexplained variation, that is, the total variation is all explained, the ratio is one. In all other cases, the ratio lies between zero and one. The ratio is always non-negative. The quantity, r, is called the coefficient of correlation, and it is given by

Explained variation Σ(yˆ − y)2 r= ± = ± (6.29) Total variation Σ(y − y)2

r varies between –1 and +1. The signs ± are used for positive linear correlation and negative error correlation respectively. r is a dimensionless quantity. The coefficient of determination equals the square of the liner correlation coefficient. EXAMPLE 6.3 Determine the correlation coefficient for the data given in Example 6.1. Solution: Refer to the solutions obtained earlier for Examples 6.1 and 6.2. We have SSxy = 81935,  SSxx = 4200  and  SSyy = 1813945.875 Hence, = r

SSxy = SSxx SSyy

81935 = 0.9387 (4200)(1813945.875)

6.10 LINEARIZATION OF NONLINEAR RELATIONSHIPS Linear regression provides a powerful technique for fitting a best line to data. There exists many situations in science and engineering that show the relationship between the quantities that are being considered is not linear. There are several examples of nonlinear functions used for curve fitting. A few of them were described in Table 6.1. Nonlinear regression techniques are available to fit these equations in Table 6.1 to data directly. A simpler alternative is to use analytical manipulations to transform the equations into a linear form. Then linear regression can be used to fit the equations to data.

ANMF.CH06_4PP.indd 429

3/1/2023 2:21:22 PM

430 • Applied Numerical Methods Using MATLAB For instance, y = bxm can be linearized by taking its natural logarithm to give = n y n b + mnx (6.30)



A plot of n y versus n x will give a straight line with a slope of m and an intercept of n b as shown in Figure 6.17. Many other nonlinear equations can be transformed into linear form in a similar way. Table 6.4 lists several such equations. Graphical representation of several functions is shown in Tables 6.4(b) and (c). n y Slope = m Intercept = n b O

n x

FIGURE 6.17  Linearized version of the exponential equation y = bxm. TABLE 6.4(a)  No.

Nonlinear equation

Linear form

Relationship to ˆy= a + bxˆ

Values for least squares regression

1.

y = cxm

n(y) = mn(x) + n(c)

yˆ = n(y), xˆ = n(x) b = m, a = n(c)

n(xi) and n(yi)

2.

y = c emx

n(y) = mx + n(c)

yˆ = n(y), xˆ = x b = m, a = n(c)

xi and n(yi)

3.

y = c 10mx

log(y) = mx + log c

yˆ = log(y), xˆ = x b = m, a = log(c)

xi and n(yi)

4.

y=

1 mx + c

1 = mx + c y

yˆ =

1, =x xˆ y

xi and

1 yi

b = m, a = c 5.

6.

ANMF.CH06_4PP.indd 430

y=

mx c+x

xyc = d Gas equation

1 c 1 = + y mx m

= log y

1 1 log d − log x c c

yˆ =

1 1 , xˆ = y x

b=

c 1 , a= m m

yˆ = log y , xˆ = log x

1 1 and xi yi

log xi and log yi

1 1 a = log d , b = − c c

3/1/2023 2:21:24 PM

Curve Fitting, Regression, and Correlation • 431

No.

Nonlinear equation

Linear form

Relationship to ˆy= a + bxˆ

Values of least squares regression

7.

y = cdx

log y = log c + x log d

yˆ − log y , xˆ = x a = log c, b = log d

xi and log yi

8.

y= c + d x

y= c + dxˆ where xˆ = x

yˆ = y and xˆ = x a = c and b = d

x i and yi

The curves in Figure 6.18 may be used as guides to some of the simpler variable transformations. TABLE 6.4(b)  Graphical Representation of Functions Relationship

Method Plot

y = ax + b

y vs. x on linear paper

Graphical Representation

y Slope = a b x

0 y = axb

log y vs. log x on loglog paper

log y

Slope = b log a log x

log x = 0 or x = 1.0 y = aebx

log y vs. x on semilog paper

log y Slope = b log e log a x

0

y=

x a + bx

1 1 vs. on linear paper y x

Slope = a Extrapolated Extrap a olated b 0

y = a + bx + cx2

y − y1 vs. x on linear paper x − x1

y  y1 x  x1

Slope = c b + cx1

0

ANMF.CH06_4PP.indd 431

x

3/1/2023 2:21:26 PM

432 • Applied Numerical Methods Using MATLAB

y y

c

y = cue–box

y = cam y=c+d

a

a x

x

y

y

y = a + b[log(x)] y = a + bx

2

a

a x

y

x

y

a

a

y = a + box + cx 2 log(y) = a + box + cx 2

y = a + box + cx2 + dx3 log(y) = a + box + cx2 + dx3

x

x

FIGURE 6.18  Nonlinear data curves.

ANMF.CH06_4PP.indd 432

3/1/2023 2:21:26 PM

Curve Fitting, Regression, and Correlation • 433

TABLE 6.4(c)  Graphical Representation of Functions Relationship y =

Method Plot

Graphical Representation

x − x1 vs. x on linear y − y1

x1 +c a + bx

x = x1 y = y1

paper

Slope = a + bx1 x

0

y = aebx +bx

log  

2

( )

y 1−(x − x1 ) y1

 vs. x on 

Slope = c log e

semilog paper

b + cx1 log e x 0

 1  log   vs. x on 1− y  semilog paper

y = 1 – e–bx

Slope = b x 0

y= a +

b x

y vs.

1 on linear paper x

y Slope = b a 1 x

y=a+ b x

x vs.

y

x on linear paper

Slope = b a

x

EXAMPLE 6.4 Fit y = cxm (power function) to the data in Example 6.1 using a logarithmic transformation. Solution: The data can be set up in tabular form as shown in Table 6.5. TABLE 6.5 

ANMF.CH06_4PP.indd 433

i

xi

yi

log xi

log yi

(log xi)2

(log xi)(log yi)

1 2 3 4 5 6 7 8

10 20 30 40 50 60 70 80

24 68 378 552 608 1218 831 1452

1.0000 1.3010 1.4771 1.6021 1.6990 1.7782 1.8451 1.9031

1.3802 1.8325 2.5775 2.7419 2.7839 3.0856 2.9196 3.1620

1.0000 1.6927 2.1819 2.5666 2.8865 3.1618 3.4044 3.6218

1.3802 2.3841 3.8073 4.3928 4.7298 5.4867 5.3870 6.0175



360

5131

12.606

20.483

20.516

33.585

3/1/2023 2:21:27 PM

434 • Applied Numerical Methods Using MATLAB The means are computed as Σ log x 12.606 = = 1.5757 n 8 Σ log y 20.483 = y = = 2.5604 n 8 The slope and the intercept are then calculated using Equations (6.13), (6.14), (6.15), and (6.16). = x



SSxy nΣ(log x i )(log y i ) − (Σ log x i )(Σ log y i ) = SSxx nΣ log x 2i − (Σ log x i )2 8(33.585) − (12.606)(20.483) = = 2.0055 8(20.516) − (12.606)2 = b



a= y − bx = 2.5604 − 2.0055(1.5757) = −0.5997



The least-square fit is

log y = –0.5997 + 2.0055 log x

The fit along with the data is shown in Figure 6.19. Transforming to the original coordinates, we have c = 10–(0.5997) = 0.2514  and  m = 2.0055



Hence the least-squares fit is y = 0.2514 x2.0055



The estimated values of the force are shown in Table 6.6. TABLE 6.6  x

10

20

30

40

50

60

70

80

y

24

68

378

552

608

1218

831

1452

Estimated y

25.4604

102.2306

230.5324

410.4843

642.1694

925.6517

1260.983

1648.209

A graph of the numerical and analytical solutions was generated as shown in Figure 6.19 with the following commands in MATLAB: >> x=[10 20 30 40 50 60 70 80]; >> y=[1 1.5 1.8 2.4 4.1 6.5];

ANMF.CH06_4PP.indd 434

3/1/2023 2:21:28 PM

Curve Fitting, Regression, and Correlation • 435

1800 analytical numerical

1600 1400 1200

y

1000 800 600 400 200 0 10

20

30

40

x

50

60

70

80

FIGURE 6.19  Plot of x versus y. The curve was drawn with the given data, whereas the points were determined numerically based on the fitted curve.

>> yp=[1.1762 1.4040 1.8019 2.4675 3.1668 7.4322]; >> yp=[25.4604 102.2306 230.5324 410.4843 642.1694 925.6517 1260.983 1648.209]; >> plot(x,y,’o’,x,yp) >>grid on >>xlabel(‘x’),ylabel(‘y’) >>legend(‘analytical’,’numerical’)

6.11 POLYNOMIAL REGRESSION The least-squares procedure described in earlier sections can be readily extended to fit the data to a higher-order polynomial. Consider a secondorder polynomial or quadratic: y = a + bx + cx2 + e



(6.31)

The sum of the squares of the residuals is

= Sr

n

∑ (y i =1

i

− a − bx i − cx 2i )2 (6.32)

To generate the lease-squares fit, we take the first derivative of Equation (6.32) with respect to each of the unknown coefficients of the polynomial.

ANMF.CH06_4PP.indd 435

n ∂Sr =−2∑ (y i − a − bx i − cx 2i ) ∂a i =1

3/1/2023 2:21:28 PM

436 • Applied Numerical Methods Using MATLAB



n ∂Sr =−2∑ x i (y i − a − bx i − cx 2i ) (6.33) ∂b i =1



n ∂Sr =−2∑ x 2i (y i − a − bx i − cx 2i ) ∂c i =1

Equation (6.33) is set equal to zero and rearranged to obtain the following set of normal equations: n  n   n  na +  ∑ x i  b +  ∑ x 2i  c = yi ∑ = i 1 i1 =   i 1= 



n  n   n 2  n 3 x i y i (6.34) ∑  ∑ xi  a +  ∑ xi  b +  ∑ xi  c = = i 1 i1=  i1 =   i 1= 



n  n 2  n 3  n 4 x a x b x c x 2i y i + + = ∑ ∑ i  ∑ i  ∑ i  = i 1 i1 =  i1 =   i 1= 

Equation (6.34) is all linear equations in three unknowns: a, b, and c. These coefficients a, b, and c can be determined directly from the observed data. The above procedure can be easily extended to an mth order polynomial as in

y = a + bx + cx2 + dx3 + … + zxm + e

(6.35)

Hence, the determination of the coefficients of an mth order polynomial is equivalent to solving a system of (m + 1) simultaneous linear equations. The standard error in this case is given by

Sy / x =

Sr (6.36) n − (m + 1)

The coefficient of determination, r2, can be computed for a polynomial regression with Equation (6.25). 6.11.1  Polynomial Fit In matrix notation, Equation (6.34) can be written as

ANMF.CH06_4PP.indd 436

Ax = B

(6.36a)

3/1/2023 2:21:28 PM

Curve Fitting, Regression, and Correlation • 437

where

n

A kj = ∑ f j (x i )f k (x i ) i =1

n

Bk = ∑ f k (x i )y i (6.36b)

and

i =1

Equation (6.36a) is known as the normal equations of the least-squares fit. Note that the coefficient matrix is symmetric, that is, Akj = Ajk A commonly used linear form is a polynomial. If the degree of polynomial is m – 1, we have n

f (x) = ∑ a j x j−1 ,  j = 1, 2, …, n



(6.36c)

i =1

Hence, Equation (6.36b) becomes n

A kj = ∑ x ij+ k −2



i =1 n

Bk = ∑ x ik −1 y i (6.36d)

and

i =1

or

 n  Σx i A=    m−1 Σx i

Σx 2i

Σx 2i  Σx mi

Σx 3i  Σx mi +1

 Σx mi    Σx mi +1     −2   Σx 2m i 

 Σy i   Σx y  i i   B= (6.36e)     m−1  Σx i y i 

and

where ∑ stands for

Σx i

n



.

i =1

The normal equations tend to become progressively ill-conditioned with increasing m. However, generally low-order polynomials are used in curve fitting and polynomials of high-order are not favored.

ANMF.CH06_4PP.indd 437

3/1/2023 2:21:29 PM

438 • Applied Numerical Methods Using MATLAB 6.11.2  MATLAB Built-in Functions for Polynomial Fit y = interpl1 (x data, y data, x, method) returns the value of the intercept y at point x according to the method specified: method = ‘linear’ uses linear interpolation between adjacent data points (it is default); method = ‘sphere’ carries out cubic spline interpolation. If x is an array, y is computed for all elements of x. a = polyfit (x data, y data, m) returns the coefficients of a polynomial of degree m that fits the data points in the least-squares sense. y = polyval (a, x) evaluates a polynomial defined by its coefficients a at point x. If x is an array, y is computed for all elements of x. s = std(x) returns the standard deviation of the elements of array x. If x is a matrix, s is computed for each column of x. xbar = mean(x) computes the mean value of the elements of x. If x is a matrix, xbar is computed for each column of x. EXAMPLE 6.4a The relative density r of air was measured at various altitudes h. The observations were: h (km)

0

1.5

3.1

4.5

6.1

7.6

9.1

ρ

1

0.85

0.74

0.63

0.53

0.45

0.37

Use a quadratic least-squares fit with MATLAB built-in function polyfit to determine the relative air density at h = 10 km. Solution: Hence, the quadratic least-square fit is r = 0.9942 – 0.0923h + 0.002647h2. The relative air density at h = 10 m is 0.33567. Trial>> format short e Trial>> xdata=[0 1.5 3.1 4.5 6.1 7.6 9.1]; Trial>> ydata=[1 0.85 0.74 0.63 0.53 0.45 0.37]; Trial>> a=polyfit(xdata,ydata,2) a=  2.6476e-003 -9.2334e-002 9.9425e-001

ANMF.CH06_4PP.indd 438

3/1/2023 2:21:29 PM

Curve Fitting, Regression, and Correlation • 439

Trial>> y=polyval(a,10) y=  3.3567e-001 EXAMPLE 6.4b Refer to Example 6.4a. Use a cubic least-squares fit with MATLAB built-in function polyfit to determine the relative air density at h = 10 km. Solution: Hence, the cubic least-square fit is r = 0.9974 – 0.09947h + 0.00477h2 – 0.0001558h3. The relative air density at h = 10 m is 0.32409. Trial>> format short e Trial>> xdata=[0 1.5 3.1 4.5 6.1 7.6 9.1]; Trial>> ydata=[1 0.85 0.74 0.63 0.53 0.45 0.37]; Trial>> a=polyfit(xdata,ydata,3) a=  -1.5579e-004  4.7717e-003 -9.9475e-002  9.9746e-001 Trial>> y=polyval(a,10) y=  3.2409e-001 EXAMPLE 6.5 Fit a second-order polynomial to the data in Table 6.7 and determine the total standard deviation, the standard error of the estimate and the correlation coefficient. TABLE 6.7 

xi

0

1

2

yi

2

8

14 27 41 61

3

4

5

Solution: Tables 6.8(a) and 6.8(b) shows the computations for an error analysis of the quadratic least-squares fit.

ANMF.CH06_4PP.indd 439

3/1/2023 2:21:29 PM

440 • Applied Numerical Methods Using MATLAB

TABLE 6.8(a) 



xi

yi

xiyi

x 2i

x 2i y i

x 3i

x i4

0 1 2 3

2 8 14 27

0 8 28 81

0 1 4 9

0 8 56 243

0 1 8 27

0 1 16 81

4 5

41 61

164 305

16 25

656 1525

64 125

256 625

15

153

586

55

2488

225

979

TABLE 6.8(b) 



xi

yi

(y i − y)2

(y i − a − bx i − cx 2i )2

0 1 2 3 4 5

2 8 14 27 41 61

552.3 306.3 132.3 2.3 240.3 1260.3

0.2500 1.3391 0.6862 0.2951 0.5300 0.1282

15

153

2493.50

3.2286

Hence, the simultaneous linear equations are

 6 15 55   a   153       15 55 225   b =  586   55 225 979   c  2488 

Refer to Appendix C (Cramer’s rule for solving a system of linear algebraic equations). 6 15 55 55 225 3920 ; = C 15 = Here 55 225 979 153 15 55 586 55 225 9800 ; = C1 = 2488 225 979 6 153 55 586 225 9884 ; C2 15 = = 55 2488 979

ANMF.CH06_4PP.indd 440

3/1/2023 2:21:30 PM

Curve Fitting, Regression, and Correlation • 441

6 15 153 55 586 7140 C3 15 = = 55 225 2488 C1 = C C2 = b = C C3 = c = C

Therefore, = a

9800 = 2.5 3920 9884 = 2.5214 3920 7140 = 1.8214 3920

These equations can also be solved to determine the coefficients using MATLAB: These equations can be solved to determine the coefficients. Here, we use MATLAB.

>>A = [6  15  55;  15  55  225;  55  225  979]; >>b = [153;  586;  2488]; >>x = A/b

or

x = 2.5  2.5214  1.8214 a = 2.5, b = 2.5214, and c = 1.8214.

Hence, the least-squares quadratic equation for this problem is

y = 2.5 + 2.5214x + 1.8214x2

Solution using MATLAB built-in function: polyfit >> x=[0 1 2 3 4 5]; >> y=[2 8 14 27 41 61]; >> coefficients=polyfit(x,y,2) coefficients =  1.8214 2.5214 2.5000 For a second-order polynomial, polyfit(x,y,2) will return the coefficients c, b, and a of the best-fit straight line. or a = 2.5, b = 2.5214, and c = 1.8214.

ANMF.CH06_4PP.indd 441

3/1/2023 2:21:30 PM

442 • Applied Numerical Methods Using MATLAB Hence, the least-squares quadratic equation for this problem is y = 2.5 + 2.5214x + 1.8214x2



The estimated values of the force are shown in Table 6.9. TABLE 6.9  x

0

1

2

3

4

5

y

2

8

14

27

41

61

Estimated y

2.5

6.84

14.82

26.44

41.7

60.6

A graph of the numerical and analytical solutions was generated as shown in Figure 6.20 with the following commands in MATLAB: >> x=[0 1 2 3 4 5]; >> y=[2 8 14 27 41 61]; >> yp=[2.5 6.84 14.82 26.44 41.7 60.6]; >> plot(x,y,’o’,x,yp) >>grid on >> xlabel(‘x’),ylabel(‘y’) >> legend(‘analytical’,’numerical’) 70 60 50 40 30 20 10 0

0

0.5

1

1.5

2

2.5

3

3.5

4

4.5

5

FIGURE 6.20  Plot of x versus y. The curve was drawn with the given data, whereas the points were determined numerically based on the fitted curve.

The standard error of the estimate is based on the regression polynomial given ˆ 2 . Here, we have Σ(y − y) by Equation (6.36), where Sr =

ANMF.CH06_4PP.indd 442

= Sy / x

Sr = n − (m + 1)

3.229 = 1.0374 6 − (2 + 1)

3/1/2023 2:21:30 PM

Curve Fitting, Regression, and Correlation • 443

The coefficient of determination is given by Equation (6.25)

= r2

St − Sr 2493.5 − 3.229 = = 0.9987 St 2493.5

where

St = Σ(y i − y)2

and

ˆ 2 Sr = Σ(y i − y)

Therefore, the correlation coefficient = is r = 0.9987 0.99935 . These results show that 99.935% of the original uncertainty has been explained by the model.

6.12 QUANTIFICATION OF ERROR OF LINEAR REGRESSION Noting that the sum of the squares is defined as

= Sr

n

∑ (y i =1

i

− a − bx 1,i − cx 2,i )2 (6.37)

Equation (6.46) is similar to the equation

= St

n

∑ (y i =1

i

− y)2 (6.38)

In Equation (6.38), the squares of the residual represented the squares of the discrepancy between the data and a single estimate of the measure of central tendency (the mean). The squares of the residual represent the squares of the vertical distance between the data and another measure of central tendency (the straight line). If the spread of the points around the line is of similar magnitude along the entire range of data, and the distribution of these points about the line is normal, then the least-squares regression will provide the best estimates of a and b. This is known as the maximum likelihood principle. Also, if these criteria are met, a standard deviation for the regression line can be determined as

Sy / x =

Sr (6.39) n−2

where Sy/x is called the standard error of the estimate.

ANMF.CH06_4PP.indd 443

3/1/2023 2:21:31 PM

444 • Applied Numerical Methods Using MATLAB The difference between the St and Sr quantifies the improvement or error reduction due to describing the data in terms of a straight line rather than as an average value. The difference is therefore normalized to St to give r2=



St − Sr S = 1 − r (6.40) St St

where r2 is called the coefficient of determination and r is the correlation coefficient. For a perfect fit, Sr = 0, and r2 = 1, indicating that the line explains 100% of the variability of the data. For r2 = 0, Sr = St and the fit represents no improvement. An alternative formulation for r is given by





n  n  n  n ∑ (x i y i ) −  ∑ x i  ∑ y i  =i 1 =  i 1=  i 1  r= 2  n 2  n    n 2  n 2   n∑ x i −  ∑ x i    n∑ y i −  ∑ y i   = i 1 =    i 1 =  i 1    i 1= (6.41) n  n  n  ∑ (x i y= i ) −  ∑ x i  ∑ y i  =i 1  i 1=  i 1  = 2  n 2  n    n 2  n 2  ∑ x i −  ∑ x i   ∑ y i −  ∑ y i   = i 1 =    i 1 =  i 1    i 1=

For perfect fit, Sr = 0 and hence r2 = 1. Hence, the closer r2 is to 1, the better the fit. The largest r2 can be 1. The value of St is an indication of how much the data are spread around the mean, and the value of the Sr indicates how much of the data spread is left unaccounted for by the model. Hence, the ratio Sr/ St indicates the fractional variation left unaccounted for by the model. A very good fit accounts for at least 99% of the data variation. This corresponds to r2 ≥ 0.99. EXAMPLE 6.6 Determine (a) the total standard deviation, (b) the standard error of the estimate and (c) the correlation coefficient for the data in Example 6.1. Solution: Table 6.10 shows the data and summation to compute the goodness-of-fit ­statistics. From Example 6.1, we have a = –236.50, b = 19.5083, x = 45 and y = 641.375.

ANMF.CH06_4PP.indd 444

3/1/2023 2:21:31 PM

Curve Fitting, Regression, and Correlation • 445

TABLE 6.10  i

xi

yi

a + bxi

(y i − y)2

(yi – a – bxi)2

1 2 3 4

10 20 30 40

24 68 378 552

–41.4167 153.6667 348.7500 543.8333

381151.8906 328758.8906 69366.3906 7987.8906

4279.3403 7338.7778 855.5625 66.6944

5 6 7 8

50 60 70 80

608 1218 831 1452

738.9167 934.0000 1129.0833 1324.6667

1113.8906 332496.3906 35957.6406 657112.8906

17139.1736 80656.0000 88853.6736 16341.3611



360

5131

5131

1813945.875

215530.583

The standard deviation is given by St n −1 where St is the total sum of the squares of the residuals between the data points and the mean. Sy =



1813945.875 = 476.1746 8 −1 The standard error of the estimate is Hence = Sy

Sr 215530.583 = = 164.1320 n−2 8−2 Since Sy/x < Sy, the linear regression model has merit.

= Sy / x

The coefficient of determination r2 is given by Equation (6.41) or

= r2 = r

St − Sr 1813945.875 − 215530.583 = = 0.8812 St 1813945.875 = 0.8812 0.9387

These results indicate that 93.87% of the original uncertainty has been explained by the linear model.

6.13 MULTIPLE LINEAR REGRESSION Consider a function y which is a linear function of x1 and x2 as in

ANMF.CH06_4PP.indd 445

y = a + bx1 + cx2 + e

(6.42)

3/1/2023 2:21:31 PM

446 • Applied Numerical Methods Using MATLAB Equation (6.42) is quite useful in fitting experimental data where variable being studied is often a function of two other variables. For this twodimensional case, the regression line becomes a plane. The best values of the coefficients are obtained by formulating the sum of the squares of the residuals: = Sr



n

∑ (y i =1

i

− a − bx 1,i − cx 2,i )2 (6.43)

Differentiating Equation (6.43) with respect to each of the unknown coefficients, we get

n ∂Sr =−2∑ (y i − a − bx 1,i − cx 2,i ) ∂a i =1



n ∂Sr =−2∑ x 1,i (y i − a − bx 1,i − cx 2,i ) ∂b i =1



n ∂Sr =−2∑ x 2,i (y i − a − bx 1,i − cx 2,i ) ∂c i =1

The coefficient giving the minimum sum of the squares of the residuals are obtained by setting the partial derivatives equal to zero and expressing the result in matrix form as



n n   a  n    n x x ∑ ∑ 1,i 2,i    ∑ yi    i1  =i 1=i 1  =     n n n n     2 x 1,i x 1,i x 2,i  b  = ∑ x 1,i y i  (6.44) ∑ ∑  ∑ x 1,i =i 1 =i 1 =i 1  i 1    = n n n       n 2 x x x x x y c     ∑ ∑ ∑ ∑ 2,i 1,i 2,i 2,i 2,i i    = =i 1  i 1=i 1  = i 1 

EXAMPLE 6.7 The following data was generated from the equation y = 7 + 3x1 + 4x2. Use multiple linear regressions to fit this data.

ANMF.CH06_4PP.indd 446

x1

0

1

2

1

4

7

2

0

x2

0

2

1

3

5

1

3

4

y

7

18

17

22

39

32

25

23

3/1/2023 2:21:32 PM

Curve Fitting, Regression, and Correlation • 447

Solution: TABLE 6.11 



yi

x1,i

x2,i

2 x 1,i

x1,i x2,i

x 22,i

x2,i yi

x1,i yi

7 18 17 22 39 32 25 23

0 1 2 1 4 7 2 0

0 2 1 3 5 1 3 4

0 1 4 1 16 49 4 0

0 2 2 3 20 7 6 0

0 4 1 9 25 1 9 16

0 36 17 66 195 32 75 92

0 18 34 22 156 224 50 0

183

17

19

75

40

65

513

504

The summations required for Equation (6.44) are computed in Table 6.11 as shown above. Substituting these values in Equation (6.44), we get

 8 17 19   a  183       17 75 40   b = 504  19 40 65   c  513 

which can be solved using MATLAB. Refer to Appendix C (Cramer’s rule for solving a system of linear algebraic equations).

8 17 19 75 40 6180 ; = C 17 = 19 40 65

183 17 19 75 40 43260 ; C1 504 = = 513 40 65

ANMF.CH06_4PP.indd 447



8 183 19 504 40 18540 ; = C2 17 = 19 513 65



8 17 183 75 504 24720 = C3 17 = 19 40 513

3/1/2023 2:21:32 PM

448 • Applied Numerical Methods Using MATLAB



C1 = C C2 = b = C C3 c = = C = a

43260 = 7 6180 18540 = 3 6180 24720 = 4 6180

MATLAB Program:

>>A = [8 17 19; 17 75 40; 19 40 65];



>>B = [183;  504;  513];



>>x = A/B



x = 7;  b = 3  and  c = 4

or

a =7, b = 3, and c = 4.

which is consistent with the original equation from which the data was derived.

6.14 WEIGHTED LEAST-SQUARES METHOD Referring to the Sections 6.3 and 6.4 and assigning weights wi to each error, ei (i = 1, 2, …, n) in Equation (6.4) such that ∑wi = 1. Equation (6.5) can be written as

= Sr

n

∑ w [y i =1

i

i

− (a + bx i )]2 (6.45)

For Sr to be a minimum, we have ∂Sr ∂S = 0 and r = 0 (6.46) ∂a ∂b We obtain the normal equations as



a + bΣw i x i =Σw i y i (6.47)



aΣw i x i + bΣw i x 2i =Σw i x i y i (6.48)

The solution of Equations (6.47) and (6.48) gives the values for a and b. These values give the minimum of Sr with respect to the weight wi.

ANMF.CH06_4PP.indd 448

3/1/2023 2:21:33 PM

Curve Fitting, Regression, and Correlation • 449

Similarly, for a parabolic equation, we need to minimize Sr = Σw i [y i − (a + bx i + cx 2i )]2 (6.49)

where ∑wi = 1.

For Sr to be minimum, we have ∂Sr ∂S ∂S = 0 , r = 0 and r = 0 (6.50) ∂a ∂b ∂c



On simplification, we obtain the following normal equations to determine a, b, and c.

a + bΣw i x i + cΣw i x 2i =Σw i y i (6.51)



aΣw i x i + bΣw i x 2i + cΣw i x 3i =Σw i x i y i (6.52)



aΣw i x 2i + bΣw i x 3i + cΣw i x 4i = Σw i x i y i (6.53)

6.15 ORTHOGONAL POLYNOMIALS AND LEAST-SQUARES APPROXIMATION The previous sections considered the problem of least-squares approximation to fit a collection of data. This method is also applicable for continuous data.

6.16 LEAST-SQUARES METHOD FOR CONTINUOUS DATA Let y = f(x) be a continuous function on [a, b] and it is to be approximated by the nth degree polynomial. y = a0 + a1x + a2x2 + …. + anxn (6.63)



Here the sum of the squares of residuals S is given by

= S



b

a

w(x)[y − (a 0 x + a 2 x 2 + .... + a n x n )]2 dx (6.64)

where w(x) is a suitable weight function.

ANMF.CH06_4PP.indd 449

3/1/2023 2:21:33 PM

450 • Applied Numerical Methods Using MATLAB The necessary conditions for minimum S are given by ∂S ∂S ∂S = = .... = = 0 (6.65) ∂a 0 ∂a 1 ∂a n



Equation (6.65) give the normal equations as b



−2 ∫ w(x)[y − (a 0 + a1 x + a 2 x 2 + .... + a n x n )]dx = 0



−2 ∫ w(x)[y − (a 0 + a1 x + a 2 x 2 + .... + a n x n )]x dx = 0



−2 ∫ w(x)[y − (a 0 + a1 x + a 2 x 2 + .... + a n x n )]x 2 dx = 0 a  



−2 ∫ w(x)[y − (a 0 + a1 x + a 2 x 2 + .... + a n x n )]x n dx = 0 (6.66)

a

b

a

b

b

a

After simplification these equations reduce to b

b

b

b

a

a

a

a

a 0 ∫ w(x)dx + a 1 ∫ xw(x)dx + .... + a n ∫ x n w(x)dx = ∫ w(x)y dx b

b

b

b

a

a

a

a

a 0 ∫ xw(x)dx + a 1 ∫ x 2 w(x)dx + .... + a n ∫ x n+1 w(x)dx = ∫ w(x)xy dx b



b

b

b

a

a

a

2 a 0 ∫ x 2 w(x)dx + a 1 ∫ x 3 w(x)dx + .... + a n ∫ x n+2 w(x)dx = ∫ w(x)x y dx a

b



b



b

b

a

a



n a 0 ∫ x n w(x)dx + a1 ∫ x n+1 w(x)dx + .... + a n ∫ x 2n w(x)dx = ∫ w(x)x y dx (6.67) a

a

Since w(x) and y = f(x) are known, Equation (6.67) form a system of linear equations with (n + 1) unknowns a0, a1, …., an. This system of equations possesses a unique solution. If a0 = a ∗0 , a1 = a 1∗ , …., an = a ∗n is the solution for a0, a1, …., an then the approximate polynomial is given by

y = a ∗0 + a 1∗ x + a ∗2 x 2 + .... + a ∗n x n

EXAMPLE 6.8 Construct a least-squares quadrate approximation to the function f(x) = sin px on [0, 1].

ANMF.CH06_4PP.indd 450

3/1/2023 2:21:34 PM

Curve Fitting, Regression, and Correlation • 451

Solution: The normal equations for P2(x) = a2x2 + a1x + a0 are

1

1

1

0

0

0

1

a 0 ∫ 1dx + a 1 ∫ x dx + a 2 ∫ x 2 dx = ∫ sin p x dx (E.1) 1

1

0

0

0

1

1

a 0 ∫ x dx + a 1 ∫ x 2 dx + a 2 ∫ x 3 dx = ∫ x sin p x dx (E.2) 0

0

1

1

1

1

0

0

0

0

2 a 0 ∫ x 2 dx + a 1 ∫ x 3 dx + a 2 ∫ x 43 dx = ∫ x sin p x dx (E.3)

Performing the integration gives

1 1 2 a 0 + a1 + a 2 = (E.4) 2 3 p 1 1 1 1 a 0 + a1 + a 2 = (E.5) 2 3 4 p p2 −4 1 1 1 a 0 + a 1 + a 2 =3 (E.6) 3 4 5 p

Equations (E.4), (E.5), and (E.6) in three unknowns can be solved to obtain and

= a0

12p 2 − 120 ≈ −0.050465 p3

720 − 60p 2 a1 = −a 2 = ≈ 4.12251 p3

Consequently, the least-squares polynomial approximation of degree 2 for f(x) = sin px on [0, 1] is P2(x) = –4.12251x2 + 4.12251x – 0.050465.

6.17 APPROXIMATION USING ORTHOGONAL POLYNOMIALS In Section 6.19, a function is approximated as a polynomial containing the terms 1, x, x2, .…, xn. These terms are called base functions, since, any function or even discrete data are approximated based on these functions. Here, we assume that the base functions are some orthogonal polynomials f0(x), f1(x), …., fn(x). Let the given function be approximated as

ANMF.CH06_4PP.indd 451

y = a0f0(x) + a1f1(x) + …. + anfn(x) (6.68)

3/1/2023 2:21:35 PM

452 • Applied Numerical Methods Using MATLAB where fi(x) is a polynomial in x of degree i. Then the residue is given by



= S



b

a

w(x)[y − {a 0 f0 (x) + a1 f1 (x) + .... + a n f n (x)}]2 dx (6.69)

For minimum S, the conditions are given by ∂S ∂S ∂S = 0,= 0,....,= 0 (6.70) ∂a 0 ∂a 1 ∂a n



Equation (6.70) yield the following normal equations: b



−2 ∫ w(x)[y − {a 0 f0 (x) + a1 f1 (x) + .... + a n f n (x)}]f0 (x)dx = 0



−2 ∫ w(x)[y − {a 0 f0 (x) + a1 f1 (x) + .... + a n f n (x)}]f1 (x)dx = 0 a  



−2 ∫ w(x)[y − {a 0 f0 (x) + a1 f1 (x) + .... + a n f n (x)}]f n (x)dx = 0 (6.71)

a

b

b

a

After simplification, the ith equation can be written as b

b

a

a



a 0 ∫ w(x)f0 (x)f i (x)dx +a1 ∫ w(x)f1 (x)f i (x)dx + ...



+ a i ∫ w(x)f i2 (x)dx + .... + a n ∫ w(x)f n (x)f i (x)dx = ∫ w(x)y fi (x) dx (6.72)



i = 0, 1, 2, …., n.

b

b

b

a

a

a

A set of polynomial {f0(x), f1(x), …., fn(x)} is said to be orthogonal with respect to the weight function w(x) if



b

a

if i ≠ j 0, (6.73) f i (x)f j (x)w(x)dx =  a 2  ∫b f i (x)w(x)dx, if i = j

Using Equation (6.73), Equation (6.72) can be written as

b

b

a

a

a i ∫ w(x)f i2 (x)dx = ∫ w(x)f i (x)dx ,   i = 0, 1, 2, …., n b

Hence,

ai

∫ w(x)y f (x)dx ,    = ∫ w(x)f (x) dx i

a

b

a

ANMF.CH06_4PP.indd 452

2 i

i = 0, 1, 2, …., n

(6.74)

3/1/2023 2:21:36 PM

Curve Fitting, Regression, and Correlation • 453

From Equation (6.74), we can find the values of a0, a1, …., and the least-squares approximation is obtained by substituting these values in Equation  (6.68). However, the functions f0(x), f1(x), …., fn(x) are unknown. Several orthogonal functions are available in literature. A few of them are given in Table 6.12. Any one of the orthogonal functions can be selected to fit a function dependent on the given problem. TABLE 6.12  Some standard orthogonal polynomials Name

f i(x)

Interval

w(x)

Legendre Leguerre Hermite Chebyshev

Pn(x) Ln(x) Hn(x) Tn(x)

[–1, 1] [0, ∞] (–∞, ∞) [–1, 1]

1 e–x 2

e− x (1 – x2)–1/2

6.18 GRAM-SCHMIDT ORTHOGONALIZATION PROCESS Let fi(x) be a polynomial in x of degree i and {fi(x)} be a given sequence of polynomials. Then the sequence of orthogonal polynomials {f i∗ (x)} over the interval [a, b] with respect to the weight function w(x) can be generated by the following equation i −1

f i∗ (x)= x i − ∑ a ir fr∗ (x) ,   i = 1, 2, …., n



(6.75)

r =0

where the constants are air, and f0∗ (x) = 1. To obtain air, we multiply Equation (6.75) with w(x)f n∗ (x) , 0 ≤ k ≤ i – 1 and integrating over [a, b], we obtain



b

a

f i∗= (x)f n∗ (x)w(x)dx



b

a

x i f n∗ (x)w(x)dx − ∫

b i −1

a

∑a r =0

∗ ir r

f (x)f n∗ (x)w(x)dx (6.76)

Using the property of orthogonal polynomial, Equation (6.76) becomes



b

a

b

x i f n∗ (x)w(x)dx − ∫ a in f n∗2 (x)w(x)dx = 0 a

b

or

a in =

∫ x f (x) w(x) dx ,  0 ≤ n ≤ i – 1 ∫ f (x) w(x) dx a

b

a

ANMF.CH06_4PP.indd 453

i ∗ n ∗2 n

(6.77)

3/1/2023 2:21:36 PM

454 • Applied Numerical Methods Using MATLAB

Hence, the set of orthogonal polynomials {f i∗ (x)} are given by

f0∗ (x) = 1



f i∗ (x)= x i − ∑ a ir fr∗ (x),    i = 1, 2, …., n

i −1

r =0

b

where

∫ x f (x) w(x)dx (6.78) = ∫ f (x) w(x)dx

a ir

a

i ∗ n

b

a

∗2 n

For the discrete data, the integral is replaced by summation. EXAMPLE 6.9 Use the Gram-Schmidt orthogonalization process to find the first two orthogonal polynomials on [–1, 1] with respect to the weight function w(x) = 1. Solution: Let

f0∗ (x) = 1 .

Hence f1∗ (x)= x − a 10 f0∗ (x) 1

where = a 10

x dx ∫= ∫ dx −1 1

0.

−1

or

∗ 1

f (x) = x.

The second orthogonal polynomial is given by

f2∗ (x) = x 2 − a 20 f0∗ (x) − a 21f1∗ (x) 1

where = a 20

x dx 1 ∫= , 3 dx ∫ x .x dx ∫= 0 x dx ∫ 2

−1 1

−1

1

= a 21

2

−1 1

2

−1

Hence, f2∗ (x) = x 2 −

ANMF.CH06_4PP.indd 454

1 1 2 = (3x − 1) 3 3

3/1/2023 2:21:37 PM

Curve Fitting, Regression, and Correlation • 455

Thus, the first two orthogonal polynomials are f2∗ (x) f0∗ (x) = 1, f1∗ (x) = x and =



1 2 (3x − 1) 3

6.19 FITTING A FUNCTION HAVING A SPECIFIED POWER Often the data can be described by a power function with a specified power. For instance, to fit the power function f = cvm with a known value of m, the least-squares criterion is = J



n

∑ (cv i =1

m

− f i )2

To obtain the value of c that minimizes J, we must solve or

∂J = 0. ∂c

n ∂J m = 2∑ v mi ( cv = 0 i − fi ) ∂c i =1

This gives

n

n

m 0 ∑ v 2m i c − ∑ v i fi =

=i 1 =i 1 n

or

c=

∑v i =1 n

m i i

∑v i =1

f

(6.79)

2m i

6.20 FITTING A CUBIC SPRING MODEL For a cubic model of spring force f = k1x + k2x3, there is no constant term and no x2 term. When MATLAB polyfit function polyfit(x, f, 3) is used, we obtain a function of the form f = a0 + a1x + a2x2 + a3x3, where a0, and a2 are nonzero in general. Hence, we cannot use the polyfit function without modification to compute k1 and k2. We can derive expressions for k1 and k2 using the leastsquares method but noting that f(–x) = –f(x) for the cubic spring function, the convenient way is to use the polyfit function with an antisymmetric data set created from the original data.

ANMF.CH06_4PP.indd 455

3/1/2023 2:21:38 PM

456 • Applied Numerical Methods Using MATLAB

6.21 ADDITIONAL EXAMPLE PROBLEMS AND SOLUTIONS EXAMPLE 6.10 Determine the equation to the best fitting exponential curve of the form y = aebx for the data given in Table 6.13(a). TABLE 6.13(a)  x

1

3

5

7

9

y

115

105

95

85

80

Solution: Refer to Table 6.15(b). TABLE 6.13(b)  i

xi

yi

log yi

x 2i

xi log yi

Estimated y

1 2 3 4 5

1 3 5 7 9

102 81 72 53 44

2.0086 1.9085 1.8573 1.7243 1.6434

1 9 25 49 81

2.0086 5.7254 9.2867 12.0699 14.7911

102.6390 83.1643 67.3847 54.5991 44.2396



25

352

9.1421

165

43.8817

352.0265

Given  y = aebx The normal equations are

∑ log yi = 5A + B ∑ xi (E.1)



∑ xi log yi = A ∑ xi + B ∑ x 2i (E.2)

where A = log a and B = b log e. or

.1421 = 5A + B (25)

(E.1)

43.8817 = A (25)+ B(165)

(E.2)

Solving the two normal Equations (E.3) and (E.4), we get

ANMF.CH06_4PP.indd 456

A = 2.0570 and B = –0.0457

3/1/2023 2:21:38 PM

Curve Fitting, Regression, and Correlation • 457

Hence a = antilog of A = 102.0570 = 114.0250 and

b B / log e = =

−0.0457 −0.0457 = = −0.1052 . log e 0.4343

Hence, the fitted equation is y = 114.0250 e–0.1052x. The estimated values of the force are shown in Table 6.14 TABLE 6.14  x

1

3

5

7

9

y

102

81

72

53

44

Estimated y

102.6390

83.1643

67.3847

54.5991

44.2395

A graph of the numerical and analytical solutions was generated as shown in Figure 6.21 with the following commands in MATLAB: >> x=[1 3 5 7 9]; >> y=[102 81 72 53 44]; >> yp=[102.639 83.1643 67.3847 54.5991 44.2395]; >> plot(x,y,’o’,x,yp) >>grid on >> xlabel(‘x’),ylabel(‘y’) >> legend(‘analytical’,’numerical’) 110 100 90 80 70 60 50 40

1

2

3

4

5

6

7

8

9

FIGURE 6.21  Plot of x versus y. The curve was drawn with the given data, whereas the points were determined numerically based on the fitted curve.

ANMF.CH06_4PP.indd 457

3/1/2023 2:21:38 PM

458 • Applied Numerical Methods Using MATLAB EXAMPLE 6.11 For the data given in Table 6.15(a), find the equation to best fitting curve of the form y = abx. TABLE 6.15(a)  x

1

2

3

4

5

y

130

150

175

190

240

Solution: The calculations are shown in Table 6.15(b). TABLE 6.15(b) 



x

y

log y

x2

x log y

ˆy (estimated)

1 2 3 4 5

130 150 175 190 240

2.1139 2.1761 2.2430 2.2788 2.3802

1 4 9 16 25

2.1139 4.3522 6.7291 9.1150 11.9011

129.2062 149.5433 173.0814 200.3144 231.8555

15

885

11.192

55

34.2113

884.0108

The normal equations are obtained as follows:

y = abx (E.1)

Taking logarithms (base 10) on both sides of the above Equation (E.1), we get

log y = log a + x log b

(E.2)

or

Y = A + Bx

(E.3)

where

Y = log y, A = log a and B = log b.

Hence, the normal equations are

∑Y = nA + B∑x (E.4)



∑xY = A∑x + B∑x2 (E.5)

Substituting the values from Table 6.15(a) into Equations (E.4) and (E.5), we have

11.1920 = 5A + 15B

(E.6)



34.2113 = 15A + 55B

(E.7)

ANMF.CH06_4PP.indd 458

3/1/2023 2:21:38 PM

Curve Fitting, Regression, and Correlation • 459

Solving Equations (E.6) and (E.7), we obtain

A = 2.0478  and  B = 0.0635

Hence

a = antilog of A = 102.0478 = 111.6349



b = antilog of B = 100.0635 = 1.1574

Hence the fitted equation is y = 111.6349(1.1574)x. The estimated values of y (denoted by yˆ ) are shown in the last column of Table 6.11(a). The estimated values of the force are shown in Table 6.16. TABLE 6.16  x

1

2

3

4

5

y

130

150

175

190

240

Estimated y

129.2062

149.5433

173.0814

200.3144

231.8555

A graph of the numerical and analytical solutions was generated as shown in Figure 6.22 with the following commands in MATLAB: >> x=[1 2 3 4 5]; >> y=[130 150 175 190 240]; >> yp=[129.2062 149.5433 173.0814 200.3144 231.8555]; >> plot(x,y,’o’,x,yp) >> grid on >> xlabel(‘x’),ylabel(‘y’) >> legend(‘analytical’,’numerical’) 240

220

200

180

160

140

120

1

1.5

2

2.5

3

3.5

4

4.5

5

FIGURE 6.22  Plot of x versus y. The curve was drawn with the given data, whereas the points were determined numerically based on the fitted curve.

ANMF.CH06_4PP.indd 459

3/1/2023 2:21:39 PM

460 • Applied Numerical Methods Using MATLAB EXAMPLE 6.12 For the data given in Table 6.17(a), find the equation to best fitting curve of the form xya = b. TABLE 6.17(a)  x

200

150

100

60

40

10

y

1

1.5

1.8

2.4

4.1

6.5

Solution: See Table 6.17(b). TABLE 6.17(b) 



x

y

log x

log y

(log x)2

(log x)(log y)

ˆy (estimated)

200 150 100 60 40 10

1 1.5 1.8 2.4 4.1 6.5

2.3010 2.1761 2.0000 1.7782 1.6021 1.0000

0 0.1761 0.2553 0.3802 0.6128 0.8129

5.2947 4.7354 4.0000 3.1618 2.5666 1.0000

0 0.3832 0.5105 0.6761 0.9817 0.8129

1.1762 1.4040 1.8019 2.4675 3.1668 7.4322

560

17.3

10.8573

2.2373

20.7585

3.3644

17.4485

Given xya = b

(E.1)

Taking logarithms (to the base 10) on both sides of the above Equation (E.1), we get

log x + a log y = log b

(E.2)

or

log b 1 log x + log y − (E.3) a a

The normal equations are given by

∑Y = 6A + B∑X (E.4)



∑XY = A∑X + B∑X2 (E.5)

where

ANMF.CH06_4PP.indd 460

Y = log y, X = log x, A =

1 log b and B = –1/a. a

3/1/2023 2:21:39 PM

Curve Fitting, Regression, and Correlation • 461

Solving Equations (E.4) and (E.5), we obtain Therefore and

A = 1.4865 and B = –0.6154 a = –1/B = –1/–0.6154 = 1.6250 b = antilog of (aA) = 10(1.6250)(1.4865) = 260.3529

Hence, the fitted equation is xy1.6250 = 260.3529. The estimated values of the force are shown in Table 6.17(c). TABLE 6.17(c)  x

200

150

100

60

40

10

y

1

1.5

1.8

2.4

4.1

6.5

Estimated y

1.1762

1.4040

1.8019

2.4675

3.1668

7.4322

A graph of the numerical and analytical solutions was generated as shown in Figure 6.23 with the following commands in MATLAB: c >> x=[200 150 100 60 40 10]; >> y=[1 1.5 1.8 2.4 4.1 6.5]; >> yp=[1.1762 1.4040 1.8019 2.4675 3.1668 7.4322]; >> plot(x,y,’o’,x,yp) >>grid on >> xlabel(‘x’),ylabel(‘y’) >> legend(‘analytical’,’numerical’) 8 analytical numerical

7 6

y

5 4 3 2 1 0

20

40

60

80

100 x

120

140

160

180

200

FIGURE 6.23  Plot of x versus y. The curve was drawn with the given data, whereas the points were determined numerically based on the fitted curve.

ANMF.CH06_4PP.indd 461

3/1/2023 2:21:39 PM

462 • Applied Numerical Methods Using MATLAB EXAMPLE 6.13 Fit the following data: x

0

2

4

6

y

11

16

19

26

to a straight line by considering that the data (2, 16) and (4, 19) are more significant or reliable with weights 6 and 11 respectively. Solution: Weighted Least-Squares Method Let the straight line be y = a + bx. The normal equations are a∑wi + b∑wixi = ∑wiyi (E.1) and a∑wixi + b∑wi x 2i = ∑wixiyi (E.2) The values in Equations (E.1) and (E.2) are calculated as shown in Table 6.18. TABLE 6.18  x

y

w

wx

wx2

wy

wxy

0 2 4 6

11 16 19 26

1 6 11 1

0 12 44 6

0 24 176 36

11 96 209 26

0 192 836 156

19

62

236

342

1184

Total

The normal equations are

19a + 62b = 342

(E.3)

and

62a + 236b = 1184

(E.4)

The solution of Equations (E.3) and (E.4) gives a = 11.4125 and b = 2.0188 Hence,

ANMF.CH06_4PP.indd 462

y = 11.4125 + 2.0188x

3/1/2023 2:21:39 PM

Curve Fitting, Regression, and Correlation • 463

Estimation of Error x

y

w

Predicted y

Absolute error

(Absolute error)2

0 2 4 6

11 16 19 26

1 6 11 1

11.4125 15.4500 19.4875 23.5250

0.4125 0.5500 0.4875 2.4750

0.1702 0.3025 0.2377 6.1256

Sum of squares of errors

6.8329

A graph of the numerical and analytical solutions was generated as shown in Figure 6.24 with the following commands in MATLAB: >> x=[0 2 4 6]; >> y=[11 16 19 26]; >> yp=[11.4125 15.4500 19.4875 23.5250]; >> plot(x,y,’o’,x,yp) >>grid on >>xlabel(‘x’),ylabel(‘y’) >>legend(‘analytical’,’numerical’) 26 analytical numerical

24 22

y

20 18 16 14 12 10

0

1

2

3 x

4

5

6

FIGURE 6.24  Plot of least-squares fit (line).

EXAMPLE 6.14 Consider the Example 6.14 with the modified weights 30 and 50 instead of 6 and 11.

ANMF.CH06_4PP.indd 463

3/1/2023 2:21:40 PM

464 • Applied Numerical Methods Using MATLAB Solution: The modified calculations are shown in Table 6.19 TABLE 6.19 

x

y

w

wx

wx2

wy

wxy

0 2 4 6

11 16 19 26

1 30 50 1

0 60 200 6

0 120 800 36

11 480 950 26

0 960 3800 156

82

266

956

1467

4916

Total

The normal equations are and

82a + 266b = 1467 266a + 956b = 4916

(E.1) (E.2)

The solution of Equations (E.1) and (E.2) gives

a = 12.4144 and b = 1.6881

Hence,

y = 12.4144 + 1.6881x Estimation of Error x

y

w

Predicted y

Absolute error

(Absolute error)2

0 2 4 6

11 16 19 26

1 30 50 1

12.4144 15.7905 19.1666 22.5427

1.4144 0.2096 0.1666 3.4573

2.0004 0.0439 0.0277 11.9530

Sum of squares of errors

14.0250

A graph of the numerical and analytical solutions was generated as shown in Figure 6.25 with the following commands in MATLAB: >> x=[0 2 4 6]; >> y=[11 16 19 26]; >>yp=[12.4144 15.7905 19.1666 22.5427]; >> plot(x,y,’o’,x,yp) >>grid on >>xlabel(‘x’),ylabel(‘y’) >>legend(‘analytical’,’numerical’)

ANMF.CH06_4PP.indd 464

3/1/2023 2:21:40 PM

Curve Fitting, Regression, and Correlation • 465

26 analytical numerical

24 22

y

20 18 16 14 12 10

0

1

2

3 x

4

5

6

FIGURE 6.25  Plot of least-squares fit (line).

It is noted that when the weights on x = 2 and x = 4 are increased then the absolute error in y are reduced at these points, but, the sum of squares of errors is increased due to the less importance of the data (0, 11) and (6, 26). EXAMPLE 6.15 Apply the least-squares method to estimate the value of c for the progressive damping model f = cv2. The data is given below: v m/s

0

0.05

0.13

0.26

0.46

f (kN)

0

0.05

0.15

0.44

1.0

Solution: Here the power is m = 2. Equation (6.79) becomes v

f

v2f

v4

0 0.05

0 0.05

0 0.000125

0 6.25 × 10–6

0.13 0.26 0.46

0.15 0.44 1.0

0.002535 0.029744 0.2116

0.000286 0.00457 0.044775

0.244004

0.049636

Sum

ANMF.CH06_4PP.indd 465

3/1/2023 2:21:40 PM

466 • Applied Numerical Methods Using MATLAB



= c

5 v 2i f i 0.244004 = = 4.91585 ∑ 4 0.049363 i =1 v i

The best-fit value of c is 4.9159. EXAMPLE 6.16 Use the least-squares method to estimate the value of c for progressive damping model f = cv2. The data is given below: v m/s

0

0.05

0.13

0.26

0.46

f (kN)

0

0.05

0.15

0.44

1.0

With the MATLAB program to find the value of c. Solution: The following MATLAB program will produce the results. >> v=[0,0.05,0.13,0.26,0.46]; >> f=[0,0.05,0.15,0.44,1.0]; >> vp=[0:0.01:0.5]; >> c=sum(v.^2.∗f)/sum(v.^4) c=  4.9158 The best-fit value of c = 4.9158. EXAMPLE 6.17 Use MATLAB to compute the quality-of-fit indicators Sr, St, and r2 for the function in Example 6.16. Solution: Continuing the MATLAB script file given in Example 6.16, we have >> mu=mean(f); >> ffit=c∗v.^2; >> Sr=sum((ffit-f).^2)

ANMF.CH06_4PP.indd 466

3/1/2023 2:21:40 PM

Curve Fitting, Regression, and Correlation • 467

Sr =  0.0191 >> St=sum((f-mu).^2) St =  0.6807 >> r2=1-Sr/St r2 =  0.9719 The results are: Sr = 0.0191, St = 0.6807, and r2 = 0.9719, which indicates a good fit. EXAMPLE 6.18 Use the least-squares method to estimate the values of k1 and k2 for the following cubic model of spring force: f = k1x + k2x3. The data is given in Table 6.20. TABLE 6.20  x(m)

0

0.015

0.03

0.045

0.06

0.075

0.09

f(N)

0

23

63

81

130

161

215

Solution: We append the negative values (–x, –f) to the original data set (x, f). The following MATLAB program will perform the computations. >> x1=[15:15:90]∗0.001; >> x=[-x1,0,x1] >> f1=[23,63,81,130,161,215] >> f=[-f1,0,f1] >> p=polyfit(x,f,3) >> xp=[0:0.1:90]∗0.001; >> fp=[polyval(p,xp) p=  1.0e+004 ∗  6.9757 -0.0000 0.1811 0.0000

ANMF.CH06_4PP.indd 467

3/1/2023 2:21:40 PM

468 • Applied Numerical Methods Using MATLAB Hence, the computed coefficients are p = [6.9757 × 104, 0, 1.8110 × 103, 0], and k1 = 1811 and k2 = 6.9757 × 104.

6.22 SUMMARY In this chapter, we have reviewed the relationship between two variables in two ways: (1) by using the regression analysis, and (2) by computing the correlation coefficient. It was shown that the regression model can be used to evaluate the magnitude of change in one variable due to a certain change in another variable. The regression model also helps to predict the value of one variable for a given value of another variable. The correlation coefficient shows how strongly two variables are related. It does not, however, provide any information about the size of change in one variable as a result of a certain change in the other variable.

EXERCISES NOTE

In addition to solving the following exercises through analytical solutions or long-hand numerical computations, try solve them using user-defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable.

6.1. Table 6.21 gives information on the monthly incomes (in hundreds of dollars) and monthly telephone bills (in dollars) for a random sample of 10 households. TABLE 6.21 



Income

16

45

35

31

30

14

40

15

36

40

Telephone bill

36

140

171

70

94

25

159

41

78

98

Use least-squares regression to determine the coefficients a and b in the function y = a + bx that best fits the data.

6.2. The following Table 6.22 lists the annual incomes (in thousands of dollars) and annual life insurance policy bills (in thousands of dollars) for six persons: TABLE 6.22 

ANMF.CH06_4PP.indd 468

Annual income

47

54

26

38

62

20

Life insurance

250

300

100

150

500

75

3/1/2023 2:21:40 PM

Curve Fitting, Regression, and Correlation • 469

a) Find the regression line y = a + bx with annual income as an independent variable and amount of life insurance policy as a dependent variable. b) Determine the estimated value of life insurance of a person with an annual income of $50,000. 6.3. Find the least-squares regression line for the data on annual incomes and food expenditures of the seven households given in Table 6.23. Use income as an independent variable and food expenditure as a dependent variable. All data is given in thousands of dollars. TABLE 6.23  Income: x

35

50

22

40

16

30

25

Expenditure: y

9

15

6

11

5

8

9

6.4. Table 6.24 gives data on age and crown-rump length for fetuses. Use least-squares regression to determine the coefficients a and b in the function y = a + bx that best fits the data: TABLE 6.24  x

10

10

13

13

18

19

19

23

25

28

y

66

66

108

106

160

165

176

227

234

279

6.5. The following data in Table 6.25 refers to the number of hours that 10 students studied for a math test and their scores on the test: TABLE 6.25  Hours studied

1

17

22

12

7

4

14

10

9

4

Test score

21

83

90

60

45

38

74

66

59

32

a) Find the equation of the least-squares line that approximates the regression of the test scores on the number of hours studied. b) Determine the average test score of a person who studied 15 hours for the test.

ANMF.CH06_4PP.indd 469

3/1/2023 2:21:40 PM

470 • Applied Numerical Methods Using MATLAB 6.6. Table 6.26 shows the first two grades, denoted by x and y respectively, of 10 students on two midterm examinations in applied statistics. a) Find the least-squares regression line of y on x. TABLE 6.26  Grade on first midterm examination (x)

60

50

80

80

70

60

100

40

90

70

Grade on second midterm examination (y)

80

70

70

90

50

80

95

60

80

60

6.7. Table 6.27 shows ages x and systolic blood pressure y of 12 men. a) Determine the least-squares regression equation of y on x b) Estimate the blood pressure of a man whose age is 45 years. TABLE 6.27  Age (x)

56

42

72

36

63

47

55

49

38

42

68

60

Blood pressure (y)

147

125

160

118

149

128

150

145

115

140

152

155

6.8. Table 6.28 shows the respective weight x and y of a sample of 12 fathers and their oldest sons. Find the least-squares regression line of y on x. TABLE 6.28  Weight of father, x (kg)

65

63

67

64

68

62

70

66

68

67

69

71

Weight of son, y (kg)

68

66

68

65

69

66

67

65

70

67

68

70

6.9. Find the least-squares regression line for the data on annual incomes and food-expenditures of seven households given in Table 6.29. Use income as independent variable and food expenditure as a dependent variable. The income and food-expenditures are in thousands of rupees. TABLE 6.29 

ANMF.CH06_4PP.indd 470

Income x

35

49

21

29

15

28

25

Food expenditure y

9

15

7

10

5

8

8.5

3/1/2023 2:21:40 PM

Curve Fitting, Regression, and Correlation • 471

6.10. A car manufacturing company wanted to investigate how the price of one of its car models depreciates with age. The company took a sample of eight cars of this model and collected the following information on the ages (in years) and prices (in hundreds of dollars) of these cars as shown in Table 6.30. TABLE 6.30  Age

8

3

6

9

2

5

6

3

Price

16

74

40

19

120

36

33

86

a) Find the regression line yˆ = a + bx with price as a dependent variable and age as independent variable. b) Give a brief interpretation of the values of a and b calculated in part (a). c) Predict the price of a 7-year old car of this model. d) Estimate the price of a 4-year old car of this model.

For Exercises 6.11 through 6.20 do the following:



Fit a least-squares regression line of the form yˆ = a + bx for the data given in Tables 6.21 to 6.30 respectively. Assume x as the independent variable and y as the dependent variable. a) Give a brief interpretation of the values of a and b calculated in yˆ = a + bx . b) Compute the standard deviation of the sample errors, se. c) Compute the error sum of squares, SSE. d) Compute the total sum of squares, SST. e) Compute the regression sum of squares, SSR. f) Compute the coefficient of determination, r2. g) Compute the correlation coefficient, r.

6.11. Use the data given in Table 6.21. 6.12. Use the data given in Table 6.22.

ANMF.CH06_4PP.indd 471

3/1/2023 2:21:40 PM

472 • Applied Numerical Methods Using MATLAB 6.13. Use the data given in Table 6.23. 6.14. Use the data given in Table 6.24. 6.15. Use the data given in Table 6.25. 6.16. Use the data given in Table 6.26. 6.17. Use the data given in Table 6.27. 6.18. Use the data given in Table 6.28. 6.19. Use the data given in Table 6.29. 6.20. Use the data given in Table 6.30. 6.21. Fit y = bxm (power function) in Exercise 6.1 using a logarithmic transformation. 6.22. Fit y = bxm (power function) to the data in Exercise 6.2 using a logarithmic transformation. 6.23. Fit y = bxm (power function) to the data in Exercise 6.3 using a logarithmic transformation. 6.24. Fit y = bxm (power function) to the data in Exercise 6.4 using a logarithmic transformation. 6.25. Fit y = bxm (power function) to the data in Exercise 6.5 using a logarithmic transformation. 6.26. Determine the coefficient of the polynomial y = a + bx + cx2 that best fits the data given in the following table.



x

1

3

5

7

10

y

2.1

5.1

5.45

6.12

6.62

Determine the standard error of the estimate and correlation coefficient.

6.27. The following data were collected in an experiment to study the relationship between shear strength in kPa (y) and curing temperature in ºC (x).

ANMF.CH06_4PP.indd 472

x

1.38

1.40

1.46

1.48

1.52

1.53

y

5.392

5.612

5.671

5.142

4.481

4.129

3/1/2023 2:21:41 PM

Curve Fitting, Regression, and Correlation • 473

a) Fit a least-squares quadratic model of the form y = a + bx + cx2 to the above data. b) Using the equation, compute the residuals. c) Compute the error sum of squares and total sum of squares. d) Compute the error variance estimate. e) Compute the coefficient of determination. 6.28. The following data were collected in an experiment to study the relationship between the number of kilograms of fertilizer (x) and the yield of tomatoes in bushels (y). x

5

10

30

40

50

y

32

42

54

50

42

a) Fit a least-squares quadratic model of the form y = a + bx + cx2 to the above data. b) Using this equation, compute the regression sum of squares n

∑ (yˆ i =1

i

− y)2 . n

c) Compute the error sum of squares ∑ (y i − yˆ i )2 and total sum of n

i =1

squares ∑ (y i − y)2 . i =1

d) Compute the error variance estimate (b) + (c). e) Compute the coefficient of determination, r2. 6.29. Fit a least-square parabola y = a + bx + c2 to the following data:



x

0

1

2

3

4

5

6

y

2.4

2.1

3.2

5.6

9.3

14.6

21.9

Determine the coefficient of determination.

6.30. The following table gives the data collected in an experiment to study the relationship between the stopping distance d(m)_ of an automobile traveling at speeds v(km/hr) at the instant the danger is sighted.

ANMF.CH06_4PP.indd 473

3/1/2023 2:21:41 PM

474 • Applied Numerical Methods Using MATLAB a) Fit a least-squares parabola of the form d = a + bv + cv2 to the data. b) Determine the coefficient of determination. Speed v(km/hr)

32

48

64

80

96

112

Stopping distance d(m)

16.5

27.5

19.5

24.5

29.3

34.2

6.31. Use multiple linear regression fit of the form y = a + bx1 + cx2 for the following data: x1



0

1

1

2

2

3

3

4

4

x2

0

1

2

1

2

1

2

1

2

y

15

18

12.8

25.7

20.4

35

30

45.3

40.1

Compute the coefficients, the standard error of the estimate, and the correlation coefficient.

6.32. Use multiple linear regression fit of the form y = a + bx1 + cx2 for the following data:



x1

0

0

1

2

1

1.5

3

3

–1

x2

0

1

0

1

2

1

2

3

–1

y

1

6

4

–4

–2

–1.5

–12

–15

17

Compute the coefficients, the standard error of estimate, and the correlation coefficient.

6.33. Use multiple linear regression fit of the form y = a + bx1 + cx2 for the following data:



x1

0

0

1

1

2

3

x2

0

1

0

1

2

0.5

2

3

4

1

y

3

8

7

12

21

15

13

26

27

24

0

2

1

4

Compute the coefficients, the standard error of estimate, and the correlation coefficient.

6.34. Use multiple linear regression fit of the form y = a + bx1 + cx2 for the following data:

ANMF.CH06_4PP.indd 474

3/1/2023 2:21:41 PM

Curve Fitting, Regression, and Correlation • 475



x1

0

0

1

1

2

0

1

2

1

1

x2

0

1

0

1

0

2

2

1

3

1

y

23

15

19

10

15

5

0

5

–10

0

Compute the coefficients, the standard error of estimate, and the correlation coefficient.

6.35. Use multiple linear regression fit of the form y = a + bx1 + cx2 for the following data: x1



0

0

1

1

2

0

1

2

1

3

x2

0

1

0

1

0

2

2

1

3

1

y

29

10

23

4

19

–10

–16

–2

–36

–8

Compute the coefficients, the standard error of estimate, and the correlation coefficient.

6.36. For the data given in Table 6.31, find the equation to the best fitting exponential curve of the form y = aebx. TABLE 6.31  x

1

2

3

4

5

y

100

90

80

75

70

6.37. For the data given in Table 6.32, find the equation to the best fitting exponential curve of the form y = aebx. TABLE 6.32  x

2

3

4

5

6

y

3.8

5.8

7.8

8.8

9.8

6.38. For the data given in Table 6.33, find the equation to the best fitting exponential curve of the form y = aebx. TABLE 6.33 

ANMF.CH06_4PP.indd 475

x

2.2

3

4

6

7

y

31

38

45

68

84

3/1/2023 2:21:41 PM

476 • Applied Numerical Methods Using MATLAB 6.39. For the data given in Table 6.34, find the equation to the best fitting exponential curve of the form y = abx. TABLE 6.34  x

1

2

3

4

5

y

22

8

3

1

0.35

6.40. For the data given in Table 6.35, find the equation to the best fitting exponential curve of the form y = abx. TABLE 6.35  x

2

4

6

8

10

y

3

13

32

57

91

6.41. For the data given in Table 6.36, find the equation to the best fitting exponential curve of the form y = abx. TABLE 6.36  x

1

3

5

7

9

y

3

2

1.3

0.72

0.43

6.42. For the data given in Table 6.37, find the equation to the best fitting exponential curve of the form y = xya = b. TABLE 6.37 x

190

134

89

55

37

8.9

y

0.97

1.14

1.32

1.63

1.92

3.5

6.43. For the data given in Table 6.38, find the equation to the best fitting exponential curve of the form y = xya = b. TABLE 6.38 x

2

3

5

7

9

11

y

1.25

1.21

1.16

1.14

1.11

1.10

6.44. For the data given in Table 6.39, find the equation to the best fitting exponential curve of the form y = xya = b.

ANMF.CH06_4PP.indd 476

3/1/2023 2:21:41 PM

Curve Fitting, Regression, and Correlation • 477

TABLE 6.39 x

232

178

99

66

51

y

1.1

1.3

1.8

2.2

2.5

6.45. a) Find a nonlinear relationship of the form y = a + b log x for the data given in Table 6.40. b) Determine the linear correlation coefficient. TABLE 6.40  x

1.2

4.7

8.3

20.9

y

0.6

5.1

6.9

10

6.46. a) Fit the following data to a straight line y = a + bx by considering the weights as given in the table. b) Compute the sum of squares of errors. x

1

17

22

12

7

4

14

10

9

4

y

21

83

90

60

45

38

74

66

59

32

w

5

1

7

1

1

8

1

11

1

4

6.47. a) Fit the following data to a straight line y = a + bx by considering the weights as given in the table. b) Compute the sum of squares of errors. x

16

45

35

31

30

14

40

15

36

40

y

50

134

107

95

90

44

120

47

110

120

w

1

6

1

3

1

7

1

11

1

16

6.48. a) Fit the following data to a straight line y = a + bx by considering the weights as given in the table. b) Compute the sum of squares of errors. x

47

54

26

38

62

20

y

250

300

100

150

500

75

w

1

5

1

3

1

7

6.49. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table.

ANMF.CH06_4PP.indd 477

3/1/2023 2:21:41 PM

478 • Applied Numerical Methods Using MATLAB b) Compute the sum of squares of errors. x

35

50

22

40

16

30

25

y

9

15

6

11

5

8

9

w

1

2

1

2

1

3

4

6.50. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table. b) Compute the sum of squares of errors. x

10

10

13

13

18

19

19

23

25

28

y

66

66

108

106

160

165

176

227

234

279

w

2

1

3

1

4

1

5

1

6

1

6.51. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table. b) Compute the sum of squares of errors. x

1

17

22

12

7

4

14

10

9

4

y

21

83

90

60

45

38

74

66

59

32

w

1

2

1

3

1

2

1

4

1

5

6.52. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table. b) Compute the sum of squares of errors. x

1

17

22

12

7

4

y

21

83

90

60

45

38

w

5

1

7

1

1

8

6.53. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table. b) Compute the sum of squares of errors. x

16

45

35

31

30

y

50

134

107

95

90

w

1

6

1

3

1

6.54. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table.

ANMF.CH06_4PP.indd 478

3/1/2023 2:21:41 PM

Curve Fitting, Regression, and Correlation • 479

b) Compute the sum of squares of errors. x

47

54

26

38

62

20

y

250

300

100

150

500

75

w

1

4

1

2

1

7

6.55. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table. b) Compute the sum of squares of errors. x

35

50

22

40

y

9

15

6

11

w

2

3

4

2

6.56. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table. b) Compute the sum of squares of errors. x

10

10

13

13

18

y

66

66

108

106

160

w

5

2

3

7

4

6.57. a) Fit the following data to a straight line y = a + bx by considering the weights given in the table. b) Compute the sum of squares of errors. x

1

17

22

12

7

y

21

75

96

60

45

w

1

2

1

3

1

6.58. Construct a least-squares quadratic approximation to the function y = ex on [0, 1]. 6.59. Construct a least-squares quadratic approximation to the function y = x ln x on [1, 3]. 6.60. Construct a least-squares quadratic approximation to the function y = x3 on [0, 2]. 6.61. Construct a least-squares quadratic approximation to the function y = 1 on [1, 3]. x

ANMF.CH06_4PP.indd 479

3/1/2023 2:21:41 PM

480 • Applied Numerical Methods Using MATLAB 6.62. Construct a least-squares quadratic approximation to the function y = x2 + 3x + 2 on [0, 1]. 6.63. Use the Gram-Schmidt orthogonalization process to construct ϕ0(x), ϕ1(x), ϕ2(x), and ϕ3(x) for the interval [0, 1]. 6.64. Use the Gram-Schmidt orthogonalization process to construct ϕ0(x), ϕ1(x), ϕ2(x), and ϕ3(x) for the interval [0, 2]. 6.65. Use the Gram-Schmidt orthogonalization process to construct ϕ0(x), ϕ1(x), ϕ2(x), and ϕ3(x) for the interval [1, 3]. 6.66. For the data given in Table 6.41 and using MATLAB, evaluate the quality of fit parameters Sr, St, and r2. TABLE 6.41  x

0

200

400

600

800

1000

1200

1400

1600

y

0

0.25

0.45

0.71

0.76

1.06

1.15

1.35

1.55

6.67. For the data given in Table 6.42 and using MATLAB, evaluate the quality of fit parameters Sr, St, and r2. TABLE 6.42  x

0

400

800

1200

1600

y

0

0.45

0.76

1.15

1.55

6.68. Use the least-squares method to estimate the value of c for the progressive damping model f = cv2. The data is given in Table 6.43. TABLE 6.43  v (m/s)

0

0.1

0.2

0.3

0.4

0.5

f (kN)

0

0.12

0.43

0.87

1.62

2.45

6.69. Use the least-squares method to estimate the value of k1 and k2 for the following cubic model of spring force: f = k1x + k2x3. The data is given in Table 6.44. TABLE 6.44

ANMF.CH06_4PP.indd 480

x(m)

0

0.03

0.06

0.09

0.12

0.15

0.18

f(N)

0

46

103

166

246

361

513

3/1/2023 2:21:42 PM

Curve Fitting, Regression, and Correlation • 481

6.70. Fit a straight line and a quadratic to the data given below using MATLAB built-in function: polyfit. x

1

2.5

3.5

4

1

2

2.2

3.7

y

6

16

27

34

5

10

11

29

6.71. The table shows the variation of the relative thermal conductivity k of sodium with temperature T. Find a quadratic that fits the data in the least-squares sense using MATLAB built-in function: polyfit. T(ºC)

9.0

19.0

35.0

52.5

69.0

k

17

98

188

76

86

6.72. The kinematic viscosity mk of water varies with temperature T as shown in the following table. Determine the cubic that best fits the data. Use MATLAB built-in function: polyfit.

ANMF.CH06_4PP.indd 481

T

11

21

37

54

71

88

100

U

179

112

68

52

34

32

30

3/1/2023 2:21:42 PM

ANMF.CH06_4PP.indd 482

3/1/2023 2:21:42 PM

CHAPTER

7

Numerical Integration 7.1 INTRODUCTION If F(x) is a differentiable function whose derivative is f(x), then we can evaluate the definite integral I as

b

I   f (x) = F (b) − F (a), F′(x) = f(x) a

(7.1)

Equation (7.1) is known as the fundamental theorem of calculus. Most integrals can be evaluated by the formula given by Equation (7.1) and there exists many techniques for making such evaluations. However, in many applications in science and engineering, most integrals cannot be evaluated because most integrals do not have antiderivatives F(x) expressible in terms of elementary functions. In other circumferences, the integrands could be empirical functions given by certain measured values. In all these instances, we need to resort to numerical methods of integration. It should be noted here that, sometimes, it is difficult to evaluate the integral by analytical methods. Numerical integration (or numerical quadrature, as it is sometimes called) is an alternative approach to solve such problems. As in other numerical techniques, it often results in approximate solution. The integration can be performed on a continuous function or a set of data. The integration given by Equation (7.1) is shown in Figure 7.1. The integration shown in Figure 7.1 is called closed since the function values at the two points (a, b) where the limits of integration are located are used to find

ANMF.CH07_4PP.indd 483

3/1/2023 2:22:18 PM

484 • Applied Numerical Methods Using MATLAB the integral. In open integration, information on the function at one or both limits of integration is not required.

FIGURE 7.1

The range of integration (b − a) is divided into a finite number of intervals in numerical integration. The integration techniques consisting of equal intervals are based on formulas known as Newton-Cotes closed quadrature formulas. In this chapter, we present the following methods of integration with illustrative examples: • trapezoidal rule • Simpson’s 1/3 rule • Simpson’s 3/8 rule • Boole’s and Weddle’s rules 7.1.1 Relative Error Suppose we are required to evaluate the definite integral b

I   f (x) dx a

In numerical integration, we approximate f(x) by a polynomial ϕ(x) of suitable degree. Then, we integrate f(x) within the limits (a, b). That is,



b

a

ANMF.CH07_4PP.indd 484

b

f (x) dx   (x) dx a

3/1/2023 2:22:24 PM

Numerical Integration • 485

Here the exact value if b

I   f (x) dx a

b

Approximate value =  (x) dx . a

The difference  b f (x) dx  b (x) dx  a  a  is called the error of approximation and  b f (x) dx  b (x) dx  a  a 



b

a

f (x) dx

is called the relative error of approximation. Hence, relative error of approximation =

Exact values − Approximate value Exact value

7.2 NEWTON-COTES CLOSED QUADRATURE FORMULA The general form of the problem of numerical integration may be stated as follows: Given a set of data points (xi, yi), i = 0, 1, 2, …., n of a function y = f(x), where f(x) is not explicitly known. Here, we are required to evaluate the definite integral

b

I   y dx (7.2) a

Here, we replace y = f(x) by an interpolating polynomial f(x) in order to obtain an approximate value of the definite integral of Equation (7.2). In what follows, we derive a general formula for numerical integration by using Newton-Cote’s method forward difference formula. Here, we assume the interval (a, b) is divided into n-equal subintervals such that h

ANMF.CH07_4PP.indd 485

ba n

3/1/2023 2:22:39 PM

486 • Applied Numerical Methods Using MATLAB a = x0 < x2 < x3 …. < xn = b

(7.3)

with xn = x0 + n h where

h is the internal size



n is the number of subintervals



a and b are the limits of integration with b > a.

Hence, the integral in Equation (7.2) can be written as xn

I   y dx (7.4)



x0

Using Newton-Cote’s method forward interpolation formula, we have xn

p(p  1) 2 p(p  1)(p  2) 3    y 0  .... dx  y 0  p y 0  2 !  y 0  3! 

 I x 0

where x = x0 + ph n



  h 0

(7.5)

  p2  p 2 p3  3 p2  2 p 3    y 0  .... dp (7.6) y  p y  y   0 0 0 2 6  

Hence, after simplification, we get xn

   n n(2 n  3) 2 n(n  2)2 3 I   y dn  nh  y 0  y 0   y0   y 0  .... (7.7) 2 12 24 x 0   The formula given by Equation (7.7) is known as Newton-Cotes closed quadrature formula. From the general formula (Equation (7.7)), we can derive or deduce different integration formulae by substituting n = 1, 2, 3, …, etc.

7.3 TRAPEZOIDAL RULE In this method, the known function values are joined by straight lines. The area enclosed by these lines between the given end points is computed to approximate the integral as shown in Figure 7.2.

ANMF.CH07_4PP.indd 486

3/1/2023 2:22:47 PM

Numerical Integration • 487

FIGURE 7.2

Each subinterval with the line approximation for the function forms a trapezoid as shown in Figure7.2. The area of each trapezoid is computed by multiplying the interval size h by the average value of the function value in that subinterval. After the individual trapezoidal areas are obtained, they are all added to obtain the overall approximation to the integral. Substituting n = 1 in Equation (7.7) and considering the curve y = f(x) through the points (x0, y0) and (x1, y1) as a straight line (a polynomial of first degree so that the differences of order higher than first become zero), we get x



1 1 1  h  h  I1    y dx  h  y 0  y 0    y 0  (y 1  y 0 )  (y 0  y 1 ) (7.8)  2 2 2  2  x 0

Similarly, we have x2 h I2    y dx  (y 1  y 2 )  x1 2 x3 h I3    y dx  (y 2  y 3 ) x 2 2

and so on. (see Figure 7.3)

ANMF.CH07_4PP.indd 487

3/1/2023 2:22:53 PM

488 • Applied Numerical Methods Using MATLAB In general, we have

xn h In    y dx  (y n 1  y n ) (7.9) x n1 2

FIGURE 7.3

Adding all the integrals (Equation (7.8), Equation (7.9)) and using the interval additive property of the definite integrals, we obtain xn n h h I   Ii    y dx  [y 0  2(y 1  y 2  y 3  ....  y n 1 )  y n ]  [X  2 I](7.10)  2 2 x0 i 1

where X = sum of the end points

I = sum of the intermediate ordinates.

Equation (7.10) is known as the trapezoidal rule. Summarizing, the trapezoidal rule signifies that the curve y = f(x) is replaced by n-straight lines joining the points (xn, yn), i = 0, 1, 2, 3, …, n. The area bounded by the curve y = f(x), the ordinates x = x0, x = xn and the x-axis is then approximately equivalent to the sum of the areas of the n-trapezoids so obtained.

ANMF.CH07_4PP.indd 488

3/1/2023 2:22:57 PM

Numerical Integration • 489

7.3.1 Error Estimate in Trapezoidal Rule Let y = f(x) be a continuous function with continuous derivatives in the interval [x0, xn]. Expanding y in a Taylor’s series around x = x0, we get x1

2 x1   x  x0  y dx    y 0  (x  x 0 )y 0    y 0  .... dx x0  2!   







hy 0 

Likewise, 

h h h  h2 (y 0  y 1 )  (y 0  y (x 0  h))   y 0  y  hy 0  y 0  .... 2 2 2 2 

x0

h2 h3 h4 y 0  y 0  y 0  .... (7.11) 2 6 24

 hy 0 

h2 h4 h4 y 0  y 0  y 0  .... (7.12) 2 4 12

Hence, the error e1 in (x0, x1) is obtained from Equations (7.11) and (7.12) as

x1 h 1 3 e1   h y 0  ....  y dx  (y 0  y 1 )  x 0 2 12

In a similar way, we can write

x2 h 1 3 e2   h y 1  ....  y dx  (y 1  y 2 )   x1 2 12



e3 

1 3 h y 2  .... 12



e4 

1 3 h y 3  .... (7.13) 12

and so on. In general, we can write

ANMF.CH07_4PP.indd 489

en 

1 3 h y n 1  .... 12

3/1/2023 2:23:16 PM

490 • Applied Numerical Methods Using MATLAB Hence, the total error E in the interval (x0, xn) can be written as

n

E   en  n 1

h 3  y 0  y 1  y 2  ....  y n 1  (7.14) 12

If y ′′( x ) is the largest value of the n quantities in the right-hand side of Equation (7.14), then we have

E

(b  a) 2 1 3 h n y ( x )   h y ( x ) (7.15) 12 12

ba , the total error in the evaluation of the integral of n Equation (7.2) by the trapezoidal rule is of the order of h2. Now, since h 

7.3.2 MATLAB Functions: trapz and cumtrapz MATLAB has a built-in function that evaluates integrals for data. It has the general syntax

z = trapz(x, y)

where the two vectors, x and y, hold the independent and dependent variables, respectively. The two vectors must be of the same length. It uses the numerical trapezoidal method of integration. In addition, MATLAB has another function, cumtrapz that computes the cumulative integral. A simple representation of the syntax is

z = cumtrapz(x, y)

where the two vectors, x and y, hold the independent and dependent variables, respectively. and z = a vector whose elements z(k) hold the integral from x(1) to x (k). EXAMPLE 7.1 1.2

Evaluate the integral ∫ e x dx , taking six intervals by using trapezoidal rule up 0 to three significant figures. Solution:

a = 0, b = 1.2, n = 6



h

ANMF.CH07_4PP.indd 490

b  a 1.2  0  = 0.2 n 6

3/1/2023 2:23:24 PM

Numerical Integration • 491

x

0

0.2

0.4

0.6

0.8

1.0

1.2

y = f(x)

0 y0

1.221 y1

1.492 y2

1.822 y3

2.226 y4

2.718 y5

3.320 y6

The trapezoidal rule can be written as

h I  [(y 0  y 6 )  2(y 1  y 2  y 3  y 4  y 5 )] 2 I

0.2 [(1  3.320)  2(1.221  1.492  1.822  2.226  2.718)] 2

 I = 2.3278 ≈ 2.328. The exact value is =



1.2

0

e x dx = 2.3201.

MATLAB Solution: >> x = [0 0.2 0.4 0.6 0.8 1.0 1.2]; >> y = exp(x); >> format short >> Integral = trapz(x, y) Integral =  2.3278 EXAMPLE 7.2 12 dx Evaluate  by using trapezoidal rule, taking n = 6, correct to give 0 1  x2 significant figures. Solution:

ANMF.CH07_4PP.indd 491

f (x ) 

1 1  x2



a = 0, b = 12



h

b  a 12  0  =2 n 6

3/1/2023 2:23:33 PM

492 • Applied Numerical Methods Using MATLAB

x

0

2

4

6

8

10

12

y = f(x)

1

1 5

1 17

1 37

1 65

1 101

1 145

y

1.00000 y0

0.20000 y1

0.05882 y2

0.02703 y3

0.01538 y4

0.00990 y5

0.00690 y6

The trapezoidal rule can be written as   = I

h [(y 0 + y 6 ) + 2(y 1 + y 2 + y 3 + y 4 + y 5 )] 2

  2 I= [(1 + 0.00690) + 2(0.2 + 0.05882 + 0.02703 + 0.01538 + 0.00990)] 2  I = 1.62916. The exact value is





12

0

12

1 dx = tan −1 x = 1.48766. 2 1+ x 0

MATLAB Solution: >> x = [0 2 4 6 8 10 12]; >> y = (1/(1+x^2)); >> integral = trapz(x, y) integral =  2.6879 EXAMPLE 7.3



Evaluate

6

2

log10 x dx by using trapezoidal rule, taking n = 8, correct to five

decimal places. Solution:

f(x) = log10x



a = 2, b = 6, n = 8



ANMF.CH07_4PP.indd 492

= h

b−a 6−2 1 = = = 0.5 n 8 2

3/1/2023 2:23:46 PM

Numerical Integration • 493

x f(x)

2 0.30103 y0

2.5 0.39794 y1

3.0 0.47712 y2

3.5 0.54407 y3

4.0 0.60206 y4

4.5 0.65321 y5

5.0 0.69897 y6

5.5 0.74036 y7

6.0 0.77815 y8

The trapezoidal rule is

h I  [(y 0  y 8 )  2(y 1  y 2  y 3  y 4  y 5  y 6  y 7 )] 2 0.5 I= [(0.30103 + 0.77815) + 2(0.39794 + 0.47712 + 0.54407 2 + 0.60206 + 0.65321 + 0.69897 + 0.74036 + 0.77815)] I = 2.32666.

MATLAB Solution: >> x = [2 2.5 3 3.5 4 4.5 5 5.5 6]; >> y = log10(x); >> trapz(x, y) ans =  2.3267

7.4 SIMPSON’S 1/3 RULE In Simpson’s rule, the function is approximated by a second degree polynomial between successive points. Since a second degree polynomial contains three constants, it is necessary to know three consecutive function values forming two intervals as shown in Figure 7.4.

FIGURE. 7.4 

ANMF.CH07_4PP.indd 493

3/1/2023 2:23:48 PM

494 • Applied Numerical Methods Using MATLAB Consider three equally spaced points x0, x1, and x2. Since the data are equally spaced, let h = xn+1 − xn (see Figure 7.5).

FIGURE. 7.5 

Substituting n = 2 in Equation (7.7) and taking the curve through the points (x0, y0), (x1, y1) and (x2, y2) as a polynomial of second degree (parabola) so that the differences of order higher than two vanish, we obtain h 1 y dx = 2h  y 0 + 4y 0 + ∆ 2 y 0  = [y 0 + 4y 1 + y 2 ] (7.16) x0 6   3 x4 h Similarly, I2 = ∫ y dx = [y 2 + 4y 3 + y 4 ] x2 3 x6 h I3 = ∫ y dx = [y 4 + 4y 5 + y 6 ] (7.17) x4 3



I1 =



x1

and so on. In general, we can write

In =



x2 n

x 2 n −2

y dx =

h [y 2n − 2 + 4y 2n −1 + y 2n ] (7.18) 3

Summing up all the above integrals, we obtain

xn h I   y dx  [y 0  4(y 1  y 3  y 5  ....  y 2 n 1 )  2(y 2  y 4  y 6  .... x0 3 + y 2n − 2 ) + y 2n ]

    =

ANMF.CH07_4PP.indd 494

h [X + 40 + 2E] (7.19) 3

3/1/2023 2:23:51 PM

Numerical Integration • 495

where X = sum of end ordinates

O = sum of odd ordinates



E = sum of even ordinates

Equation (7.19) is known as Simpson’s 1/3 rule. Simpson’s 1/3 rule requires the whole range (the given interval) must be divided into even number of equal sub-intervals. 7.4.1 Error Estimate in Simpson’s 1/3 Rule Expanding y = f(x) around x = x0 by Taylor’s series, we obtain



x2

x0

y dx  





x0 2 h

x0

  (x  x 0 )2  ( ) y  x  x y  y 0  .... dx 0 0  0 2!  

 2h y 0 

4 h 2 i 8 h 3 ii 16 h 4 iii 32 h 5 iv y0  y0  y0  y 0  .... 2! 3! 4! 5!

4 2 h 4 iii 4 iv  2 h y 0  2 h 2 y 0i  h 3 y 0ii  y 0  y 0  .... (7.20) 3 3 15

In addition, we have     h 2 ii i y  4 y hy y 0  ....    0    0 0 h h 2!    [y 0  4 y 1  y 2 ]   2 3 3 3  4 h 8 h i y 0ii  y 0iii  ....   (y 0  hy 0  2! 3!  

 2 h y 0  2 h 2 y 0i  4

h 3 ii 2 h 4 iii 5h 5 iv y0  y0  y 0  .... (7.21) 3 3 18

Hence, from Equations (7.20) and (7.21), the error in the subinterval (x0, x2) is given by

ANMF.CH07_4PP.indd 495

x2 h 5 4 e1   y dx  (y 0  4 y 1  y 2 )     h 5 y 0iv  .... x0 3  15 18  



 h 5 iv h 5 iv y 0  ....  y0 90 90

(7.22)

3/1/2023 2:24:09 PM

496 • Applied Numerical Methods Using MATLAB Likewise, the errors in the subsequent intervals are given by

e2 

 h 5 iv y2 90



e3 

 h 5 iv y 4 (7.23) 90

and so on. Hence, the total error E is given by  h 5 iv nh 5 iv h0 ( x )  y 0  y 2iv  y 4iv  ....  y 2ivn  2   90 90



E   en 

or

E

where

y iv ( x ) = largest value of the fourth order derivatives



h = (b − a)/n

(b  a) 2 iv h y ( x ) (7.24) 180

The error in Simpson’s 1/3 rule can be written as

e

 nh 5 iv (b  a)5 iv f ()  f () 180 2880 n 4

where a = x0 < x < xn = b (for n subintervals of length h). 7.4.2 MATLAB Program for Simpson’s Integration: simpsonint function intf=Simpsonint(f,a,b,n,varargin) %Integral of f(x) over [a, b] by Simpson rule with n segments eps = 1e−10; if nargin quad ('exp(x)', 0, 1.2) ans =  2.3201 Note that the function is typed with element-by-element operations. Another method is to first create a user-defined function that computes the function to be integrated. The function file (named y = example(x)) is function y = example(x) y = exp(x) >> q=quad (@example, 0, 1.2) q=  2.3201 MATLAB Solution: The use of the quad command in the Command Window with the function to be integrated typed in as a string as shown below: >> quad ('exp(x)', 0, 1.2) ans =  2.3201 MATLAB Solution using Simpsonint Program: >> func = inline ('exp(x)'); >> a = 0; b = 1.2; n = 6; >> intf = Simpsonint (func, a, b, n) intf =  2.3201

ANMF.CH07_4PP.indd 499

3/1/2023 2:24:23 PM

500 • Applied Numerical Methods Using MATLAB EXAMPLE 7.5 12 dx Evaluate  by using Simpson’s 1/3 rule, taking n = 6. 0 1  x2 Solution: 1 f(x) = 1 + x2

a = 0, b = 12, n = 6



h

b  a 12  0  =2 n 6

x

0

2

4

6

8

10

12

y = f(x)

1 y0

0.2 y1

0.05882 y2

0.02703 y3

0.01538 y4

0.0099 y5

0.0069 y6

The Simpson’s 1/3 rule is

h I  [(y 0  y 6 )  4(y 1  y 3  y 5 )  2(y 2  y 4 )] 3



I=

2 [(1 + 0.0069) + 4(0.2 + 0.02703 + 0.0099) + 2(0.05882 + 0.01538)] 3

 I = 1.40201. MATLAB Solution: The use of the quad command in the Command Window with the function to be integrated typed in as a string as shown below: function y = fex7_5(x) y = (1. / (1 + x. ^2)); >> quad (@fex7_5, 0, 12) ans =  1.4877

ANMF.CH07_4PP.indd 500

3/1/2023 2:24:27 PM

Numerical Integration • 501

MATLAB Solution using Simpsonint Program: function y = fex7_5(x) y = (1. / (1+x. ^2)); >> a = 0; b = 12; n = 6; >> intf = Simpsonint (@fex7_5, a, b, n) intf =  1.4020 EXAMPLE 7.6 Evaluate



6

2

log10 x dx by using Simpson’s 1/3 rule, taking n = 6.

Solution:

f(x) = log10x



a = 2, b = 6, n = 6



h

ba 62 2   n 6 3

x

2 = 6/3

8/3

10/3

12/3 = 4

14/3

16/3

18/3 = 6

y = f(x)

0.30103 y0

0.42597 y1

0.52288 y2

0.60206 y3

0.66901 y4

0.72700 y5

0.77815 y6

The Simpson’s 1/3 rule is h I  [(y 0  y 6 )  4(y 1  y 3  y 5 )  2(y 2  y 4 )] 3

2/3 [(0.30103 + 0.77815) + 4(0.42597 + 0.60206 + 0.72700) 3 + 2(0.52288 + 0.66901)]

I=



I = 2.32957.

MATLAB Solution using Simpsonint Program: function y = fex7_6(x) y = log10(x);

ANMF.CH07_4PP.indd 501

3/1/2023 2:24:38 PM

502 • Applied Numerical Methods Using MATLAB >> a = 2; b = 6; n = 6; >> intf = Simpsonint (@fex7_6, a, b, n) intf =  2.3296 MATLAB Solution: The use of the quad command in the Command Window with the function to be integrated typed in as a string as shown below: >> quad ('log10(x)', 2, 6) ans =  2.3297

7.5 SIMPSON’S 3/8 RULE Putting n = 3 in Equation (7.7) and taking the curve through (xn, yn), n = 0, 1, 2, 3 as a polynomial of degree three such that the differences higher than the third order vanish, we obtain x



3 3 3 2 1 3   I1    y dx  3 h  y 0  y 0   y 0   y 0  2 2 8   x 0

3    h[h 0  3 y 1  3 y 2  y 3 ] (7.25) 8 Similarly, we get

x6 3 I2    y dx  h[y 3  3 y 4  3 y 5  y 6 ] x 3 8



x9 3 I3    y dx  h[y 6  3 y 7  3 y 8  y 9 ] (7.26) x 6 8

and so on.

ANMF.CH07_4PP.indd 502

3/1/2023 2:24:47 PM

Numerical Integration • 503

Finally, we have x3 n 3 In    y dx  h[y 3 n  3  3 y 3 n  2  3 y 3 n 1  y 3 n ] (7.27)  x 3 n 3 8



Summing up all the expressions above, we obtain x3 n h I  y dx  [y 0  3(y 1  y 2  y 4  y 5  y 7  y 8  ....  y 3 n  2  y 3 n 1 ) x 0 8

2(y 3  y 6  y 9  ....  y 3 n  3 )  y 3 n ] (7.28)



Equation (7.28) is called the Simpson’s 3/8 rule. Here, the number of subintervals should be taken as multiples of 3. Simpson’s 3/8 rule is not as accurate as Simpson’s 1/3 rule. The dominant term in the error of this for−3 5 iv mula is y y ( x ) . Simpson’s 3/8 rule can be applied when the range (a, b) is 80 divided into a number of subintervals, which must be a multiple of 3. The error in  nh 5 iv f () , where x0, x xn (for n subintervals of Simpson’s 3/8 rule is e  80 length h). EXAMPLE 7.7 Evaluate the integral ordinates.



1.2

0

e x dx, by using Simpson’s 3/8 rule and taking seven

Solution: n+1=7⇒n=6



The points of division are

ANMF.CH07_4PP.indd 503

0,

1 2 3 4 5 , , , , , 1, 6 6 6 6 6

h=

1 6

x

0

1/6

2/6

3/6

4/6

5/6

1

y = f(x)

1 y0

1.18136 y1

1.39561 y2

1.64872 y3

1.94773 y4

2.30098 y5

2.71828 y6

3/1/2023 2:25:10 PM

504 • Applied Numerical Methods Using MATLAB The Simpson’s 3/8’s rule is I  3 h [(y 0  y 6 )  3(y 1  y 2  y 4  y 5 )  2(y 3 )] 8 I =

3(1 / 6) [(1 + 2.71828) + 3(1.18136 + 1.39561 + 1.94773 + 2.30098) 8 + 2(1.64872)]

I = 2.3201 MATLAB Solution using Simpsonint Program: function y = fex7_7(x) y = exp(x); >> a = 0; b = 1.2; n = 7; intf=Simpsonint(@fex7_7,a,b,n) intf =  2.3201 MATLAB Solution: The use of the quad command in the Command Window with the function to be integrated typed in as a string as shown below: >> quad('exp(x)',0,1.2) ans =  2.3201 EXAMPLE 7.8 Evaluate



12

0

dx by using Simpson’s 3/8 rule and taking seven ordinates. 1  x2

Solution:

ANMF.CH07_4PP.indd 504

n + 1 = 7 ⇒ n = 6, h = 2.

3/1/2023 2:25:17 PM

Numerical Integration • 505

The points of division are 0, 2, 4, 6, 8, 10, 12. x

0

2

4

6

8

10

12

y = f(x)

1 y0

0.2 y1

0.05882 y2

0.02703 y3

0.01538 y4

0.00990 y5

0.00690 y6

The Simpson’s 3/8’s rule is 3 I  h[(y 0  y 6 )  3(y 1  y 2  y 4  y 5 )  2(y 3 )] 8



3 I = 2 [(1 + 0.00690) + 3(0.2 + 0.05882 + 0.01538 + 0.00990) 8 + 2(0.02703)] I = 1.43495.



EXAMPLE 7.9 Repeat Example 7.6 by using Simpson’s 3/8 rule, taking n = 6, correct to five decimal places. Solution: The points of division are

2,

8 10 12 14 16 18 , , , , , 3 3 3 3 3 3

x

6/3

8/3

10/3

12/3

14/3

16/3

18/3

y = f(x)

0.30103 y0

0.42597 y1

0.52288 y2

0.60206 y3

0.66901 y4

0.727 y5

0.77815 y6

Here

h=

2 3

The Simpson’s 3/8’s rule is

I



I=



ANMF.CH07_4PP.indd 505

3.h [(y 0  y 6 )  3(y 1  y 2  y 4  y 5 )  2(y 3 )] 8 3(2 / 3) [(0.30103 + 0.77815) + 3(0.42597 + 0.52288 + 0.66901 8 + 0.72700) + 2(0.60206)]

I = 2.32947.

3/1/2023 2:25:36 PM

506 • Applied Numerical Methods Using MATLAB MATLAB Solution using Simpsonint Program: function y = fex7_6(x) y = log10(x); >> a = 2; b = 6; n = 6; >> intf = Simpsonint(@fex7_6,a,b,n) intf =  2.3296 MATLAB Solution: The use of the quad command in the Command Window with the function to be integrated typed in as a string as shown below: >> quad('log10(x)',2,6) ans =  2.329669568682355

7.6 BOOLE’S AND WEDDLE’S RULES 7.6.1 Boole’s Rule Substituting n = 4 in Equation (7.7) and taking the curve through (xn, yn), n = 0, 1, 2, 3, 4 as a polynomial of degree 4, so that the difference of order higher than four vanish (or neglected), we obtain x



4 5 2 7   y dx  4 h  y 0  2 y 0   2 y 0   3 y 0   4 y 0    3 3 90   x 0





2h [7 y 0  32 y 1  12 y 2  32 y 3  7 y 4 ] (7.29) 45

x8 2h Likewise  (7 y 4  32 y 5  12 y 6  32 y 7  7 y 8 )  y dx  x 4 45

and so on.

ANMF.CH07_4PP.indd 506

3/1/2023 2:25:43 PM

Numerical Integration • 507

Adding all the above integrals from x0 to xn, where n is a multiple of 4, we obtain x n 2h I  y dx  [7 y 0  32(y 1  y 3  y 5  y 7  ....)  12(y 2  y 6  y 10  ....) x 0 45 +14(y 4 + y 8 + y 12 + ....) + 7 y n ] (7.30)



Equation (7.30) is known as Boole’s rule. It should be noted here that the number of subintervals should be taken as a multiple of 4. The leading term in the error of formula can be shown as

−8 7 vi h y (x) . 945

7.6.2 Weddle’s Rule Substituting n = 6 in Equation (7.7) and taking the curve y = f(x) through the point (xn, yn), n = 0, 1, 2, 3, 4, 5, 6 as a polynomial of degree 6 so that the differences of order higher than 6 are neglected, we obtain x



6 9 2 123 4    3  y 0  3 y 0  2  y 0  4  y 0  60  y 0     y dx  6 h    11  5 y  41  6 y   0 0  140  20 x 0

3h    [y 0  5 y 1  y 2  6 y 3  y 4  5 y 5  y 6 ] (7.31) 10 Approximating

41 6 3 6 ∆ y 0 as ∆ y 0 , we have, similarly, we can write 140 10

x12

3h   y dx  [y 6  5 y 7  y 8  6 y 9  y 10  5y 11  y 12 ] (7.32) x 6 10

and so on.

ANMF.CH07_4PP.indd 507

3/1/2023 2:25:58 PM

508 • Applied Numerical Methods Using MATLAB Adding all the above integrals from x0 to xn, where x is a multiple of 6, we obtain xn 3h   y dx  [y 0  5(y 1  y 5  y 7  y 11  ....)  (y 2  y 4  y 8  y 10  ....)) x 0 10 +6(y 3 + y 9 + y 15 + ....) + 2(y 6 + y 12 + y 18 + ....) + y n ] (7.33)



Equation (7.33) is known as Weddle’s rule. Weddle’s rule was found to be more accurate than most of the other rules. The error estimate is given by −h 7 vi y ( x ) . In Weddle’s rule, the number of subintervals should be taken as 140 multiple of 6. A summary of the Newton-Cotes formulas and their errors is presented in Table 7.1. TABLE 7.1  Summary of Newton-Cotes formula. No.

Integral



1.

2.

x2

x0



3.



x4

5.



x6

x0

x0

Error −h 3 ii y (x) 12

Simpson’s 1/3 Rule

h [y 0 + 4 y 1 + y 2 ] 3

−h 5 iv y (x) 90

Simpson’s 3/8 Rule

3h [y 0 + 3 y 1 + 3 y 2 + y 3 ] 8

−3 h 5 iv y (x) 80

y dx

Boole’s Rule

2h [7 y 0 + 32 y 1 + 12 y 2 + 32 y 3 + 7 y 4 ] 45

−8 7 vi h y (x) 945

y dx

Weddle’s Rule

3h [y 0 + 5 y 1 + y 2 + 6 y 3 + y 4 + 5 y 5 + y 6 ] 10

−h 7 vi y (x) 140

y dx

y dx x3

x0

4.

Integration formula h [y 0 + y 1 ] 2

x1

x0



Name

y dx

Trapezoidal Rule

EXAMPLE 7.10 1.2

Evaluate the integral ∫ e x dx by using Boole’s rule using exactly five func0 tional evaluations and correct to five significant figures. Solution:

1.2 and applying Boole’s rule, we have 4

Taking h =



1.2





1.2

ANMF.CH07_4PP.indd 508

0

0

f (x) dx 

2h [7y0 + 32y1 + 12y2 + 32y3 + 7y4] 45

f (x) dx 

2  0.3 [7f(0) + 32f(0.3) + 12f(0.6) + 32f(0.9) + 7f(1.2)] 45

3/1/2023 2:26:34 PM

Numerical Integration • 509

x

0

0.3

0.6

0.9

1.2

y = f(x)

1 y0

1.34986 y1

1.82212 y2

2.45960 y3

3.32012 y4

1.2

∫0 f (x) dx = 0.01333[7x1 + 32 × 1.34986 + 12 × 1.82212 + 32 × 2.45960 + 7 × 3.32012]



1.2

f (x) dx = 2.31954.

0

EXAMPLE 7.11

dx by using Boole’s rule using exactly five func1  x2 tional evaluations and correct to five significant figures. Evaluate the integral



12

0

Solution: x

0

3

6

9

12

y = f(x)

1

0.1

0.02703

0.01220

0.00690

The Boole’s rule is



12

0

f (x) dx 

2h [7f(0) + 32f(3) + 12f(6) + 32f(9) + 7f(12)] 45

2x3 I= [7x(1) + 32x(0.1) + 12x(0.02703) + 32x(0.01220) 45 + 7x(0.00690)]

I = 1.46174.

EXAMPLE 7.12 Evaluate the integral



1.2

0

e x dx by using Weddle’s rule and taking n = 6, correct

to five significant figures. Solution:

ANMF.CH07_4PP.indd 509



f (x) = ex; a = x0 = 0; b = xn = 1.2; n = 6



h

1.2  0 = 0.2 6

3/1/2023 2:26:50 PM

510 • Applied Numerical Methods Using MATLAB The Weddle’s rule is I=





3h [y0 + 5y1 + y2 +6y3 + y4 + 5y5 + y6] 10

x

0

0.2

0.4

0.6

0.8

1

1.2

y = f(x)

1 y0

1.2214 y1

1.4918 y2

1.8221 y3

2.2255 y4

2.7183 y5

3.3201 y6

I=



3(0.2) [1 + 5(1.2214) + 1.4918 + 6(1.8221) + 2.2255) + 5(2.7183) 10  + 3.3201]

EXAMPLE 7.13

dx by using Weddle’s rule and taking n = 6, cor1  x2 rect up to five significant figures. Evaluate the integral



12

0

Solution: a = 0; b = 12; n = 6

h

b  a 12  0  =2 n 6

x

0

2

4

6

8

10

12

y = f(x)

1 y0

0.2 y1

0.05882 y2

0.02703 y3

0.01538 y4

0.00990 y5

0.00690 y6

The Weddle’s rule is

I=

3h [y0 + 5y1 + y2 +6y3 + y4 + 5y5 + y6] 10

32 I [1 + 5 × 0.2 + 0.05882 + 6 × 0.02703 + 0.01538 + 5 × 0.00990 10 + 0.00690]

ANMF.CH07_4PP.indd 510

I = 1.37567.

3/1/2023 2:27:03 PM

Numerical Integration • 511

EXAMPLE 7.14 Repeat Example 7.6 by using Weddle’s rule, taking n = 6, correct to five decimal places. Solution:

a = 2; b = 6; n = 6



h

ba 62 2   n 6 3

x

6/3

8/3

10/3

12/3

14/3

16/3

18/3

y = f(x)

0.30103 y0

0.42597 y1

0.52288 y2

0.60206 y3

0.66901 y4

0.727 y5

0.77815 y6

The Weddle’s rule is 3h I= [y0 + 5y1 + y2 +6y3 + y4 + 5y5 + y6] 10 I=

3(2 / 3) [0.30103 + 5 × 0.42597 + 0.52288 + 6 × 0.60206 + 0.66901 10  + 5 × 0.727 + 0.77815]

I = 2.32966.

EXAMPLE 7.15 Repeat Example 7.6 by Boole’s rule, using exactly five functional evaluations and correct to five significant figures. Solution: We use five functional evaluations here. Taking h = 1 and applying Boole’s rule, we have

I=h



I=



ANMF.CH07_4PP.indd 511

2 [7f(2) + 32f(3) + 12f(4) + 32f(5) + 7f(6)] 45

2 [7 × 0.30103 + 32 × 0.47712 + 12 × 0.60206 + 32 × 0.69897 45  + 7 × 0.77815] x

2

3

4

5

6

y = f(x)

0.30103

0.47712

0.60206

0.69897

0.77815

I = 2.32950.

3/1/2023 2:27:11 PM

512 • Applied Numerical Methods Using MATLAB

7.7 ROMBERG’S INTEGRATION Romberg’s integration employs a successive error reduction technique. It applies the trapezoidal rule with different interval sizes in order to obtain some preliminary approximations to the integral to start with. The method starts with the preliminary approximations obtained by the trapezoidal rule and then applies the Richardson extrapolation procedure which refines these values successfully to a single more accurate approximation. 7.7.1 Richardson’s Extrapolation Richardson extrapolation is a simple method for improving the accuracy of certain numerical procedures, including the finite difference approximations and in numerical integration. Assume that we have an approximate means of computing some quantity G. In addition, assume that the result depends on a parameter h. Let us denote the approximation by g(h), then we have G = g(h) + E(h), where E(h) denotes the error. Richardson extrapolation can remove the error, provided that it has the form E(h) = chp, where c and p are constants. We begin by computing g(h) with some value of h, say h = h1. In this case, we have

G  g(h1 )  ch1p (7.34)

Repeating the calculations with h = h2, such that

G  g(h 2 )  ch 2p (7.35)

Now, eliminating c and solving for G from Equations (7.34) and (7.35), we get p



 h1    g(h 2 )  g(h1 ) h G 2  (7.36) p  h1    1  h2 

Equation (7.36) is called the Richardson extrapolation formula.

ANMF.CH07_4PP.indd 512

3/1/2023 2:27:18 PM

Numerical Integration • 513

It is general practice to use h 2 =



h1 and in this case Equation (7.36) becomes 2

h  2 p g  1   g(h1 )  2  G (7.37) 2p  1

7.7.2 Romberg Integration Formula As mentioned earlier, Romberg’s integration provides a simple modification to the approximate quadrature formula obtained with the aid of finite difference method in order to obtain their better approximations. Consider as an example to improve the value of the integral

b

b

a

a

I   y dx   f (x) dx (7.38)

by the trapezoidal rule. We can evaluate Equation (7.38) by means of the trapezoidal rule, namely



n xn h I   Ii   y dx  [y 0  2(y 1  y 2  y 3  ....  y n 1 )  y n ] x0 2 i 1



h  [X  2I] (7.39) 2

where X = sum of end ordinates and I = sum of intermediate ordinates. Equation (7.39) signifies that the curve y = f(x) is replaced by n straight lines joining the points (xi, yi), i = 0, 1, 2, 3, …., n. The area bounded by the curve y = f(x) the ordinates x = x0, x = xn and the x-axis is then approximated equivalent to the sum of the areas of the n-trapeziums so obtained. Now, we evaluate Equation (7.38) by means of two different widths, h1 and h2, in order to obtain the approximate values I1 and I2, respectively. The corresponding errors E1 and E2 are given by

ANMF.CH07_4PP.indd 513



E1 

(b  a)h12 y ( x ) 12

and

E2 

(b  a) y ( x ) (7.40) 12

3/1/2023 2:27:28 PM

514 • Applied Numerical Methods Using MATLAB

Noting that y ′′( x ) is also the largest value of y″(x), we can assume that the quantities y ′′(x) and y ′′( x ) are nearly equal. Hence, we can write

E1 h12 h2 E2  2   2 2 2 (7.41) E2 h 2 E2  E1 h 2  h1

Noting, now that I = I1 − E1 = I2 − E2, we have

E2 − E1 = I1 − I2(7.42)

From Equations (7.41) and (7.42), we have

E2 

h 22 h2 (E2  E1 )  2 2 2 (I1  I2 ) 2 h 2  h1 h  h1 2 2

or I  I2  E2 

I1 h 22  I2 h12 (7.43) h 22  h12

Equation (7.43) gives a better approximation for I. In order to compute I, we let h1 = h and h2 = h/2 such that Equation (7.43) gives



 h2 I1  4 I  2 h 4

or

 h I  h,    2

 2   I2 h 4I  I I I   2 1  I2  2 1 3 3  h2 h 4 I    I(h) 2 (7.44) 3

If we apply the trapezoidal rule several times successively halving h, every time the error is reduced by a factor 1/4. The above computation is continued with two successive values are very close to each other. This refinement of Richardson’s method is known as the Romberg integration. The values of the integral in Romberg integration can be tabulated in the following scheme:

ANMF.CH07_4PP.indd 514

3/1/2023 2:27:44 PM

Numerical Integration • 515

Romberg Integration Scheme I(h)  h I  h,   2 h I  2

h h I ,  2 4

h I  4

 h h I  h, ,   2 4  h h h I  h, , ,   2 4 8 h h h I , ,  2 4 8

h h I ,  4 8 h I  8

where

  h 1 h I  h,    4I    I(h) 2 3 2        h h  1   h   h  I  ,    4I    I     2 4  3   4   2  M M



 h h  1   h h   h  I  h, ,    4I  ,   I  h,    2 4  3   2 4   2 



 h h h  1   h h   h h  I  , ,    4I  ,   I  ,    2 4 8  3   4 8   2 4 



 h h h  1   h h h   h h  I  h, , ,    4I  , ,   I  h, ,   (7.45)  2 4 8  3   2 4 8   2 4 

The computations are continued until the successive values are close to each other. The general extrapolation formula used in this scheme is

ANMF.CH07_4PP.indd 515

R i, j 

4 j 1 R i , j 1  R i 1 , j 1 4 j 1  1

, i > 1, j = 2, 3, …., I

(7.46)

3/1/2023 2:28:03 PM

516 • Applied Numerical Methods Using MATLAB A pictorial representation of Equation (7.46) is shown below:

where the multipliers a and b depend on j in the following manner: j

2

3

4

5

6

α

−1/3

−1/15

−1/63

−1/255

−1/1023

β

4/3

16/15

64/63

256/255

1024/1023

7.7.3 MATLAB Program for Romberg Integration: Romberg The algorithm for Romberg integration has been implemented in the function Romberg. It returns the value of the integral and the required number of function evaluations. Richardson’s extrapolation is performed by the subfuction Richardson. The MATLAB program is listed below: function [Int,neval] = Romberg(Func,a,b,tol,kmax) % Romberg integration % USAGE: [I,neval] = Romberg(Func,a,b,tol,kmax) % INPUT % Func = Handle of function % a,b = Integration % tol = Error tolerance (default is 1.0e-8) % kmax = Limit on the number of panel doublings %    (default is 20) % OUTPUT % I = value of the integral. % neval = number of function evaluations

ANMF.CH07_4PP.indd 516

3/1/2023 2:28:04 PM

Numerical Integration • 517

if nargin < 5; kmax = 20; end if nargin < 4; tol = 1.0e-8; end r = zeros(kmax); r(1) = Trap(Func,a,b,0,1); rold = r(1); for k = 2:kmax r(k) = Trap(Func,a,b,r(k−1),k); r = Richardson(r, k); if abs(r(1) − rold) < tol neval = 2^(k−1) + 1; I = r(1); return end rold = r(1); end error(‘Romberg integration had failed to converge’) function r = Richardson(r, k) % Richardson’s extrapolation for j = k−1:−1:1 c = 4^(k−j); r(j) = (c∗r(j+1) − r(j))/(c−1); end function Ih = Trap(Func,a,b,Ith,k) % Recursive trapezoidal rule % USAGE % Ih = Trap(Func,a,b,Ith,k)

ANMF.CH07_4PP.indd 517

3/1/2023 2:28:04 PM

518 • Applied Numerical Methods Using MATLAB % Func = Handle of Function % a,b = Limits of integration % Ith = Integral with 2^(k−1) panels % Ih = Integral with 2^k panels if k = = 1 fa = feval(Func,a); fb = feval(Func,b); Ih = (fa + fb)∗(b − a)/2.0; else % Number - new points n = 2^(k − 2); % Spacing - new points h = (b − a)/n; % First new point coordinates x = a + h/2.0; sum = 0.0; for i = 1:n fx = feval(Func,x); sum = sum + fx; x = x + h; end Ih = (Ith + h∗sum)/2.0; end EXAMPLE 7.16 Apply Romberg’s integration to find





0

f (x) dx , where f(x) = sin x.

Solution: From the recursive trapezoidal rule in Equation (7.9), we have

ANMF.CH07_4PP.indd 518

3/1/2023 2:28:05 PM

Numerical Integration • 519

 R1,1  I()  [f (0)  f ()]  0 2       1    R 2,1  I    I()  f    1.5708 2 2 2 2    1          3      R 3,1  I    I    f    f     1.8961  4  2  2  4   4   4     1          3    5   7       R 4,1  I    I    f    f    f    f     1.9742  8  2  4  8   8   8   8   8  Using the extrapolation formula in Equation (7.46), we obtain the following table:



R1,1 R  2,1 R 3,1  R 4,1

R 2 ,2 R 3 ,2 R 4 ,2

R 3 ,3 R 4 ,3

  0   1.5708 2.0944     1.8961 2.0046 1.9986     R 4,4  1.9742 2.0003 2.0000 2.0000 

The above table shows that the procedure has converged. Hence,   sin x dx  R 4,4  2.0000 , which is the exact result. 0

MATLAB Solution: function y = fex7_16(x) y = sin(x); >> [Integral,neval]=Romberg(@fex7_16,0,pi) Integral =  2.000000000001321 neval =  33

ANMF.CH07_4PP.indd 519

3/1/2023 2:28:19 PM

520 • Applied Numerical Methods Using MATLAB EXAMPLE 7.17 Apply Romberg’s integration method to find imal places.



1.2

0

 1    dx correct to five dec1 x 

Solution: f (x ) 



1 1 x

Let h = 0.6, 0.3, and 0.15 or h = 0.6, h/2 = 0.3 and h/4 = 0.15. x

0

0.15

0.30

0.40

0.60

0.75

0.90

1.05

1.20

y = f(x)

1

0.86957

0.76923

0.71429

0.62500

0.57143

0.52632

0.48780

0.45455

Applying trapezoidal rule for h = 0.6, we obtain

I(h) = I(0.6) = I1 =

For h =

0.6 = 0.3, we obtain 2

0.6 [(1 + 0.45455) + 2(0.6250)] = 0.81132 2

0.3 h [(1  0.45455)  2(0.76923  0.6250  0.52632)]   I    I(0.3)  I2  2 2 For h =

= 0.79435 0.6 = 0.15, we have 4

0.15 h [(1  0.45455)  2(0.86957  0.76923  0.71429)   I    I(0.15)  I3  2 4 0.15 [2(0.6250  0.57143  0.52632  0.48780)]  0.78992   2  h Now I  h,   I(0.6, 0.3)  2 1 1 Therefore, I(0.6, 0.3)  [4I(0.3)  I(0.6)]  [4(0.79435)  0.81132]  0.78864 3 3

ANMF.CH07_4PP.indd 520

3/1/2023 2:28:44 PM

Numerical Integration • 521

In a similar manner, we obtain 1 h h I  ,   I(0.3, 0.15)  [4I(0.15)  I(0.3)] 3 2 4



1  [4(0.78992  0.79435)]  0.78846 3  h h Hence I  h, ,   I(0.6, 0.3, 0.15)  2 4 1 I(0.6, 0.3, 0.15)  [4I(0.15, 0.3)  I(0.3, 0.6)] 3

or

1  [4(0.78846)  0.78864]  0.78832 3  



The computations are summarized in the table below: 0.81132 0.7864 0.79435

0.78832 0.78846

0.78992

Hence



1.2

0

1 dx  0.78832 correct to five decimal places. 1 x

MATLAB Solution: function y = fex7_17(x) y = 1/(1+x); >> [Integral,neval]=Romberg(@fex7_17,0,1.2) Integral =  0.788457360377354 neval =  33

ANMF.CH07_4PP.indd 521

3/1/2023 2:28:56 PM

522 • Applied Numerical Methods Using MATLAB EXAMPLE 7.18 Apply Romberg’s integration method to find places. Take h = 0.5, 0.25. and 0.125.



1

0

dx , correct to four decimal 1  x2

Solution: Applying the trapezoidal rule, for h = 0.25, we obtain

x

0

0.5

1

1 y = f(x) = (1 + x 2 )

1

0.8

0.5

1 0.5  [1  2(0.8)  0.5]  0.775 2 0 1 x 2

Hence I  

1

For h = 0.25, we have x

0

0.25

0.5

0.75

1

1 y = f(x) = (1 + x 2 )

1

0.9412

0.8

0.64

0.5

Hence I  

1

0

dx 0.25  [1  2(0.9412  0.8  0.64)  0.5]  0.7848 2 1 x 2

Similarly, when h = 0.125, we find I = 0.7848. Applying Equation (7.46), we obtain the table as follows:

Hence, I =



1

0

0.5

0.775

0.25

0.7828

0.7854

0.125

0.7848

0.7855

0.7855

dx = 0.7855 correct to four decimal places. 1  x2

MATLAB Solution: function y = fex7_18(x) y = 1/(1+x^2); >> [Integral, neval] =Romberg (@fex7_18, 0, 1)

ANMF.CH07_4PP.indd 522

3/1/2023 2:29:07 PM

Numerical Integration • 523

Integral =  0.785398163409561 neval =  33

7.8 GAUSSIAN QUADRATURE The Newton-Cotes formulas in Section 7.1 were derived by integrating interpolating polynomials. Since the error term in the interpolating polynomial of degree n involves the (n + 1)st derivative of the function being approximated, a formula of this type is exact when approximating any polynomial of degree less than or equal to n. All Newton-Cotes formulas use values of the function at equally-spaced points. This restriction is convenient when the formulas are combined to form the composite rules, but it can significantly decrease the accuracy of the approximation. The integration formulas of equations for the trapezoidal rule in Section 7.3 and the formulas of equations for the Simpson’s rule in Sections 7.4 and 7.5 are known as interpolating quadratures since the integrand is approximated by an interpolating polynomial at control points x0 through xn. The control points are equally spaced for n values grater than 1. Extended forms of these rules, such as those in Section 7.6 and in Romberg integration, are known as compound quadratures. In all cases, the form of the quadrature is

Integral =

n

 i 0

i

f (x i ) (7.47)

in which the multipliers ai are selected after the control points xi have been chosen. 7.8.1 Gaussian Integration Formulas We have observed in Sections 7.2 to 7.6 that the Newton-Cotes formulas for b

approximating ∫ f (x) dx works very well if f(x) is a smooth function, such as a a

polynomial. This is true for Gaussian integration.

ANMF.CH07_4PP.indd 523

3/1/2023 2:29:13 PM

524 • Applied Numerical Methods Using MATLAB When the nodes xi, i = 0, 1, 2, …., n are unknown then one can develop some methods which give exact result for the polynomials of degree up to (2n − 1). These methods are known as Gaussian quadrature methods. Gaussian formulas are also good at estimating integrals of the form b



∫ w(x) f (x) dx (7.48) a

where w(x), called the weight function, can contain singularities, as long as they are integrable. The basic theorem of Gaussian quadrature states that “the optimal nodes of the m-point Gaussian quadrature formula are precisely the zeros of the orthogonal polynomial for the same integral and weight function.” Gaussian integration is optimal because it fits all polynomials up to degree 2n exactly. To obtain the weights corresponding to the Gaussian nodes xi, we use Lagrange’s interpolating polynomial for f(x) by assuming

m

(x)   (x  x j ) (7.49) j 1

then

m

(x j )   (x j  x i ) (7.50) i 1 i j

The Lagrange’s interpolating polynomial through m points is then given by

(x) f (x j ) (7.51) j1 (x  x j )(x j ) m

(x)  

for arbitrary points x. Now, determine a set of points xi and wj such that for a weight function y(x) the following conditions are valid:

ANMF.CH07_4PP.indd 524

b

b m

a

a

m (x)(x) f (x j ) dx   w i f (x j ) (7.52) j 1 j 1 (x  x i )[(x j )]

 (x) (x) dx   

3/1/2023 2:29:25 PM

Numerical Integration • 525

where the weights in Equation (7.52) are computed from

wj 

b

1 (x)(x) dx     (x j ) a (x  x j )

(7.53)

It can be verified that the error is given by

E

b

f (2 n ) () (x)[(x)]2 dx (7.54) (2 n)! a

We also know that using linear transformation we can transfer any finite interval (a, b) to the interval [−1, 1]. Hence

x

ba ba t  qt  p (7.55) 2 2

b

1

a

1

 f (x) dx   f (qt  p)q dt (7.56)

Therefore, the study of Gaussian quadrature considers the integral of the form

1

m

1

i 1

 (x) f (x) dx   w i f (x i )  E (7.57)

7.8.2 Orthogonal Polynomials The well-known properties of orthogonal polynomials can be employed in the study of Gaussian quadrature. The polynomials jn(x), n = 0, 1, 2, …, n, where n is the degree of the polynomial are said to form an orthogonal set in the interval (a, b) with respect to the weight function w(x) if b



 w(x)  (x)  (x) dx  0, m ≠ n m

n

(7.58)

a

By the choice of the weighting function and the limits of integration, the set can be determined except for a constant factor. In other words, each set of orthogonal polynomials associated with certain w(x), a and b. The constant factor is specified by standardization. Table 7.2 lists the classical orthogonal polynomials in which the last column in the table shows the standardization used.

ANMF.CH07_4PP.indd 525

3/1/2023 2:29:33 PM

526 • Applied Numerical Methods Using MATLAB

TABLE 7.2  Standardization Name

Symbol

a

b

w(x)

b

∫ w(x)[f (t)] dx 2

n

a

Legendre

−1

pn(x)

1

2/(2n + 1)

1

Chebyshev

Tn(x)

−1

1

(1 − x2)−½

Laquerre

Ln(x)

0



e−x

Hermite

Hn(x)

−∞



e− x

π  (n > 0) 2 1

2

π 2n n!

Orthogonal polynomials follow the recurrence relations of the form.

anfn+1(x) = (bn + cn x)fn(x) − dnfn−1(x)(7.59)

Equation (7.59) can be used to compute the other members of the set when the first two polynomials of the set are known. The coefficients in the recurrence formula, along with f0(x) and f1(x), are summarized in Table 7.3. TABLE 7.3  Name

f0(x)

f1(x)

an

bn

cn

dn

Legendre

1

x

n+1

0

2n + 1

n

Chebyshev

1

x

1

0

2

1

Laquerre

1

1−x

n+1

2n + 1

−1

n

Hermite

1

2x

1

0

2

2

The classical orthogonal polynomials can also be determined from the following relationships: (1)n d n [(1  x 2 )n ] n n 2 n ! dx



p n (x ) 



Tn(x) = cos(n cos−1x), n > 0



L n (x ) 



Hn (x)  (1)n e x

ANMF.CH07_4PP.indd 526

ex d n n  x (x e ) n ! dx n 2

d n  x2 (e ) (7.60) dx n

3/1/2023 2:29:42 PM

Numerical Integration • 527

The derivatives in Equation (7.60) are computed from

(1 − x2) p′n(x) = n[−x pn(x) + pn−1(x)]



(1 − x2) T′n(x) = n[−x Tn(x) + nTn−1(x)]



xL′n(x) = n[Ln(x) − Ln−1(x)]



H′n(x) = 2n Hn−1(x)(7.61)

Additional properties of orthogonal polynomials that have relevance to Gaussian quadrature are given below: a) fn(x) has n real, distinct zeros in the interval (a, b) b) the zeros of fn(x) lie between the zeros of fn+1(x) c) any polynomial pn(x) of degree n can be expressed in the form n

p n (x)   c i i (x) 



(7.62)

i 0

d) From Equation (7.62), the orthogonality property in Equation (7.58) is b



 w (x )p

n

(x)n  m (x) dx  0, m ≥ 0 

(7.63)

a

7.8.3 Gauss-Lagendre Quadrature In the quadrature y(x) is 1 and the Equation (7.57) becomes

1

n

1

i 1

 f (x) dx   w i f (x i ) (7.64)

Here we observe that wi and xi are 2n parameters and hence the weights and nodes can be obtained such that the formula is exact when f(x) is a polynomial of degree not exceeding (2n − 1). Let f(x) = a0 + a1x + a2x2 + …. + a2n−1x2n−1(7.65) Hence

1

1

1

1

2 2 n 1  f (x) dx   [a 0  a1 x  a 2 x  ....  a 2 n 1 x ]dx

2 2     2a 0  a 2  a 4  .... (7.66) 3 5

ANMF.CH07_4PP.indd 527

3/1/2023 2:29:53 PM

528 • Applied Numerical Methods Using MATLAB when x = xi, Equation (7.66) becomes

f (x i )  a 0  a 1 x i  a 2 x 2i  a 3 x 3i  ....  a 2 n 1 x 2i n 1 (7.67)

From Equations (7.64) and (7.67), we obtain 1

 f (x) dx  w [a



1

1

0

 a 1 x 1  a 2 x 12  ....  a 2 n 1 x 12 n 1 ]



 w 2 [a 0  a 1 x 2  a 2 x 22  ....  a 2 n 1 x 22 n 1 ]



 w 3 [a 0  a 1 x 3  a 2 x 32  ....  a 2 n 1 x 23 n 1 ]



…….

……



 w n [a 0  a 1 x n  a 2 x 2n  ....  a 2 n 1 x 2nn 1 ]



= a0(w1 + w2 + …. + wn) + a1(w1x1 + w2x2 + …. + wnxn)



+ a 2 (w 1 x 12 + w 2 x 22 + .... + w n x 2n ) + ....



 a 2 n 1 (w 1 x 12 n 1  w 2 x 22 n 1  ....  w n x 2nn 1 ) (7.68)

Since Equations (7.67) and (7.68) are identical, we can compare the coefficients of ai and find 2n equations as given below:

w1 + w2 + …. + wn = 2



w1x1 + w2x2 + …. + wnxn = 0

2 3 ….. …..

w 1 x 12  w 2 x 22  ....  w n x 2n 



w 1 x 12 n 1  w 2 n 22 n 1  ....  w n x 2n1  0 (7.69)

Equation (7.69) is a set of 2n nonlinear equations consisting of 2n unknowns wi and xi, i = 1, 2, …, n. The solution of Equation (7.69) yields the values of wi and xi. Let wi = w *i and xi = x*i , i = 1, 2, …, n be the solution of Equation (7.69). The Gauss-Legendre formula is then given by

ANMF.CH07_4PP.indd 528

1

n

1

i 1

* *  f (x)dx   w i f (x i ) (7.70)

3/1/2023 2:30:16 PM

Numerical Integration • 529

The solution of Equation (7.70) is determined separately for n = 1, 2, and 3 as follows: Case 1: n = 1 The integral is given by 1



 f (x) dx  w f (x ) , 1

1

1

where w1 = 2 and w1x1 = 0 or x1 = 0. Hence, for n = 1 1



 f (x)dx  2f (0) (7.71)

1

Case 2: n = 2 The integral is given by 1



 f (x) dx  w f (x )  w f (x 1

2

1

2

) (7.72)

1

Now, Equation (7.69) becomes

w 1 + w2 = 2



w1x1 + w2x2 = 0



w 1 x 12  w 2 x 22 



w 1 x 13  w 2 x 32  0 (7.73)

2 3

The solution of Equation (7.73) is

w1 = w2 = 1, x 1 

1 1 , x2 = 3 3

Hence, the Equation (7.72) becomes 1



1

ANMF.CH07_4PP.indd 529

 1   1   f 3  3

 f (x) dx  f 

3/1/2023 2:30:33 PM

530 • Applied Numerical Methods Using MATLAB Case 3 n = 3 The integral is given by 1



 f (x) dx = w f(x ) + w f(x ) + w f(x )(7.74) 1

1

2

2

3

3

1

The six unknowns are given as

w1 + w2 + w3 = 2



w1x1 + w2x2 + w3x3 = 0



w 1 x 12  w 2 x 22  w 3 x 23 



w 1 x 13  w 2 x 32  w 3 x 33  0



w 1 x 14  w 2 x 24  w 3 x 34 



w 1 x 15  w 2 x 25  w 3 x 35  0 (7.75)

2 3 2 5

Equation (7.75) may also be obtained by substituting f(x) = 1, x, x2, x3, x4, x5 in Equation (7.74). The solution of Equation (7.75) gives

x1  

3 3 5 8 5 , x2 = 0, x 3   , w 1 = , w 2 = , and w 3 = . 5 5 9 9 9

Substituting the above values in Equation (7.74), we obtain 1



1



 f (x) dx  9  5f  

1



 3  3   8f (0)  5f    (7.76) 5  5  

It can also be shown that xi, i = 1, 2, …., n are the zeros of the nth degree Lagendre’s polynomial

p n (x ) 

1 dn [(x 2  1)n ] 2 n n ! dx n

which can be generated from the recurrence relation given by

ANMF.CH07_4PP.indd 530

(n + 1)pn+1(x) = (2n + 1)x pn(x) − npn−1(x)(7.77)

3/1/2023 2:30:55 PM

Numerical Integration • 531

where p0(x) = 1 and p1(x) = x. Other lower-order Legendre polynomials are

p0(x) = 1 p1(x) = x 1 p2(x) = (3x2 − 1) 2 1 p3(x) = (5x3 − 3x) 2 1 p4(x) = [35x4 − 30x2 + 3] 8

(7.78)

The weights wi are given by

wi 

1

 x  xi   dx (7.79)  i 1  i  x j  n

   x

1

j1

where xi are the known nodes. The weights wi may also be obtained from the relationship 2 wi  (7.80) 2 1  x i [pn (x i )]2





The error of this formula is

E

2 2 n 1 (n !)4 f (2 n ) () , −1 < x < 1 (2 n  1)[(2 n)!]3

(7.81)

The nodes and weights for different values of n are listed in Table 7.4. TABLE 7.4  Values of xi and wi for Gauss-Legendre quadrature. n

Node xi

Weight wi

2 3

±0.57735027 0.00000000 ±0.77459667 ±0.33998104 ±0.86113631 0.00000000 ±0.53846931 ±0.90617985 ±0.23861919 ±0.66120939 ±093246951

1.00000000 0.88888889 0.55555556 0.65214515 0.34785485 0.56888889 0.47862867 0.23692689 0.49791393 0.36076157 0.17132449

4 5

6

ANMF.CH07_4PP.indd 531

Order of truncation error f(4)(x) f(6)(x) f(8)(x) f(10)(x)

f(12)(x)

3/1/2023 2:31:09 PM

532 • Applied Numerical Methods Using MATLAB EXAMPLE 7.19

2

Evaluate the integral I   sin x dx using Gauss-Legendre quadrature. 0

Solution:

We first change variables to x = x + 1, and evaluate the equivalent integral by Gauss-Legendre quadrature. 1



I   sin(  1) d 1

The result for the n = 5 pint calculation is [Refer to Table 7.4]. I = w0f(0) + w1[f(x1) + f(−x1)] + w2[f(x2) + f(−x2) = 0.569 sin(1.0) + 0.237[sin(0.906 + 1) + sin(−0.906 + 1] + 0.479[sin(0.538 + 1) + sin(−0.538 + 1) = 1.4161467. The exact solution is 1 − cos(2) = 1.4161468. EXAMPLE 7.20

1

Approximate the integral I   e x dx using Gaussian quadrature. 0

Solution:

Transforming the integral limits to (−1, 1), we have 1



x  e dx  0

where

 t 1   2 

1

1  1 2 e

2

dt   w i f (t i ) i 1

1 (2 x  a  b) ; a = 0 and b = 1 ba t 1 1 t = 2x − 1 or x  and dx = dt 2 2 t

1  1  Therefore I  1.0  e(0.57735 1)/ 2   1.0  e( 0.57735 1)/ 2   1.71789 2  2  The exact solution is 1



 e dx  e 0

x

x 1 0

 2.71828  1  1.71828

Better approximation to the solution is obtained when we use n = 3 or 4.

ANMF.CH07_4PP.indd 532

3/1/2023 2:31:25 PM

Numerical Integration • 533

EXAMPLE 7.21



Evaluate the integral I   x 3 e x dx . Use Gauss-Lagendre quadrature using 0





n = 1 and n = 3 points. The exact solution is I(0, 2)  ln 1  2 compute the percent error in each case. Solution:



I   x 3 e x dx 0

Using Table 7.4 with f(x) = x3 and n = 1

I1(0, ∞) = w1f(x1) = f(1) = 1

The exact solution is I(0, ∞) = 3! = 1.2.3 = 6  I (0, )  I(0, )  The percent error = 100  1   83.33% I(0, )   For n = 3: Using Table 7.4 for f(x) = x3 and n = 3

I3(0, ∞) = w1f(x1) + w2f(x2) + w3f(x3) = 0.71109301 f(0.41577456)

  + 0.27851773 f(2.29428036) + 0.01038926 f(6.28994509) = 5.987  I (0, )  I(0, )  Percent error = 100  3   2.087% I(0, )   7.8.4 Gauss-Chebyshev Quadrature Method In Gauss-Chebyshev quadrature method, the weight function is assumed as y(x) = (1 − x2)−1/2. The general form of this method is written as 1



n  1 f (x) dx   w i f (x i ) (7.82)   1  x2 i 1 1

The method gives an exact result for polynomial of degree up to (2n − 1). The nodes xi, i = 1, 2, …, n are the zeros of Chebyshev polynomials.

ANMF.CH07_4PP.indd 533



Tn(x) = cos(n cos−1 x)

(7.83)

or

 (2 i  1)  x i  cos  , i = 1, 2, …, n  2 n 

(7.84)

3/1/2023 2:31:39 PM

534 • Applied Numerical Methods Using MATLAB For n = 3, Equation (7.82) becomes 1



 1 f (x) dx  w 1 f (x 1 )  w 2 f (x 2 )  w 3 f (x 3 ) (7.85)   1  x2 1

Gauss-Chebyshev method gives exact values for the polynomials of degree up to 2n − 1, or n = 5. Hence, for f(x) = 1, x, x2, x3, x4, x5, the following relationships can be obtained from Equation (7.85):

w1 + w2 + w3 = p



w1x1 + w2x2 + w3x3 = 0



w 1 x 12  w 2 x 22  w 3 x 23 



 2 3 3 3 w 1 x1  w 2 x 2  w 3 x 3  0



w 1 x 14  w 2 x 24  w 3 x 44 



w 1 x 15  w 2 x 25  w 3 x 54  0 (7.86)

3 8

The nodes xi, i = 1, 2, 3 are obtained from the relationship

 x i  cos(2 i  1) , 6

Hence, x 1 =

i = 1, 2, 3.

(7.87)

 3 3 , x2 = 0, x 3  . 2 2

The solution of the above Equation (7.86) gives

w1  w 2  w 3 

 . 3

Equation (7.85) now becomes 1



ANMF.CH07_4PP.indd 534

  3 

  3   1 f (x) dx  f    f (0)  f    (7.87)  2 3   2  2    1x  1

3/1/2023 2:31:50 PM

Numerical Integration • 535

The nodes are given by  (2 i  1)  x i  cos  , i = 1, 2, …, n  2 n 



and the weights are wi 



   , Tn 1 (x i )Tn (x i ) n

i = 1, 2, …, n

(7.88)

The error term is given by E

2 f (2 n ) (), 2 (2 n)!

−1 < x < 1

2n

(7.89)

The explicit formula is then given by 1



2n

f (x) dx  n   (2 i  1)      2n f (2 n ) () (7.90)   f cos  2 n n ( n )! 2 2 2      1 i 1x 1

Table 7.5 lists the values for the first few points and weights for GaussChebyshev quadrature. TABLE 7.5  Nodes and weights for Gauss-Chebyshev quadrature n

Node xi

Weight wi

2 3

±0.7071068 0.0000000 ±0.8660254 ±0.3826834 ±0.9238795 0.0000000 ±0.5877853 ±0.9510565

1.5707963 1.0471976 1.0471976 0.7853982 0.7853982 0.6283185 0.6283185 0.6283185

4 5

Order of truncation error f(4)(ξ) f(6)(ξ) f(8)(ξ) f(10)(ξ)

7.8.5 Gauss-Laguerre Quadrature Gauss-Laguerre quadrature is a Gaussian quadrature over the internal (0, ∞) with weight function y(x) = e−x. The general form is given by

ANMF.CH07_4PP.indd 535



n

0

i 1

x  e f (x) dx   w i f (x i ) (7.91)

3/1/2023 2:31:58 PM

536 • Applied Numerical Methods Using MATLAB The nodes xi are given by the zeros of the Laguerre polynomial dn x n (e x ) (7.92) dx n which satisfies the recurrence relation

L n (x)  (1)n e x



xL′n(x) = nLn(x) − nLn−1(x)(7.93)

For lower-order Laguerre polynomials are given by

L0(x) = 1, L1(x) = x − 1, L2(x) = x2 − 4x + 2, L3(x) = x3 − 9x2 + 18x − 6

The weights wi are given by

wi 

xi 1  (7.94) 2 2  x i [L n (x i )] (n  1) [L n 1 (x i )]2

The error term is given by

E

(n !)2 (2 n ) f (), 0 < x < ∞(7.95) (2 n)!

Table 7.6 lists the nodes and the corresponding weights of Gauss-Laguerre quadrature. TABLE 7.6  Nodes and weights for Gauss-Laguerre quadrature n

Node xi

Weight wi

2

0.5857864376 3.4142135624 0.4157745568 2.2942803603 6.2899450829 0.3225476896 1.7457611012 4.5366202969 9.3950709123 0.2635603197 1.4134030591 3.5964257710 7.0858100059 12.6408008443

0.8535533906 0.1464466094 0.7110930099 0.2785177336 0.0103892565 0.9031541043 0.3574186924 0.0388879085 0.0005392947 0.5217556106 0.3986668111 0.0759424497 0.0036117587 0.0000233700

3

4

5

ANMF.CH07_4PP.indd 536

Order of Truncation error f(4)(ξ)

f(6)(ξ)

f(8)(ξ)

f(10)(ξ)

3/1/2023 2:32:02 PM

Numerical Integration • 537

7.8.6 Gauss-Hermite Quadrature Gauss-Hermite quadrature is a Gaussian quadrature over the interval (−∞, ∞) − x2 with weight function y(x) = e . The nodes xi are the zeros of the Hermite polynomial Hn(x), where

Hn (x)  (1)n e x

2

d n  x2 (e ) (7.96) dx n

and the zeros are symmetrical about the origin. These polynomials satisfy the recurrence relation

H′n(x) = 2nHn−1(x) = 2nHn(x) − Hn+1(x)

A few Hermite polynomials are:

H0(x) = 1, H1(x) = 2x, H2(x) = 2(2x2 − 1), and H3(x) = 4(2x3 − 3x)

The weights wi are given by

wi 

2 n 1 n !  2 n 1 n !   (7.97) [Hn 1 (x i )]2 n 2 [Hn 1 (x i )]2

The Gauss-Hermite formula is 



e

 x2



n

f (x) dx   w i f (x i ) (7.98) i 1

The error term is given by

E

n !  (2 n ) f () (7.99) 2 n (2 n)!

Table 7.7 lists the nodes and weights for Gauss-Hermite quadrature.

ANMF.CH07_4PP.indd 537

3/1/2023 2:32:10 PM

538 • Applied Numerical Methods Using MATLAB

TABLE 7.7  Nodes and weights for Gauss-Hermite quadrature n

Node xi

Weight wi

2 3

±0.7071068 0.0000000 ±1.2217449 ±0.5246476 ±1.6506801 0.0000000 ±0.958572 ±2.02018

0.8862269 1.1816359 0.2954090 0.8049141 0.0813128 0.945309 0.393619 0.0199532

4 5

Order of truncation error f(4)(ξ) f(6)(ξ) f(8)(ξ) f(10)(ξ)

EXAMPLE 7.22 Evaluate the interval 



I(, ) 

xe

2  x2

dx



using Gauss-Hermite quadrature. Use n = 1 and n = 3 points. The exact solution is

I(, )   / 2

compute the percent error in each case. Solution: 



I(, ) 

xe

2  x2

dx



Using Table 7.7 with f(x) = x2 and n = 1, we have

I(, )  w 1 f (x 1 )  f (0)  0

 I (0, )  I(0, )  Percent error =  1   100% I(0, )   2 Now, using Table 7.7 with f(x) = x and n = 3, we have I3(−∞, ∞) = w1f(x1) + w2f(x2) + w3f(x3)

= 1.18163590f(0) + 0.2950893[f(1.22474487) + f(−1.22474487)] = 0.8862

 I(0, )  I(0, )  Percent error = 100   1.414  10 6%  I(0, )  

ANMF.CH07_4PP.indd 538

3/1/2023 2:32:19 PM

Numerical Integration • 539

7.8.7 MATLAB Programs for Gaussian Quadrature: gaussnodes and gaussquad The user-specified MATLAB function gaussnodes computes the nodal abscissa xi and the corresponding weights wi used in Gauss-Legendre quadrature. The MATLAB program is listed below: function I = Gaussquad (func, a, b, n) % Gauss-Legendre quadrature method % Usage: I = (func, a, b, n) % INPUT: % func = Handle of function to be integrated % a, b = Integration limits % n = Order of integration % OUTPUT: % I = Integral % Constants c1 = (b + a)/2; c2 = (b − a)/2; % Nodal abscissas and weights [x, w] = Gaussnodes (n); sum = 0; for i = 1: length(x) % Function evaluation at node i y = feval (func, c1 + c2∗x (i)); sum = sum + w (i)∗y; end I = c2∗sum; b

The user-specified MATLAB function gaussquad evaluates

∫ f (x) dx

with

a

Gauss-Lagendre quadrature using n nodes. The function defining f(x) should

ANMF.CH07_4PP.indd 539

3/1/2023 2:32:20 PM

540 • Applied Numerical Methods Using MATLAB be provided by the user. The nodal abscissa and the weights are determined by calling gaussnodes MATLAB user-specified function. The MATLAB program is listed below: function [p, dp] = Legendre (t, n) % Evaluates Legendre polynomial p of degree n and its derivative dp at x = t p0 = 1.0; p1 = t; for k = 1: n−1 p = ((2∗k + 1)∗t∗p1 − k∗p0)/ (k + 1); p0 = p1; p1 = p; end dp = n ∗(p0 − t∗p1)/ (1 − t^2); function [x, w] = Gaussnodes (n, tol) % Computes the nodal abscissas x and weights w of Gauss-Legendre n-point quadrature % USAGE: [x, w] = Gaussnodes (n, epsilon, maxIter) % tol = Error tolerance (default value is 1.0e4∗eps) if nargin < 2; tol = 1.0e4∗eps; end w = zeros (n, 1); x = zeros (n, 1); % Number of non negative roots nrts = fix (n + 1)/2; for i = 1: nrts t = cos (pi∗(i − 0.25)/ (n + 0.5)); for j = i: 30 % Newton’s method of root finding [p, dp] = Legendre (t, n); dt = −p/dp; t = t + dt; if abs (dt) < tol

ANMF.CH07_4PP.indd 540

3/1/2023 2:32:20 PM

Numerical Integration • 541

x (i) = t; x (n−i+1) = -t; w (i) = 2/ (1−t^2)/dp^2; w (n−i+1) = w (i); break end end end EXAMPLE 7.23 Evaluate the integral 



2

  sin x  I    dx  x  

with Gauss-Legendre quadrature with n = 4 and n = 5. Solution: For n= 4 function y = fex7_21(x) y = (sin(x)/x) ^2; format long Trial>> a=−pi; b=pi; n=4; Trial>> I=Gaussquad (@fex7_21, a, b, n) I=  2.811347961439513 For n = 6 format long function y = fex7_21(x) y = (sin(x)/x) ^2; Trial>> a=−pi; b=pi; n=6; Trial>> I=Gaussquad (@fex7_21, a, b, n)

ANMF.CH07_4PP.indd 541

3/1/2023 2:32:22 PM

542 • Applied Numerical Methods Using MATLAB I=  2.836185357709455 EXAMPLE 7.24 The error function in statistics is defined as

ef (x) 

x

2 2 e t dt  0

Write MATLAB program that uses Gauss-Legendre quadrature to find ef(x) for a given x to six decimal places. It is known that ef(x) = 1 correct to 6 decimal places when x > 5. Check the program by verifying that ef(1.0) = 0.842701. Solution: Trial>> clear format long func=inline (‘exp (−t^2)’,’t’); x=input (‘x==>’); if x>5; ef=1 else Iold=Gaussquad (func, 0, x, 2); for n=3:12 Inew=Gaussquad (func, 0, x, n); if abs (Inew-Iold) 1 Iold =  0.746814584191254

ANMF.CH07_4PP.indd 542

3/1/2023 2:32:23 PM

Numerical Integration • 543

Iold =  0.746824468130286 ef =  0.842700786127327 EXAMPLE 7.25 Evaluate the integral 2 



  y sin x  x cos y  0

using MATLAB built-in command function dblquad. Solution: Pass M-file function handle @integrnd to dblquad: Q = dblquad(@integrnd,pi,2∗pi,0,pi); where the M-file integrnd.m is function z = integrnd(x, y) z = y∗sin(x)+x∗cos(y); Pass anonymous function handle F to dblquad: F = @(x,y)y∗sin(x)+x∗cos(y); Q = dblquad(F,pi,2∗pi,0,pi); The integrnd function integrates y∗sin(x)+x∗cos(y) over the square pi > y=Euler(a,b,y0,n,@fex8_6) y= 1.0000 0.7654 0.6208 0.5209 0.4472 0.3904 0.3452 0.3085 0.2779 0.2522

ANMF.CH08_4PP.indd 573

3/1/2023 2:38:06 PM

574 • Applied Numerical Methods Using MATLAB EXAMPLE 8.7 Apply Euler’s method to approximate the solution of the initial value problem dy = −2t y2 with y(0) = 1 in the interval 0 ≤ t ≤ 0.5, using h = 0.1 and compute dt the error and the percentage error. The exact solution is y = 2 1 . (t + 1) Solution: Here, Equation (8.26) becomes

yn+1 = yn + h f(xn, yn)

Since h = 0.1 and f(xn, yn) = −2tn y 2n , we have

yn+1 = yn − 2h tn y 2n n = 0, 1, 2, ….

For h = 0.1, we set n = 0 and compute n = 0:

y1 = y0 − 2(0.1) t0 y 20 = 1 − 2(0.1)(0)(1)2 = 1

which will be our estimate to the value y(0.1). Continuing, we obtain n = 1:

y2 = y1 − 2(0.1) t1 y 12 = 1 − 2(0.1)(0.1)(1)2 = 0.98

n = 2:

y3 = y2 − 2(0.1) t2 y 22 = 0.98 − 2(0.1)(0.2)(0.98)2 = 0.9416

and so on.

1 . t +1 Table 8.3 gives a summary of the results computed for 0 ≤ t ≤ 0.5. The exact value is given by y =

2



Error = Exact value − yn (from Euler’s method)



Percentage error =

| Error | × 100 Exact value

From Table 8.3, we note that Euler’s method works quite well and the errors are acceptable in many practical applications. TABLE 8.3

ANMF.CH08_4PP.indd 574

n

t

yn

Exact value

Error

Percentage error

0 1 2 3 4 5

0 0.1 0.2 0.3 0.4 0.5

1 1 0.98 0.9416 0.8884 0.8253

1 0.9901 0.9615 0.9174 0.8621 0.8

0 0.0099 0.0185 0.0242 0.0263 0.0253

0 0.9998 1.9241 2.6379 3.0507 3.1625

3/1/2023 2:38:17 PM

Numerical Solution of Ordinary Differential Equations • 575

MATLAB Solution: function y = fex8_7(t,y) >>y = −2∗ t∗ y^2; >> a=0;b=0.5;y0=1.0;n=5; >> y=Euler(a,b,y0,n,@fex8_7) y= 1.0000 0.9800 0.9416 0.8884 0.8253 0.7571 EXAMPLE 8.8

dy = x + y, dx y(0) = 1 using increments of size h = 0.2. The exact solution is y = −1 − x + 2ex. Determine the error and the percentage error at each step. Apply the Euler’s method to the ordinary differential equation

Solution: dy =x+y dx when x = 0 and y(0) = 1. Hence

dy = x + y = 0 + 1 = 1 or y0 = 1. dx

Now, h = 0.2 and y1 = y0 + h f(xn, yn) or y1 = y0 + h f(x0, y0) = 1 + 0.2(1.0) = 1.2 The process is continued as shown in Table 8.4. Exact value at xn = 0.2 is

ANMF.CH08_4PP.indd 575

y0.2 = −1 − 0.2 + 2e0.2 = 1.2428

3/1/2023 2:38:20 PM

576 • Applied Numerical Methods Using MATLAB Table 8.8 gives the summary of the computations. Error = Exact value − value from Euler’s method. | Error | Percentage error = Exact value TABLE 8.4 n

t

yn

Exact value

Error

Percentage error

0 1 2 3 4 5

0 0.2 0.4 0.6 0.8 1.0

1 1.2 1.48 1.856 2.3472 2.9766

1 1.2428 1.5836 2.0442 2.6511 3.4366

0 0.0428 0.1036 0.1882 0.3039 0.46

0 3.4438 6.5421 9.2065 11.4632 13.3853

MATLAB Solution: function y = fex8_8(t,y) >>y = x+y; >> a=0;b=1;y0=1.0;n=5; >> y=Euler(a,b,y0,n,@fex8_8) y= 1.0000 1.2400 1.5680 2.0016 2.5619 3.2743 EXAMPLE 8.9

dy = 1 − t + 4y, y(0) = 1, dt in the interval 0 ≤ t ≤ 0.5 with h = 0.1. The exact value is −9 1 19 y = + t + e4 t 16 4 16 Compute the error and the percentage error. Use Euler’s method to solve the initial value problem

ANMF.CH08_4PP.indd 576

3/1/2023 2:38:31 PM

Numerical Solution of Ordinary Differential Equations • 577

Solution: Here, f(tn, yn) = 1 − tn + 4yn and thus

yn+1 = yn + (0.1)(1 − tn + 4yn)

For n = 0:

y1 = y0 + (0.1)(1 − t0 + 4y0) = 1 + (0.1)(1 − 0 + 4) = 1.5

n = 1:

y2 = y1 + 0.1(1 − t1 + 4y1) = 1.5 + (0.1)(1 − 0.1 + 6) = 2.19

The exact value is computed from −9 1 19 + t + e4 t 16 4 16



y=



Error = Exact value − value from Euler’s method



Percentage error =

| Error | Exact value

Table 8.5 summarizes the computed results. TABLE 8.5 n

t

yn

Exact value

Error

Percentage error

0 1 2 3 4 5

0 0.1 0.2 0.3 0.4 0.5

1 1.5 2.19 3.146 4.4744 6.3242

1 1.6090 2.5053 3.8301 5.7942 8.7120

0 0.109 0.3153 0.6841 1.3192 2.3878

0 6.7768 12.5864 17.8620 22.7783 27.4082

MATLAB Solution: function y = fex8_9(t,y) y =1−t + 4∗ y; >> a=0;b=1;y0=1.0;n=5; >> y=Euler(a,b,y0,n,@fex8_9) y= 1.0000 1.9600 3.6480 6.6464 12.0035 21.6063

ANMF.CH08_4PP.indd 577

3/1/2023 2:38:34 PM

578 • Applied Numerical Methods Using MATLAB EXAMPLE 8.10

dy 1 = y, Use Euler’s method to solve the following differential equation dx 2 y(0) = 1 and 0 ≤ x ≤ 0.5. Use h = 0.1. Solution: Using Equation (8.26)

y1 = y0 + h f(x0, y0) = 1 + 0.1 f(0, 1)

1 1 y 0 = (1) = 1 /2 2 2 Hence y1 = 1 + 0.1(1/2) = 1.05.

f(0, 1) = f(x0, y0) =

For n = 1: where

y2 = y1 + h f(x1, y1) = 1.05 + 0.1 f(0.1, 1.05)

f(0.1, 1.05) =

1 (1.05) = 0.525 2

Therefore, at x2 = 2h = 2(0.1) = 0.2 y2 is

y2 = 1.05 + 0.1(0.525) = 1.1025

The exact values of y = ex/2 (from direct integration). This procedure is repeated for n = 2, …, 5 and a summary of the results obtained is given in Table 8.6. TABLE 8.6  Euler’s method versus exact solution n

xn

yn

f(xn, yn)

yn+1 (Euler)

yn+1 = ex/2 (exact)

0 1 2 3 4 5

0 0.1 0.2 0.3 0.4 0.5

1 1.05 1.1025 1.1576 1.2155 1.2763

0.5 0.525 0.5513 0.5788 0.6077 0.6381

1.05 1.1025 1.1576 1.2155 1.2763 1.3401

1.0513 1.1052 1.1619 1.2214 1.2840 1.3499

MATLAB Solution: function y = fex8_10(x,y) y =0.5∗ y; >> a=0;b=1;y0=1.0;n=5; >> y=Euler(a,b,y0,n,@fex8_10)

ANMF.CH08_4PP.indd 578

3/1/2023 2:38:37 PM

Numerical Solution of Ordinary Differential Equations • 579

y= 1.0000 1.1250 1.2656 1.4238 1.6018 8.3.3 Modified Euler’s Method The modified Euler’s method (also called Heun’s method) is a single-step, explicit, numerical technique for solving a first-order ordinary differential equation. The method is a modification of Euler’s explicit method. In Section 8.3.1 on Euler’s method, we stated the assumption in that method is that in each subinterval or step, the derivative or the slope between points (xn, yn) and (xn+1, yn+1) is constant and equal to the slope of y(x) at point (xn, yn). This assumption causes error. In the modified Euler’s method, the slope used for computing the value of yn+1 is modified to include the effect of that the slope changes within the subinterval. This slope is the average of the slope at the beginning of the interval and an estimate of the slope at the end of the interval. Hence, the slope at the beginning of the interval is given by

dy dx

= slope at x = xn = f(xn, yn)(8.27) x = xn

The slope at the end of the interval is estimated by finding first an approximate value for yn+1, written as y mn +1 using Euler’s explicit method. That is y mn +1 = ym + f(xn, yn)h 

(8.28)

The estimation of the slope at the end of interval is obtained by substituting dy the point (xn+1, y mn +1) in the equation for , dx dy m = f (x n +1 , y n +1 )  or (8.29) dx yx +=xy mn+1 n +1

yn+1 is then estimated using the average of the two slopes. That is, y n +1 = y n +

ANMF.CH08_4PP.indd 579

f (x n , y n ) + f (x n +1 , y mn +1 ) h (8.30) 2

3/1/2023 2:38:45 PM

580 • Applied Numerical Methods Using MATLAB The modified Euler’s method is illustrated in Figure 8.3. The slope at the beginning of the interval (given by Equation (8.27)) and the value of y mn +1 as per Equation (8.28) are shown in Figure 8.3(a). Figure 8.3(b) shows the estimated slope at the end of the interval as per Equation (8.29). The value of yn+1 obtained using Equation (8.30) is shown in Figure 8.3(c).

y

y y(x)

y(x) Exact solution

m yn+1

yn

xn+1

h

Slope: f(xn+1, ym n+1 )

yn

Slope: f(xn, yn)

xn

ymn+1

Exact solution

x

(a) Slope atthe beginning of the interval

h

xn



xn+1

x

(b) Estimate of the slope at the end of the interval

y y(x)

yn+1 Exact solution Slope: f (xn , yn ) + f (x n+1 , y m n+1 ) 2

yn

xn

h

xn+1

x

(c) Using the average of the two slopes

FIGURE 8.3  The modified Euler’s method.

In the modified Euler’s method, instead of approximating (x, y) by f(x0, y0) in Equation (8.22), the integral in Equation (8.23) is approximated using the trapezoidal rule. Therefore

y 1(1) = y 0 +

h f (x , y ) + f (x 1 , y 1(0) ) (8.31) 2 0 0

where y 1(0) = y0 + h f(x0, y0) obtained using Euler’s formula.

ANMF.CH08_4PP.indd 580

3/1/2023 2:38:51 PM

Numerical Solution of Ordinary Differential Equations • 581

Similarly, we obtain

y 1(2) = y 0 +

h f (x , y ) + f (x 1 , y 1(1) ) 2 0 0



y 1(3) = y 0 +

h f (x , y ) + f (x 1 , y 1(2) ) 2 0 0



y 1( 4) = y 0 +

h f (x 0 , y 0 ) + f (x 1 , y 1(3) ) (8.32) 2

and so on. Therefore, we have

y 1( n +1) = y 0 +

h f (x , y ) + f (x 1 , y 1( n ) ) n = 0, 1, 2, 3, … 2 0 0

(8.33)

where y 1(n ) is the nth approximation to y1. The iteration formula given by Equation (8.33) can be started by selecting y 1(0) from the Euler’s formula. The formula given by Equation (8.33) is terminated at each step if the condition y (nn ) − y (nn −1) < ∈, where ∈ is a very small arbitrary value selected depending on the level of accuracy to be accomplished is satisfied. If this happens for sa, n = k, then we consider y n = y (nk ) and continue to compute the value of y at the next point by repeating the procedure described above. Equation (8.33) can also be written as

1 y n +1 = y n + (K1 + K2 ) + (O)h 3  2

where K1 = h f(xn, yn) 

K2 = h + (xn+1, yn + K1) 

(8.33a) (8.33b) (8.33c)

8.3.4 MATLAB Program for the Modified Euler’s Method: modeuler function y = ModEuler(dydx,n,xf,x0,y0) % ModEuler integrates a set of first order differential equations of % the form y’ = dydx(x,y) with initial conditions y(x0)=y0. The % Equations are integrated in n steps from x0 to xf. % Usage: y = ModEuler(dydx,n,xf,x0,y0) dx = (xf−x0)/n; F = feval(dydx,x0,y0); Y = [y0 y0] + dx∗ [F 2∗ feval(dydx,x0+dx,y0+dx∗ F)];

ANMF.CH08_4PP.indd 581

3/1/2023 2:39:03 PM

582 • Applied Numerical Methods Using MATLAB for i = 2:n−1 F = feval(dydx,x0+i∗ dx,Y(:,2)); Y = [Y(:,2) Y(:,1)+2∗ dx∗ F]; end y = .5∗ (Y(:,2)+Y(:,1)+dx∗ feval(dydx,xf,Y(:,2))); EXAMPLE 8.11

dy Use the modified Euler’s method to solve the differential equation = x + y2 dx with y(0) = 1. Take the step size h = 0.1. Solution: From Equation (8.31), we have

where

h f (x , y ) + f (x 1 , y 1(0) ) 2 0 0 y 1(0) = y0 + h f(x0, y0)

Therefore

y 1(1) = 1 +



y 1(1) = y 0 +

h (0 + 12 ) + (0.1 + (1 + 0.1(0 + 12 )2 )] [ 2

= 1 + 0.05[1 + (0.1 + 1.12)] = 1.1155 is the improved Euler’s estimate. h y 1(2) = y 0 + f (x 0 , y 0 ) + f (x 1 , y 1(1) ) Similarly, 2 (1) where = 1.1155 y1

h f (x , y (1) ) + f (x 2 , y 1(1) + h f (x 1 , y 1(1) ) 2 1 1 0.1 = 1.1155 + [(0.1 + 1.11552 ) + (0.2 + (1.1155 + 0.1(0.1 + 1.11552 )))]] = 1.2499 2 y 1(2) = y 1(1) +

0.1 = 1.1155 +   [(0.1 + 1.11552 ) + (0.2 + (1.1155 + 0.1(0.1 + 1.11552 )))]] = 1.2499 2 is the Euler’s method estimate starting from (x 1 , y 1(1) ). Now, starting from [x1, y0 + h f(x0, y0)], we have

2 2 y 1(2) = 1.1155 + 0.05[(0.1 + 1.1155 ) + (0.2 + 1.2499 )] = 1.2708

is the improved Euler’s estimate.

ANMF.CH08_4PP.indd 582

3/1/2023 2:39:20 PM

Numerical Solution of Ordinary Differential Equations • 583

MATLAB Solution: function y = fex8_11(x,y) y =x+y^2; >> y=ModEuler(@fex8_11,1,0.1,0,1.0) y= 1.2656 EXAMPLE 8.12 Use the modified Euler’s method to obtain an approximate solution of dy = −2ty2, y(0) = 1, in the interval 0 ≤ t ≤ 0.5 using h = 0.1. Compute the error dt and the percentage error. Given the exact solution is given by

y=

1 (1 + t 2 )

Solution: For n = 0:

y 1(1) = y0 − 2h t0 y 20 = 1 − 2(0.1) (0) (1)2 = 1

Now

y 1(1) = y 0 +

h  −2 t 0 y 20 − 2 t 1 y 1(1)2  2

= 1 − (0.1)[(0) (1)2 + (0.1) (1)2] = 0.99



Table 8.7 shows the remaining calculations. Table 8.7 also shows the values obtained from the Euler’s method, the modified Euler’s method, the exact values, and the percentage error for the modified Euler’s method. TABLE 8.7

ANMF.CH08_4PP.indd 583

n

tn

Euler yn

Modified Euler yn

Exact value

Error

Percentage Error

0 1 2 3 4 5

0 0.1 0.2 0.3 0.4 0.5

1 1 0.9800 0.9416 0.8884 0.8253

1 0.9900 0.9614 0.9173 0.8620 0.8001

1 0.9901 0.9615 0.9174 0.8621 0.8000

0 0.0001 0.0001 0.0001 0.0001 0.0001

0 0.0101 0.0104 0.0109 0.0116 0.0125

3/1/2023 2:39:30 PM

584 • Applied Numerical Methods Using MATLAB In the Table 8.7, Error = Exact value − value from the modified Euler’s method | Error | Percentage error = . Exact value MATLAB Solution: function y = fex8_12(t,y) y =−2∗ t∗ y^2; >> y=ModEuler(@fex8_12,5,0.5,0,1.0) y= 0.8002 EXAMPLE 8.13 Use the modified Euler’s method to find the approximate value of y(1.5) for dy the solution of the initial value problem = 2xy, y(1) = 1. Take h = 0.1. The dx x 2 −1 exact solution is given by y = e . Determine the relative error and the percentage error. Solution: With x0 = 1, y0 = 1, f(xn, yn) = 2xnyn, n = 0 and h = 0.1, we first compute y 1(0) = y0 + h f(x0, y0) from Equation (8.31).

y 1(0) = y0 + (0.1) 2(x0, y0) = 1 + (0.1) 2(1)(1) = 1.2

We use this value in Equation (8.33) along with

x1 = 1 + h = 1 + 0.1 = 1.1 0.1  0.1  y 11 = y 0 +  2 x y + 2 x 1 y 1 = 1 +  2(1)(1) + 2(1.1)(1.22) = 1.232  2  0 0  2 

Table 8.8 gives the values computed for the modified Euler’s method, exact value, relative error, and the percentage error. Exact value is calculated 2 from y = e x −1. Error = Exact value − value from the modified Euler’s method | Error | Percentage relative error = . Exact value

ANMF.CH08_4PP.indd 584

3/1/2023 2:39:51 PM

Numerical Solution of Ordinary Differential Equations • 585

TABLE 8.8 n

xn

yn

Exact value

Absolute error

Percentage Relative error

0 1 2

1 1.1 1.2

1 1.2320 1.5479

1 1.2337 1.5527

0 0.0017 0.0048

0 0.14 0.31

3 4 5

1.3 1.4 1.5

1.9832 1.5908 3.4509

1.9937 2.6117 3.4904

0.0106 0.0209 0.0394

0.53 0.80 1.13

MATLAB Solution: function y = fex8_13(x,y) y = 2∗ x∗ y; >> y=ModEuler(@fex8_13,5,1.5,1.0,1.0) y= 3.4456 EXAMPLE 8.14 Repeat Example 8.10 using the Modified Euler’s method. Solution: From Equations (8.33a) to (8.33c), we have

1 1 K1 = h f (x 0 , y 0 ) = h  y 0  = 0.1   = 0.05 2   2

1 + 0.05   y + K1  = 0.0525 K 2 = h f ( x 1 , y 0 + K1 ) = h  0 = 0.1    2   2  The functional approximate at x1 = 0.1 (n = 1) is given by and

1 1 y 1 = y 0 + (K1 + K2 ) = 1 + (0.05 + 0.0525) = 1.05125 ≈ 1.0513 2 2 Hence, at x2 = 0.2, we have



ANMF.CH08_4PP.indd 585

0.05125  = 0.0526 K1 = 0.1   2 

3/1/2023 2:39:57 PM

586 • Applied Numerical Methods Using MATLAB



1.0513 + 0.0526  K2 = 0.1   = 0.0552 2 



1 y 2 = 1.0513 + (0.0526 + 0.0552) = 1.1051 2

This procedure is repeated for n = 2, 3, 4, and 5 to give the functional approximations shown in Table 8.9. TABLE 8.9 n

xn

yn

K1

K2

yn+1 (modified Euler)

yn+1 (exact)

0 1 2 3 4 5

0 0.1 0.2 0.3 0.4 0.5

1 1.0513 1.1051 1.1618 1.2213 1.2839

0.05 0.0526 0.0526 0.0581 0.0611 0.0642

0.0525 0.0552 0.0581 0.0699 0.0641 0.0674

1.0513 1.1051 1.1618 1.2213 1.2839 1.3513

1.0513 1.1052 1.1619 1.2214 1.2840 1.3499

Table 8.9 clearly shows that the modified Euler’s method gives better accuracy for the same h interval when compared with the basic Euler’s method. MATLAB Solution: function y = fex8_13(x,y) y =0.5∗ y; >> y=ModEuler(@fex8_14,5,0.5,0,1.0) y= 1.2839 8.3.5 Runge-Kutta Methods Runge-Kutta methods are a family of single-step, explicit, numerical techniques for solving a first-order ordinary differential equation. Various types of Runge-Kutta methods are classified according to their order. The order identifies the number of points within the subinterval that are utilized for finding the value of the slope in Equation (8.7). For instance, second-order RungeKutta methods use the slope at two points, third-order methods use threepoints, and so on. The classical Runga-Kutta method is of order four and uses four points. Runge-Kutta methods give more accurate solutions compared to

ANMF.CH08_4PP.indd 586

3/1/2023 2:40:00 PM

Numerical Solution of Ordinary Differential Equations • 587

the simpler Euler’s explicit method. The accuracy increases with increasing the order of the Runge-Kutta method. 8.3.5.1 The Runge-Kutta Method of Order Two In the Runge-Kutta method of order two, we consider up to the second derivative term in the Taylor series expansion and then substitute the derivative terms with the appropriate function values in the interval. Consider the Taylor series expansion of the function about yn.

h2 y′′(xn, yn) 2 2 yn+1 = yn + hg(xn, yn) + h g′(xn, yn) 2 h   yn+1 = yn + h g(x n , y n ) + g ′(x n , y n ) (8.34) 2   yn+1 = yn + hy′(xn, yn) +

Now, substituting

g ′( x n , y n ) =

∂g ∂g + g( x n , y n ) ∂x ∂y

dy = g(xn, yn) dx From the differential equation, we obtain where



h ∂g h ∂g   g(x n , y n ) (8.35) + y n + 1 = y n + h  g( x n , y n ) + 2 ∂x 2 ∂y  

It should be noted here that the factor inside the square brackets consisting of the derivatives may be substituted with a function of the type ag(x + α, y + β) in a Taylor series expansion, such that from Equation (8.34), we have

yn+1 = yn + h[ag(xn + α, yn + β)] 

(8.36)

Now, expanding the function g(xn + α, yn + β) in Equation (8.36) in a Taylor series expansion with two variables about (xn, yn) and considering only the first derivative terms, we obtain ∂g ∂g   y n +1 = y n + ha g(x n , y n ) + α + β  (8.37) ∂x ∂y  

ANMF.CH08_4PP.indd 587

3/1/2023 2:40:19 PM

588 • Applied Numerical Methods Using MATLAB Now, equating the coefficients of the respective terms on the right-hand side of Equations (8.35) and (8.37), we obtain

a=1



α = h/2 

and

β = h/2 g(xn, yn)

(8.38)

Therefore, Equation (8.36) becomes

h h y n +1 = y n + hg  x n + , y n + g(x n , y n ) (8.39) 2 2  

Equation (8.39) can also be rewritten as

yn+1 = yn + hK­2(8.40)

where

K  h K2 = hg  x n + , y n + 1  (8.41) 2 2  

in which K1 = hg(xn, yn) 

(8.42)

The Runge-Kutta method of order two is also known as the midpoint method because the derivative is replaced by functions evaluated at the midpoint xn + h/2. The midpoint method is illustrated schematically in Figure 8.4. The determination of the midpoint with Euler’s explicit method using ym = yn + f(xn, yn) h/2 is shown in Figure 8.4(a). Figure 8.4(b) shows the estimated slope that is computed with the equation

dy dx

x = xm

= f (x m , y m ).

Figure 8.4(c) shows the value of yn+1 obtained using yn+1 = yn + f(xm, ym)h. The local truncation error in the Runge-Kutta method of order two is O(h3), and the global truncation error is O(h2). Note that this is smaller by a factor of h than the truncation errors in Euler’s explicit method. In other words, for the same accuracy, a larger step size can be used. However, in each step, the function f(x, y) in the Runge-Kutta method of order two is computed twice.

ANMF.CH08_4PP.indd 588

3/1/2023 2:40:24 PM

Numerical Solution of Ordinary Differential Equations • 589

y

y Exact solution

Exact solution

y(x)

y(x)

yh/2

yh/2 Slope: f(xn, yn)

yn xn

xh/2

h/2

xn+1

xn

h



Slope: f(xh/2, yh/2)

yn

x

(a) Euler’s method to calculate yh/2

xh/2

xn+1

x

  (b) Calculation of the slope at (xh/2, yh/2)

y Exact solution y(x)

yn Slope: f(xh/2, yh/2) xn

h

x xn+1

(c) Calculation of the numerical solution yn+1 FIGURE 8.4  The midpoint method.

EXAMPLE 8.15 Use the second-order Runge-Kutta method with h = 0.1, find y1 and y2 for dy = −xy2, y(2) = 1. dx Solution: For f(x, y) = −xy2, the modified Euler’s method, Equation (8.40) is

ANMF.CH08_4PP.indd 589



yn+1 = yn − 0.1(xn + 0.05)[yn + 0.05fn]2,

where

f n = − x n y 2n .

3/1/2023 2:40:27 PM

590 • Applied Numerical Methods Using MATLAB n = 0: Here

x0 = 2 and y0 = 1, hence f0 = −2(1)2 = −2



y1 = 1 − 01(2 + 0.05)[1 + 0.05(−2)]2 = 0.83395

n = 1: Now

x1 = 2.1 and y1 = 0.83395; hence f1 = −x1 y 2n = −1.46049

Hence, y2 = 0.83395 − 0.1(2.1 + 0.05)[0.83395 + 0.05(−1.46049)]2 = 0.70946. Relative error when n = 0 is

E1(0.1) = 0.8299 − 0.83395 − 0.00405

and

E2(0.1) = 0.7042 − 0.70946 − 0.00526.

Comparing these values (y1 and y2) with the exact values obtained in Table 8.10, we see that the second-order Runge-Kutta method do indeed give accuracy comparable to the second-order Taylor’s series method without requiring partial derivatives. EXAMPLE 8.16

dy = sin y with dx y(0) = 1 from x = 0 to 0.5 in steps of h = 0.1. Keep four decimal places in the calculations. Use the Runge-Kutta method of order two to integrate

Solution: Here

g(x, y) = sin y

Hence, the integration formulas in Equations (8.41) to (8.42) are

K1 = hg(x, y ) = 0.1 sin y

K 1 h K2 = hf  x + , y + K1  = 0.1 sin  y + 1     2 2 2 y(x + h) = y(x) + K2

Given that y(0) = 1, we can carry out the integration as follows:

K1 = 0.1 sin(1) = 0.0841



0.0841  K2 = 0.1 sin  1 +  = 0.0863  2 

ANMF.CH08_4PP.indd 590

3/1/2023 2:40:34 PM

Numerical Solution of Ordinary Differential Equations • 591



y(0.1) = 1 + 0.0863 = 1.0863



K1 = 0.1 sin(1.0863) = 0.0885



0.0885  K2 = 0.1 sin  1.0863 +  = 0.0905  2 



y(0.2) = 1.0863 + 0.0905 = 1.1768

and so on. The computations are summarized in Table 8.10 to four decimal places. TABLE 8.10 x

y

K1

K2

0 0.1 0.2 0.3 0.4 0.5

1 1.0863 1.1768 1.2708 1.3676 1.4664

0.0841 0.0885 0.0925 0.0955 0.0979

0.0863 0.0905 0.0940 0.0968 0.0988

8.3.5.2 The Runge-Kutta Method of Order Four In the classical Runge-Kutta method of order four, the derivatives are evaluated at four points, once at each end and twice at the interval midpoint as given below: h y(xn+1) = y(xn) + (K1 + 2K2 + 2K3 + K4)(8.43) 6 where K1 = g[xn, yn(xn)]

h 1 K2 = g  x n + , y (x n ) + K1 h  2 2  



h 1 K3 = g  x n + , y (x n ) + K2 h  2 2  

and

K4 = g[xn + h, y(xn) + K3h](8.44)

The classical Runge-Kutta method of order four is illustrated schematically in Figure 8.5, Figures 8.5(a) through 8.5(c) show the determination of the slopes in Equation (8.4). Figure 8.5(a) shows the slope K1 and how it is used to compute slope K2. Figure 8.5(b) shows how slope K2 is used to find the slope K3. Figure 8.5(c) shows how slope K3 is used to find the slope K4.

ANMF.CH08_4PP.indd 591

3/1/2023 2:40:43 PM

592 • Applied Numerical Methods Using MATLAB Figure 8.5(d) shows the application of Equation (8.43) where the slope used for evaluating yn+1 is a weighted average of the slopes K1, K2, K3, and K4. y

y

y(x)

y(x)

Exact solution 1 y n + K1h 2

Slope: K3 Slope: K2

yn

yn

Slope: K2

Slope: K1 xn

Exact solution

1 y n + K1h 2

1 xn + h 2 h

x

xn

xn+1

1 xn + h 2 h

(a)

x xn+1

(b) y(x)

y

y yn + K3h

1 Slope: (K1+2K2+2K3+K4) 6

Exact solution

y(x)

Numerical solution Exact solution

Slope: K4 Slope: K3

yn

yn x xn+1 = xn + h

x xn

h

xn+h

(c)

xn

h (d)

FIGURE 8.5  The classical fourth-order Runge-Kutta method.

The local truncation error in the classical Runge-Kutta method of order four is O(h5), and the global truncation error is O(h4). This method gives the most accurate solution compared to the other methods. Equation (8.44) is the most accurate formula available without extending outside the interval [xn, xn+1]. Equations (8.43) and (8.44) can also be written as 1 yn+1 = yn + [K1 + 2K2 + 2K3 + K4(8.44a) 6 where K1 = hf(xn, yn)

ANMF.CH08_4PP.indd 592

h h K2 = hf  x n + , y n +   2 2

3/1/2023 2:40:47 PM

Numerical Solution of Ordinary Differential Equations • 593

and

K h K3 = hf  x n + , y n + 2   2 2 K4 = hf(xn + h, yn + K3)(8.44b)

EXAMPLE 8.17 Use the Runge-Kutta method of order four with h = 0.1 to obtain an approxidy mation to y(1.5) for the solution of = 2xy, y(1) = 1. The exact solution is dx 2 given by y = e x −1. Determine the relative error and the percentage relative error. Solution: For n = 0, from Equation (8.44), we have

K1 = g(x0, y0) = 2x0 y0 = 2



1 1 1 1  K2 = g  x 0 + (0.1), y 0 + (0.1)(2) = 2  x 0 + (0.1)  y 0 + (0 0.2) 2 2 2 2     



= 2.31 1 1 1 1 K3 = g  x 0 + (0.1), y 0 + (0.1)2.31 = 2  x 0 + (0.1)  y 0 + (0.231) 2 2 2 2      = 2.3426 K4 = g[x0 + 0.1, y0 + 0.1(2.3426) = 2(x0 + 0.1)(y0 + 0.2343) = 2.7154

h Hence y 1 = y 0 + [K1 + 2K2 + 2K3 + K 4 6 0.1 =1+ [2 + 2(2.31) + 2(2.3426) + 2.7154] 6 = 1.2337 Table 8.11 summarizes the computations. In Table 8.11, exact value is 2 computed from y = e x −1. The absolute error = exact value minus the value from the Runge-Kutta method. Percentage relative error = |Error|/Exact value.

ANMF.CH08_4PP.indd 593

3/1/2023 2:40:59 PM

594 • Applied Numerical Methods Using MATLAB

TABLE 8.11 n

xn

yn

Exact value

Absolute error

Percentage relative error

0 1 2

1 1.1 1.2

1 1.2337 1.5527

1 1.2337 1.5527

0 0 0

0 0 0

3 4 5

1.3 1.4 1.5

1.9937 2.6116 3.4902

1.9937 2.6117 3.4904

0 0.0001 0.0001

0 0 0

8.3.5.3 MATLAB Program for the Runge Kutta Method of Order Four: RK4 function [xsolution,ysolution] = RK4(eqn,x,y,xf,h) % Fourth order Runge-Kutta integration method % USAGE: [xsolution,ysolution] = RK4(eqn,x,y,xf,h) % INPUT: % eqn = Handle of function that specifies the % first order differential equation % x,y = Initial values and y must be row vector % xf = Terminal value of x. % h = Increment of x used in integration % OUTPUT: % xsolution = x values of the solution % ysolution = y values corresponding to the x values if size(y,1) > 1 ; y = y’; end xsolution = zeros(2,1); ysolution = zeros(2,length(y)); xsolution(1) = x; ysolution(1,:) = y; i = 1; while x < xf i = i + 1; h = min(h,xf − x); K1 = h∗feval(eqn,x,y); K2 = h∗ feval(eqn,x + h/2,y + K1/2); K3 = h∗ feval(eqn,x + h/2,y + K2/2); K4 = h∗ feval(eqn,x+h,y + K3);

ANMF.CH08_4PP.indd 594

3/1/2023 2:41:00 PM

Numerical Solution of Ordinary Differential Equations • 595

y = y + (K1 + 2∗ K2 + 2∗ K3 + K4)/6; x = x + h; xsolution(i) = x; % Store the current solution ysolution(i,:) = y; end MATLAB Solution: The function RK4 implements the Runge-Kutta method of order four. The user provides RK4 with the function eqn that defines the first-order differential equations. function y = fex8_17(x,y) y = 2∗ x∗ y; >> x=1;y=1;xf=1.5;h=0.1; >> [xsolution,ysolution]=RK4(@fex8_17,x,y,xf,h) xsolution = 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 ysolution = 1.0000 1.2337 1.5527 1.9937 2.6116 3.4902 EXAMPLE 8.18 Use the Runge-Kutta method of order four with h = 0.1 on (2, 3) for the dy initial problem = −xy2, y(2) = 1. Compute the results obtained with the dx 2 exact solution y (x) = 2 . x −2

ANMF.CH08_4PP.indd 595

3/1/2023 2:41:07 PM

596 • Applied Numerical Methods Using MATLAB Solution: Starting with t0 = 2, y0 = 1, Equation (8.44) gives K1 = f(2.0, 1) = −(2)(1)2 = −2 K2 = f(2.05, 1 + 0.05(−2)) = −(2.05)(0.9)2 = −1.6605 K3 = f(2.05, 1 + 0.05(−1.6605)) = −(2.05)(0.916975)2 B −1.72373 K4 = f(2.1, 1 + 0.1(−1.72373)) = −(2.1)(0.82763)2 B −1.43843 0.1 y 1 = y 0 − {2 + 2(1.6605 + 1.72373) + 1.43843} B 0.829885 6 Table 8.12 shows both the Runge-Kutta method of order 4 values and the exact values of y(2.0), y(2.1), …, y(3.0) rounded to six decimal places. The 2 exact values in Table 8.18, yn were computed from y (x) = 2 . x −2 TABLE 8.12

xn

yn

y(xn)

2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0

1.000000 0.829885 0.704237 0.607914 0.531924 0.470596 0.420175 0.378078 0.342471 0.312017 0.285718

1.000000 0.829876 0.704225 0.607903 0.531915 0.470588 0.420168 0.378072 0.342466 0.312012 0.285714

The reasons for the popularity of the Runge-Kutta method of order 4 are evident from Table 8.12. Clearly the method is more accurate. However, four slope values must be computed at each step. This is a short coming of the method. MATLAB Solution: The function RK4 implements the Runge-Kutta method of order four. The user provides RK4 with the function eqn that defines the first-order differential equations.

ANMF.CH08_4PP.indd 596

3/1/2023 2:41:11 PM

Numerical Solution of Ordinary Differential Equations • 597

function y = fex8_18(x,y) y = −x∗ y^2; >> x=2.0;y=1.0;xf=3.0;h=0.1; >> [xsolution,ysolution]=RK4(@fex8_18,x,y,xf,h) xsolution = 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 ysolution = 1.0000 0.8299 0.7042 0.6079 0.5319 0.4706 0.4202 0.3781 0.3425 0.3120 0.2857 EXAMPLE 8.19 Using the Runge-Kutta method of order four and with h = 0.2 to obtain an dy approximate solution of = −2ty2, y(0) = 1, in the initial 0 ≤ t ≤ 1 with h = 0.2. dt 1 The exact value of y is given by y = . Compute the relative error and the 1 + t2 percentage relative error.

ANMF.CH08_4PP.indd 597

3/1/2023 2:41:14 PM

598 • Applied Numerical Methods Using MATLAB Solution: Here K1 = −2tn y 2n

K2 = −2(tn + 0.1), (yn + 0.1 K1)2



K3 = −2(tn + 0.1), (yn + 0.1K2)2



K4 = −2(tn+1)(yn + 0.2K3)2

For

n = 0:



K1 = 0, K2 = −0.2, K3 = −0.192 and K4 = −0.37. y1 = 1 −

Therefore,

0.2 [2(0.2) + 2(0.192) + 0.37] = 0.9615. 6

Table 18.13 gives the summary of the calculations. In the Table 8.13, the 1 exact values are calculated using y = . The absolute error = exact value 1 + t2 minus the value from the Runge-Kutta method. Percentage relative error = |error|/exact value. TABLE 8.13 n

xn

yn

Exact value

Absolute error

Percentage relative error

0 1 2 3 4 5

0 0.2 0.4 0.6 0.8 1.0

1.0 0.9615 0.8621 0.7353 0.6098 0.5

1.0 0.9615 0.8621 0.7353 0.6098 0.5

0 0 0 0 0 0

0 0 0 0 0 0

MATLAB Solution: The function RK4 implements the Runge-Kutta method of order four. The user provides RK4 with the function eqn that defines the first-order differential equations. function y = fex8_19(t,y) y = −2∗ t∗ y^2; >> x=0.0;y=1.0;xf=1.0;h=0.1; >> [xsolution,ysolution]=RK4(@fex8_19,x,y,xf,h)

ANMF.CH08_4PP.indd 598

3/1/2023 2:41:20 PM

Numerical Solution of Ordinary Differential Equations • 599

xsolution =  0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.0000 ysolution = 1.0000 0.9901 0.9615 0.9174 0.8621 0.8000 0.7353 0.6711 0.6098 0.5525 0.5000 0.5000 EXAMPLE 8.20

dy = 1 − t + 4y, dt y(0) = 1, in the initial 0 ≤ t ≤ 0.5 using the Runge-Kutta method of order four −9 1 19 4 t with h = 0.1. Compute the exact value given by y = + + e . Compute 16 4 16 the absolute error and the percentage relative error. Find an approximate solution to the initial value problem

ANMF.CH08_4PP.indd 599

3/1/2023 2:41:25 PM

600 • Applied Numerical Methods Using MATLAB Solution: For n = 0, from Equation (8.44), we have

K1 = f(x0, y0) = 5



K2 = f(0 + 0.05, 1 + 0.25) = 5.95



K3 = f(0 + 0.05, 1 + 0.2975) = 6.14



K4 = f(0.1, 1 + 0.614) = 7.356

Hence y 1 = 1 +

0.1 [5 + 2(5.95) + 2(6.14) + 7.356] = 1.6089 6

Table 8.14 gives a summary of all the calculations for yn, exact value, absolute error, and the percentage relative error. TABLE 8.14 n

tn

Runge-Kutta yn

Exact value

Absolute error

Percentage relative error

0 1 2 3 4 5

0 0.1 0.2 0.3 0.4 0.5

1 1.6089 2.5050 3.8294 5.7928 8.7093

1 1.6090 2.5053 3.8301 5.7942 8.7120

0.0001 0.0002 0.0007 0.0014 0.0027

0.0062 0.0119 0.07 0.14 0.27

The superiority of the Runge-Kutta method of order four is clearly ­ emonstrated in this Table 8.14 in comparison with the Euler’s method d [Table 8.5]. MATLAB Solution: The function RK4 implements the Runge-Kutta method of order four. The user provides RK4 with the function eqn that defines the first-order ­differential equations. function y = fex8_20(t,y) y = 1−t+4∗ y; >> x=0.0;y=1.0;xf=0.5;h=0.1; >> [xsolution,ysolution]=RK4(@fex8_20,x,y,xf,h)

ANMF.CH08_4PP.indd 600

3/1/2023 2:41:27 PM

Numerical Solution of Ordinary Differential Equations • 601

xsolution =  0 0.1000 0.2000 0.3000 0.4000 0.5000 ysolution = 1.0000 1.6089 2.5050 3.8294 5.7928 8.7093 EXAMPLE 8.21 Use the Runge-Kutta method of order four and with h = 0.1 to find an approxdy 2 imate solution of = x + y at x = 0.1, 0.2 and 0.4. Given that y = −1 when dx x = 0. Solution: Equation (8.44) can be written as

K1 = g(x0, y0)h = [02 − 1]0.1 = −0.1



h 1 K2 = g  x 0 + , y 0 + K1  h = [(0.05)2 − 1.05]0.1 = −0.1047 2 2  



h 1 K3 = g  x 0 + , y 0 + K2  h = [(0.05)2 − 1.0524]0.1 = −0.1050 2 2  



K4 = g[x0 + h, y0 + K3]h = [(0.1)2 − 1.105]0.1 = −0.1095

1 ∆y 1 = [K1 + 2K2 + 2K3 + K 4 ] 6 1 = [−0.1 + 2(−0.1047) + 2(−0.1050) + (−0.1095) = −0.1048 6 Hence y1 = y0 + Δy1 = −1.1048 Let

ANMF.CH08_4PP.indd 601

3/1/2023 2:41:35 PM

602 • Applied Numerical Methods Using MATLAB For the second increment, we have

K1 = −0.1095, K2 = −0.1137, K3 = −0.1139 and K4 = −0.1179



Δy2 = −0.1138

Therefore y2 = y1 + Δy2 = −1.2186 For the third increment, we have

K1 = −0.1179, K2 = −0.1215, K3 = −0.1217 and K4 = −0.1250

and

Δy3 = −0.1215

Hence y3 = y2 + Δy2 = −1.3401. MATLAB Solution: The function RK4 implements the Runge-Kutta method of order four. The user provides RK4 with the function eqn that defines the first-order differential equations. function y = fex8_21(x,y) y = x^2+y; >> x=0.0;y=−1.0;xf=0.3;h=0.1; >> [xsolution,ysolution]=RK4(@fex8_21,x,y,xf,h) xsolution =  0 0.1000 0.2000 0.3000 ysolution = –1.0000 –1.1048 –1.2186 –1.3401 EXAMPLE 8.22 Repeat Example 8.10 using the Runge-Kutta method of order four. Use h = 1.

ANMF.CH08_4PP.indd 602

3/1/2023 2:41:35 PM

Numerical Solution of Ordinary Differential Equations • 603

Solution: Here

f(x, y) = y/2

From Equation (8.44b), we have

1 1 K1 = hf(x0, y0) = 1 f(0, 1) = 1   =  2 2



5 5 K h 1 5 K2 = hf  x 0 + , y 0 + 1  = 1 f  ,  = 4 =   2 4 2 8 2 2



21 21 1 21 K h   K3 = hf  x 0 + , y 0 + 2  = 1 f  ,  = 16 =   2 16  2 32 2 2



53  53  32 53 = K4 = hf(x0 + h, y0 + K3) = 1 f  1,  =  32  2 64

From Equation (8.44a), we have

1 [K1 + 2K2 + 2K3 + K4] 6 1 1  5  21  53 = 1.6484 y(1) = 1 +   + 2   + 2   +  8  32  64 6  2

y = y(1) = y0 +

The exact value

y(1) = ex/2 = e1/2 = 1.6487.

MATLAB Solution: The function RK4 implements the Runge-Kutta method of order four. The user provides RK4 with the function eqn that defines the first-order differential equations. function y = fex8_22(x,y) y = 0.5∗ y; >> x=0.0;y=1.0;xf=0.5;h=1.0; >> [xsolution,ysolution]=RK4(@fex8_22,x,y,xf,h)

ANMF.CH08_4PP.indd 603

3/1/2023 2:41:51 PM

604 • Applied Numerical Methods Using MATLAB xsolution = 0 1 ysolution = 1.0000 1.6484 8.3.4 Predictor-Corrector Methods Predictor-corrector methods refer to a family of schemes for solving ordinary differential equations using two formulas: predictor and corrector formulas. In predictor-corrector methods, four prior values are required to find the value of y at xn. Predictor-corrector methods have the advantage of giving an estimate of error from successive approximations to yn. The predictor is an explicit formula and is used first to determine an estimate of the solution yn+1. The value yn+1 is calculated from the known solution at the previous point (xn, yn) using single-step method or several previous points (multistepmethods). If xn and xn+1 are two consecutive mesh points such that xn+1 = xn + h, then in Euler’s method we have

yn+1 = yn + h f(x0 + nh, yn), n = 0, 1, 2, 3, …

(8.45)

Once an estimate of yn+1 is found, the corrector is applied. The corrector uses the estimated value of yn+1 on the right-hand side of an otherwise implicit formula for computing a new, more accurate, value for yn+1 on the left-hand side. The modified Euler’s method gives us

y n +1 = y n +

h f ( x n , y n ) + f ( x n +1 , y n +1 )  2

(8.46)

The value of yn+1 is first estimated by Equation (8.45) and then utilized in the right-hand side of Equation (8.46) resulting in a better approximation of yn+1. The value of yn+1 thus obtained is again substituted in Equation (8.46) to find a still better approximation of yn+1. This procedure is repeated until two consecutive iterated values of yn+1 are very close. Here, the corrector ­Equation (8.46) which is an implicit equation is being used in an explicit manner since no solution of a nonlinear equation is required.

ANMF.CH08_4PP.indd 604

3/1/2023 2:41:54 PM

Numerical Solution of Ordinary Differential Equations • 605

In addition, the application of corrector can be repeated several times such that the new value of yn+1 is substituted back on the right-hand side of the corrector formula to obtain a more refined value for yn+1. The technique of refining an initially crude estimate of yn+1 by means of a more accurate formula is known as predictor-corrector method. Equation (8.45) is called the predictor and Equation (8.46) is called the corrector of yn+1. In what follows, we describe two such predictor-corrector methods: 1. Adams-Moulton method. 2. Milne’s predictor-corrector method. 8.3.4.1 Adams-Moulton Predictor-Corrector Method The Adams-Moulton method is an implicit multistep method for solving first-order ordinary differential equations. There are several versions of Adams-Moulton formulas available for computing the value of yn+1 by using the previously obtained solution at two or more points. These formulas are classified based on their order, that is, based on the number of points used in the formula and the order of the global truncation error. For instance, in the second-order formula, two points (xn, yn) and (xn+1, yn+1) are used. In the third-order formula, three points (xn, yn), (xn−1, yn−1) and (xn−2, yn−2) are used and so on. Consider the differential equation

dy = f (x, y ), y(x0) = y0(8.47) dx

Integrating Equation (8.47), we obtain y = y 0 + ∫ xx 0 f (x, y )dx 

(8.48)

or y 1 = y 0 + ∫ xx 0 f (x, y )dx, x0 ≤ x ≤ x1(8.49) Applying the Newton’s backward difference formula, we have

ANMF.CH08_4PP.indd 605



f (x, y ) = f0 + n∇f0 +

where

n=

n(n + 1) 2 n(n + 1)(n + 2) 3 ∇ f0 + ∇ f0 + … (8.50) 2 6

x − x0 and f0 = f ( x 0 , y 0 ) h

3/1/2023 2:42:03 PM

606 • Applied Numerical Methods Using MATLAB Now, substituting f(x, y) from Equation (8.50) into the right-hand side of Equation (8.49), we obtain

n(n + 1) 2   y 1 = y 0 ∫ xx10 f1 + n∇f0 + ∇ f0 + … dx 2   n(n + 1) 2   ∇ f0 + … dx y 1 = y 0 + h ∫ 10 f0 + n∇f0 + 2  

5 2 3 3 251 4   1 ∇ + …. f0 (8.51) or y = y 0 + h 1 + ∇ + ∇ + ∇ + 2 12 8 720   We note here that the right-hand side of Equation (8.51) depends on y0, y−1, y−2, … all of which are known. Hence, we can write Equation (8.51) as 5 3 251 4   1 ∇ + … f0 (8.52) y 1p = y 0 + h 1 + ∇ + ∇2 + ∇3 + 2 12 8 720   Equation (8.52) is called the Adams-Bashforth formula and is used as a predictor formula.

A corrector formula is derived by applying the Newton’s backward difference formula at f1. Therefore,

f (x, y ) = f1 + n∇f1 +

n(n + 1) 2 n(n + 1)(n + 2) 3 ∇ f1 + ∇ f1 + … (8.53) 2 6

Now, substituting f(x, y) from Equation (8.53) into the right-hand side of Equation (8.49), we obtain



n(n + 1) 2   y 1 = y 0 + ∫ xx10 f1 + n∇f1 + ∇ f1 + … dx 2   1 + ( n ) n   ∇2 f1 + … dx = y 0 + h ∫ 0−1 f1 + n∇f1 + 2  

1 1 19 4   1 ∇ + …. f1  or y = y 0 + h 1 − ∇ − ∇2 − ∇3 − 12 24 720  2 

(8.54)

Equation (8.54) shows that the right-hand side depends on y1, y0, y−1, y−2, …, where y 1p is used for y1. Hence, the new value of y1 is given by

ANMF.CH08_4PP.indd 606

 1 −5 2 3 3 251 4  p ∇ − ∇ − ∇ f1  y 1c = y 0 + h 1 − 8 720   2 12

(8.55)

3/1/2023 2:42:19 PM

Numerical Solution of Ordinary Differential Equations • 607

where

(

f1p = f1 x 1 , y 1p

)

the formula, Equation (8.55) is called the Adams-Moulton corrector formula. Now expressing the remaining difference operators in their functional values and neglecting the fourth and higher order differences, Equations (8.51) and (8.55) become respectively, h (8.56) [ 55f0 − 59f −1 + 37f −2 − 9f3 ]  24 h 9f1p + 19f0 − 5f −1 + f −2   and (8.57) y 1c = y 0 + 24  Equation (8.57), known as the corrector formula is repeatedly applied by computing an improved value of f1 at each stage, to obtain a better value of y1 unless it becomes stable and remains unchanged and then we proceed to calculate y2. 251 5 ( 4) h f0 and The approximate errors in Equations (8.56) and (8.57) are 720 −19 5 ( 4) h f0 respectively. 720

y 1p = y 0 +

It should be noted here that in order to apply Adams-Moulton method, we require four starting values of y, which can be obtained by using Picard’s method of successive approximation or the Taylor series method or Euler’s method or Runge-Kutta methods. Summarizing, the Adams-Bashforth and Adam-Moulton formulae are given by h [ 55fn − 59fn −1 + 37fn −2 − 9fn −3 ] (8.58) 24 h and y cn +1 = y n + [ 9f n +1 + 19f n − 5f n −1 + f n − 2 ] (8.59) 24 respectively. The local error estimates for Equations (8.58) and (8.59) are

y np +1 = y n +



251 5 v −19 5 v h y (ξ1 ) and h y (ξ 2 ) (8.60) 720 720

Let y 0n+1 represents the value of yn+1 found using Equation (8.58) and y 1n+1 the solution obtained with one application of Equations (8.58) and (8.59). If y(xn+1) represents the exact value of y at xn+1 and the values of f are assumed to be exact at all points including xn, then from Equation (8.60), we obtain the order estimates

ANMF.CH08_4PP.indd 607

3/1/2023 2:42:40 PM

608 • Applied Numerical Methods Using MATLAB



y ( x n +1 ) − y 0n +1 =

251 5 v h y (ξ1 ) (8.61) 720



y ( x n +1 ) − y 1n +1 =

−19 5 v h y (ξ 2 ) (8.62) 720

which leads to the estimate of yv, based on the assumption that the over the interval of interest yv(x) is approximately constant, as 720 1  y n +1 − y 0n +1  . 270  Hence, from Equation (8.62), we obtain

h5 y v =

y ( x n +1 ) − y 1n +1 =

−19 1 −1 1  y n +1 − y 0n +1  ≈  y n +1 − y 0n +1  = Dn +1 (8.63) 720 14

Hence, the error of the corrected value is approximately −1/14 of the difference between the corrected and the predicted values. EXAMPLE 8.23 Use the Adams-Moulton method on (2, 3) with h = 0.1 for the initial value 2 dy problem . = − xy 2, y(2) = 1. Exact solution is y (x) = 2 x −2 dx Solution: We will try to obtain about four significant digits. To start the method, we use the following exact values to seven significant digits.



x 0 = 2.0 : y 0 = y (2.0) = 1.0;

f0 = − x 0 y 20 = −2.0

x 1 = 2.1 : y 1 = y (2.1) = 1.8298755;

f1 = − x 1 y 12 = −1.446256

x 2 = 2.2 : y 2 = y (2.2) = 0.7042254; f2 = − x 2 y 22 = −1.091053 x 3 = 2.3 : y 3 = y (2.3) = 0.6079027; f3 = x 3 y 23 = −0.8499552

n = 3

h [ 55f3 − 59f2 + 37f1 − 9f0 ] = 0.5333741 24 2 h  y c4 = y 3 + 9 − x 4 y 4p + 19f3 − 5f2 + f1  = 0.5317149  24  y 4p = y 3 +

(

ANMF.CH08_4PP.indd 608

( )

)

3/1/2023 2:42:56 PM

Numerical Solution of Ordinary Differential Equations • 609

The local truncation error estimate is from Equation (8.62),

−19 c  y 4 − y 4p  720 −19 = [0.5317149 − 0.5333741] = 0.0001144 720

y ( x n +1 ) − y 1n +1 =

Since the local truncation error estimate indicates possible inaccuracy in the 4th decimal place (4th significant digit) of y c4, we take y c4 as an improved y 4p to get an improved y c4 as follows: h 9  − x 4 (0.5117149)2  + 19f3 − 5f2 + f1  = 0.5318739  24   The local truncation error estimate of this y c4 is

y c4 = y 3 +

−19 [0.5318739 − 0.5317149] = −0.0000112 720 indicating that y c4 should be accurate to about five significant digits.

n = 4:

f 4 = f ( x 4 , y 4 ) = −(2.4)(0.5318739)2 = −0.6789358

h [ 55f4 − 59f3 + 37f2 − 9f1 ] = 0.4712642 24 2 h  y c5 = y 4 + −9 − x 5 y 5p + 19f 4 − 5f3 + f2  = 0.4704654   24  −19 c  y 5 − y 5p  = 0.0000562. As before, The local truncation error estimate is 720 this estimate indicates possible inaccuracy in the 4th significant digit of y C5. Hence, we get an improved y5 as y c5 = y 4 +

(



y c5 = y 4 +

)

h  −9 − x 5 (0.4704654)2 + 19f 4 − 5f3 + f2  = 0.4705358  24 

(

)

The local truncation error estimate for this y C5 is

=

−19 [0.4705358 − 0.4704654] = −0.0000050 270

indicating this y C5 should be accurate to about five significant digits. Table 8.15 summarizes the computations and comparison with the exact solution y(xn) and the relative error En(h).

ANMF.CH08_4PP.indd 609

3/1/2023 2:43:20 PM

610 • Applied Numerical Methods Using MATLAB

TABLE 8.15  Adams-Moulton method value for xn

Exact y(xn)

dy = −xy2, y(2) = 1 with h = 0.1 dx

Using Adams-Moulton method yn

x0 =  2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 xF = 3.0

1.000000 0.829876 0.704225 0.607903 0.531915 0.470588 0.420168 0.378072 0.342466 0.312012 0.285714

Exact Exact Exact Exact 0.531874 0.470536 0.420114 0.378020 0.342419 0.311971 0.285674

En(h) — — — — 0.000041 0.000052 0.000054 0.000052 0.000047 0.000041 0.000040

EXAMPLE 8.24 Approximate the y value at x = 0.4 of the following differential equation dy = 0.5y, y(0) = 1.0 using the Adams-Moulton method. dx Solution: The predicted value at x = 0.4 is given by Equation (8.58) h [55f n − 59f n −1 + 37 f n − 2 − 9f n − 3 ] 24 0.1 or y 4p = y 3 + [55f0 − 59f2 + 37 f1 − 9f0 ] (E.1) 24

y np +1 = y n +

where f0, f1 and f2 values are obtained from the Table 8.6. Substituting the values of f0, f1 and f2 from Table 8.6, Equation (E.1) becomes 0.1 [55(0.5) − 59(0.5513) + 37(0.525) − 9(0.5)] = 1.1988 24 The corrected value is obtained by first evaluating f(x4, y4), then substituting into Equation (8.59). That is,

y 4p = 1.1576 +



1 f 4 = f (x 4 , y 4 , p) = (1.2213) = 0.6106 2

ANMF.CH08_4PP.indd 610

3/1/2023 2:43:31 PM

Numerical Solution of Ordinary Differential Equations • 611

and from Equation (8.59)

h [9f n +1 + 19f n − 5f n −1 + f n − 2 ] 24 0.1 y C4 = y 3 + [9f 4 + 19f3 − 5f2 + f1 ] 24 y Cn +1 = y n +

 1.1576 

0.1 [9(0.6129)  19(0.5788)  5(0.5513)  0.5250]  1.2171 24

The corrected value (1.2171) is clearly more accurate than the predicted value (1.1988) when compared with the exact value of y4 = 1.2214. 8.3.4.2 Milne’s Predictor-Corrector Method Consider the differential equation dy = f (x, y ), y(0) = 0 dx Integrating Equation (8.64), we obtain

(8.64)

x

y = y 0 + ∫ f (x) dx x0 x

or y = y 0 + 4 f (x, y ) dx ∫ x0

in the range x0 ≤ x ≤ x4(8.65)

Applying Newton’s forward difference formula, we get

f(x, y) = f0 + nΔf0 +

n(n + 1) 2 f (n + 1)(n + 2) 3 Δ f0 + Δ f0 + …. 2 6

(8.66)

Substituting Equation (8.66) into the right-hand side of Equation (8.65), we get x4 n(n − 1) 2   ⌠ ∆ f0 + .... dx y 4 = y 0 +  f0 + n∆f n + 2  ⌡x 0  x4



= y 0 + h⌠  ⌡x 0

n(n − 1) 2   ∆ f0 + .... dn (8.67) f0 + n∆f n + 2 

20 2 8   or y = y 1 + h  4f0 + 8 ∆f0 + ∆ f0 + ∆ 3 f0 + ..... 3 3  

ANMF.CH08_4PP.indd 611

3/1/2023 2:43:48 PM

612 • Applied Numerical Methods Using MATLAB Neglecting the fourth and higher order differences and expressing the ­differences Δf0, Δ2f0, and Δ3f0 in terms of the functional values, we get 4 (8.68) h[2f1 − f2 + 2f3 ]  3 Equation (8.68) can be used to predict the value of y4 when those of y0, y1, y2 and y3 are known. Once we obtain y4, we can then find a first approximation to

y = y0 +



f4 = f(x0 + 4h, y4)

A better value of y4 can then be obtained by applying Simpson’s rule as h y 4 = y 2 + [f2 + 4f3 + f 4 ]  (8.69) 2 Equation (8.64) called a corrector. An improved value of f4 is calculated and again the corrector is applied to obtain a still better value of y4. The procedure is repeated until y4 remains unchanged. After obtaining y4 and f4 to a desired degree of accuracy, then y5 = (x0 + 5h) is obtained from the predicted as and

4 h[2f2 − f3 + 2f 4 ] 3 f5 = f[x0 + 5h, y5] y 5 = y1 +

is computed. A better approximation to the value of y5 is then obtained from the corrector as h y 5 = y 3 + [f3 + 4f 4 + f 5 ] 3 This step is repeated until y5 becomes stable and then we proceed to compute y6 as before. This procedure is known as Milne’s predictor-corrector method. The accuracy of the method is improved if we must improve the starting values and then sub-divide the intervals. Summarizing, the predictor and corrector formulae are given by 4h [2f n − f n −1 + 2f n − 2 ]  3



y np +1 = y n − 3 +

and

h y Cn +1 = y n −1 + [f np+1 + 4f n + f n −1 ]  3

ANMF.CH08_4PP.indd 612

(8.70) (8.71)

3/1/2023 2:43:55 PM

Numerical Solution of Ordinary Differential Equations • 613

The corresponding error estimates for Equations (8.70) and (8.71) are given by 28 5 v h y (ξ1 )  29

(8.72)

1 5 v h y (ξ 2 )  90

(8.73)



ep =



emp = −

The local error estimate can be shown to be −1 1  y − y 0n +1   (8.74) 29  n +1 It should be noted here that Equation (8.71) can be subjected to numerical instability in some cases.

Dn + 1 =

EXAMPLE 8.25

1 Approximate the y value at x = 0.4 of the differential equation dy = y , 2 y(0) = 1.0 using the Milne predictor-corrector method. Solution: The predicted y value at x = 4 and n = 3 is given by Equation (8.70). Hence 4h y np +1 = y n − 3 + [ 2f n − f n −1 + 2f n − 2 ] 3 4(0.1) [2f (x 1 , y 1 ) − f (x 2 , y 2 ) + 2f (x 3 , y 3 )] 3 Here, we use the past values given in Example 8.10. or y 4p = y 0 +

0.4 [2(0.5250) − 0.5513 + 2(0.5788)] = 1.2259 3 The derivative at x = 0.4 can be approximated by using the predicted value to obtain 1 dy = f (x 4 , y 4 ) = ( y 4p ) = 0.6129 2 dx x 4

y 4p = 1 +

Hence, the corrected y4 is obtained using Equation (8.71) h y Cn +1 = y n −1 + [f np+1 + 4f n + f n −1 ] 3

ANMF.CH08_4PP.indd 613

3/1/2023 2:44:06 PM

614 • Applied Numerical Methods Using MATLAB

0.1 [f (x 2 , y 2 ) + 4f (x 3 , y 3 ) + f (x 4 , y 4 ) 3 0.1     = 1.1025 + [0.5513 + 4(0.5788) + 0.6129] = 1.2185 3 or y C4 = y 2 +

The predicted value 1.2259 is noted to be closer to the exact value of 1.2214 than the corrected value.

8.4 MATLAB FUNCTIONS FOR ORDINARY DIFFERENTIAL EQUATIONS: ODE45, ODE23, ODE113, ODE15S, ODE23S, ODE23T, ODE23TB A number of numerical methods have been developed for the solution of firstorder ordinary differential equations (ODEs), and some of these methods are readily available as MATLAB built-in functions. Table 8.16 lists several ODE MATLAB solver commands. The MATLAB ODE Solvers can be divided into two categories according to their ability to solve stiff problems and according to whether they use onestep or multistep methods. One step solvers use information from one-point to obtain a solution at the next point whereas multistep solvers use information from several previous points to find the solution at the next point. Stiff problems are the ones that include fast and slowly changing components and require small time steps in their solution. It is often difficult to judge which solver is the most appropriate one for a specific problem. The general practice is to first try ode45 and if a good solution is not obtained, then it is recommended to try the solver ode15s. TABLE 8.16  MATLAB ODE Solvers MATLAB ODE Solver

Description

ode45

The ode45 function uses an algorithm developed by Dormand and Prince (1990), which simultaneously uses fourth- and fifth-order Runge-Kutta formulas to solve the ODE and make error estimates for step-size adjustment. For nonstiff problems, one step solver, it is best to apply as a first try for most problems.

ode23

For nonstiff problems, one-step solver; based on the explicit Runge-Kutta method. This method is quicker but less accurate than ode45.

ode113

For nonstiff problems; multistep solver.

ode15s

This function is a variable-order solver based on numerical differentiation formulas. It is a multistep solver that optionally uses the Gear backward differentiation formulas. Low to medium accuracy. Use if ode45 failed.

ANMF.CH08_4PP.indd 614

3/1/2023 2:44:08 PM

Numerical Solution of Ordinary Differential Equations • 615

MATLAB ODE Solver

Description

ode23s

This function is based on a modified Rosenbrock formula of order 2. Since it is a one-step solver, it may be more efficient than ode15s at crude tolerances. For stiff problems. Can solve some problems that ode 15s cannot.

ode23t

This function is an implementation of the trapezoidal rule with a “free” interpolant. The method is used for moderately stiff problems with low accuracy where a solution is obtained without numerical damping.

ode23tb

For stiff problems. Often more efficient than ode15s. This is an implementation of an implicit Runge-Kutta formula with a first stage that is a trapezoidal rule and a second stage that is a backward differentiation formula of order 2.

These functions are handled in a number of different ways. The general form of the command that is used to solve an initial value ODE problem is the same for all the solvers and all the equations that are solved. The general form is

[t y] = solver_name (ODEfun, tspan, y0)

where solver_name = the name of the solver (numerical method) that is used (e.g., ode45 or ode15s) ODEfun = The function that computes dy/dt for given value of t and y. If it was written as a user-defined function, the function handle is entered. If it was written an anonymous function, the name of the anonymous function is entered. tspan = A vector that specifies the interval of the solution. The vector must have at least two elements but can have more. If the vector has only two elements, the elements must be [t0 tf], which are the initial and final points of the solution interval. The vector tspan can have, however, additional points between the first and last points. The number of elements in tspan affects the output from the command. y0 = the initial value of y, that is, the value of y at the first point of the interval. [t y] = the output (the solution of the ODE). t and y are column vectors. The first and last points are the beginning and end points of the interval. The spacing and number of points in between depends on the input vector tspan. If tspan has two elements, the vectors t and y contain the solution at every integration step computed by the solver. If tspan has more than two points the vectors t and y contain the solution only at these points. The number of points in tspan does not affect the time steps used for the solution by the program.

ANMF.CH08_4PP.indd 615

3/1/2023 2:44:08 PM

616 • Applied Numerical Methods Using MATLAB EXAMPLE 8.26 Solve Example 8.18 using the MATLAB built-in function ode45. MATLAB Solution: The ODE to be solved can be written as a user-defined function (in a function file), or as an anonymous function. Both of these calculate dy/dt for given values of t and y. In this problem, the user-defined function which is saved as a separate file is given below: function dydx=ODEexp1(x, y) dydx=-x∗ y^2 When an anonymous function is used, it can be defined in the Command window, or be within a script file. For instance, here we have >>ode1=@(x, y) (−x∗ y^2) Ode1=

@(x, y) (−x∗ y^2)

When the ode function is written as a user-defined function, then the solution with MATLAB built-in function ode45 is obtained by >> [x, y]=ode45(@ODEexp1,[2:0.1:3],1.0) The solution starts at x = 2.0 and ends at x = 3 with increments of 0.1 (according to the vector tspan) x= 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000

ANMF.CH08_4PP.indd 616

3/1/2023 2:44:08 PM

Numerical Solution of Ordinary Differential Equations • 617

y= 1.0000 0.8299 0.7042 0.6079 0.5319 0.4706 0.4202 0.3781 0.3425 0.3120 0.2857 >> [x y]=ode45(@ODEexp1,[2:0.01:3],1.0) The solution is plotted with the plot command. >> plot(x, y) >> xlabel(‘x’),ylabel(‘y’) The plot is shown in Figure 8.6. 1 0.9 0.8

y

0.7 0.6 0.5 0.4 0.3 0.2

2

2.1

2.2

2.3

2.4

2.5 x

2.6

2.7

2.8

2.9

3

FIGURE 8.6

ANMF.CH08_4PP.indd 617

3/1/2023 2:44:09 PM

618 • Applied Numerical Methods Using MATLAB EXAMPLE 8.27 Solve Example 8.19 using the MATLAB built-in function ode45. MATLAB Solution: The ODE to be solved can be written as a user-defined function (in a ­function file), or as an anonymous function. Both of these calculate dy/dt for given values of t and y. In this problem, the user-defined function which is saved as a separate file is given below: function dydt=ODEexp2(t, y) dydx= −2∗ t∗ y^2 When an anonymous function is used, it can be defined in the Command window, or be within a script file. For instance, here we have >>ode2=@(t, y) (−2∗ t∗ y^2) Ode2= @(t, y) (−2∗ t∗ y^2) When the ode function is written as a user-defined function, then the solution with MATLAB built-in function ode45 is obtained by >> [t, y]=ode45(@ODEexp2,[0:0.1:0.5],1.0) The solution starts at x = 0 and ends at x = 0.5 with increments of 0.1 (according to the vector tspan) t=  0 0.1000 0.2000 0.3000 0.4000 0.5000 y= 1.0000 0.9901 0.9615 0.9174 0.8621 0.8000

ANMF.CH08_4PP.indd 618

3/1/2023 2:44:09 PM

Numerical Solution of Ordinary Differential Equations • 619

>> [t y]=ode45(@ODEexp1,[0:0.01:0.5],1.0) The solution is plotted with the plot command. >> plot(t, y) >> xlabel(‘t’),ylabel(‘y’) The plot is shown in Figure 8.7. 1

0.98

y

0.96

0.94

0.92

0.9

0.88

0

0.05

0.1

0.15

0.2

0.25 t

0.3

0.35

0.4

0.45

0.5

FIGURE 8.7

dy = x + y, dx y(0) = 1 using increments of size h = 0.2. The exact solution is y = −1 − x + 2ex. Determine the error and the percentage error at each step. Apply the Euler’s method to the ordinary differential equation

Solution: dy =x+y dx when x = 0 and y(0) = 1.

Now,

dy = x + y = 0 + 1 = 1 or y0 = 1. dx h = 0.2 and y1 = y0 + h f(xn, yn)

or

y1 = y0 + h f(x0, y0) = 1 + 0.2(1.0) = 1.2

Hence

The process is continued as shown in Table 8.17.

ANMF.CH08_4PP.indd 619

3/1/2023 2:44:13 PM

620 • Applied Numerical Methods Using MATLAB Exact value at xn = 0.2 is

y0.2 = −1 − 0.2 + 2e0.2 = 1.2428

Table 8.17 gives the summary of the computations.

Error = Exact value − value from Euler’s method.



| Error | Percentage error = Exact value TABLE 8.17 n

t

yn

Exact value

Error

Percentage error

0 1 2 3 4 5

0 0.2 0.4 0.6 0.8 1.0

1 1.2 1.48 1.856 2.3472 2.9766

1 1.2428 1.5836 2.0442 2.6511 3.4366

0 0.0428 0.1036 0.1882 0.3039 0.46

0 3.4438 6.5421 9.2065 11.4632 13.3853

MATLAB Solution: The ODE to be solved can be written as a user-defined function (in a function file), or as an anonymous function. Both of these calculate dy/dt for given values of t and y. In this problem, the user-defined function which is saved as a separate file is given below: function dydx=ODEexp3(x, y) dydx = x+y When an anonymous function is used, it can be defined in the Command window, or be within a script file. For instance, here we have >>ode3=@(x, y) (x+y) Ode3=

@(x, y) (x+y)

When the ode function is written as a user-defined function, then the solution with MATLAB built-in function ode45 is obtained by >> [x, y]=ode45(@ODEexp3,[0:0.2:1],1.0)

ANMF.CH08_4PP.indd 620

3/1/2023 2:44:14 PM

Numerical Solution of Ordinary Differential Equations • 621

The solution starts at x = 0 and ends at x = 1 with increments of 0.2 (according to the vector tspan) x=  0 0.2000 0.4000 0.6000 0.8000 1.0000 y= 1.0000 1.2428 1.5836 2.0442 2.6511 3.4366 >> [x y]=ode45(@ODEexp3,[0:0.01:1],1.0) The solution is plotted with the plot command. >> plot(x, y) >> xlabel(‘x’),ylabel(‘y’) The plot is shown in Figure 8.8. 3.5

3

y

2.5

2

1.5

1

0

0.1

0.2

0.3

0.4

0.5 x

0.6

0.7

0.8

0.9

1

FIGURE 8.8

ANMF.CH08_4PP.indd 621

3/1/2023 2:44:14 PM

622 • Applied Numerical Methods Using MATLAB EXAMPLE 8.28

dy = 1 − t + 4y, y(0) = 1, dt in the interval 0 ≤ t ≤ 0.5 with h = 0.1. The exact value is Use Euler’s method to solve the initial value problem −9 1 19 + t + e4 t 16 4 16 Compute the error and the percentage error.

y=

Solution: Here, f(tn, yn) = 1 − tn + 4yn and thus

yn+1 = yn + (0.1)(1 − tn + 4yn)

For n = 0:

y1 = y0 + (0.1)(1 − t0 + 4y0) = 1 + (0.1)(1 − 0 + 4) = 1.5

n = 1:

y2 = y1 + 0.1(1 − t1 + 4y1) = 1.5 + (0.1)(1 − 0.1 + 6) = 2.19

The exact value is computed from −9 1 19 + t + e4 t 16 4 16



y=



Error = Exact value − value from Euler’s method



| Error | Percentage error = Exact value

Table 8.18 summarizes the computed results. TABLE 8.18 n

t

yn

Exact value

Error

Percentage error

0 1 2 3 4 5

0 0.1 0.2 0.3 0.4 0.5

1 1.5 2.19 3.146 4.4744 6.3242

1 1.6090 2.5053 3.8301 5.7942 8.7120

0 0.109 0.3153 0.6841 1.3192 2.3878

0 6.7768 12.5864 17.8620 22.7783 27.4082

MATLAB Solution: The ODE to be solved can be written as a user-defined function (in a function file), or as an anonymous function. Both of these calculate dy/dt for given values of t and y.

ANMF.CH08_4PP.indd 622

3/1/2023 2:44:21 PM

Numerical Solution of Ordinary Differential Equations • 623

In this problem, the user-defined function which is saved as a separate file is given below: function dydt=ODEexp4(t, y) dydt= 1−t+4∗ y When an anonymous function is used, it can be defined in the Command window, or be within a script file. For instance, here we have >>ode2=@(t, y) (1−t+4∗ y) Ode2=

@(t, y) (1−t+4∗ y)

When the ode function is written as a user-defined function, then the solution with MATLAB built-in function ode45 is obtained by >> [t, y]=ode45(@ODEexp4,[0:0.1:0.5],1.0) The solution starts at x = 0 and ends at x = 0.5 with increments of 0.1 (according to the vector tspan) t=  0 0.1000 0.2000 0.3000 0.4000 0.5000 y= 1.0000 1.6090 2.5053 3.8301 5.7942 8.7120 >> [t y]=ode45(@ODEexp4,[0:0.01:0.5],1.0) The solution is plotted with the plot command. >> plot(t, y) >> xlabel(‘t’),ylabel(‘y’)

ANMF.CH08_4PP.indd 623

3/1/2023 2:44:21 PM

624 • Applied Numerical Methods Using MATLAB The plot is shown in Figure 8.9. 9 8 7

y

6 5 4 3 2 1

0

0.05

0.1

0.15

0.2

0.25 t

0.3

0.35

0.4

0.45

0.5

FIGURE 8.9

EXAMPLE 8.29 Use Euler’s method to solve the following differential ­equation y(0) = 1 and 0 ≤ x ≤ 0.5. Use h = 0.1.

dy 1 = y, dx 2

Solution: Here, we have

y1 = y0 + h f(x0, y0) = 1 + 0.1 f(0, 1)



f(0, 1) = f(x0, y0) =

Hence

y1 = 1 + 0.1(1/2) = 1.05.

For n = 1:

y2 = y1 + h f(x1, y1) = 1.05 + 0.1 f(0.1, 1.05)

where

f(0.1, 1.05) =

1 1 y 0 = (1) = 1/2 2 2

1 (1.05) = 0.525 2

Therefore, at x2 = 2h = 2(0.1) = 0.2 y2 is y2 = 1.05 + 0.1(0.525) = 1.1025. The exact values of y = ex/2 (from direct integration). This procedure is repeated for n = 2, …, 5 and a summary of the results obtained is given in Table 8.19.

ANMF.CH08_4PP.indd 624

3/1/2023 2:44:27 PM

Numerical Solution of Ordinary Differential Equations • 625

TABLE 8.19  Euler’s method versus exact solution. n

xn

yn

f(xn, yn)

yn+1 (Euler)

yn+1 = ex/2 (exact)

80 1 2

0 0.1 0.2

1 1.05 1.1025

0.5 0.525 0.5513

1.05 1.1025 1.1576

1.0513 1.1052 1.1619

3 4 5

0.3 0.4 0.5

1.1576 1.2155 1.2763

0.5788 0.6077 0.6381

1.2155 1.2763 1.3401

1.2214 1.2840 1.3499

MATLAB Solution: The ODE to be solved can be written as a user-defined function (in a function file), or as an anonymous function. Both of these calculate dy/dt for given values of t and y. In this problem, the user-defined function which is saved as a separate file is given below: function dydx=ODEexp5(x, y) dydx = 0.5∗ y When an anonymous function is used, it can be defined in the Command window, or be within a script file. For instance, here we have >>ode5=@(x, y) (0.5∗ y) Ode5=

@(x, y) (0.5∗ y)

When the ode function is written as a user-defined function, then the solution with MATLAB built-in function ode45 is obtained by >> [x, y]=ode45(@ODEexp5,[0:0.1:0.5],1.0) The solution starts at x = 0 and ends at x = 0.5 with increments of 0.1 (according to the vector tspan) x=  0 0.1000 0.2000 0.3000

ANMF.CH08_4PP.indd 625

3/1/2023 2:44:27 PM

626 • Applied Numerical Methods Using MATLAB 0.4000 0.5000 y= 1.0000 1.0513 1.1052 1.1618 1.2214 1.2840 >> [x y]=ode45(@ODEexp5,[0:0.01:0.5],1.0) The solution is plotted with the plot command. >> plot(x, y) >> xlabel (‘x’), ylabel (‘y’) The plot is shown in Figure 8.10. 1.35 1.3 1.25

y

1.2 1.15 1.1 1.05 1

0

0.05

0.1

0.15

0.2

0.25 x

0.3

0.35

0.4

0.45

0.5

FIGURE 8.10

8.5 SYSTEM OF FIRST-ORDER ORDINARY DIFFERENTIAL EQUATIONS The general form of first-order equation can be written as

y′ = f(x, y)

where y ′ =

ANMF.CH08_4PP.indd 626

(8.75)

dy and f(x, y) is a given function of x and y. dx

3/1/2023 2:44:29 PM

Numerical Solution of Ordinary Differential Equations • 627

The solution of Equation (8.75) contains an arbitrary constant also known as constant of integration. In order to determine the constant we need to know a point on the ­solution curve. We can describe this as an auxiliary condition and can be expressed as

y(a) = c

(8.76)

th

An n order differential equation can be transformed into a number of first-order differential equations. Let the nth order differential equation be given as

y(n) = f(x, y, y′, …., y(n−1))(8.77)

with a ≤ t ≤ b and with the initial conditions

y(a) = c1, y′(a) = c2, c3, …., y(n−1)(a) = cn(8.77a)

as described below: In the Equation (8.77a), t is the independent parameter and y is the unknown dependent quantity which is to be solved. Using the notation

y1 = y y2 = y′ y3 = y′′ ….

yn = y(n−1)(8.77b)

the equivalent first-order equations are y′1 = y2 y′2 = y3 y′3 = y4      y′n = f(x, y1, y2, …., yn)(8.77c)

8.6 INITIAL VALUE PROBLEMS The solution of the Equations (8.77c) requires the specification of n auxiliary conditions. When these conditions are specified at the same value of x, the problem is said to b e an initial value problem. The auxiliary conditions are called the initial conditions. When yn are specified at different values of x, the problem is known as a boundary value problem. EXAMPLE 8.30A Consider y′′ = −y, y(0) = 0 and y′(0) = 1 is an initial value problem, since both auxiliary conditions imposed on the solution are given at x = 0.

ANMF.CH08_4PP.indd 627

3/1/2023 2:44:29 PM

628 • Applied Numerical Methods Using MATLAB If y′′ = −y, y(0) = 0, and y(π) = 1, then it is a boundary value problem since the two conditions are specified at different values of x. The use of vector notation allows us to manipulate sets of first-order differential equations in a concise form. For instance, Equation (8.77c) can be written as

y′ = F(x, y)

y(a) = c

where

y1  y   2   (8.79) F(x, y ) =    y n  f (x, y )  

(8.78)

The numerical solutions of differential equations is basically a table of x and y values listed at discrete intervals of x. 8.6.1 The Taylor Series Method The Taylor series is a simple method based on the truncated Taylor series for y about x: 1 1 1 y ′′(x)h 2 + y ′′′(x)h 3 + .... + y (m) (x)h m (8.80) 2! 3! n! Since Equation (8.80) predicts y at x + h from the information available at x, it is also a formula for numerical integration. The last term kept in the series determines the order of integration. For the series in Equation (8.80) the integration order is m. y (x + h) ≈ y (x) + y ′(x)h +

The truncation error, due to the terms omitted from the series, is given by

E=

1 y (m +1) (ξ)h m +1 , x < ξ < x + h (m + 1)!

Using the finite difference approximation, we can write

y (m +1) (ξ) ≈

or E ≈

ANMF.CH08_4PP.indd 628

y (m) (x + h) − y (m) (x) h

hm [y (m) (x + h) − y (m) (x)] (8.81) (m + 1)!

3/1/2023 2:44:38 PM

Numerical Solution of Ordinary Differential Equations • 629

EXAMPLE 8.30B Develop the Runge-Kutta method for a pair of ordinary first-order ­differential equations. Solution: The Runge-Kutta methods can be extended to solve a pair of first-order differential equations. Consider a pair of first-order differential equations

dy = f (x, y , z) dx dz = g(x, y , z) (E.1) dx

with initial conditions

x = x0, y(x0) = y0, z(x0) = z0(E.2)

Then the values of yi and zi at xi are obtained by the formulae

ANMF.CH08_4PP.indd 629

1 (i)  k 1 + 2 k (2i ) + 2 k (3i ) + k (4i )  6 1 = z i + (1i ) + 2(2i ) + 2(3i ) + (4i )  (E.3) 6



y i +1 = y i +



z i +1

where

k 1(i ) = hf(xi, yi, zi)



(1i ) = hg(x , y , z )



k = hf (x i + h / 2, y i + k 1( i ) / 2, z i + (1i ) / 2)



(2i ) = hf (x i + h / 2, y i + k 1( i ) / 2, z i + (1i ) / 2)



k (3i ) = hf (x i + h / 2, y i + k (2i ) / 2, z i + (2i ) / 2)



(3i ) = hf (x i + h / 2, y i + k (2i ) / 2, z i + (2i ) / 2)



k (4i ) = hf (x i + h, y i + k (3i ) , z i + (3i ) )



(4i ) = hf (x i + h, y i + k (3i ) , z i + (3i ) ) (E.4)

i

i

i

(i) 2

3/1/2023 2:44:58 PM

630 • Applied Numerical Methods Using MATLAB EXAMPLE 8.31 Use the fourth-order Runge-Kutta method to integrate y′ − sin y, y(0) = 1 from x = 0 to 0.5 in steps of h = 0.1. Solution: The function specifying the differential equation is function y = fex8_31(x,y) y = sin(y); Trial>> x=0;y=1;xf=0.5;h=0.1; Trial>> [xsolution,ysolution]=RK4(@fex8_31,x,y,xf,h) xsolution =  0 0.1000 0.2000 0.3000 0.4000 0.5000 ysolution = 1.0000 1.0864 1.1768 1.2708 1.3676 1.4664 8.6.2 Picard’s Method The simultaneous differential equations of form

dy = f (x, y , z) dx

and

dz = φ(x, y , z) (8.82) dx

with the initial conditions y(x0) = y0 and z(x0) = z0 can be solved by the Picard’s method.

ANMF.CH08_4PP.indd 630

3/1/2023 2:45:03 PM

Numerical Solution of Ordinary Differential Equations • 631

Picard method gives

y1 = y0 + ∫f(x, y0, z0)dx, z1 = z0 + ∫ϕ(x, y0, z0)dx y2 = y0 + ∫f(x, y1, z1)dx, z2 = z0 + ∫ϕ(x, y1, z1)dx y3 = y0 + ∫f(x, y2, z2)dx, z3 = z0 + ∫ϕ(x, y2, z2)dx,(8.83)



and so on.

8.6.3 Second-Order Runge-Kutta Method The second-order Runge-Kutta method assumes an integration formula of the form

y(x + h) = y(x) + c0F(x, y)h + c1F[x + ph.y + qhF(x, y)]h

(8.84)

with the objective of finding the parameters c0, c1, p and q by matching Equation (8.84) to the Taylor series given by 1 y(x + h) = y(x) + y′(x)h + y′′(x)h2 + O(h3) 2! 1 = y(x) + F(x, y)h + F(x, y)h2 + O(h3)(8.85) 2 We know that ∂F n ∂F ∂F n ∂F +∑ y ′i = +∑ Fi (x, y ) F(x, y ) = ∂x i = 1 ∂y i ∂x i = 1 ∂y i where n is the number of first-order equations. Now, we can express Equation (8.85) as

 1  ∂F n y(x + h) = y(x) + F(x, y)h +  + ∑ Fi (x, y ) h 2 + O(h 3 ) (8.86)  2  ∂x i = 1

We can rewrite the last term of Equation (8.84) by applying a Taylor series in several variables as n ∂F ∂F F [ x + ph . y + qh F ( x , y )] = F ( x , y ) + ph + qh Fi (x, y ) + O(h 2 ) ∑ ∂x ∂ y i =1 i such that Equation (8.84) becomes n  ∂F  ∂F y(x + h) = y(x) + (c 0 + c1 )F(x, y)h + c1  ph + qh ∑ Fi (x, y) h + O(h 3 ) i = 1 ∂y i  ∂x  (8.87)

ANMF.CH08_4PP.indd 631

3/1/2023 2:45:14 PM

632 • Applied Numerical Methods Using MATLAB Equations (8.86) and (8.87) are identical if

c 0 + c1 = 1

c1 p =

1 2

c1 q =

1 (8.88) 2

Since Equation (8.88) represent three equations in four unknown parameters, we can arbitrarily assign any value to one of the parameters. Some common choices and the names associated with the resulting formulas are given below:

c0 = 0 c1 = 1 c0 = 1/2 c1 = 1/2 c0 = 1/3 c1 = 2/3

p = 1/2 q = 1/2 [modified Euler’s method] p = 1 q = 1 [Heun’s method] p = 3/4 q = 3/4 [Ralston’s method] (8.89)

The formula described in Equation (8.89) are classified as second-order Runge-Kutta methods. Selecting the modified Euler’s method, we substitute the corresponding parameters into Equation (8.84) to give h h   y(x + h) = y(x) + F  x + .y + F(x, y ) h (8.90) 2 2   This integration formula can be conveniently evaluated by the following sequence of operations.



K1 = hF(x, y)



h 1   K 2 = hF  x + .y + K 1   2 2 



y(x + h) = y(x) + K2



(8.91)

Second-order methods are not popular ones for computer application. 8.6.4 Fourth-Order Runge-Kutta Method The fourth-order Runge-Kutta method is also derived from the Taylor’s series and the process is a lengthy one. The fourth-order Runge-Kutta method is very popular than the second-order Runge-Kutta method. The method consists of the following sequence of operations:

K1 = hF(x, y)



K  h  K 2 = hF  x + .y + 1   2 2 



K  h  K 3 = hF  x + .y + 2   2 2 

ANMF.CH08_4PP.indd 632

3/1/2023 2:45:34 PM

Numerical Solution of Ordinary Differential Equations • 633



K4 = hF(x + h. y + K3) 1 y(x + h) = y(x) + (K1 + 2K2 + 2K3 + K4)(8.92) 6

EXAMPLE 8.32 Develop the Runge-Kutta method for a system of first-order differential equations. Solution: dy 1 = f1 (x, y 1 , y 2 ,..., y n ) dx dy 2 = f2 (x, y 1 , y 2 ,..., y n ) dx …. …. …. …. Let

dy n = f n (x, y 1 , y 2 ,..., y n )  dx with initial conditions

(E.1)



y1(x0) = y10, y2(x0) = y20, …., yn(x0) = yn0(E.2)

be a system of first-order differential equations, where y1, y2, …., yn are n dependent variables and x is the only independent variable. The above system can be written as

dy = f (x, y 1 , y 2 ,...., y n ) with y(x0) = y0.(E.3) dx

The fourth-order Runge-Kutta method for the above system is 1 yj+1 = yj + [k1 + 2k2 + 2k3 + k4](E.4) 6

ANMF.CH08_4PP.indd 633

where

 k 11   k 12   k 13   k 14  k  k  k  k  21 22 23 24 k1 =  , k 2 =   , k 3 =  , k 4 =  (E.5)                      k n1  k n2  k n3  k n4 

and

ki1 = hfi(xj, y1j, y2j, …., ynj) ki2 = hfi(xj + h/2, y1j + k11/2, y2j + k21/2, …., ynj + kn1/2)

3/1/2023 2:45:50 PM

634 • Applied Numerical Methods Using MATLAB

ki3 = hfi(xj + h/2, y1j + k12/2, y2j + k22/2, …., ynj + kn2/2) ki4 = hfi(xj + h, y1j + k13, y2j + k23, …., ynj + kn3)

for i = 1, 2, …., n. It may be noted that ykj is the value of the kth dependent variable yk evaluated at xj. The above formula in explicit vector notation is



 y 1 j+1   y 1 j    k 11   k 13   k 14   k 12                y 2 j+1  =  y 2 j  + 1   k 21  + 2  k 22  + 2  k 23  +  k 24  (E.6)       6                       k    n1   y n j+1   y n j   k n 3   k n 4  k n2 

8.6.5 Euler’s Formula Euler’s method is based on the first-order Taylor’s series integration formula

y(x + h) = y(x) + y′(x)h = y(x) + F(x, y)h

(8.93)

Figure 8.11 shows the graphical representation of Euler’s formula. y(x)

Error Euler’s formula f(x, y) x

x+h

x

FIGURE 8.11  Graphical representation of Euler’s formula.

If we assume, for simplicity that there is a single dependent variable y, such that the differential equation is y′ = f(x, y), then the change in the solution y between x and x + h is given by

y (x + h) − y (h) =

x+h

∫ x

y ′dx =

x+h

∫ f (x, y) dx (8.94) x

Equation (8.94) represents the area of the panel under the y′(x) plot as shown in Figure 8.11. Euler’s formula approximates this area by the area of the cross-hatched rectangle. Note that the area between the rectangle and the plot corresponds to the truncation error. It is obvious that the truncation error is proportional to the slope of the plot. Hence it is proportional to y′′(x).

ANMF.CH08_4PP.indd 634

3/1/2023 2:45:54 PM

Numerical Solution of Ordinary Differential Equations • 635

8.6.6 Modified Euler’s Formula Figure 8.12 shows the graphical representation of the modified Euler’s ­formula for a single ordinary differential equation: y′ = f(x, y). y(x) f(x, y)

h/2 h/2 f(x + h/2.y + K1/2) x x x+h

FIGURE 8.12  Graphical representation of the modified Euler’s formula.

The first of Equation (8.91) gives us an estimate of y at the midpoint of the panel by the Euler’s formula:

K h h  y  x +  = y (x ) + f (x , y ) = y (x ) + 1  2 2 2

The second of Equation (8.91) then approximates the area of the panel by the area K2 of the cross-hatched rectangle. The error is proportional to the curvature y ′′′ of the plot. 8.6.7 Burlirsch-Stoer Method (Mid-Point Method) When high accuracy is desired, Burlirsch-Stoer method is preferred. The basis of Burlirsch-Stoer method is the mid-point formula of numerical integration of y′ = F(x, y):

y(x + h) = y(x − h) + 2hF[x, y(x)]

(8.95)

y(x) h

f(x,y) h x

x–h

x

x+h

FIGURE 8.13  Graphical representation of midpoint formula.

Figure 8.13 shows the graphical representation of the midpoint formula for a single differential equation:

ANMF.CH08_4PP.indd 635

y′ = F(x, y)

3/1/2023 2:45:56 PM

636 • Applied Numerical Methods Using MATLAB The change in y over the two panels as shown in Figure 8.13 is given by

y (x + h) − y (x − h) −

x+h



y ′(x) dx 

(8.96)

x −h

Which equals the area under the y′(x) curve. The midpoint method approximate this area by the area 2hf(x, y) of the cross-hatched rectangle. 8.6.8 The Runge-Kutta-Fehlberg Method Runge-Kutta methods are very popular as computational techniques. Some of the variants of Runge-Kutta methods are RK-Butcher, RK-Fehlberg, RK-Merson, RK-Centroidal mean, RK-arithmetic mean, and so on. The Runge-Kutta-Fehlberg method gives better accuracy by solving the problem twice using step sizes h and h/2. In this method, at each step two different approximations for the solution are computed. If the two approximations are close enough, then the solution is obtained. If the approximation is not acceptable, then the step size is reduced. In each step, the following six values are computed:

k1 = hf(xi, yi)



k  h  k 2 = hf  x i + , y i + 1   4 4



3h 3 9   k 3 = hf  x i + , y i + k1 + k 2   8 32 32 



12 1932 7200 7296   k 4 = hf  x i + h, y i + k1 − k2 + k3   13 2197 2197 2197 



439 3680 845   k 5 = hf  x i + h, y i + k1 − 8k 2 + k3 − k4   216 513 4106 



8 3544 1859 11  h  k 6 = hf  x i + , y i − k 1 + 2 k 2 − k3 + k4 − k5   2 27 2565 4104 40 

An approximation using the fourth-order Runge-Kutta method is given by

ANMF.CH08_4PP.indd 636

y i +1 = y i +

25 1408 2197 1 k1 + k3 + k4 − k5 216 2565 4104 5

3/1/2023 2:46:08 PM

Numerical Solution of Ordinary Differential Equations • 637

The value of k2 is not used in the above formula. The other value of y is determined by the fifth-order Runge-Kutta method as follows: 16 6656 28561 9 2 k1  k3  k4  k5  k6 135 12825 56430 50 55  If [y i 1  y i 1 ] is small enough then the method is completed; if not, the computation is repeated by reducing the step size h. The local truncation error of this method is given by y i 1  y i 1 .

y i 1  y i 

8.6.9 The Runge-Kutta-Butcher Method The Runge-Kutta-Butcher method is a sixth-order method since it requires six function evaluations (but is a fifth-order method only), but in reality the working order is close to five. In this method, a pair of relationships are developed to determine y as follows:

y i +1 = y i +

1 (7 k 1 + 32 k 3 + 12 k 4 + 32 k 5 + 7 k 6 ) 90

1 y i 1  y i  (k 1  4 k 4  k 6 ) 6 where k1 = hf(xi, y+) k2 = hf(xi + h/4, yi + k1/4) k3 = hf(xi + h/4, yi + k1/8 + k2/8) k4 = hf(xi + h/2, yi + k1/2 + k3) k5 = hf(xi + 3h/4, yi + 3k1/16 + 9k4/16) k6 = hf(xi + h, yi − 3k1/7 + 2k2/7 + 12k3/7 − 12k4/7 + 8k5/7) and

The method is terminated when [y i 1  y i 1 ] is small enough. The local truncation error of this method is given by y i 1  y i 1 .

8.7 TWO-POINT BOUNDARY VALUE PROBLEMS In two-point boundary value problems the auxiliary conditions associated with the differential equation, called the boundary conditions, are specified at two different values of x. The boundary value problems require solution of a differential equation in a region R subject to the various conditions on the boundary of R.

ANMF.CH08_4PP.indd 637

3/1/2023 2:46:25 PM

638 • Applied Numerical Methods Using MATLAB There exist many engineering practical applications that give rise to various ­boundary value problems. One simple example of a two-point linear boundary value problem is represented by

y′′(x) + f(x)y′(x) + g(x)y(x) = r(x)

(8.97)

with the boundary conditions:

y(x0) = a, y(xn) = b, y′(x) + p(x)y(x) = q(x)

(8.98)

Also, the conditions

y(x0) = y′(x0) = a, and y(xn) = y′′(xn = b)

(8.99)

There are many numerical methods available for solving such boundary value problems. Here, we present the finite difference method, where the differential equations are approximated by finite differences at evenly spaced mesh points. As a consequence of this, a differential equation is transformed into a set of simultaneous algebraic equations. In addition, we present the shooting method. This method can be applied to both linear and nonlinear problems and required good initial guesses (estimates) for the slope. 8.7.1 Finite Difference Method y

yn–2

yn–1 yn

y3 y0 x0

yn+1

y2

y1 x1

x2 a

x3

xn–2 xn–1

xn xn+1

x

b

FIGURE 8.14  Finite difference mesh.

In the finite difference method the range of integration (a, b) is divided into n − 1 equal subintervals of length h each, as shown in Figure 8.14. The values of the numerical solution at the mesh points are denoted by yi, i = 1, 2, …, n. Two approximations are made:

ANMF.CH08_4PP.indd 638

3/1/2023 2:46:26 PM

Numerical Solution of Ordinary Differential Equations • 639

1. T  he derivatives of y in the differential equation are replaced by the finite difference expressions. The first central difference approximations are used y − y i −1 y − 2 y i + y i +1 y i′ = i +1 (8.100) y i′′= i −1 2h h2 and so on. 2. The differential equation is enforced only at the mesh points. Hence, the differential equations are replaced by n simultaneous algebraic equations, the unknowns being yi, i = 1, 2, …., n. If the differential equation is nonlinear, the algebraic equations will also be nonlinear and they are solved by the Newton’s method. Since the truncation error in a first central difference approximation is O(h2), the finite difference method is not good accurate method. 8.7.2 Second-Order Differential Equations Consider the second-order differential equation

y′′ = f(x, y, y′)

with the boundary conditions

y(a) = α or y′(a) = α y(b) = β or y′(a) = β

By approximating the derivatives at the mesh points by finite differences, we can write y i −1 − 2 y i + y i + 1 y − y i −1   = f  x i , y i , i +1 (8.101)  , i = 1, 2, …., n 2  h 2h y − y0 = α  y1 = α or 2 (8.102) 2h y − y n −1 yn = β or n +1 (8.103) = β  2h Here y0 and yn+1 are associated with points outside the solution domain (a, b). These can be eliminated by using the boundary conditions. Equation (8.101) can be rewritten as



ANMF.CH08_4PP.indd 639

y − y0   y 0 − 2y 1 + y 2 − h2 f  x1 , y 1 , 2  = 0   2h 

(8.104)

3/1/2023 2:46:42 PM

640 • Applied Numerical Methods Using MATLAB



y − y i −1   y i −1 − 2 y i + y i + 1 − h 2 f  x i , y i , i + 1  = 0,  2h

  i = 2, 3, …., n − 1

(8.105)

y − y n −1   y n −1 − 2 y n + y n + 1 − h 2 f  x n , y n , n + 1  = 0 (8.106)  2h

The boundary conditions on y: Equation (8.104) is simply replaced by y1 − α = 0 and Equation (8.106) is replaced by yn − β = 0. If y′ are prescribed, we obtain from Equation (8.102) and Equation (8.103) y0 = y2 − 2hα and yn+1 = yn−1 + 2hβ, which are then substituted into Equations (8.104) and (8.106) respectively. Hence, we finish up with n equations in the unknowns yi, i = 1, 2, …., n:

y1 − α = 0 −2 y 1 + 2 y 2 − h 2 f (x 1 , y 1 , α) − 2 hα = 0

if y (a) = α   (8.107) if y ′(a) = α 

y − y i −1   y i −1 − 2 y i + y i +1 − h 3 f  x i , f i , i +1  = 0 i = 2, 3, …., n − 1  2h

yn − β = 0 2 y n −1 + 2 y n − h f (x n , y n , β) − 2 hβ = 0 2

(8.108)

if y (b) = β   (8.109) if y ′(b) = β

EXAMPLE 8.33 Solve y′′ = −0.2y′ − x, y(0) = 0 and y′(0) = 1 from x = 0 to 2 in increments of h = 0.25 with the fourth-order Runge-Kutta method. Solution: Letting y1 = y and y2 = y′, we write the equivalent first-order equations as y2  y 1′    F(x, y ) = y ′ =   =    y 2′   −0.2 y 2 − x  function F = fex8_33(x,y) F=zeros(1,2); F(1)=y(2); F(2)=−0.2∗ y(2)−x; Trial>> [xsolution,ysolution]=RK4(@fex8_33,0,[0 1],2.0,0.25) xsolution =  0 0.2500 0.5000

ANMF.CH08_4PP.indd 640

3/1/2023 2:47:00 PM

Numerical Solution of Ordinary Differential Equations • 641

0.7500 1.0000 1.2500 1.5000 1.7500 2.0000 ysolution =   0 1.0000 0.2413 0.9205 0.4555 0.7839 0.6287 0.5930 0.7477 0.3505 0.7998 0.0588 0.7732 −0.2796 0.6563 −0.6625 0.4384 −1.0877 8.7.3 The Shooting Method The shooting method involves three steps: 1. F  irst, the given boundary value problem is transformed into two initial value problems. 2. T  he solution of these two initial value problems is determined by such methods as the Taylor’s series and Runge-Kutta methods. 3. T  he required solution of the given boundary value problem is given by the addition of the two solutions found in step 2. Let the boundary value problem is given as y′′ + p(x)y′ + q(x)y = r(x) with y(a) = α and y(b) = β(8.110) Assuming that u(x) is the unique solution of the initial value problem, we can write u′′(x) − p(x)u′(x) − q(x)u(x) = r(x) with u(a) = α and u′(b) = 0

(8.111)

Additionally, assuming that v(x) is the unique solution to the initial value problem, we have v′′(x) − p(x)v′(x) − q(x)v(x) = 0 with v(a) = 0 and v′(b) = 1

ANMF.CH08_4PP.indd 641

(8.112)

3/1/2023 2:47:00 PM

642 • Applied Numerical Methods Using MATLAB Then the linear combination y(x) = c1u(x) + c2v(x) 

(8.113)

is the solution of Equation (8.110) for some constants c1, c2. As y(a) = α and y(b) = β, we have α = c1u(a) + c2v(a)

β = c1u(b) + c2v(b) β − u(b) From the Equation (8.114), c1 = 1 and from the Equation (8.115), c2 = v(b) are obtained. Hence, Equation (8.113) reduces to

y (x) = u(x) +

β − u(b) v(x).  v(b)

(8.114)

Equation (8.116) a solution of the boundary value problem in Equation (8.110) and it also satisfies the boundary conditions y(a) = α, y(b) = β. The method involves solving two systems of equations over [a, b]. The first system is

u′ = w(x) with u(a) = α

and

w′(x) = p(x)w(x) + q(x)u(x) + r(x) with w(a) = u′(a) = 0 

(8.115)

and the second system is

v′ = z(x) with v(a) = 0



z′(x) = p(x)z(x) + q(x)v(x) with z(a) = v′(a) = 1 

(8.116)

Finally, the desired solution y(x) is obtained from Equation (8.95). However, when the expression p(x)y′(x) + q(x)y(x) − r(x) is nonlinear then several iterations are needed to obtain the solution at b to a prescribed accuracy. Difficulties may be encountered to obtain a convergent solution if y(b) is a very sensitive function of y′(a). When that happens, it is recommended to integrate from the opposite direction by guessing a value of y′(b) and iterating until y(a) is sufficiently close to y0. Generally speaking, the speed of convergence depends on how good the initial estimate was selected. EXAMPLE 8.34 The position of falling object is governed by the following differential equation:

ANMF.CH08_4PP.indd 642

d 2 x c  dx  +   −g=0 dt 2 m  dt 

3/1/2023 2:47:08 PM

Numerical Solution of Ordinary Differential Equations • 643

where c = a drag coefficient = 12 kg/s, m = 80 kg, and g = acceleration due to gravity = 9.82 m/s2. Use the shooting method to solve this equation for the following boundary conditions: x(0) = 0 x(12) = 600. Solution: The second-order equation can be rewritten as a pair of first-order equations: dx =v dt c dv =g− v dt m A function can be developed to hold these equations. Function dy = The MATLAB program below generate a plot of all the shots as well as the analytical solution. function dy=prob2221sys(x,y) g=9.82;c=12;m=80; dy=[y(2);g−c/m∗ y(2)]; Trial>> ti=0;tf=12; za1=0;za2=50;xa=0;xb=600; [t1,x1]=ode45(@prob2221sys,[ti tf],[xa za1]); xb1=x1(length(x1)); [t2,x2]=ode45(@prob2221sys,[ti tf],[xa za2]); xb2=x2(length(x2)); za=za1+(za2−za1)/(xb2−xb1)∗ (xb−xb1); [t,x]=ode45(@prob2221sys,[ti tf],[xa za]); plot(t,x(:,1),t1,x1(:,1),’--’,t2,x2(:,1),’--’) disp(‘results:’) fprintf(‘1st shot: za1 = %8.4g xb1 = %8.4g\n’,za1,xb1) fprintf(‘2nd shot: za2 = %8.4g xb2 = %8.4g\n’,za2,xb2) fprintf(‘Final shot: za = %8.4g x = %8.4g\n’,za,x(length(x))) fprintf(‘\n t x dx/dt\n’) disp([t x]) Trial>> xlabel(‘Time’) ylabel(‘d/dt’)

ANMF.CH08_4PP.indd 643

3/1/2023 2:47:10 PM

644 • Applied Numerical Methods Using MATLAB The results obtained are shown below: 1st shot: za1 = 0 xb1 = 421.3 2nd shot: za2 = 50 xb2 = 699.5 Final shot: za = 32.11 x = 600  t  x  0  0 0.0000 0.0001 0.0000 0.0001 0.0000 0.0002 0.0000 0.0002 0.0000 0.0005 0.0000 0.0007 0.0000 0.0010 0.0000 0.0012 0.0001 0.0025 0.0001 0.0037 0.0002 0.0050 0.0002 0.0062 0.0004 0.0125 0.0006 0.0188 0.0008 0.0251 0.0010 0.0314 0.0020 0.0628 0.0029 0.0942 0.0039 0.1256 0.0049 0.1570 0.0098 0.3142 0.0147 0.4715 0.0196 0.6289 0.0244 0.7864 0.0489 1.5758 0.0733 2.3682 0.0978 3.1636 0.1222 3.9619 0.2444 7.9972 0.3667 12.1046 0.4889 16.2827 0.6111 20.5303 0.9111 31.2431

ANMF.CH08_4PP.indd 644

dx/dt 32.1134 32.1134 32.1134 32.1134 32.1134 32.1135 32.1135 32.1136 32.1136 32.1138 32.1140 32.1142 32.1144 32.1154 32.1163 32.1173 32.1183 32.1232 32.1281 32.1330 32.1379 32.1623 32.1867 32.2111 32.2355 32.3571 32.4783 32.5990 32.7193 33.3142 33.8982 34.4717 35.0347 36.3738

3/1/2023 2:47:10 PM

Numerical Solution of Ordinary Differential Equations • 645

1.2111 42.3487 37.6540 1.5111 53.8298 38.8778 1.8111 65.6700 40.0478 2.1111 77.8533 41.1663 2.4111 90.3648 42.2356 2.7111 103.1900 43.2578 3.0111 116.3150 44.2350 3.3111 129.7267 45.1693 3.6111 143.4124 46.0624 3.9111 157.3602 46.9162 4.2111 171.5584 47.7325 4.5111 185.9961 48.5129 4.8111 200.6627 49.2589 5.1111 215.5482 49.9721 5.4111 230.6428 50.6539 5.7111 245.9375 51.3057 6.0111 261.4233 51.9288 6.3111 277.0920 52.5245 6.6111 292.9354 53.0940 6.9111 308.9459 53.6384 7.2111 325.1160 54.1589 7.5111 341.4389 54.6564 7.8111 357.9077 55.1321 8.1111 374.5161 55.5869 8.4111 391.2578 56.0216 8.7111 408.1271 56.4372 9.0111 425.1183 56.8345 9.3111 442.2261 57.2144 9.6111 459.4453 57.5775 9.9111 476.7710 57.9246 10.2111 494.1985 58.2565 10.5111 511.7234 58.5738 10.8111 529.3414 58.8771 11.1111 547.0483 59.1670 11.4111 564.8403 59.4442 11.5583 573.6019 59.5758 11.7055 582.3827 59.7044 11.8528 591.1822 59.8303 12.0000 600.0000 59.9534

ANMF.CH08_4PP.indd 645

3/1/2023 2:47:10 PM

646 • Applied Numerical Methods Using MATLAB

700 600 500

d/dt

400 300 200 100 0

0

2

4

6 Time

8

10

12

FIGURE 8.15

8.8 SECOND-ORDER INITIAL VALUE PROBLEM (IVP) Consider the second-order linear initial value problem of the form

y′′ + p(x)y′ + q(x)y = r(x)

(8.117)

with the initial conditions

x = x0, y(x0) = y0,

y′(x0) = y′0(8.118)

The values of y′(xi) and y′′(xi) can be substituted from Equations (8.116) to (8.117) and Equation (8.118) into obtain the equation y i + 1 − 2 y i + y i −1 y − y i −1 + q(x i )y i = r(x i ). + p(x i ) i +1 2 h 2h The above equation can be simplified to



[2 − hp(xi)]yi−1 + [2h2q(xi) − 4]yi +[2 + hp(xi)]yi+1 = 2h2r(xi)(8.119)

Now, consider and

ANMF.CH08_4PP.indd 646

Ci = 2 − hp(xi) Ai = 2h2q(xi) − 4 Bi = 2 + hp(xi) Di = 2h2r(xi)(8.120)

3/1/2023 2:47:14 PM

Numerical Solution of Ordinary Differential Equations • 647

Equation (8.119) can be simplified to

Ciyi−1 + Aiyi + Biyi+1 = Di(8.121)

The initial condition y′(x0) = y′0 gives y 1 − y −1 2h or y−1 = y1 − 2hy′0 

y 0′ =

(8.122)

Now from Equation (8.123), we have

C0y−1 + A0y0 + B0y1 = D0(8.123)

The quantity y−1 can be eliminated between Equations (8.122) and (8.123), and the value of y1 can be written as

y1 =

D0 − A 0 y 0 + 2 hC0 y 0′ (8.124) C 0 + B0

Since p, q, r are known functions, A, B, C, D can easily be determined at x = x0, x1, …. That is, the right-hand side of (8.124) is a known quantity. Hence, (8.124) gives the value of y1. Also, y0 is known. Hence, the value of yi+1 can be obtained from (8.121) as

y i +1 =

Di − C i y i − 1 − A i y i , xi+1 = xi + h for i = 1, 2, …. Bi

(8.125)

Hence, the values of y1, y2, … are determined recursively from (8.125). Error The local truncation error is given by

 y − y i −1   y − 2 y i + y i +1  ELTE =  i −1 − y ′′i  + p i  i +1 − y ′i  (8.126) 2     2h h

Expanding the terms yi−1 and yi+1 by the Taylor’s series and simplifying, Equation (8.126) gives

ELTE =

h 2 iv 4 (y + p i y ′′′ i ) + O(h ). 12

Hence, the finite difference approximation has second-order accuracy for the functions with continuous fourth derivatives.

ANMF.CH08_4PP.indd 647

3/1/2023 2:47:23 PM

648 • Applied Numerical Methods Using MATLAB

8.9 SECOND-ORDER BOUNDARY VALUE PROBLEM (BVP) Let us consider the linear second-order differential equation y′′ + p(x)y′ + q(x)y = r(x),

a < x < b

(8.127)

with boundary conditions y(a) = γ1 and y(b) = γ2. Let the interval [a, b] be divided into n subintervals with spacing h. That is, xi = xi−1 + h, i = 1, 2, …., n − 1. Now, Equation (8.127) is satisfied by x = xi. Hence y′′i + p(xi)y′i + q(xi)yi = r(xi) 

(8.128a)

Now, y′′i and y′i are replaced by finite difference expressions y ′′= i

y i −1 − 2 y i + y i + 1 + O(h 2 ) , 2 h

y ′i =

y i + 1 − y i −1 + O(h 2 ) .(8.128b) 2h

Using Equation (8.128a), Equation (8.128b) becomes y i −1 − 2 y i + y i + 1 y − y i −1 + q(x i )y i = r(x i ). + p(x i ) i +1 2 h 2h That is, yi−1[2 − hp(xi)] + yi[2h2q(xi) − 4] + yi+1[2 + hp(xi)] = 2h2r(xi)(8.128)

Let us consider and

Ci = 2 − hp(xi) Ai = 2h2q(xi) − 4 Bi = 2 + hp(xi) Di = 2h2r(xi)(8.129)

Equation (8.129) can be written as

Ciyi + Aiyi + Biyi+1 = Di,

for i = 1, 2, …, n − 1

(8.130)

The boundary conditions then are y0 = γ1, yn = γ2. For i = 1, n − 1, Equation (8.130) becomes

C1y0 + A1y1 + B1y2 = D1 or A1y1 + B1y2 = D1 − C1g1 (as y0 = γ1)

and

Cn−1yn−2 + An−1yn−1 + Bn−1yn = Dn−1

or, Cn−1yn−2 + An−1yn−1 = Dn−1 − Bn−1γ2 (as yn = γ2) Equation (8.130) in matrix notation can be written as where

ANMF.CH08_4PP.indd 648

Ay = b 

(8.131) t

y = [y1, y2, …., yn−1]

3/1/2023 2:47:33 PM

Numerical Solution of Ordinary Differential Equations • 649



b = 2h2[r(x1) − {C1γ1}/(2h2), r(x2), …., r(xn−2), r(xn−1) − {Bn−1γ2)/(2h2)]t

and

 A1 C  2 A=0    0

B1 A2 C3  0

0 0 B2 0 A 3 B3   0 0

 0  0  0    C n −1

0  0   0  (8.132)    A n −1 

Equation (8.132) is a tri-diagonal system which can be solved by the methods described in Chapter 2. The solution of this system, i.e., the values of y1, y2, …., yn−1 constitutes the approximate solution of the BVP.

8.10 MATLAB BUILT-IN FUNCTIONS MATLAB has the following function for solution of boundary value problem: sol = bvp4c(@dEqs, @residual, solinit) uses a high-order finite difference method with an adaptive mesh to solve boundary value problems. The output sol is a structure (a MATLAB data type) created by bvp4c. The first input arguments are handles to the following user-supplied functions. F = dEqs(x, y) specifies the first-order differential equations F(x, y) = y′. Both F and y are column vectors. r = residual(ya, yb) specifies all the applicable the boundary residuals yi(a) − αi and yi(b) − βi in a column vector r, where αi and βi are the prescribed boundary values. The third input argument solinit is a structure that contains the x and y-­values at the nodes of the initial mesh. This structure can be generated with MATLAB function bvpinit: Solinit = bvpinit(xinit,@yguess) where xinit is a vector containing the x-­coordinates of the nodes; yguess(x) is a user-supplied function that returns a column vector containing the trial solutions for the components of y. The numerical solution at user-defined mesh points can be extracted from the structure sol with the MATLAB function deval: y = deval(sol,xmesh) where xmesh is an array containing the x-­coordinates of the mesh points. The function returns a matrix with the ith row containing the values of yi at the mesh points.

ANMF.CH08_4PP.indd 649

3/1/2023 2:47:35 PM

650 • Applied Numerical Methods Using MATLAB

8.11 SUMMARY Differential equations arise in scientific and engineering applications when a dependent variable y varies with the independent variable either time t or position x. In this chapter, the numerical techniques commonly used for solving ordinary differential equations are presented. There are two categories of methods to solve ordinary differential equations: one-step methods and multistep methods. In one-step methods, the value of the increment function is based on information at a single point i. The class of methods called multistep methods use information from several previous points as the basis for extrapolating to a new value. Among the one-step methods or single-step methods, Picard’s method of successive approximation method and the Taylor’s series method were presented. Among the step-by-step methods or the marching methods, ­ Euler’s method, the modified Euler’s method, the Runge-Kutta methods of order two and four, the Adam-Moulton predictor-corrector method, and Milne’s ­predictor-­corrector method are presented. These methods have been illustrated with example problems and solution.

EXERCISES NOTE

In addition to solving the following exercises through analytical ­solutions or long-hand numerical computations, try to solve them using user-defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 8.1. Use Picard’s method of successive approximation to solve the equation dy = 1 + xy, y (0) = 1 at x = 0.1. dx dy 8.2. Solve = x + y with the initial condition x0 = 0, y0 = 1 using Picard’s dx method of successive approximation. 8.3. Use Picard’s method of successive approximation to find y (0.2) correct dy to five decimal places by solving = x − y, with y (0) = 1. dx 8.4. Use Picard’s method of successive approximation to tabulate the values dy of y (0.1), y (0.2), …., y(1.0) and form the solution of = x(1 + x3y), dx y(0) = 3.

ANMF.CH08_4PP.indd 650

3/1/2023 2:47:36 PM

Numerical Solution of Ordinary Differential Equations • 651

8.5. Use Picard’s method of successive approximation to find y(0.1) from dy y − x = the equation , y(0) = 1. dx y + x 8.6. Use Picard’s method of successive approximation to find y(0.2) by solvdy ing the equation = x + y2 with y(0) = 0. dx 8.7. Using the Taylor’s series method for y(x), and find y(0.1) correct to four decimal places from the initial value problem y′ = xy + 1, y(0) = 1. 8.8. Find the values of y(1.1) and y(1.2) correct to three decimal places dy given that = xy1/3, y(1) = x(1) = 1 using the first three terms of the dx Taylor’s series expansions. 8.9. Find the value of y at x = 0.1 and x = 0.2 using the Taylor’s series dy method from = x2y − 1, y(0) = 1 accurate to five decimal places. dx 1 8.10. Given that y + 4y = x2, y(0) = 1, determine y(0.2) with the fourth-order Taylor’s series method using a single integration step. 8.11. Using the Taylor’s series method for y(x) given that y1 = y2 − x, y(0) = 1, find y(0.1) and y(0.3) correct to four decimal places. 8.12. Use the Taylor’s series method to solve the differential equation dy 1 = 2 dx x + y , y(4) = 4 to find y(4) and y(4.2). dy 8.13. Use Euler’s method to find y(1) from the differential equation = dx x + y, y(0) = 1. dy = −1.2y + 7e−0.3x from x = 0 to x = 2 with dx the initial condition y = 3 at x = 0. Take h = 0.5.

8.14. Use Euler’s method to solve

dy = x + y2, y(1) = 0 at x = 1.3 with h = 0.5 dx and at x = 1.175 with h = 0.025.

8.15. Using Euler’s method solve

8.16. Solve the following differential equation using Euler’s method for x = 0 dy to 0.4. = 3yt2 + 2yt = 1 with y(0) = 1. Take step size h = 0.1. dx

ANMF.CH08_4PP.indd 651

3/1/2023 2:47:39 PM

652 • Applied Numerical Methods Using MATLAB 8.17. Use Euler’s method to approximate the solution of the following initial value problem. dy = te3t − 2y, 0 ≤ t ≤ 1, y(0) = 0, with h = 0.5. dt dy 8.18. Solve = x2(1 + y) with y(1) = 1 to find y(1.1) by using Euler’s method dx and taking h = 0.025. 8.19. Use the modified Euler’s method to find an approximate value of y when x = 0.3. Given that

dy = x + y, y(0) = 1. dx

8.20. Repeat Exercise 8.14 using the modified Euler’s method. 8.21. Use the modified Euler’s method to find the value of y at x = 0.1, given that y(0) = 1 and y1 = x2 + y. 8.22. Using the modified Euler’s method to find the value of y(2) in steps of dy 0.1, given that = 2 + xy , y(1) = 1. dx dy 8.23. Solve = (xy + 1) with y(0) = 1 for finding y(0.075) by using the dx modified Euler’s method taking h = 0.025. 8.24. Use the modified Euler’s method to approximate the solution to the following initial value problem. dy = 1 + (t − y)2, 2 ≤ t ≤ 3, y(2) = 1, with h = 0.5. dx 8.25. Find y(0.1), y(0.2), y(0.3), and y(0.4) correct to four decimal places dy given that = y − x, y(0) = 2. Take h = 0.1 and use the second-order dx Runge-Kutta method. 8.26. Use the second-order Runge-Kutta method to solve the equation dy = sin y, y(0) = 1 dx from x = 0 to 0.5 in steps of h = 0.1. Keep four decimal places in the computations.

ANMF.CH08_4PP.indd 652

3/1/2023 2:47:43 PM

Numerical Solution of Ordinary Differential Equations • 653

8.27. Use the second-order Runge-Kutta method to solve the equation dy 2 = t − y + 1 0 ≤ t ≤ 0.5 dt with y(0) = 1 and h = 0.1. Keep five decimal places in the computations. 8.28. Using the Runge-Kutta method of order 2, find y for x = 0.1, given dy that y = 1 when x = 0 and = x + y. Use h = 0.1 and keep five decimal dx places in the computations. 8.29. Use the second-order Runge-Kutta method to solve the equation dy = y − x, dx

y(0) = 2

and find y(0.1) correct to four decimal places with h = 0.1 and keep four decimal places.

dy (1 + xy ) 8.30. Solve dx = (x + y ) , y(1) = 1.2 by the Runge-Kutta method of order 2. Take h = 0.1 for y(1.2). 8.31. Use the classical Runge-Kutta method of fourth order to find the numerical solution at x = 0.8 for dy = x + y , y = (0.4) = 0.41. dx Assume a step length of h = 0.2. 8.32. Use the Runge-Kutta fourth-order method to find the value of y when x = 1 given that dy y − x = , y(0) = 1. dx y + x 8.33. Use the Runge-Kutta fourth-order method to solve the equation dy y 2 − x 2 = with y(0) = 1 at x = 0.2, 0.4. Take h = 0.2. dx y 2 + x 2 8.34. Use the classical fourth-order Runge-Kutta method to solve dy = −1.2y + 7e−0.3x from x = 0 to x = 1.5 with the initial condition y = 3 dx at x = 0. Take h = 0.5.

ANMF.CH08_4PP.indd 653

3/1/2023 2:47:56 PM

654 • Applied Numerical Methods Using MATLAB 8.35. Use the classical fourth-order Runge-Kutta method to integrate f(x, y) = −2x3 + 12x2 − 20x + 8.5 using a step size of h = 0.5 and an initial condition of y = 1 at x = 0. Compute y(0.5). 8.36. Use the Runge-Kutta fourth-order method to find y(0.2), y(0.4), and dy y(0.6) given that = 1 + y2, y(0) = 0 and take h = 0.2. dx 8.37. Use the Adams-Moulton method to solve the differential equation dy = x2 − y + 1, 0 ≤ x ≤ 1 with y(0) = 1. Use h = 0.1 and find the solution dx of y(0.2) accurate to six digits. 8.38. Use the Adams-Moulton method to solve the differential equation dy = x − y2, y(0) = 1 to find y(0.4). Given that y(0.1) = 0.9117, dx y(0.2) = 0.8494 and y(0.3) = 0.8061. dy 8.39. Use Adams-Moulton method to find y(0.8) given that = 1 + y2, dx y(0) = 0. 8.40. Use the Adams-Moulton method to solve the differential equation dy = x2 − y + 1, 0 ≤ x ≤ 1 with y(0) = 1. Find y(0.4) given that dx y(0.1) = 1.0003, y(0.2) = 1.00243 and y(0.3) = 1.00825. dy = x2(1 + y) dx and that y(1) = 1, y(1.1) = 1.233, y(1.2) = 1.543 and y(1.3) = 1.979.

8.41. Use the Adams-Moulton method to find y(1.4) given that

8.42. Use the Adams-Moulton method to approximate the solution to the following initial value problem. Use the exact starting values dy = 1 + (t − y)2, 2 ≤ t ≤ 3, y(2) = 1, with h = 0.2. dx 1 Actual solution is y(t) = t + . 1−t 8.43. Use Milne’s predictor-corrector method to find y(0.8) taking h = 0.2. dy Given that = y + x2 with y(0) = 1. dx dy 8.44. Use Milne’s predictor-corrector method to solve = x + y, with the dx initial condition y(0) = 1, from x = 0.2 to x = 0.3.

ANMF.CH08_4PP.indd 654

3/1/2023 2:47:59 PM

Numerical Solution of Ordinary Differential Equations • 655

8.45. Use Milne’s method to compute the solution at x = 0.4 given that dy = xy + y2, y(0) = 1. Take h = 0.1 and obtain the starting values for dx Milne’s method using Runge-Kutta method of order 4. 8.46. Use Milne’s method to solve the differential equation for x = 0.4 and dy x = 0.5 given that = x2 − y + 1, 0 ≤ x ≤ 1 and y(0) = 1, given y(0.1) = 1, dx y(0.2) = 1.0024 and y(0.3) = 1.0083. dy = 1 + y2, dx y(0) = 0 and y(0.2) = 0.2027, y(0.4) = 0.4228 and y(0.6) = 0.6841. Take h = 0.2.

8.47. Use Milne’s method to find y(0.8) and y(1.0) given that

dy = −y with y(0) = 1 by using Milne’s predictor-corrector dx method for x = 0.5 to 0.7 with h = 0.1.

8.48. Solve

8.49. Solve the following first-order ordinary differential equation y′ − yx + x2 = 0 from x = 0 to x = 1.8 with y(0) = 1 by applying a) Euler’s method b) fourth-order Runge-Kutta method with h = 0.6

Compare the results in both cases with the exact solution: 2

y = x 2 − e0.5 x + 2 by computing the error between the computed and exact solution. 8.50. Convert the following differential equations into first-order equations of the form y′ = f(x, y): a) y′′y − xy′ − 2y2 = 0 b) (y′′)2 = |32 y′x − y2| 8.51. Solve the following simultaneous equations over the interval from t = 0 to 0.4 with a step size of 0.1. The initial conditions are: y(0) = 2 and z(0) = 4. Use a) Euler’s method b) fourth-order Runge-Kutta method

ANMF.CH08_4PP.indd 655

3/1/2023 2:48:01 PM

656 • Applied Numerical Methods Using MATLAB

Obtain a plot of the results.

8.52. Solve the initial value problem over the interval from t = 0 to 2 where dy y(0) = 1. Use = yt 2 − 1.1y. dt a) analytical method b) Euler’s method with h = 0.25 and 0.5 c) midpoint method with h = 0.5 d) fourth-order Runge-Kutta method with h = 0.5 dy = (1 + 2 x) y over the interval from x = 0 to 1 dx using a step size of 0.25. Given that y(0) = 1. Use

8.53. Solve the equation

a) analytical method b) Euler’s method c) fourth-order Runge-Kutta method 8.54. Given that y′ + 4y − x2 = 0, y(0) = 1. Determine y(0.2) with the fourthorder Taylor series method using a single integration step. Compute also the estimated error from the relationship hm E = [y m (x + h) − y m (x)]. (m + 1)!

Compare this error with the actual error. The analytical solution of the differential equation is given by

y=

31 −4 x 1 2 1 1 e + x − x+ . 32 4 8 32

8.55. Solve the equation y′ − x + y = 0 from x = 0 to x = 1.5 with y(0) = 1. a) Euler’s method with h = 0.5 b) fourth-order Runge-Kutta method with h = 0.5 c) the exact solution is given by y = x + 2e−x − 1

ANMF.CH08_4PP.indd 656

Compare the computed and analytical solutions in each of the above cases (a) to (c)

3/1/2023 2:48:08 PM

Numerical Solution of Ordinary Differential Equations • 657

8.56. Apply the fourth-order Runge-Kutta method to integrate dy = 4e0.8 t − 0.5 y from t = 0 to 1 using a step size of 1 with y(0) = 2. dx 8.57. Solve y′′ + 0.1y′ + x = 0, y(0) = 0, and y′(0) = 1 from x = 0 to 2 with the Taylor series method of order four using h = 0.25. 8.58. Use the second-order Runge-Kutta method to integrate y′ = sin y, y(0) = 1 from x = 0 to 0.5 in steps of h = 0.1. Maintain four decimal places in the computations. 8.59. Solve y′′ = −0.1y′ − x, y(0) = 0, y′(0) = 1 from x = 0 to 2 in increments of h = 0.25 with the fourth-order Runge-Kutta method. 8.60. Consider the following system of two ordinary differential equations: x′ − xt + y = 0 y′ − yt − x = 0

from t = 0 to t = 1.2 with x(0) = 1, and y(0) = 0.5. Solve the equations by a) Euler’s method with h = 0.4 b) fourth-order Runge-Kutta method using h = 0.4 dy = 4e0.8 t − 0.5 y from t = 0 to 4 with a step size of 1 with the dx initial conditions at t = 0, y = 2. The exact solution is given by

8.61. Integrate



y=

4 0.8 t −0.5t  + 2e −0.5 t . − e  1.3  e

8.62. Use the fourth-order Runge-Kutta method to integrate y′ = 3y − 4e−x, y(0) = 1 from x = 0 to 10 in steps of h = 0.1. Compare the result with the analytical solution y = e−x. 8.63. Use the fourth-order Runge-Kutta method to solve the equation y′ = −1.2y + 7e−0.3x from x = 0 to x = 1.5 with the initial condition y = 3 at x = 0. Take h = 0.5. 8.64. Use Picard’s method to obtain the second approximation to the d2 y dy solution of 2 − x 3 − x 3 y = 0 with the initial conditions y(0) = 1, dx dx and y′(0) = 1/2.

ANMF.CH08_4PP.indd 657

3/1/2023 2:48:12 PM

658 • Applied Numerical Methods Using MATLAB 8.65. Use Euler’s method to solve y′ = −1.2y + 7e−0.3x from x = 0 to x = 2.5 with the initial condition y = 3 at x = 0. Take h = 0.01. Compare the results with the exact (analytical) solution: y=

70 −0.3 x 43 −1.2 x e − e . 9 9

8.66. Use Euler’s method to solve y′′ − 2y′ + 2y − e2t sin t = 0 with 0 ≤ t ≤ 1, y(0) = −0.4 and y′(0) = 0.6. Take the time step of h = 0.1 and determine y(0.2). 8.67. Solve y′′ − x(y′)2 − y2 for x = 0.2 correct to four decimal places with the initial conditions x = 0, y = 1, and y′ = 0. Use the Runge-Kutta method. 8.68. Use the midpoint method to solve y′′ − 2y′ + 2y − e2t sin t with 0 ≤ t ≤ 1, y(0) = −0.4 and y′(0) = −0.6. Take the time step of h = 0.1 and determine y(0.2). 8.69. Use the second-order Runge-Kutta method to solve the equation y′ = −1.2y + 7e−0.3x from x = 0 to 2.0 with the initial condition y = 3 at x = 0. Take h = 0.5. 8.70. Solve the equation y′′ + y + 1 = 0 with the boundary conditions y(0) = 0 and y′(0) = 0. 8.71. Solve the equation y′′ + xy′ + 1 = 0, y(0) = y(1) = 0. 8.72. Solve the initial value problem y′′ + 9y = 0 from t = 0 to 4 with the ­initial conditions: y(0) = 1 and y′(0) = 0. Apply a) Euler’s method b) fourth-order Runge-Kutta method

In both cases use a step size of 0.1. Compare your results in both cases with the exact solution: y = cos 3t.

8.73. Solve equation y′′ − y − x = 0 with y(0) = 0 and y′(0) = 1 using the finite difference method for x = 0.01, 0.02, …., 0.10. 8.74. The deflection of a beam is governed by the equation d4y + 8Iy − φ(x) = 0. Use the data [x, ϕ(x)] for x = 1/3 to 1. The boundary dx 4 conditions are y(0) = y′(0) = y′′(1) = y′′(1) = 0. Determine the deflection at the pivot points of the beam using three subintervals.

ANMF.CH08_4PP.indd 658

3/1/2023 2:48:14 PM

Numerical Solution of Ordinary Differential Equations • 659

8.75. Solve the nonlinear value problem y′′ + 3yy′ = 0 with the conditions y(0) = 0 and y(2) = 1 by using the shooting method. 8.76. Use MATLAB built-in function bvp4c to solve the equation y′′ + 2xy′ + 5y − cos 3x = 0, for 0 ≤ x ≤ π with the boundary conditions: y(0) = 1.5 and y(π) = 0. 8.77. Solve the boundary value problem y′′(x) with the initial conditions y(0) = 0 and y(1) = 1.175 by the shooting method. Take m0 = 0 and y(1) = 0.9. 8.78. Solve the boundary value problem y′′ − y + x = 0, y(0) = y(1) = 0. Apply the shooting method.

ANMF.CH08_4PP.indd 659

3/1/2023 2:48:14 PM

ANMF.CH08_4PP.indd 660

3/1/2023 2:48:14 PM

CHAPTER

9

Direct Numerical Integration Methods 9.1 INTRODUCTION The behavior of many dynamic systems undergoing time-dependent changes (transients) can be described by ordinary differential equations. When the solution to the differential equation(s) of motion of a dynamic system cannot be obtained in closed form, a numerical procedure is warranted. Many numerical integration methods are available for the approximate solution of such equation(s) of motion. All the numerical integration methods have two basic characteristics. First, they do not satisfy the differential equation(s) at all time t, but only at discrete time intervals, say Δt apart. Secondly, within each ⋅ time interval Δt, a specific type of variation of the displacement X, ­velocity X, ⋅⋅ and acceleration X is assumed. Thus. several numerical integration schemes ⋅ ⋅⋅ are available depending on the type of variation assumed for X, X, and X within each time interval Δt. In this chapter, we present several widely used step-by-step numerical integration schemes for the solutions of both single and multi-degree of freedom systems. A brief description of these methods is presented for linear dynamic response analysis and their application is illustrated with several examples.

9.2 SINGLE DEGREE OF FREEDOM SYSTEM The general equation of a viscously damped single degree of freedom dynamical system, which is linear, can be expressed in the following general form:   CX  KX  F(t) (9.1) MX

ANMF.CH09_4PP.indd 661

3/1/2023 2:48:57 PM

662 • Applied Numerical Methods Using MATLAB where M, C, and K are the mass, damping, and stiffness of the system; F (t) is ⋅ ⋅⋅ the applied force; and X, X and X are the displacement, velocity, and acceleration of the system. 9.2.1 Finite Difference Method If the equilibrium relation (9.1) is regarded as an ordinary differential equation with constant coefficients, it follows that any convenient finite difference expressions to approximate the velocities and accelerations in terms of displacements can be used. The central idea in the finite difference method is to use approximations to derivatives. Hence, the general differential equation such as (9.1) and the associated boundary conditions, if any, are replaced by the corresponding finite difference equations. The continuous variable t is replaced by the discrete variable ti and the differential equation is solved progressively in time increments h = Δt starting from known initial conditions. The solution obtained is approximate but by suitably selecting the time increment the accuracy of the solution can be improved. In this method, we replace the solution domain with a finite number of points, known as mesh or grid points, and obtain the solution at these points. The mesh or grid points are generally equally spaced along the independent coordinate as shown in Figure 9.1. The central difference method is based on the Taylor’s series expansion of Xi+1 and Xi−1 about the grid point i.

FIGURE 9.1

ANMF.CH09_4PP.indd 662

3/1/2023 2:48:57 PM

Direct Numerical Integration Methods • 663



h 2  h 3  X i 1  X i  hX i  X X i  ...  i  2 6

(9.2)



h 2  h 3 . X i 1  X i  hX i  X X i  ...  i  2 6

(9.3)

where X = X(t = t i ) and the interval h  t i 1  t i  t. By taking the first two i terms only and subtracting Equation (9.3) from Equation (9.2), we obtain

1 X i  (X i 1  X i 1 ) (9.4) 2h

Adding Equations (9.3) and (9.2), we get

  1 (X  2X  X ) (9.5) X i i 1 i i 1 h2

Although there exist a number of finite difference schemes, here we consider only two methods selected for their simplicity. They are the central difference method and the Runge-Kutta method. 9.2.2 Central Difference Method Let the duration over which the numerical solution of Equation (9.1) is required be divided into n equal parts of interval h = Δt each. The initial con = 0)= X . ditions are assumed to be given by X(t = 0) = Xo and X(t o The accuracy of the solution always depends on the size of the time step. The critical time step is given by t cri  n / , where τn is the natural period of the system. If Δt is selected to be larger than Δtcri, the method becomes unstable, that is the truncation of higher-order terms or rounding-off in the computer causes errors to grow and makes the dynamic response calculations meaningless. Numerical methods, which require the use of time, step Δt smaller than the critical time step Δtcri are said to be conditionally stable. A safe rule to use is to choose h  n / 10 . Substituting Equations (9.5) and ⋅⋅ ⋅ (9.4) for X i and Xi respectively in Equation (9.1) at mesh or grid point i give  X  2 X i  X i 1   M  i 1  t 2 

ANMF.CH09_4PP.indd 663

  X i  1  X i 1    C   KX i  Fi (9.6)  2t  

3/1/2023 2:49:13 PM

664 • Applied Numerical Methods Using MATLAB = where X i X(t = F(t i ) . Solving Equation (9.6) for Xi+1 gives i ) and Fi

  X i 1

      2M  1 M   C    X i 1  Fi (9.7)  K  X i        2 2 M C   (t)  2t (t)       2  (t) 2t 

which is known as the recurrence formula. Thus, the displacement of the mass Xi+1 can be calculated using Equation (9.7) if we know the previous displacements, Xi, Xi−1 and the present external force Fi. Repeated application of Equation (9.7) gives us the response time history of the behavior of the system. Since the solution of Xi+1 given in Equation (9.7) is based on the previous displacement Xi given in Equation (9.6), the integration procedure is known as an explicit integration method. Note that in order to compute X1, both Xo and X−1 are required but the initial ⋅ conditions provide only the values of Xo and Xo. Therefore, the central difference method is not self-starting. However, the value of X−1 can be obtained ⋅⋅ from Equations (9.4) and (9.5) as follows. First calculate the value of X o by ⋅ substituting the given values of Xo and Xo in Equation (9.1) as follows,   X o

1 F(t  0)  CX o  KX o  (9.8) M

The value of X−1 is then obtained by the application of Equations (9.4) and (9.5) at i = 0. (t)  X o (9.9) X 1  X o  t X o  2 2

9.2.3 The Runge-Kutta Method In the Runge-Kutta method, an approximation to Xt+Δt is obtained from Xt in such a way that the power series expansion of the approximation coincides, up to terms of a certain order (Δt)N in the time interval Δt, with the actual Taylor series expansion of (t + Δt) in powers of Δt. The method is based on the assumption that the higher derivatives exist at points required. The Runge-Kutta method is self-starting and has the advantage that no initial values are needed beyond the prescribed values. A brief discussion of

ANMF.CH09_4PP.indd 664

3/1/2023 2:49:21 PM

Direct Numerical Integration Methods • 665

its basis is represented here. In the Runge-Kutta method, the second-order differential equation is first reduced to two first-order equations. Consider the differential equation for the single degree of freedom system given in Equation (9.1). Equation (9.1) can be rewritten as

  1 F(t)  CX  KX   f (X,X,t)  (9.10) X  M

By letting = X 1 X= and X 2 X , Equation (9.10) can be reduced to the following two first-order equations;

X 1 = X 2



X 2 = f(X 1, X 2 , t) (9.11)

By defining  X 1 (t)  X(t)     X 2 (t) X2   F(t)   and  f(X 1 , X 2 , t) the following recurrence formula is obtained to find the values of X(t) at mesh or grids points ti according to the fourth-order Runge-Kutta method. We omit the details of the derivation of the method. 1 X i 1  X i  K1  2K2  2K3  K 4  6 where K1 = hF(X i , t i ) 1 1 K2  hF(X i  K1 , t i  h) 2 2 1 1 K3  hF(X i  K2 , t i  h) (9.12) 2 2 1 K 4  hF(X i  K3 , t i 1 ) and 2

Although the Runge-Kutta method does not require the computation of derivatives beyond the first, its higher accuracy is obtained by four evaluations of the first derivatives to obtain agreement with the Taylor series solution

ANMF.CH09_4PP.indd 665

3/1/2023 2:49:55 PM

666 • Applied Numerical Methods Using MATLAB through terms of order h4. Since the fourth-order Runge-Kutta method is an explicit method, the maximum time step is usually governed by stability considerations. The change in time step can be easily implemented between iterations and hence the method can be considered as an inherently stable method. The main drawback of the method is that each forward step requires several computations of the functions thus increasing the computational cost. The Runge-Kutta method is applicable and extendable to a system of differential equations.

9.3 MULTI-DEGREE OF FREEDOM SYSTEMS The general form of the equations of motion for a multi-degree of freedom system is written as

MX  CX   KX  F(t) (9.13)

where [M], [C], and [K] are the mass, damping and stiffness matrices for the  ,X  and X refer to the acceleration, velocity, and displacesystem and X ment vectors, respectively. F(t) is the force vector. Several numerical direct integrating schemes are available to determine the approximate solution of a system of equations of motion. For a linear dynamic system, matrices [M], [C] and [K] are independent of time and therefore remain unchanged during the integration procedure. These matrices vary with time for a nonlinear dynamic system and must be modified during the integration of equations of motion. For the solution of equations of motion for a linear dynamic system, either the normal mode superposition method of dynamic analysis or direct numerical integration methods can be used. However, for the solution of nonlinear equations of motion, direct numerical integration methods are generally recommended. In a direct integration method, the system of equations of motion is integrated successively by using a step-by-step numerical procedure. No transformation of the equations of motion is needed prior to integration and using difference formulas that involve one or more increments of time usually approximates time derivatives. Basically, there are two principal approaches used in the direct integration method: explicit and implicit schemes. In an explicit scheme, the response quantities are expressed in terms of previously determined values of displacement, velocity, and acceleration. In an implicit scheme, the difference equations are combined with the equations of motion, and the displacements are calculated directly by solving the equations.

ANMF.CH09_4PP.indd 666

3/1/2023 2:50:03 PM

Direct Numerical Integration Methods • 667

In this section, only selected numerical integration schemes widely used for linear and nonlinear dynamic analyses are considered. Three explicit and four implicit direct integration schemes are examined. A brief description of these schemes is presented and their application is illustrated. The explicit schemes presented are the central difference method, two-cycle iteration with trapezoidal rule, and fourth-order Runge-Kutta. The implicit schemes include the Houbolt, Wilson-theta, Newark-beta, and Park Stiffly stable methods. The accuracy, stability, and efficiency of these schemes are examined by comparing the results for sample problems.

9.4 EXPLICIT SCHEMES As mentioned earlier, in an explicit formulation, the response quantities are expressed in terms of previously determined values of displacement, velocity, and acceleration. 9.4.1 Central Difference Method The procedure indicated for the case of a single degree of freedom system can be directly extended to this case. Consider a displacement time history curve as shown in Figure 9.2. At the middle of the time interval Δt, the velocity is given by

X

and

X

i

1 2

1 i 2



X i 1  X i (9.14) t



X i  X i 1 (9.15) t

FIGURE 9.2

ANMF.CH09_4PP.indd 667

3/1/2023 2:50:07 PM

668 • Applied Numerical Methods Using MATLAB The acceleration is   X i

Substituting X

i

X

i

and X

1 2

i

Equation (10.16), we get

 X

1 2

t

i

1 2

(9.16)

from the Equations (10.14) and (10.15) into

1 2

  1 (X  2X  X ) (9.17) X i i 1 i i 1 t 2



The difference formulas in the central difference method for velocity and acceleration are written in terms of displacement as

X   21t X   X  (9.18)



X   tt X   2X   X  (9.19)

t  t

t

t

t  t

2

t  t

t  t

t

  Substituting X t  and X t  from Equations (9.18) and (9.19), respectively into Equation (9.13), we get

 

M X t t   Ft (9.20)  



 

where M, the effective mass matrix, and Ft , the effective force vector, is given by M  1 M 1  C (9.21)   t 2 2t

F   F    K  2t t

t

2

1 1 M  C X t  t  (9.22) 2    t 2 t  

M X t    

At time t + Δt, the displacements X t  t  can be computed by solving Equation (9.20), and the velocities and accelerations at time t are determined by substituting these values of X t  t  into Equations (9.18) and (9.19). Note

ANMF.CH09_4PP.indd 668

3/1/2023 2:50:32 PM

Direct Numerical Integration Methods • 669

that the calculation of X t  t  involves X t  and X t  t  . Hence, in order to obtain the solution at time Δt, a special starting procedure is needed. Table 9.1 summarizes the time integration schedule as suitable for integration in the computer. TABLE 9.1  Algorithms based on the central-difference method. (a) Initial Computations: 1. Form stiffness [K], and damping [C] matrices. . mass [M], .. 2. Initialize {X0}, {X0}, and {X 0}. 3. Select time step Δt and calculate integration constants ai: a0 

1 1 1 ;a1  ;a 2  2a 0 ;a 3  . t 2 2t a2

  . 4. Calculate X  t   X 0   t X 0   a 3 X 0 5. Form effective mass matrix M  a 0 M  a1 C . 6. Triangularize M : M  L  D L 

T

(b) For each time step: 1. Calculate effective force vector at time t:

F   F   K  a t

t

2

M X t    a 0 M  a1 C X tt 

2. Solve for displacements at time t + Δt: M X t t   Ft  . .. 3. Calculate {X}, and {X } at time t:

X   a   X   X  X   a X   2 X   X  t

1

t

0

t  t

t  t

t  t

t

t  t

The local truncation error of this method is of the order of Δt2. An important consideration in the use of the central difference method is that the integration method requires that the time step Δt smaller than a critical value, Δtcr which is limited by the highest frequency of the discrete system ωmax, where t  t cr 

2 (9.23) max

If the time step is longer than Δtcr, the integration is unstable, meaning that any errors resulting from the numerical integration of round off in the computer grow and make the dynamic response calculations questionable.

ANMF.CH09_4PP.indd 669

3/1/2023 2:50:52 PM

670 • Applied Numerical Methods Using MATLAB 9.4.2 Two-Cycle Iteration with Trapezoidal Rule The equations of motion at any time t are expressed in the incremental form as MX t   Ft   KX t   CX t  (9.24) The increments in the velocities and displacements are estimated by the use of the following relationships in the first iteration cycle: For the first time step:   X t    t X t  t  (9.25) For other time steps:



X   2t X   X  (9.26) X   X   X  (9.27)



X t  



t  t

t

t t

t

t  t

t

1 t X t  t   X t  (9.28) 2 

 By substituting the relations X t  and X t  from Equations (9.26) and (9.28) into Equation (9.24) we obtain the increments in the accelerations as  

X   M F   KX   CX  (9.29) 1

t

t

t

t

These are then employed to obtain the estimate of the acceleration at time t as    X t   X t t   X t  (9.30) In the second iteration cycle, the increments in the velocities and the accelerations are refined as



(9.31) X   12 t X   X   X   X   X  (9.32)



X t  



ANMF.CH09_4PP.indd 670

t  t

t

t

t t



t

t



1 t X t  t   X t  (9.33) 2

3/1/2023 2:51:24 PM

Direct Numerical Integration Methods • 671

Finally, the relations for X t  and X t  in Equations (9.31) and (9.33) are substituted into Equation (9.29) to compute the new increments in the accelerations. These are then used in Equation (9.30) to calculate the accelerations at time t. The computational algorithm based on this method is summarized in Table 9.2. TABLE 9.2  Algorithm based on two-cycle iteration with trapezoidal rule. 1. Form stiffness K  , mass M and damping  C matrices   2. Initialize X 0  , X 0  and X 0 3. Select time step Δt and calculate for the first time step in the first iteration cycle:

X   t X  t  t

t

4. For other time steps:

X   2t X   X  X   X   X  t X   X   X  2 t  t

t

t  t

t

t  t

t

t  t

t

t

  and X  at time t: 5. Compute X t t

X   M F   KX   CX  X   X   X  1

t t

t

t

t  t

t

t

6. Second iteration cycle:

X   2t X   X  X   X   X  t X   X   X  2 t  t

t

t

t  t

t

t

t

t  t

t

  in step 5 using X  , X  , and X  from step 6. 7. Compute X t t t t   from step 5, using X   and X   from step 7. 8. Finally, compute X t t t

9.4.3 Fourth-Order Runge-Kutta Method In the fourth-order Runge-Kutta method, the system of second-order differential Equations (9.13) are converted into state variable form. That is, both the displacements and velocities are treated as unknowns {y} defined by  X    (9.34) X 

 y  

ANMF.CH09_4PP.indd 671

3/1/2023 2:51:59 PM

672 • Applied Numerical Methods Using MATLAB Using Equation (9.34), Equation (9.13) can be rewritten as

X   M KX  M CX   m 1



1

1

F(t) 

(9.35)

Using the identity

y   y  (9.36)     we obtain from Equations (9.35) and (9.36) X    0 I   

  1 1  X    M K   M  C 

 y   

 X    0      (9.37) 1 X   M F(t) 

or

y   Ey  F * (t) (9.38)

That is

y   f(t,y) (9.39)

In the Runge-Kutta method, an approximation to {yy+Δt} is obtained from {yt} in such a way that the power series expansion of the approximation coincides, upto the terms of a certain order (Δt)N in the time interval Δt, with the actual Taylor series expansion of (t + Δt) in powers of Δt. This method has the advantage that no initial values are required beyond the prescribed ones. The general fourth-order algorithms are based on formulas of the form

y t  t   y t   t(aK1  bK2  cK3  dK4 ) (9.40)

where a, b, c, and d are constants and K1, K2, K3 and K4 are the approximate derivative values computed in the interval tK < t < tK+Δt. Several fourth-order algorithms have been proposed. The following is due to Runge-Kutta and we omit presenting the details of its derivation.

y t  t   y t  

t K1  2K2  2K3  K4   6

K1  f(t,y t ) t t K2  f(t  ,y t  K1 ) 2 2 in which  t t K3  f(t  ,y t  K2 ) 2 2 K 4  f(t  tt,y t  K3 t)

ANMF.CH09_4PP.indd 672

(9.41)

(9.42)

3/1/2023 2:52:14 PM

Direct Numerical Integration Methods • 673

The Runge-Kutta algorithm does not require the calculation of higher ­derivatives. This method is completely self-starting and the step size can be changed easily between iterations and hence the method can be considered inherently stable. The truncation error et for the fourth-order Runge-Kutta scheme is of the form et = c(Δt)5(9.43) where c is constant, which depends on f (t, y) and its higher-order partial derivatives. The Runge-Kutta method generates an artificial damping that unduly suppresses the amplitude of the response of a dynamic system to some extent.

9.5 IMPLICIT SCHEMES In an implicit scheme, the difference equations are combined with the equations of motion, and the displacements are calculated directly by solving the equations. 9.5.1 The Houbolt Method The Houbolt method is based on third-order interpolation of displace⋅ ⋅⋅ ments X t, and the multistep implicit formulas for X t and X t are obtained in terms of X t by using backward differences. The difference formulas are summarized in the following with reference to Figure 9.3. By considering a cubic curve that passes through the four successive ordinates, the following equations can be obtained:

FIGURE 9.3

ANMF.CH09_4PP.indd 673

3/1/2023 2:52:14 PM

674 • Applied Numerical Methods Using MATLAB

t 2  t 3 . X t  t  X t  t (9.44) 2 6



X t  X t  t  tX t  t 



(2t)  (2t)  X t  t  X t  t  (2t) X t  t  X t  t  X t  t (9.45) 2 6



(3t) X t  2t  X t  t  (3t) X t  t  2

2

2

3

(3t)  X t  t  6

3

 X .t t (9.46)

  Solving Equations (9.44) to (9.46) for X t  t and X t  t we get

1  X (2X t  t  5X t  4X t  t  X t  2t )  t  t  t 2



1 X t  t  (11X t  t  18X t  9X t  t  2X t  2t ) (9.48) 6t

(9.47)

The difference formulas in the Houbolt algorithm are therefore given by

X   1t



X   61t 11X   18X   9X   2X

t  t

2

2 X t  t   5X t   4 X t  t   X t  2t  (9.49)

t  t

t  t

t  t

t

t  2t

 (9.50)

  By substituting the expressions for X t  t  and X t  t  from Equations (9.49) and Equation (9.50), respectively, into (9.13), we get





     M X t  t   F t  t  where

M  



2 11 M  C  K (9.52) 6t t 2

5 3  M   C  X t  2  t  t  1 1 3  4   2 M  C  X t  t    2 M  C X t 2t  (9.53) 2t 3t  t   t  

F   F

ANMF.CH09_4PP.indd 674



is the effective mass matrix and Ft  t is the effective force vector.

M       t  t

(9.51)

t  t

  

3/1/2023 2:52:45 PM

Direct Numerical Integration Methods • 675

Note that the equilibrium equation at time t + Δt, Equation (9.51) is used in finding the solution for X t  t . For this reason, this method is called an implicit integration method. It can be seen that the velocities and accelerations at time t + Δt are obtained by substituting for X t  t  in Equations (9.50) and (9.49) respectively. Also, that a knowledge of X t , X t  t , and X t  2t is needed to find solution for X t  t  . Since there is no direct method available to find X t  t  and X t 2t , initially we use the central difference method to find solution at time Δt and 2Δt. This makes the method non-self-starting. The method also requires large computer storage to store displacements for the previous time steps. The step-by-step procedure to be used in the Houbolt method is summarized in Table 9.3. A basic difference between the Houbolt method in Table 9.3 and the central difference method in Table 9.2 is the appearance of the stiffness matrix K as a factor to the required displacements Xt+Δt. The term KXt+Δt appears because the equilibrium is considered at time t + Δt and not at time t as in the central-difference method. There is no critical time-step limit, and Δt can in general be selected much larger than that given for the central difference method. TABLE 9.3  Algorithm based on the Houbolt method. (a) Initial Computations: 1. Form stiffness [K], and damping [C] matrices . mass [M], .. 2. Initialize {X0}, {X0}, and {X 0}. 3. Select time step Δt and calculate integration constants: a 2 11 5 3 ; a1  ; a 2  2 ; a 3  ; a 4  2a 0 ; a 5  3 ; t 2 t t 6t 2 a0 a3 a6  ; a7  . 2 9 a0 

4. Use special starting procedure, such as central-difference method to calculate {XΔt} and {X2Δt}. 5. Calculate effective stiffness matrix: K   K   a 0 M  a1  C 6. Triangularize K  : K   L  D L 

T

(b) For each time step: 1. Calculate effective force vector at time t + Δt:

F   F   M  a X   a X   a X   C   a X   a X   a X  t  t

t  t

2

3

ANMF.CH09_4PP.indd 675

t

t

5

4

t  t

t  t 7

6

t 2t



t 2t

3/1/2023 2:53:01 PM

676 • Applied Numerical Methods Using MATLAB

2. Solve for displacements at time t + Δt K  . .. 3. Calculate {X0} and {X 0} at time t + Δt:

X tt   Ftt 

X   a X   a X   a X   a X  X   a X   a X   a X   a X  t  t

1

t  t

3

t

5

t  t

7

t 2t

t  t

0

t  t

2

t

4

t  t

6

t 2t

9.5.2 Wilson Theta Method The Wilson theta method assumes that the acceleration of the system varies linearly between two instants of time. Referring to Figure 9.4, the acceleration is assumed to be linear from time t between ti = iΔt to time t i    t i  t, where θ ≥ 1.0. Because of this reason, the method is known as the Wilson theta method. If τ is the increase in time t between t and t + θΔt (0 ≤ τ ≤ θΔt), then for time interval t to t + θt, it can be assumed that   X   X   t t

   ) (9.54) (X t  t  X t t

FIGURE 9.4

ANMF.CH09_4PP.indd 676

3/1/2023 2:53:09 PM

Direct Numerical Integration Methods • 677

Successive integration for Equation (9.54) gives the following expressions for X t   and X t   : 2     (X   X t    X t  X t t  t  X t ) (9.55) 2t 1  2 3   ) (9.56) X t    X t  X t   X   (X t  t  X t t 2 6t



Substituting τ = θΔt into the . above Equations (9.55) and (9.56), we obtain the following expressions for X and X at time t + θΔt:

t   X t  t  X t  (X t  X t  t ) (9.57) 2

2 t 2   ) (9.58) X t  t  X t  t X t  (X t  t  2X t 6 . ⋅⋅ Solving Equations (9.57) and (9.58) for X t+θΔt and Xt+θΔt in terms of Xt+θΔt, we get

6 6   (X t  t  X t )  (X t )  2X t 2  t t (9.59) 3 t   (X t  t  X t )  2 X t  Xt t 2

 X t  t 

X t  t

2

The difference formulas in the Wilson theta algorithm are then given by

X

t  t

   6t X



X

t  t

  3t X

2

2

t  t

t  t

  X t  

6  X t   2X t  (9.60) t

  X t   2 X t  

t  (9.61) X t  2

We employ Equation (9.13) at time t + Δt to obtain a solution for displacement, velocity, and acceleration at time t + Δt. Since accelerations vary ­linearly, a linear projected force vector is used such that

ANMF.CH09_4PP.indd 677



MX t  t   CX t  t   KX t  t   Ft  t  (9.62)

where

Ft  t   Ft   t Ft  t   Ft  .

3/1/2023 2:53:28 PM

678 • Applied Numerical Methods Using MATLAB . ⋅⋅ By substituting the expressions for {X t+θΔt} and {Xt+θΔt} from Equations (9.60) and (9.61), respectively, into Equation (9.62), we get M X t  t   Ft  t  (9.63) where the effective mass matrix M and the effective force vector Ft  t  are given by 6 3     M  2 2 M  C  K (9.64)    t t 6 3 M  C X t  2  t   t   t  6  (9.65) M  2 C X t    2 M  C  X t  2  t   

F   F t  t

t  t

  

2

The solution of Equation (9.63) gives {Xt+θΔt} which is then substituted into the following relationships to obtain the displacements, velocities, and accelerations at time t + Δt.

X    6t X



X   X   2t X   X  (9.67)



X t  t   X t   t X t  

t  t

t  t

2

2

t

t  t

  X t   t  t

6  3  (9.66) X t   (1  )X  t  t  2

t

t 2 6

X   2X  (9.68) t  t

t

When θ = 1.0, the method reduces to the linear acceleration scheme. The  method is unconditionally stable for linear dynamic systems when θ  ≥  1.37  and a value of θ =1.4 is often used for nonlinear dynamic systems. It may also be noted that no special starting procedures are needed, since ⋅ ⋅⋅ X, X and X are expressed at time t + Δt in terms of the same quantities at time t only. The complete algorithm used in the Wilson theta method is given in Table 9.4.

ANMF.CH09_4PP.indd 678

3/1/2023 2:53:48 PM

Direct Numerical Integration Methods • 679

TABLE 9.4  Algorithm based on the Wilson theta method. (a) Initial Computations: 1. Form stiffness [K], and damping [C] matrices . mass [M], .. 2. Initialize {X0}, {X0}, and {X 0}. 3. Select time step Δt and calculate integration constants, θ = 1.4(say): a0 

a t a 6 3 ; a1  ; a 2  2a1 ; a 3  ; a4  0 ; a5  2 ;   t (t)2 2

t t 2 3 a6  1  ; a7  : a8  .  2 6 4. Form effective stiffness matrix: K   K   a 0 M  a1  C 5. Triangularize K  : K   L DL 

T

(b) For each time step: 1. Calculate effective force vector at time t + Δt:

F   F    F t  t

t  t

t



  Ft )  M (a 0 X t   a 2 X t   2 X t 

    C a1 X 1   2 X t   a 3 X t



2. Solve for displacements at time t + θΔt



K  X t t   Ft t . .. 3. Calculate {X}, {X} and {X } at time t + Δt:



X   a (X   X )  a X   a X  X   X   a (X   X  ) X   X   t X   a (X   2 X  ) t  t

t  t

4

t  t

t

t  t

t

t

5

t  t

7

t

t

6

t

t

8

t  t

t

9.5.3 The Newmark Beta Method The Newmark beta integration method is also based on the assumption that the acceleration varies linearly between two instants of time. Two parameters α and β are used in this method, which can be changed to suit the requirements of a particular problem. The expressions for velocity and displacements are given by

ANMF.CH09_4PP.indd 679



  X   t X t  t  X t  (1  )X t t  t  (9.69)



 1   X   t 2 (9.70) X t  t  X t  X t t  (  )X t t  t   2 

3/1/2023 2:54:03 PM

680 • Applied Numerical Methods Using MATLAB The parameters α and β indicate how much the acceleration enters into the velocity and displacement equations at the end of the interval Δt. Therefore, α and β are chosen to obtain the desired integration accuracy and stability. When α = 1/6 and β = 1/2, Equations (9.69) and (9.70) correspond to the linear acceleration method (which can also be obtained using θ = 1 in Wilson ⋅⋅ method). When α = 1/2 and β = 0, the acceleration is constant and equal to X t during each time interval Δt. If α =1/2 and β = 1/8, the acceleration is constant ⋅⋅ ⋅⋅ from the beginning as X t and then changes to X t+Δt in the middle of the time interval Δt. When α =1/2 and β = 1/4, this corresponds to the assumption that ⋅⋅ ⋅⋅ the acceleration remains constant at an average value of (X t + X t+Δt)/2. The finite difference formulas for the Newmark beta scheme are    1 (X   X )  1 X   ( 1  1)X    X t  t t  t t t t 2 t 2 t

(9.71)

     (9.72) (X t  t   X t )  (  1)X t   t(  1)X X t  t   t 2 t  Equation (9.13) can be employed to obtain a solution for displacements, ­velocity, and accelerations at time t + Δt. Therefore, by substituting the expres. ⋅⋅ sions for {X t+Δt} and {Xt+Δt} from Equations (9.71) and (9.72), respectively, into Equation (9.13), we get





M X t  t   F t  t (9.73)



where the effective mass matrix M and the effective force vector F t  t are given by

1   M    t 2 M  t  C  K 



(9.74)

 1     1) M  t (  1)  C X t 2  2   1    (9.75) M  (  1) C X t    t 

F   F t  t



t  t

  (

 1    M   C   X t  2  t  t 

ANMF.CH09_4PP.indd 680

3/1/2023 2:54:11 PM

Direct Numerical Integration Methods • 681

Solution of Equation (9.73) gives {Xt+Δt}, which is then substituted into (9.71) and (9.72) in order to obtain the accelerations and velocities at time t + Δt. One of the features of the Newmark beta method is that for linear systems the amplitude is conserved and the response is unconditionally stable, provided 1 1 1 2 1 1 that   and   (  ) . For values of   and   the largest 2 4 2 2 4 truncation errors occur in the frequency of the response as opposed to other 1 β values. It is also important to note that unless   , there is a spurious 2 1 (  ) damping introduced, proportional to . If β = 0, a negative damping 2 results; this involves a self-excited vibration arising solely from the numerical 1 procedure. In a likewise manner, if β is greater than , a positive damping is 2 introduced which reduces the magnitude of the response even without real damping in the problem. For a multidegree of freedom system in which a number of modes constitute the total response, the peak amplitude may not be correct. The complete algorithm using the Newmark beta integration method is given in Table 9.5. TABLE 9.5  Algorithm based on the Newmark beta method. (a) Initial Computations: 1. Form stiffness [K], and damping [C] matrices . mass [M], .. 2. Initialize {X0}, {X0}, and {X 0}. 3. Select time step Δt, parameters α and β, and calculate integration constants,   0.5;   0.25 (0.5  )2 a0 

  1 1 1 ; a1  ; a2  ; a3   1; a 4   1; (t)2 t t  2

a5 

 t     2  ; a 6  t 1    ; a 7  t 2  

4. Form effective stiffness matrix: K   K   a 0 M  a1  C 5. Triangularize K  : K   L DL 

T

(b) For each time step: 1. Calculate effective force vector at time t + Δt:

F   F t  t

ANMF.CH09_4PP.indd 681

  M (a 0 X t   a 2 X t   a 3 X t  )   )   C (a1 X t   a 4 X t   a 5 X t t  t

3/1/2023 2:54:27 PM

682 • Applied Numerical Methods Using MATLAB

2. Solve for displacements at time t + Δt



K  X t t   F t t . .. 3. Calculate {X} and {X } at time t + Δt:



X   a (X   X )  a X   a X  X   a X   X   a X   a X  t  t

0

t  t

t

2

t

3

t

t  t

1

t  tt

t

4

t

5

t

9.5.4 Park Stiffly Stable Method The Park Stiffly stable method is an accurate method for low frequency ranges and stable for all higher-frequency components. Using a linear combination of the following two difference formulas derives the velocity in the Park Stiffly Stable method:

1 X t  t  11X t  t  18X t  9X t t  2X t 2t  (9.76) 6t



1 X t  t  (2X t  t  4X t  X t  t ) (9.77) 2 t

The linear combination of Equations (9.76) and (9.77) gives



1 X t  t  (3X t  t  4X t  X t  t ) 4t (9.78) 1  (11X t  t  18X t  9X t  t  2X t  2t ) 12t

or

1 X t  t  (10X t  t  15X t  6X t  t  X t  2t ) (9.79) 6t

Similarly, for the acceleration we obtain

1  X (10X t  t  15X t  6X t  t  X t-2t ) (9.80) t  t  6t

The difference formulas in the Park Stiffly method are given by

ANMF.CH09_4PP.indd 682

X   61t 10X   15X   6X   X   t  t

t  t

t

t  t

t  2t

(9.81)

3/1/2023 2:54:45 PM

Direct Numerical Integration Methods • 683

X   61t 10X   15X   6X   X



t  t

t  t

t

t  t

t  2t

 (9.82)

We consider Equation (9.13) to obtain solution for the displacements, veloci⋅⋅ ties, and . accelerations at time t + Δt. By substituting the expressions for {X t+Δt} and {Xt+Δt} from Equations (9.81) and (9.82), respectively, into Equations (9.13), we get M X t  t   Ft  t  (9.83)



where the effective mass matrix M and the effective force vector F t  t   are given by

100 10 (9.84) M    36t 2 M  6t  C  K 

F   615t MX   1t MX   61t MX t  t





t

t  t

t  2t



150 15 10 1 M  C)X t   ( 2 M  C)X t  t  (9.85) 2  6t 6t 36t t 1 1 ( M  C)X t 2t  36t 2 6t (

The solution of Equation (9.83) gives {Xt+Δt}, which is then substituted in ⋅⋅ Equation (9.82) to obtain velocities. The values of {X t+Δt} are then obtained by the use of Equation (9.81). Note that in the Park Stiffly stable method, the calculation of {Xt+Δt} requires the displacements and velocities at t, t − Δt and t − 2Δt. Therefore, in order to obtain the solution at time Δt and 2Δt, a special starting procedure is needed, which makes the method non-self-starting. The complete algorithm based on the Park Stiffly stable method used in the integration is given in Table 9.6. The method requires a large computer memory in order to store the displacements and velocities for the two previous time steps.

ANMF.CH09_4PP.indd 683

3/1/2023 2:54:53 PM

684 • Applied Numerical Methods Using MATLAB

TABLE 9.6  Algorithm based on the Park Stiffly stable method. (a) Initial Computations: 1. Form stiffness [K], and damping [C] matrices . mass [M], .. 2. Initialize {X0}, {X0}, and {X 0}. 3. Select time step Δt and calculate integration constants: 15 1 10 1 ; a1  ; a2  ; a3  ; 6t 6t t 6t 4. Form effective stiffness matrix: a0 

K   a 20 M  a 0  C  K  5. Triangularize K  : K   L DL 

T

(b) For each time step: 1. Calculate effective force vector at time t + Δt:

F   (  a X   a X   a X   a a X   a a t  t

1

t

2

t  t

3

t 2t

0 1

t

0 2

X tt 

 a 23 X t 2t  ) M  (a1 X t   a 2 X t t   a 3 X t 2t  )  C 2. Solve for displacements at time t + Δt



M X t t   F t t . .. 3. Calculate {X} and {X } at time t + Δt:



X   a X   a X   a X   a X  X   a X   a X   a X   a X  t  t

0

t  t

1

t

2

t  t

3

t 2t

t  t

0

t  t

1

t

2

t  t

3

t 2t

9.6 EXAMPLE PROBLEMS AND SOLUTIONS USING MATLAB EXAMPLE 9.1 Find the response of a viscously damped single degree of freedom system subjected to a force πt F(t) = F0 (1 − sin ) 2t 0 with the following data: F0 = 2 N, t0 = π seconds, m = 2kg, c = 0.3Ns/m and k = 1 N/m. The values of the displacement and velocity of the mass at t = 0 are zero. Use the central difference method. Choose Dt = 1, 0.1 and 0.5 seconds and compare the results. Solution: This is a single degree of freedom system problem with all initial conditions zero. The following MATLAB program is executed to obtain the results.

ANMF.CH09_4PP.indd 684

3/1/2023 2:55:10 PM

Direct Numerical Integration Methods • 685

% INITIAL VALUES m = 2;k = 1;c = 0.3;dt = 0.1; x0 = 0;x0d = 0; F0 = 2; T = 5; x0dd = inv(m)*(F0-c*x0d-k*x0); xprev = x0 − (dt*x0d) + ((dt^2)*x0dd/2); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = a0*m + a1*c; t = 0; v(1) = x0d;a(1) = x0dd; i = 1; for t = 0:dt:T+dt X(i) = x0; f = F0*(1 − sin(0.5*t)); fbar = f + (a2*m−k)*x0 + (a1*c−a0*m)*xprev; x = inv(mbar)*fbar; xprev = x0; x0 = x; i = i + 1; p = i; end for i = 2:p−1 if i < p−1 v(i) = (X(i + 1) − X(i − 1))/(2*dt); a(i) = (X(i + 1) − 2*X(i) + X(i − 1))/dt^2; end end

ANMF.CH09_4PP.indd 685

3/1/2023 2:55:10 PM

686 • Applied Numerical Methods Using MATLAB fprintf(‘\ntime\t\tdisplacement\tvelocity\tacceleration\n’); i = 1; for t = 0:dt:T fprintf(‘%f\t%f\t%f\t%f\n’,t,X(i),v(i),a(i)); i = i + 1; end t = [0:dt:T+dt]; plot(t,X,’−p’); xlabel(‘Time(s)’); The output of the program is shown below for various values of Dt. When Dt = 1 sec, the following results are obtained Time

displacement velocity

acceleration

0.000000 0.000000

0.000000 1.000000

1.000000 0.500000

0.590965 0.181930

2.000000 1.181930

0.433220 −0.497419

3.000000 1.366441

−0.144974

−0.658969

4.000000 0.891982

−0.606607

−0.264297

5.000000 0.153226

−0.536092 0.405328

When Dt = 0.5 sec, the following results are obtained Time

displacement velocity

acceleration

0.000000 0.000000

0.000000 1.000000

0.500000 0.125000

0.407252 0.629008

1.000000 0.407252

0.620473 0.223877

1.500000 0.745473

0.638891 −0.150209

2.000000 1.046143

0.491762 −0.438307

2.500000 1.237235

0.231599 −0.602342

3.000000 1.277742

−0.075255

ANMF.CH09_4PP.indd 686

−0.625078

3/1/2023 2:55:10 PM

Direct Numerical Integration Methods • 687

3.500000 1.161980

−0.359295

−0.511081

4.000000 0.918447

−0.558261

−0.284782

4.500000 0.603719

−0.625966 0.013962

5.000000 0.292481

−0.538461 0.336057

When Dt = 0.1 sec, the following results are obtained Time

ANMF.CH09_4PP.indd 687

displacement velocity

acceleration

0.000000 0.000000

0.000000 1.000000

0.100000 0.005000

0.096651 0.933023

0.200000 0.019330

0.186429 0.862537

0.300000 0.042286

0.269009 0.789068

0.400000 0.073132

0.344120 0.713147

0.500000 0.111110

0.411543 0.635310

0.600000 0.155441

0.471113 0.556092

0.700000 0.205332

0.522719 0.476028

0.800000 0.259985

0.566303 0.395644

0.900000 0.318593

0.601858 0.315459

1.000000 0.380356

0.629430 0.235982

1.100000 0.444479

0.649114 0.157706

1.200000 0.510179

0.661055 0.081110

1.300000 0.576690

0.665443 0.006652

1.400000 0.643268

0.662514 −0.065229

1.500000 0.709193

0.652547 −0.134117

1.600000 0.773777

0.635860 −0.199624

1.700000 0.836365

0.612810 −0.261384

1.800000 0.896339

0.583787 −0.319064

1.900000 0.953122

0.549216 −0.372359

3/1/2023 2:55:10 PM

688 • Applied Numerical Methods Using MATLAB

1.4 1.2

displacement(m)

1 0.8 0.6 0.4 0.2 0

0

1

2

3 time(s)

4

5

6

FIGURE 9.5  Displacement response plot for Δt = 0.1 seconds.

EXAMPLE 9.2

. ⋅⋅ Find the solution of the equation 5X + 2.5X + 4000X = F(t), where F(t) is . as shown in Figure 9.2 for the duration 0 ≤ t ≤ 1. Assume that X0 = X0 = 0 and Δt = 0.05. Use the central difference method.

FIGURE 9.6

ANMF.CH09_4PP.indd 688

3/1/2023 2:55:11 PM

Direct Numerical Integration Methods • 689

Solution: MATLAB program: % INITIAL VALUES m = 5;k = 4000;c = 2.5;dt = 0.05; x0 = 0;x0d = 0; F0 = 200; T = 1; x0dd = inv(m)*(F0−c*x0d−k*x0); xprev = x0 − (dt*x0d) + ((dt^2)*x0dd/2); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = a0*m + a1*c; t = 0; v(1) = x0d;a(1) = x0dd; i = 1; for t = 0:dt:T + dt X(i) = x0; if t 0.2 & t 0.6 f = 0; end end end fbar = f + (a2*m − k)*x0 + (a1*c − a0*m)*xprev; x = inv(mbar)*fbar; xprev = x0; x0 = x; i = i + 1; p = i;

ANMF.CH09_4PP.indd 689

3/1/2023 2:55:11 PM

690 • Applied Numerical Methods Using MATLAB end for i = 2:p − 1 if i < p − 1 v(i) = (X(i+1) − X(i−1))/(2*dt); a(i)=(X(i+1) − 2*X(i)+X(i−1))/dt^2; end end fprintf(‘\ntime\t\tdisplacement\tvelocity\tacceleration\n’); i = 1; for t = 0:dt:T fprintf(‘%f\t%f\t%f\t%f\n’,t,X(i),v(i),a(i)); i = i + 1; end t = [0:dt:T+dt]; plot(t,X,’−p’); xlabel(‘Time(s)’); ylabel(‘displacement(m)’); grid on; The output obtained is presented below: Time

displacement velocity

acceleration

0.000000 0.000000

0.000000 40.000000

0.050000 0.050000

0.987654 −0.493827

0.100000 0.098765

−0.000000

0.150000 0.050000

−0.963268 0.481634

0.200000 0.002439

0.000000 38.049078

0.250000 0.050000

0.816027 −5.408013

0.300000 0.084041

−0.246914

0.350000 0.025309

−1.042791 5.274482

ANMF.CH09_4PP.indd 690

−39.012346

−37.109594

3/1/2023 2:55:11 PM

Direct Numerical Integration Methods • 691

−0.020238

0.400000

−0.006097 36.193308

0.450000 0.024699

0.770130 −5.144248

0.500000 0.056775

−0.240967

0.550000 0.000602

−0.998028 5.017230

−35.299646

0.600000

−0.043028

−0.011896 34.428050

0.650000

−0.000587 0.849928 0.044924

0.700000 0.041965

0.011602 −33.577975

0.750000 0.000573

−0.828943

−0.043815

0.800000

−0.040929

−0.011316 32.748889

0.850000

−0.000559 0.808475 0.042733

0.900000 0.039918

0.011036 −31.940274

0.950000 0.000545

−0.788513

−0.038933

1.000000

−0.041678

−0.010764 31.151626

Figure 9.7 shows the response history. 0.1 0.08

displacement(m)

0.06 0.04 0.02 0 -0.02 -0.04 -0.06

0

0.2

0.4

0.6

time(s)

0.8

1

1.2

1.4

FIGURE 9.7  MATLAB output for Δt = 0.05 s.

ANMF.CH09_4PP.indd 691

3/1/2023 2:55:11 PM

692 • Applied Numerical Methods Using MATLAB EXAMPLE 9.3

⋅⋅ Solve numerically the differential equation 4X + 2000X = F(t) with the initial . conditions X0 = X0 = 0 and forcing function F(t) as shown in Figure 9.3. Use the central difference method with Δt = 0.02 sec.

FIGURE 9.8

Solution: %MATLAB program % INITIAL VALUES m = 4;k = 2000;c = 0;dt = 0.02; x0 = 0;x0d = 0; F0 = 150; T = 1; x0dd = inv(m)*(F0 − c*x0d − k*x0); xprev = x0 − (dt*x0d) + ((dt^2)*x0dd/2); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = a0*m + a1*c; t = 0; v(1) = x0d;a(1) = x0dd; i = 1; for t = 0:dt:T + dt

ANMF.CH09_4PP.indd 692

3/1/2023 2:55:11 PM

Direct Numerical Integration Methods • 693

X(i) = x0; if t 0.1 & t 0.2 f = 0; end end t=[0:dt:T + dt]; plot(t,X,‘−p’); xlabel(‘Time(s)’); ylabel(‘displacement(m)’); grid on; The output obtained is presented below: Time

ANMF.CH09_4PP.indd 693

displacement velocity

acceleration

0.000000 0.000000

0.000000 37.500000

0.020000 0.007500

0.712500 33.750000

0.040000 0.028500

1.282500 23.250000

0.060000 0.058800

1.596000 8.100000

0.080000 0.092340

1.590300 −8.670000

0.100000 0.122412

1.266540 −23.706000

0.120000 0.143002

0.614472 −41.500800

0.140000 0.146991

−0.310490

−50.995440

0.160000 0.130582

−1.323355

−50.290992

0.180000 0.094057

−2.221548

−39.528346

0.200000 0.041720

−2.825432

−20.860030

0.220000

−0.018961

−2.939229 9.480291

0.240000

−0.075849

−2.465181 37.924555

0.260000

−0.117568

−1.498096 58.783907

3/1/2023 2:55:11 PM

694 • Applied Numerical Methods Using MATLAB 0.280000

−0.135773

−0.231392 67.886478

0.300000

−0.126824 1.081590 63.411753

0.320000

−0.092509 2.178254 46.254678

0.340000

−0.039693 2.839268 19.846667

0.360000 0.021061

2.932428 −10.530677

0.380000 0.077604

2.439102 −38.801886

0.400000 0.118625

1.457956 −59.312718

0.420000 0.135922

0.185219 −67.961006

0.440000 0.126034

−1.124562

−63.017093

0.460000 0.090940

−2.209431

−45.469761

0.480000 0.037657

−2.852413

−18.828477

0.500000

−0.023157

−2.924913 11.578502

0.520000

−0.079340

−2.412430 39.669781

0.540000

−0.119654

−1.417461 59.827104

0.560000

−0.136038

−0.139000 68.019006

0.580000

−0.125214 1.167261 62.607106

0.600000

−0.089348 2.240070 44.673786

0.620000

−0.035611 2.864865 17.805708

0.640000 0.025247

2.916687 −12.623511

0.660000 0.081056

2.385171 −40.528028

0.680000 0.120654

1.376622 −60.326940

0.700000 0.136121

0.092748 −68.060463

0.720000 0.124364

−1.209676

−62.181894

0.740000 0.087734

−2.270164

−43.866946

0.760000 0.033557

−2.876620

−16.778609

0.780000

−0.027331

−2.907751 13.665450

0.800000

−0.082753

−2.357333 41.376419

ANMF.CH09_4PP.indd 694

3/1/2023 2:55:11 PM

Direct Numerical Integration Methods • 695

0.820000

−0.121624

−1.335447 60.812104

0.840000

−0.136171

−0.046473 68.085368

0.860000

−0.123483 1.251797 61.741559

0.880000

−0.086099 2.299706 43.049438

0.900000

−0.031495 2.887675 15.747429

0.920000 0.029408

2.898109 −14.704065

0.940000 0.084429

2.328921 −42.214747

0.960000 0.122565

1.293948 −61.282479

0.980000 0.136187

0.000186 −68.093715

1.000000 0.122572

−1.293613

−61.286209

Figure 9.9 shows the response history. 0.15 0.1

displacement(m)

0.05 0 -0.05 -0.1 -0.15 -0.2

0

0.2

0.4

0.6

time(s)

0.8

1

1.2

1.4

FIGURE 9.9  Response history.

ANMF.CH09_4PP.indd 695

3/1/2023 2:55:12 PM

696 • Applied Numerical Methods Using MATLAB EXAMPLE 9.4 Solve numerically the solution to the problem of a spring mass system excited by a triangular impulse. The differential equation of motion and the initial conditions are given as   82 X  F(t) 0.5 X . with X1 = X 1 = 0

The triangular force is defined in Figure 9.10. Use central difference method with Δt = 0.05 sec.

FIGURE 9.10

Solution: %MATLAB Program % INITIAL VALUES m = 0.5;k = 8*pi^2;c = 0;dt = 0.05; x0 = 0;x0d = 0; F0 = 0;F = 150; T = 1; x0dd = inv(m)*(F0 − c*x0d − k*x0); xprev = x0 − (dt*x0d) + ((dt^2)*x0dd/2); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = a0*m + a1*c; t = 0; v(1) = x0d;a(1) = x0dd;

ANMF.CH09_4PP.indd 696

3/1/2023 2:55:21 PM

Direct Numerical Integration Methods • 697

i = 1; for t = 0:dt:T + dt X(i) = x0; if t 0.2 & t 0.4 f = 0; end end end fbar = f + (a2*m−k)*x0 + (a1*c−a0*m)*xprev; x = inv(mbar)*fbar; xprev = x0; x0 = x; i = i + 1; p = i; end for i = 2:p−1 if i < p−1 v(i) = (X(i+1) − X(i−1))/(2*dt); a(i) = (X(i+1) − 2*X(i) + X(i−1))/dt^2; end end fprintf(‘\ntime\t\tdisplacement\tvelocity\tacceleration\n’); i = 1; for t = 0:dt:T fprintf(‘%f\t%f\t%f\t%f\n’,t,X(i),v(i),a(i)); i = i + 1; end

ANMF.CH09_4PP.indd 697

3/1/2023 2:55:21 PM

698 • Applied Numerical Methods Using MATLAB t = [0:dt:T + dt]; plot(t,X,‘−p’); xlabel(‘Time(s)’); ylabel(‘displacement(m)’); grid on; The output obtained is presented below: Time

displacement velocity

acceleration

0.000000 0.000000

0.000000 0.000000

0.050000 0.000000

1.875000 75.000000

0.100000 0.187500

6.759780 120.391187

0.150000 0.675978

12.725905 118.253838

0.200000 1.460091

17.418045 69.431745

0.250000 2.417782

15.233816 −156.800902

0.300000 2.983472

3.285518 −321.131034

0.350000 2.746334

−13.709851

−358.683716

0.400000 1.612487

−29.042788

−254.633742

0.450000

−0.157945

−34.785091 24.941603

0.500000

−1.866022

−26.794791 294.670399

0.550000

−2.837424

−8.226331 448.067983

0.600000

−2.688655 13.589754 424.575418

0.650000

−1.478448 30.040819 233.467197

0.700000 0.315427

34.632244 −49.810180

0.750000 1.984776

25.551408 −313.423286

0.800000 2.870567

6.383280 −453.301838

0.850000 2.623104

−15.304866

−414.223998

0.900000 1.340081

−30.950893

−211.617078

ANMF.CH09_4PP.indd 698

3/1/2023 2:55:21 PM

Direct Numerical Integration Methods • 699

0.950000

−0.471985

−34.377997 74.532916

1.000000

−2.097719

−24.233212 331.258494

Figure 9.11 shows the plot of the response versus time. 3

2

displacement(m)

1

0

-1

-2

-3

0

0.2

0.4

0.6

time(s)

0.8

1

1.2

1.4

1.2

1.4

FIGURE 9.11a  MATLAB output for Dt = 0.05 s. 3

2

displacement(m)

1

0

-1

-2

-3

0

0.2

0.4

0.6

time(s)

0.8

1

FIGURE 9.11b  MATLAB output for Δt = 0.005 s.

ANMF.CH09_4PP.indd 699

3/1/2023 2:55:21 PM

700 • Applied Numerical Methods Using MATLAB EXAMPLE 9.5 Solve the following nonlinear vibration problem, using the central difference method.   CX  KX  K X 3  Fcos t MX with M = 1.0, C .= 0.5, K = 1.0, K* = 0.5, Dt = 0.05, tmax = 5.0, and the initial conditions X0 = X0 = 0. Plot the variation of X with t. Take ω = 1 and F = 10. Solution: Here the in Xi+1 an additional term with –K*Xi3 will come and other things will remain same. Assuming F = 10N. %MATLAB Program   % INITIAL VALUES m = 1;k = 1;c = 0.5;ks = 0.5;dt = 0.05; x0 = 0;x0d = 0;omega = 1; F0 = 10; T = 5; x0dd = inv(m)*(F0−c*x0d−k*x0); xprev = x0 − (dt*x0d)+((dt^2)*x0dd/2); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = a0*m+a1*c; t = 0; v(1) = x0d;a(1) = x0dd; i = 1; for t = 0:dt:T+dt X(i) = x0; f = F0*cos (omega*t); % NON-LINEAR TERM fbar = f + (a2*m−k)*x0 + (a1*c−a0*m)*xprev−ks*(x0^3); x = inv(mbar)*fbar;

ANMF.CH09_4PP.indd 700

3/1/2023 2:55:23 PM

Direct Numerical Integration Methods • 701

xprev = x0; x0 = x; i = i + 1; p = i; end for i = 2:p−1 if i < p−1 v(i) = (X(i+1) − X(i−1))/(2*dt); a(i) = (X(i+1) − 2*X(i) + X(i−1))/dt^2; end end fprintf(‘\ntime\t\tdisplacement\tvelocity\tacceleration\n’); i = 1; for t = 0:dt:T fprintf(‘%f\t%f\t%f\t%f\n’,t,X(i),v(i),a(i)); i = i + 1; end t=[0:dt:T+dt]; plot(t,X,‘−p’); xlabel(‘Time(s)’); The output obtained is presented below: Time

ANMF.CH09_4PP.indd 701

displacement velocity

acceleration

0.000000 0.000000

0.000000 10.000000

0.050000 0.012500

0.493210 9.728397

0.100000 0.049321

0.971789 9.414766

0.150000 0.109679

1.433672 9.060536

0.200000 0.192688

1.876835 8.665983

3/1/2023 2:55:23 PM

702 • Applied Numerical Methods Using MATLAB 0.250000 0.297362

2.299209 8.229010

0.300000 0.422609

2.698528 7.743753

0.350000 0.567215

3.072103 7.199215

0.400000 0.729819

3.416537 6.578158

0.450000 0.908869

3.727404 5.856518

0.500000 1.102560

3.998908 5.003655

0.550000 1.308760

4.223595 3.983832

0.600000 1.524919

4.392175 2.759342

0.650000 1.747977

4.493550 1.295680

0.700000 1.974274

4.515166 −0.431058

0.750000 2.199494

4.443769 −2.424815

0.800000 2.418651

4.266666 −4.659319

0.850000 2.626160

3.973458 −7.069001

0.900000 2.815997

3.558129 −9.544163

0.950000 2.981973

3.021179 −11.933829

1.000000 3.118115

2.371360 −14.058926

1.050000 3.219109

1.626489 −15.735916

1.100000 3.280764

0.812907 −16.807359

1.150000 3.300400

−0.036584

−17.172266

1.200000 3.277105

−0.886080

−16.807592

1.250000 3.211792

−1.700621

−15.774049

1.300000 3.107043

−2.450079

−14.204274

1.350000 2.966784

−3.112116

−12.277189

1.400000 2.795832

−3.673705

−10.186361

1.450000 2.599413

−4.131135

−8.110872

1.500000 2.382718

−4.488775

−6.194716

ANMF.CH09_4PP.indd 702

3/1/2023 2:55:23 PM

Direct Numerical Integration Methods • 703

1.550000 2.150536

−4.757067

−4.536959

1.600000 1.907011

−4.950278

−3.191475

1.650000 1.655508

−5.084393

−2.173152

1.700000 1.398572

−5.175400

−1.467123

1.750000 1.137968

−5.238034

−1.038230

1.800000 0.874769

−5.284965

−0.839002

1.850000 0.609472

−5.326325

−0.815407

1.900000 0.342136

−5.369468

−0.910322

1.950000 0.072525

−5.418854

−1.065097

−5.475975

−1.219747

−0.199749

2.000000 etc-

Figure 9.12 shows the MATLAB response. 4 3

displacement(m)

2 1 0 -1 -2 -3 -4

0

1

2

3 time(s)

4

5

6

FIGURE 9.12  MATLAB output for Δt = 0.05 s.

ANMF.CH09_4PP.indd 703

3/1/2023 2:55:23 PM

704 • Applied Numerical Methods Using MATLAB EXAMPLE 9.6 Solve Example 9.1 using the fourth-order Runge-Kutta method. Solution:

Here Y = f(x 1 ,x 2 ,t) is a vector of functions



For single degree of freedom system, it contains



x2    x    Y     f(x 1 ,x 2 ,t)   1   x    m  F(t)  kx 1  cx 2  



Final solution takes the form t Yi+1 = Yi + K1  2K2  2K3  K4  , where 6 p K1 = f(x1, x2, t) =   q  r  K2 = f(x1 + p/2, x2 + q/2, t + Dt/2) =   s 



u  K3 = f(x1 + r/2, x2 + q/2, t + Dt/2) =   v 



m  K4 = f(x1 + u, x2 + v, t + Dt) =   n

%MATLAB Program dt = 0.5;T = 10; h = dt; x1 = 0; x2 = 0; i = 1; for t = 0:h:T f1 = h*f(t,x1,x2); g1 = h*g(t,x1,x2); f2 = h*f((t+h/2),(x1+f1/2),(x2+g1/2));g2 = h*g((t+h/2),(x1+f1/2),(x2+g1/2));

ANMF.CH09_4PP.indd 704

3/1/2023 2:55:35 PM

Direct Numerical Integration Methods • 705

f3 = h*f((t+h/2),(x1+f2/2),(x2+g2/2));g3 = h*g((t+h/2),(x1+f2/2),(x2+g2/2)); f4 = h*f((t+h),(x1+f3),(x2+g3)); g4 = h*g((t+h),(x1+f3),(x2+g3)); x1 = x1 + ((f1+f4) + 2*(f2+f3))/6.0; x2 = x2 + ((g1+g4) + 2*(g2+g3))/6.0; X(i) = x1; Y(i) = x2; i = i + 1; end t = [0:h:T]; plot(t,X,‘−p’,t,Y,‘−*’); grid on; xlabel(‘time(s)’); legend(‘displacement(m)’,‘velocity(m/s)’,2)  his program is executed with two other separate programs f.m and g.m T given below: % file f.m function v1 = f(t,x1,x2) v1 = x2; % file g.m function v2 = g(t,x1,x2) k = 1; m = 1; c = 0; F = 100*(1−cos(t)); v2 = (F−k*x1−c*x2)/m; Output: Time

ANMF.CH09_4PP.indd 705

displacement velocity

0.000000 0.000000

0.000000

0.500000 0.110437

0.411687

3/1/2023 2:55:35 PM

706 • Applied Numerical Methods Using MATLAB 1.000000 0.379029

0.629789

1.500000 0.707386

0.653171

2.000000 1.004189

0.510442

2.500000 1.198540

0.253323

3.000000 1.249282

−0.052604

3.500000 1.149336

−0.338363

4.000000 0.924789

−0.541854

4.500000 0.629191

−0.616591

5.000000 0.334076

−0.537864

The output of the program is shown in Figure 9.13. 1.5

displacement(m) velocity(m/s)

1

0.5

0

-0.5

-1

0

0.5

1

1.5

2

2.5 time(s)

3

3.5

4

4.5

5

FIGURE 9.13  MATLAB output.

EXAMPLE 9.7 Solve Example 9.2 using the fourth-order Runge-Kutta method.

ANMF.CH09_4PP.indd 706

3/1/2023 2:55:36 PM

Direct Numerical Integration Methods • 707

Solution: %MATLAB program for this problem is given below. dt = 0.05;T = 1; h = dt; x1 = 0;

% displacement

x2 = 0;

% velocity

i = 1; fprintf(‘time\t\tdisplacement\tvelocity\n’); for t = 0:h:T fprintf(‘%f\t%f\t%f\n’,t,x1,x2); X(i) = x1; Y(i) = x2; f1 = h*f(t,x1,x2); g1 = h*g(t,x1,x2); f2 = h*f((t+h/2),(x1+f1/2),(x2+g1/2));g2 = h*g((t+h/2),(x1+f1/2),(x2+g1/2)); f3 = h*f((t+h/2),(x1+f2/2),(x2+g2/2));g3 = h*g((t+h/2),(x1+f2/2),(x2+g2/2)); f4 = h*f((t+h),(x1+f3),(x2+g3)); g4 = h*g((t+h),(x1+f3),(x2+g3)); x1 = x1 + ((f1+f4)+2*(f2+f3))/6.0; x2 = x2 + ((g1+g4)+2*(g2+g3))/6.0; i = i + 1; end time = [0:h:T]; plot(time,X,‘−p’); grid on; xlabel(‘time(s)’); ylabel(‘displacement(m)’)

ANMF.CH09_4PP.indd 707

3/1/2023 2:55:36 PM

708 • Applied Numerical Methods Using MATLAB function g.m is given below: function v2 = g(t,x1,x2)  k = 4000; m = 5; c = 2.5; if t 0.2 & t0.6 F = 0; end end end v2 = (F−k*x1−c*x2)/m; Output: Time

displacement velocity

0.000000 0.000000

0.000000

0.050000 0.041253

1.316874

0.100000 0.091824

0.439092

0.150000 0.071773

−1.031937

0.200000 0.019836

−0.736985

0.250000 0.018390

0.574514

0.300000 0.056157

0.655850

0.350000 0.060285

−0.490566

0.400000 0.018109

−0.945601

0.450000

−0.009407

−0.071506

0.500000 0.009400

0.627115

0.550000 0.030533

0.077915

0.600000 0.010993

−0.730337

ANMF.CH09_4PP.indd 708

3/1/2023 2:55:36 PM

Direct Numerical Integration Methods • 709

0.650000

−0.022121

−0.405278

0.700000

−0.017213 0.518375

0.750000 0.014055

0.535490

0.800000 0.020088

−0.285294

0.850000

−0.005878

0.900000

−0.019935 0.063796

0.950000

−0.001387 0.535146

1.000000 0.017375

−0.574286

0.121352

The displacement response is shown in Figure 9.14. 0.1 0.08

displacement(m)

0.06 0.04 0.02 0 -0.02 -0.04

0

0.1

0.2

0.3

0.4

0.5 time(s)

0.6

0.7

0.8

0.9

1

FIGURE 9.14  MATLAB output.

ANMF.CH09_4PP.indd 709

3/1/2023 2:55:36 PM

710 • Applied Numerical Methods Using MATLAB EXAMPLE 9.8 Solve Example 9.3 by using the Runge-Kutta method. Solution:   %MATLAB program dt = 0.02;T = 1; h = dt; x1 = 0;

% displacement

x2 = 0;

% velocity

i = 1; fprintf(‘time\t\tdisplacement\tvelocity\n’); for t = 0:h:T fprintf(‘%f\t%f\t%f\n’,t,x1,x2); X(i) = x1; Y(i) = x2; f1 = h*f(t,x1,x2); g1 = h*g(t,x1,x2); f2 = h*f((t+h/2),(x1+f1/2),(x2+g1/2));g2 = h*g((t+h/2),(x1+f1/2),(x2+g1/2)); f3 = h*f((t+h/2),(x1+f2/2),(x2+g2/2));g3 = h*g((t+h/2),(x1+f2/2),(x2+g2/2)); f4 = h*f((t+h),(x1+f3),(x2+g3)); g4 = h*g((t+h),(x1+f3),(x2+g3)); x1 = x1 + ((f1+f4) + 2*(f2+f3))/6.0; x2 = x2 + ((g1+g4) + 2*(g2+g3))/6.0; i = i + 1; end time = [0:h:T]; plot(time,X,‘−p’); grid on; xlabel(‘time(s)’); ylabel(‘displacement(m)’)

ANMF.CH09_4PP.indd 710

3/1/2023 2:55:36 PM

Direct Numerical Integration Methods • 711

The function g.m defining the force signal is given below: function v2 = g(t,x1,x2) k = 2000; m = 4; c = 0; if t 0.1 & t0.2 F = 0; end end end v2 = (F−k*x1−c*x2)/m; Output: Time

ANMF.CH09_4PP.indd 711

displacement velocity

0.000000 0.000000

0.000000

0.020000 0.007375

0.725000

0.040000 0.028041

1.307417

0.060000 0.057936

1.632787

0.080000 0.091181

1.637183

0.100000 0.121242

1.319778

0.120000 0.141711

0.669243

0.140000 0.146114

−0.260185

0.160000 0.130641

−1.285790

0.180000 0.095386

−2.205969

0.200000 0.044333

−2.839867

0.220000

−0.014931

−2.989164

0.240000

−0.071253

−2.550901

3/1/2023 2:55:36 PM

712 • Applied Numerical Methods Using MATLAB 0.260000

−0.113564

−1.611284

0.280000

−0.133548

−0.355057

0.300000

−0.127280 0.970822

0.320000

−0.095995 2.105735

0.340000

−0.045845 2.826625

0.360000 0.013311

2.991841

0.380000 0.069845

2.568967

0.400000 0.112643

1.641187

0.420000 0.133296

0.390919

0.440000 0.127747

−0.936053

0.460000 0.097088

−2.078891

0.480000 0.047349

−2.812982

0.500000

−0.011691

−2.994079

0.520000

−0.068427

−2.586645

0.540000

−0.111707

−1.670828

0.560000

−0.133025

−0.426696

0.580000

−0.128194 0.901173

0.600000

−0.098165 2.051764

0.620000

−0.048845 2.798940

0.640000 0.010071

2.995880

0.660000 0.067001

2.603933

0.680000 0.110755

1.700204

0.700000 0.132735

0.462383

0.720000 0.128622

−0.866188

0.740000 0.099228

−2.024359

0.760000 0.050333

−2.784500

0.780000

ANMF.CH09_4PP.indd 712

−0.008450

−2.997242

3/1/2023 2:55:36 PM

Direct Numerical Integration Methods • 713

0.800000

−0.065566

−2.620828

0.820000

−0.109788

−1.729309

0.840000

−0.132425

−0.497977

0.860000

−0.129031 0.831104

0.880000

−0.100275 1.996680

0.900000

−0.051812 2.769666

0.920000 0.006829

2.998167

0.940000 0.064122

2.637329

0.960000 0.108805

1.758141

0.980000 0.132097

0.533471

1.000000 0.129421

−0.795925

0.15 0.1

displacement(m)

0.05 0 -0.05 -0.1 -0.15 -0.2

0

0.1

0.2

0.3

0.4

0.5 time(s)

0.6

0.7

0.8

0.9

1

FIGURE 9.15  MATLAB output.

ANMF.CH09_4PP.indd 713

3/1/2023 2:55:36 PM

714 • Applied Numerical Methods Using MATLAB EXAMPLE 9.9 Solve Example 9.4 by using the Runga-Kutta method. Solution: Triangular pulse is defined with the following MATLAB m function function v2 = g(t,x1,x2) k = 8*pi^2;m = 0.5; c = 0; if t 0.2 & t 0.4 F = 0; end end end v2 = (F−k*x1−c*x2)/m; Here dt = 0.05s and T = 1s. Output: Time

displacement velocity

0.000000 0.000000

0.000000

0.050000 0.031250

1.813315

0.100000 0.231900

6.553226

0.150000 0.706315

12.411526

0.200000 1.454472

17.155388

0.250000 2.309559

15.351753

0.300000 2.826498

4.081899

0.350000 2.627673

−12.352850

0.400000 1.608467

−27.686924

0.450000 0.008154

−34.265866

ANMF.CH09_4PP.indd 714

3/1/2023 2:55:36 PM

Direct Numerical Integration Methods • 715

0.500000

−1.593966

−27.784720

0.550000

−2.587509

−10.723309

0.600000

−2.594445 10.409650

0.650000

−1.612934 27.559532

0.700000

−0.017719 34.195743

0.750000 1.582951

27.798535

0.800000 2.579242

10.815730

0.850000 2.592074

−10.273897

0.900000 1.617357

−27.432202

0.950000 0.027244

−34.125340

1.000000

−1.571956

−27.811835

3

2

displacement(m)

1

0

-1

-2

-3

0

0.1

0.2

0.3

0.4

0.5 time(s)

0.6

0.7

0.8

0.9

1

FIGURE 9.16  MATLAB output.

ANMF.CH09_4PP.indd 715

3/1/2023 2:55:36 PM

716 • Applied Numerical Methods Using MATLAB EXAMPLE 9.10 Solve Example 9.5 by using the Runga-Kutta method. Solution: Here the function defining the system g.m is given below: % g.m function v2 = g(t,x1,x2) k = 1;m = 1; c = 0.5;omega = 1; ks = 0.5 % CUBIC STIFFNESS F = 10*cos (omega*t); v2 = (F−k*x1−c*x2−ks*x1^3)/m; Output: Time

displacement velocity

0.000000 0.000000

0.000000

0.050000 0.012391

0.493389

0.100000 0.049095

0.972141

0.150000 0.109327

1.434195

0.200000 0.192202

1.877534

0.250000 0.296734

2.300105

0.300000 0.421830

2.699660

0.350000 0.566273

3.073539

0.400000 0.728700

3.418380

0.450000 0.907555

3.729795

0.500000 1.101028

4.002029

0.550000 1.306982

4.227665

0.600000 1.522864

4.397443

0.650000 1.745611

4.500284

ANMF.CH09_4PP.indd 716

3/1/2023 2:55:37 PM

Direct Numerical Integration Methods • 717

ANMF.CH09_4PP.indd 717

0.700000 1.971567

4.523624

0.750000 2.196426

4.454170

0.800000 2.415222

4.279150

0.850000 2.622401

3.988046

0.900000 2.811982

3.574682

0.950000 2.977828

3.039377

1.000000 3.114021

2.390700

1.050000 3.215294

1.646314

1.100000 3.277487

0.832466

1.150000 3.297926

−0.018051

1.200000 3.275671

−0.869257

1.250000 3.211578

−1.686035

1.300000 3.108154

−2.438054

1.350000 2.969237

−3.102755

1.400000 2.799563

−3.666911

1.450000 2.604294

−4.126654

1.500000 2.388573

−4.486248

1.550000 2.157169

−4.756089

1.600000 1.914225

−4.950445

1.650000 1.663116

−5.085340

1.700000 1.406414

−5.176825

1.750000 1.145909

−5.239713

1.800000 0.882705

−5.286762

1.850000 0.617325

−5.328190

1.900000 0.349853

−5.371441

3/1/2023 2:55:37 PM

718 • Applied Numerical Methods Using MATLAB 4 3

displacement(m)

2 1 0 -1 -2 -3 -4

0

0.5

1

1.5

2

2.5 time(s)

3

3.5

4

4.5

5

FIGURE 9.17  MATLAB output.

EXAMPLE 9.11 Find the response of the two degree of freedom system when F1(t) = 0 and F2(t) = 10, using the central difference method. The mass, stiffness, and damping matrices for this system are given as 1

21 , K    0 10  1

M  

0

 1

 0.5 ,  C    1 0.1

 0.1 0.1 

All the initial conditions are given as zero. Use Δt = 0.05 Solution: % INITIAL VALUES M = [1 0;0 10]; K = [21 −1;−1 1]; C = [0.5 −0.1;−0.1 0.1]; dt = 0.05;

ANMF.CH09_4PP.indd 718

3/1/2023 2:55:38 PM

Direct Numerical Integration Methods • 719

x0 = [0;0];x0d = [0;0]; F0 = [0;10]; T = 2; x0dd = inv(M)*(F0−C*x0d−K*x0); xprev = x0−(dt.*x0d) + ((dt^2).*(x0dd/2)); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = (a0.*M) + (a1.*C); t = 0; v(:,1) = x0d;a(:,1) = x0dd; i = 1; fprintf(‘time\t\tX(1)\t\tX(2)\n’); for t = 0:dt:T+dt X(:,i) = x0; F = F0; Fbar = F + (a2.*M−K)*x0 + (a1.*C−a0.*M)*xprev; x = inv(mbar)*Fbar; xprev = x0; x0 = x; fprintf(‘%f\t%f\t%f\n’,t,X(1,i),X(2,i)); i = i + 1; p = i; end for i = 2:p−1 if i < p−1 v(:,i) = (X(:,i+1)−X(:,i−1)).*(1/(2*dt)); a(:,i) = (X(:,i+1)−2*X(:,i)+X(:,i−1)).*(1/dt^2); end end

ANMF.CH09_4PP.indd 719

3/1/2023 2:55:38 PM

720 • Applied Numerical Methods Using MATLAB t=[0:dt:T+dt]; plot(t,X(1,:),‘−p’,t,X(2,:),‘−*’); xlabel(‘time(s)’); ylabel(‘displacement(m)’); legend(‘DOF-1’,‘DOF-2’,2); grid on; Output: Time X(1) X(2) 0.000000 0.000000 0.050000

0.000000

−0.000000 0.001250

0.100000 0.000015

0.004998

0.150000 0.000067

0.011243

0.200000 0.000178

0.019981

0.250000 0.000376

0.031210

0.300000 0.000688

0.044924

0.350000 0.001142

0.061120

0.400000 0.001762

0.079793

0.450000 0.002571

0.100937

0.500000 0.003586

0.124545

0.550000 0.004821

0.150611

0.600000 0.006281

0.179128

0.650000 0.007969

0.210088

0.700000 0.009880

0.243481

0.750000 0.012004

0.279301

0.800000 0.014325

0.317535

0.850000 0.016824

0.358176

0.900000 0.019481

0.401212

ANMF.CH09_4PP.indd 720

3/1/2023 2:55:38 PM

Direct Numerical Integration Methods • 721

0.950000 0.022271

0.446631

1.000000 0.025170

0.494422

2.5

DOF-1 DOF-2

2

displacement(m)

1.5

1

0.5

0

-0.5

0

0.5

1

time(s)

1.5

2

2.5

FIGURE 9.18  MATLAB output response.

EXAMPLE 9.12 Solve Example 9.11 using the two-cycle iteration with trapezoidal rule. Solution: For an undamped system the following equations are applicable. . .. We now have:  4 2 M  K   Un 1  R n 1  M  4 2 Un  4 Un  Un   t   . ..  t −1 t The initial conditions are: U0 = 0, U0 = 0, U0 = [M] (R0 − [K] U0). .. .. . .. . . 4 4 . t .. 2 U n  1  2 U n  1  U n   Un  Un , Un 1  [Un 1  Un ]  Un  Un 1  Un   Un t t 2 t

ANMF.CH09_4PP.indd 721

3/1/2023 2:55:43 PM

722 • Applied Numerical Methods Using MATLAB %MATLAB Program % INITIAL VALUES M = [1 0;0 10]; K = [21 −1;−1 1]; C = [0.5 −0.1;−0.1 0.1]; dt = 0.05; T = 2;dt = 0.05; t = [0:dt:T]; i = 1; x(:,i) = [0;0];xd(:,i) = [0;0]; f(:,i) = [0;10]; xdd(:,i) = inv(M)*(f(:,i) − C*xd(:,i) − K*x(:,i)); % FIRST time step dxd(:,2) = dt*xdd(:,1); for i = 2:length(t) f(:,i) = [0;10]; df(:,i) = f(:,i)−f(:,i−1); xd(:,i) = xd(:,i−1)+dxd(:,i); dx(:,i) = (dt/2)*(xd(:,i−1)+xd(:,i)); dxdd(:,i) = inv(M)*(df(:,i)−K*dx(:,i)−C*dxd(:,i)); xdd(:,i) = xdd(:,i−1)+dxdd(:,i);  UPDATING VALUES OF VELOCITY AND DISPLACMENT IN % CURRENT CYCLE dxd(:,i) = (dt/2)*(xdd(:,i−1) + xdd(:,i)); xd(:,i) = xd(:,i−1) + dxd(:,i); dx(:,i) = (1/2)*(xd(:,i−1) + xd(:,i)); % REVISED DISPLACMENT IN CURRENT CYCLE

ANMF.CH09_4PP.indd 722

3/1/2023 2:55:43 PM

Direct Numerical Integration Methods • 723

x(:,i) = x(:,i−1)+dx(:,i); % DELTA x DOT FOR NEXT CYCLE dxd(:,i+1) = 2*dt*xdd(:,i)−dxd(:,i); end fprintf(‘time\t\tX(1)\t\tX(2)\n’); p=1 for time = 0:dt:T fprintf(‘%f\t%f\t%f\n’,time,x(1,p),x(2,p)); p = p + 1; end plot(t,x(1,:),‘−p’,t,x(2,:),‘−*’); xlabel(‘time(s)’); ylabel(‘displacement(m)’); legend(‘DOF-1’,’DOF-2’,2); grid on; The output is as follows: Time X(1) X(2)

ANMF.CH09_4PP.indd 723

0.000000 0.000000

0.000000

0.050000 0.000078

0.024992

0.100000 0.000492

0.099950

0.150000 0.001642

0.224830

0.200000 0.004000

0.399578

0.250000 0.008087

0.624125

0.300000 0.014442

0.898392

0.350000 0.023594

1.222290

0.400000 0.036037

1.595716

3/1/2023 2:55:43 PM

724 • Applied Numerical Methods Using MATLAB 0.450000 0.052204

2.018558

0.500000 0.072446

2.490690

0.550000 0.097016

3.011977

0.600000 0.126057

3.582274

0.650000 0.159599

4.201422

0.700000 0.197561

4.869252

40

DOF-1 DOF-2

35

displacement(m)

30 25 20 15 10 5 0

0

0.2

0.4

0.6

0.8

1 time(s)

1.2

1.4

1.6

1.8

2

FIGURE 9.19  MATLAB output.

EXAMPLE 9.13 Solve Example 9.11 using the fourth-order Runge-Kutta method x 3   x1  x  x   4  2    Here Y = f(x 1 ,x 2 ,x 3 ,x 4 ,t) , where Y = and f(x 1 ,x 2 ,x 3 ,x 4 ,t) =    x 3  x 3   x 4   x 4  Solution:

ANMF.CH09_4PP.indd 724

3/1/2023 2:55:51 PM

Direct Numerical Integration Methods • 725

 x 3   x1  x 3  1 1 1 Where    [M] [K]    [M] [C]    [M] F(t)  x 4  x 2  x 4   x 1   x  [I]   2   [0] In total it can be written as     1 1   x 3   [M] [K] [M] [C]  x 4 

 x1  x   2   [0]    +  1   x 3  [M] F(t)  x 4 

Or    Y  [E]Y  F %MATLAB Program dt = 0.05;T = 1; h = dt; x1 = 0; x2 = 0; % displacements x3 = 0; x4 = 0; % velocity M = [1 0;0 10];K = [21 −1;−1 1];C = [0.5 −0.1; -0.1 0.1];f = [0;10]; E = [zeros(size(M)) eye(size(M));-inv(M)*K -inv(M)*C]; F = [0;0;inv(M)*f]; i = 1; Y = [x1;x2;x3;x4]; fprintf(‘time\t\tX(1)\t\tX(2)\n’); for t = 0:h:T X(:,i) = Y; fprintf(‘%f\t%f\t%f\n’,t,Y(1),Y(2)); K1 = h*(E*Y+F); K2 = h*(Y+ (0.5*K1))+F); K3 = h*(Y+ (0.5*K2))+F) K4 = h*(Y+K3+F)

ANMF.CH09_4PP.indd 725

3/1/2023 2:56:04 PM

726 • Applied Numerical Methods Using MATLAB Y = Y + (K1+2*K2+2*K3+K4)/6; i = i + 1; end time = [0:h:T]; plot(time,X(1,:),‘−p’,time,X(2,:),‘−*’); grid on; xlabel(‘time(s)’); ylabel(‘displacement(m)’) legend(‘DOF-1’,‘DOF-2’); Output: Time X(1) X(2) 0.000000 0.000000 0.000000 0.050000 0.000002 0.001250 0.100000 0.000020 0.004998 0.150000 0.000074 0.011242 0.200000 0.000188 0.019980 0.250000 0.000388 0.031208 0.300000 0.000702 0.044922 0.350000 0.001156 0.061118 0.400000 0.001776 0.079790 0.450000 0.002584 0.100933 0.500000 0.003597 0.124541 0.550000 0.004828 0.150606 0.600000 0.006285 0.179122 0.650000 0.007969 0.210081 0.700000 0.009874 0.243474

ANMF.CH09_4PP.indd 726

3/1/2023 2:56:04 PM

Direct Numerical Integration Methods • 727

0.750000 0.011993 0.279292 0.800000 0.014309 0.317526 0.850000 0.016805 0.358166 0.900000 0.019458 0.401200 0.950000 0.022246 0.446618 1.000000 0.025144 0.494409 2

DOF-1 DOF-2

1.8 1.6

displacement(m)

1.4 1.2 1 0.8 0.6 0.4 0.2 0

0

0.2

0.4

0.6

0.8

1 time(s)

1.2

1.4

1.6

1.8

2

FIGURE 9.20  MATLAB output.

EXAMPLE 9.14 Solve Example 9.11 using the Houbolt method. Solution: %MATLAB Program K = [21 −1;−1 1]; M = [1 0;0 10];

ANMF.CH09_4PP.indd 727

3/1/2023 2:56:04 PM

728 • Applied Numerical Methods Using MATLAB C = [0.5 −0.1;−0.1 0.1]; dt = 0.05;T = 2; X0 = [0;0];X0d = [0;0];F = [0;10]; t = [0:dt:T]; X(:,2) = X0; X0dd = inv(M)*(F−C*X0d−K*X0); % USING CENTRAL DIFFERENCE METHOD TO OBTAIN PREVIOUS 3 VALUES Xprev = X0 − (dt*X0d) + ((dt^2)*(X0dd/2)); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = (a0*M) + (a1*C); kbar = (K−a2*M); cbar = (a0*M−a1*C); X(:,1) = X0; Fbar = F−kbar*X0−cbar*Xprev; X(:,2) = inv(mbar)*Fbar; Fbar = F−kbar*X(:,2)−cbar*X0; X(:,3) = inv(mbar)*Fbar; % HOUBOLT METHOD BEGINS a0 = 2/(dt^2);a1 = 11/(6*dt);a2 = 5/(dt^2);a3 = 3/dt;a4 = −2*a0; a5 = −a3/2;a6 = a0/2;a7 = a3/9; Kb = K+a0*M+a1*C; p = 3; for i=3:length(t) F=[0;10];% F(t+2dt) Fb = F+M*(a2*X(:,i) + a4*X(:,i−1)+a6*X(:,i−2))+ C*(a3*X(:,i)+a5*X(:,i−1)+ a7*X(:,i−2));

ANMF.CH09_4PP.indd 728

3/1/2023 2:56:04 PM

Direct Numerical Integration Methods • 729

X(:,i+1) = inv(Kb)*Fb; Xdd(:,i+1) = a0*X(:,i+1)−a2*X(:,i)−a4*X(:,i−1)−a6*X(:,i−2); Xd(:,i+1) = a1*X(:,i+1)−a3*X(:,i)−a5*X(:,i−1)−a7*X(:,i−2); p = p + 1; end fprintf(‘\ntime\t\tX1\t\tX2\n’); for i = 1:p time(i)=(i−1)*dt; fprintf(‘%f\t%f\t%f\n’,time(i),X(1,i),X(2,i)) end plot(time,X(1,:),‘−p’,time,X(2,:),‘−*’); grid on; xlabel(‘Time(s)’); ylabel(‘displacement (m)’); legend(‘DOF-1’,‘DOF-2’); Output: Time X1 X2 0.000000 0.000000 0.000000 0.050000

−0.000000 0.001250

0.100000 0.000015 0.004998 0.150000 0.000069 0.011243 0.200000 0.000184 0.019980 0.250000 0.000387 0.031207 0.300000 0.000704 0.044920 0.350000 0.001161 0.061114 0.400000 0.001782 0.079784

ANMF.CH09_4PP.indd 729

3/1/2023 2:56:04 PM

730 • Applied Numerical Methods Using MATLAB 0.450000 0.002588 0.100924 0.500000 0.003595 0.124528 0.550000 0.004817 0.150590 0.600000 0.006260 0.179102 0.650000 0.007927 0.210055 0.700000 0.009814 0.243443 0.750000 0.011912 0.279255 0.800000 0.014208 0.317483 0.850000 0.016686 0.358115 0.900000 0.019325 0.401142 0.950000 0.022104 0.446553 1.000000 0.025000 0.494334 Figure 9.21 shows the plot of histories of two degrees of freedom. 2.5

DOF-1 DOF-2

displacement (m)

2

1.5

1

0.5

0

-0.5

0

0.5

1

time(s)

1.5

2

2.5

FIGURE 9.21  MATLAB output (Houbolt’s method).

ANMF.CH09_4PP.indd 730

3/1/2023 2:56:05 PM

Direct Numerical Integration Methods • 731

EXAMPLE 9.15 Solve Example 9.11 by using the Wilson theta method. Solution: %MATLAB Program K = [21 −1;−1 1]; M = [1 0;0 10]; C = [0.5 −0.1;−0.1 0.1]; dt = 0.05;T = 2; X0 = [0;0];X0d = [0;0];F0 = [0;10]; X0dd = inv(M)*(F0−C*X0d−K*X0); theta = 1.4; a0 = 6/(theta*dt)^2;a1 = 3/(theta*dt);a2 = 2*a1; a3 = 2;a4 = (1/2*theta*dt);a5 = −a2/theta; a6 = 1−3/theta; a7 = dt/2;a8 = dt^2/6; Kb = K+a0*M+a1*C; i = 1; X(:,1) = X0;Xd(:,1) = X0d;Xdd(:,1) = X0dd;t = 0; fprintf(‘time(s)\t\tX1\t\tX2\n’); fprintf(‘%f\t%f\t%f\n’,t,X(1,1),X(2,1)); for t = dt:dt:T i = i + 1; F = [0;10]; Ftb=F0+M*(a0*X(:,i−1)+a2*Xd(:,i−1)+a3*Xdd(:,i−1))+C*(a1*X(:,i−1) +a3*Xd(:,i−1)+a4*Xdd(:,i−1))+theta*(F−F0); Xt(:,i) = inv(Kb)*Ftb;

ANMF.CH09_4PP.indd 731

3/1/2023 2:56:05 PM

732 • Applied Numerical Methods Using MATLAB Xdd(:,i) = (a0/theta)*(Xt(:,i)−X(:,i−1))+a5*Xd(:,i−1)+a6*Xdd(:,i−1); Xd(:,i) = Xd(:,i−1)+a7*(Xdd(:,i)+Xdd(:,i−1)); X(:,i) = X(:,i−1)+dt*Xd(:,i-1)+a8*(Xdd(:,i)+2*Xdd(:,i−1)); F0 = F; fprintf(‘%f\t%f\t%f\n’,t,X(1,i),X(2,i)); end t = [0:dt:T]; plot(t,X(1,:),‘−p’,t,X(2,:),‘−*’) xlabel(‘Time(s)’); ylabel(‘displacement(m)’); legend(‘DOF-1’,‘DOF-2’); grid on; Output: Time(s) X1

X2

0.000000 0.000000 0.000000 0.050000 0.000003 0.001250 0.100000 0.000022 0.004998 0.150000 0.000079 0.011242 0.200000 0.000197 0.019979 0.250000 0.000400 0.031206 0.300000 0.000717 0.044919 0.350000 0.001173 0.061113 0.400000 0.001793 0.079784 0.450000 0.002598 0.100926

ANMF.CH09_4PP.indd 732

3/1/2023 2:56:05 PM

Direct Numerical Integration Methods • 733

0.500000 0.003607 0.124531 0.550000 0.004831 0.150595 0.600000 0.006279 0.179109 0.650000 0.007952 0.210065 0.700000 0.009847 0.243455 0.750000 0.011954 0.279271 0.800000 0.014259 0.317502 0.850000 0.016745 0.358138 0.900000 0.019392 0.401169 0.950000 0.022176 0.446584 1.000000 0.025074 0.494370 1.050000 0.028063 0.544516 1.100000 0.031122 0.597008 1.150000 0.034233 0.651833 1.200000 0.037379 0.708978 1.250000 0.040549 0.768426 1.300000 0.043738 0.830164 1.350000 0.046942 0.894174 1.400000 0.050165 0.960442 1.450000 0.053413 1.028950 1.500000 0.056697 1.099681

ANMF.CH09_4PP.indd 733

3/1/2023 2:56:05 PM

734 • Applied Numerical Methods Using MATLAB

2

DOF-1 DOF-2

1.8 1.6

displacement(m)

1.4 1.2 1 0.8 0.6 0.4 0.2 0

0

0.2

0.4

0.6

0.8

1 time(s)

1.2

1.4

1.6

1.8

2

FIGURE 9.22  MATLAB response (Wilson-theta).

EXAMPLE 9.16 Solve Example 9.11 by using the Newmark beta method. Solution: Table 9.7  The Newmark scheme. • un+1 = un + hvn + h2(1/2−β)an + h2βan+1 • vn+1 = vn + h(1−γ)an + hγan+1 • Man+1 + Cvn+1 + Kun+1 = Fn+1

Here for β = 0.5 and γ = 1/6, the following values are obtained. %MATLAB Program K = [21 −1;−1 1]; M = [1 0; 0 10]; C = [0.5 −0.1;−0.1 0.1]; dt = 0.05;T = 2;

ANMF.CH09_4PP.indd 734

3/1/2023 2:56:05 PM

Direct Numerical Integration Methods • 735

X0 = [0;0];X0d = [0;0];F = [0;10]; X0dd = inv(M)*(F−C*X0d−K*X0); beta = 0.5;gama = 1/6;%0.25* (0.5+beta); a0 = 1/(beta*dt^2); a1 = gama/ (beta*dt); a2 = 1/(beta*dt); a3 = (1/2*beta)−1; a4 = (gama/beta−1); a5 = 0.5*(gama/beta−2)*dt; a6 = dt*(1−beta);a7 = beta*dt; Kb = K+a0*M+a1*C; i = 1; X(:,1) = X0;Xd(:,1) = X0d;Xdd(:,1) = X0dd;t = 0; fprintf(‘time(s)\t\tX1\t\tX2\n’); fprintf(‘%f\t%f\t%f\n’,t,X(1,1),X(2,1)); for t = dt:dt:T i = i + 1; F = [0;10]; Fb = F+M*(a0*X(:,i−1)+a2*Xd(:,i−1)+a3*Xdd(:,i−1))+C*(a1*X(:,i−1)+ a4*Xd(:,i−1)+a5*Xdd(:,i−1)); X(:,i) = inv(Kb)*Fb; Xdd(:,i) = a0*(X(:,i)−X(:,i−1))−a2*Xd(:,i−1)−a3*Xdd(:,i−1); Xd(:,i) = a1*(X(:,i)−X(:,i−1))−a4*Xd(:,i−1)−a5*Xdd(:,i−1); fprintf(‘%f\t%f\t%f\n’,t,X(1,i),X(2,i)); end t = [0:dt:T]; plot(t,X(1,:),‘−p’,t,X(2,:),‘−*’) xlabel(‘Time(s)’); ylabel(‘displacement(m)’); legend(‘DOF-1’,‘DOF-2’); grid on;

ANMF.CH09_4PP.indd 735

3/1/2023 2:56:05 PM

736 • Applied Numerical Methods Using MATLAB Output: Time(s) X1

X2

0.000000 0.000000 0.000000 0.050000 0.000005 0.000312 0.100000 0.000017 0.002654 0.150000 0.000045 0.007027 0.200000 0.000102 0.013427 0.250000 0.000205 0.021853 0.300000 0.000372 0.032303 0.350000 0.000622 0.044774 0.400000 0.000976 0.059264 0.450000 0.001454 0.075768 0.500000 0.002075 0.094284 0.550000 0.002854 0.114806 0.600000 0.003808 0.137330 0.650000 0.004947 0.161851 0.700000 0.006278 0.188365 0.750000 0.007804 0.216864 0.800000 0.009526 0.247345 0.850000 0.011437 0.279799 0.900000 0.013529 0.314220 0.950000 0.015790 0.350601 1.000000 0.018203 0.388935 Figure 9.23 shows the plot of histories of two degrees of freedom.

ANMF.CH09_4PP.indd 736

3/1/2023 2:56:05 PM

Direct Numerical Integration Methods • 737

1.8

DOF-1 DOF-2

1.6 1.4

displacement(m)

1.2 1 0.8 0.6 0.4 0.2 0

0

0.2

0.4

0.6

0.8

1 time(s)

1.2

1.4

1.6

1.8

2

FIGURE 9.23  MATLAB output-Newmark method.

EXAMPLE 9.17 Solve Example 9.11 by using the Park Stiffly stable method. Solution: %MATLAB Program K = [21 −1;−1 1]; M = [1 0;0 10]; C = [0.5 −0.1;−0.1 0.1]; dt = 0.05;T = 2; X0 = [0;0];X0d = [0;0];F = [0;10]; t = [0:dt:T]; X(:,2) =X0; X0dd = inv(M)*(F−C*X0d−K*X0);

ANMF.CH09_4PP.indd 737

3/1/2023 2:56:05 PM

738 • Applied Numerical Methods Using MATLAB % USING CENTRAL DIFFERENCE METHOD TO OBTAIN PREVIOUS 3 VALUES of X AND Xd Xprev = X0−(dt*X0d)+((dt^2)*(X0dd/2)); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = (a0*M) + (a1*C); kbar = (K−a2*M); cbar = (a0*M−a1*C); X(:,1) = X0;Xd(:,1) = X0d; Fbar = F−kbar*X0−cbar*Xprev; X(:,2) = inv(mbar)*Fbar; Fbar = F−kbar*X(:,2)−cbar*X0; X(:,3) = inv(mbar)*Fbar; Fbar = F−kbar*X(:,3)−cbar*X0; X(:,4) = inv(mbar)*Fbar; Xd(:,2) = a1*(X(:,3)−X(:,1)); Xd(:,3) = a1*(X(:,4)−X(:,2)); % PARK METHOD BEGINS a0 = 10/(6*dt);a1 = −15/(6*dt);a2 = 1/dt;a3 = −1/(6*dt); Kb = K + (a0^2)*M−a0*C; p = 3; for i = 3:length(t) F = [0;10];% F(t+3dt) mass = M*(−a1*Xd(:,i)−a2*Xd(:,i−1)−a3*Xd(:,i−2)−(a0*a1)*X(:,i)−(a0*a2)*X (:,i−1)+(a3^2)*X(:,i−2)); damp = C*(a1*X(:,i)+a2*X(:,i−1)+a3*X(:,i−2)); Fb = F + mass-damp;

ANMF.CH09_4PP.indd 738

3/1/2023 2:56:05 PM

Direct Numerical Integration Methods • 739

X(:,i+1) = inv(Kb)*Fb; Xd(:,i+1) = a0*X(:,i+1)+a1*X(:,i)+a2*X(:,i−1)+a3*X(:,i−2); Xdd(:,i+1) = a0*Xd(:,i+1)+a1*Xd(:,i)+a2*Xd(:,i−1)+a3*Xd(:,i−2); p = p + 1; end fprintf(‘\ntime\t\tX1\t\tX2\n’); for i = 1:p time(i)=(i-1)*dt; fprintf(‘%f\t%f\t%f\n’,time(i),X(1,i),X(2,i)) end plot(time,X(1,:),‘−p’,time,X(2,:),‘−*’); grid on; xlabel(‘Time(s)’); ylabel(‘displacement (m)’); legend(‘DOF-1’,‘DOF-2’); Output: Time X1 X2 0.000000 0.000000 0.000000 0.050000

−0.000000 0.001250

0.100000 0.000015 0.004998 0.150000 0.000010 0.010911

ANMF.CH09_4PP.indd 739

0.200000

−0.000103 0.017760

0.250000

−0.000428 0.024395

0.300000

−0.001067 0.029618

3/1/2023 2:56:05 PM

740 • Applied Numerical Methods Using MATLAB Figure 9.24 shows the MATLAB plot. 10

DOF-1 DOF-2

8 6

displacement (m)

4 2 0 -2 -4 -6 -8 -10

0

0.5

1

time(s)

1.5

2

2.5

FIGURE 9.24  MATLAB output.

EXAMPLE 9.18 The numerical values of the mass, damping and stiffness are chosen as M1 = 1, M2 = 10, C1 = 0, C2 = 0.15, K1 = 19 and K2 = 1. The initial conditions are selected as zero and the forcing vector is F1 (t)  0         for t > 0 F2 (t) 5 

and



F1 (t)  0         for t < 0 F2 (t) 0 

In matrix motion, these equations may be written as   C  C  C   X  M1 0   X 1 1 2 2 1            C2 C2   X 2  0 M2   X 2  K1  K 2     K 2

ANMF.CH09_4PP.indd 740

K 2   X 1  0.5K 2 (X 2  X 1 )3  F1      K 2   X 2   0.5K2 (X 2  X 1 )3  F2 

3/1/2023 2:56:12 PM

Direct Numerical Integration Methods • 741

Take Δt = 0.05 sec. Use the central difference method and compute the response of the system. Solution: Substituting the given values, the following matrices are obtained: 1 0  M=  ,C= 0 10 

 0.15 0.15   0.15 0.15  , K =  

 20 1  1 1  , F =  

0    5

%MATLAB Program % INITIAL VALUES M = [1 0;0 10]; K = [20 −1;−1 1]; C = [0.15 −0.15;−0.15 0.15]; dt = 0.05; x0 = [0;0];x0d = [0;0]; F0 = [0;5]; T = 2; x0dd = inv(M)*(F0−C*x0d−K*x0); xprev = x0−(dt*x0d)+((dt^2)*(x0dd/2)); a0 = 1/dt^2;a1 = 1/(2*dt);a2 = 2*a0; mbar = (a0*M) + (a1*C); t = 0; v(:,1) = x0d;a(:,1) = x0dd; i = 1; fprintf(‘time\t\tX(1)\t\tX(2)\n’); for t = 0:dt:T+dt X(:,i) = x0; % NONLINEAR SPRING

ANMF.CH09_4PP.indd 741

3/1/2023 2:56:19 PM

742 • Applied Numerical Methods Using MATLAB Fr = [−0.5*1*(X(2,i)−X(1,i))^3;0.5*1*(X(2,i)−X(1,i))^3]; F = F0 + Fr; Fbar = F+(a2*M−K)*x0+(a1*C−a0*M)*xprev; x = inv(mbar)*Fbar; xprev = x0; x0 = x; fprintf(‘%f\t%f\t%f\n’,t,X(1,i),X(2,i)); i = i + 1; p = i; end for i = 2:p−1 if i> (or EDU >>), which indicates that MATLAB is waiting for a command to be entered. In order to quit MATLAB, type quit or exit after the prompt, followed by pressing the enter or return key. 10.1.2

Display Windows

MATLAB has three display windows. They are 1. A Command Window which is used to enter commands and data to display plots and graphs 2. A Graphics Window which is used to display plots and graphs 3. An Edit Window which is used to create and modify M-files. M-files are files that contain a program or script of MATLAB commands 10.1.3

Entering Commands

Every command has to be followed by a carriage return 〈cr〉 (enter key) in order that the command can be executed. MATLAB commands are case sensitive and lower case letters are used throughout.

ANMF.CH10_4PP.indd 758

3/1/2023 2:58:35 PM

MATLAB Basics • 759

To execute an M-file (such as Project_1.m), simply enter the name of the file without its extension (as in Project_1). 10.1.4

MATLAB Expo

In order to see some of the MATLAB capabilities, enter the demo command. This will initiate the MATLAB EXPO. MATLAB Expo is a graphical demonstration environment that shows some of the different types of operations which can be conducted with MATLAB. 10.1.5 Abort In order to abort a command in MATLAB, hold down the control key and press c to generate a local abort with MATLAB. 10.1.6

The Semicolon (;)

If a semicolon (;) is typed at the end of a command the output of the command is not displayed. 10.1.7

Typing %

When the percent symbol (%) is typed in the beginning of a line, the line is designated as a comment. When the enter key is pressed, the line is not executed. 10.1.8

The clc Command

Typing the clc command and pressing enter cleans the command window. Once the clc command is executed, a clear window is displayed. 10.1.9 Help MATLAB has a host of built-in functions. For a complete list, refer to MATLAB user’s guide or refer to the on-line help. To obtain help on a particular topic in the list, for example, inverse, type help inv. 10.1.10 Statements and Variables Statements have the form >> variable = expression

ANMF.CH10_4PP.indd 759

3/1/2023 2:58:35 PM

760 • Applied Numerical Methods Using MATLAB The equals (“=”) sign implies the assignment of the expression to the ­variable. For instance, to enter a 2 × 2 matrix with a variable name A, we write >> A = = [1 2 ; 3 4] 〈ret〉 The statement is executed after the carriage return (or enter) key is pressed to display A = 1 2 3 4

10.2 ARITHMETIC OPERATIONS The symbols for arithmetic operations with scalars are summarized in Table 10.1. TABLE 10.1 Arithmetic operation Addition Subtraction Multiplication Right division Left division Exponentiation

Symbol + − ∗ / \ ^

Example 6+3=9 6−3=3 6 ∗ 3 = 18 6/3=2 6 \ 3 = 3 / 6 = 1/2 6 ^ 3 (63 = 216)

10.3 DISPLAY FORMATS MATLAB has several different screen output formats for displaying numbers. These formats can be found by typing the help command: help format in the Command Window. A few of these formats are shown in Table 10.2 for 2π. TABLE 10.2  Display formats. Command

Description

Example

format short

Fixed-point with 4 decimal digits

>> 351/7 ans = 50.1429

format long

Fixed-point with 14 decimal digits

>> 351/7 ans = 50.14285714285715

format short e

Scientific notation with 4 decimal digits

>> 351/7 ans = 5.0143e + 001 (continued)

ANMF.CH10_4PP.indd 760

3/1/2023 2:58:35 PM

MATLAB Basics • 761

TABLE 10.2  Display formats. (continued) Command

Description

Example

format long e

Scientific notation with 15 decimal digits

>> 351/7 ans = 5.014285714285715e001

format short g

Best of 5 digit fixed or floating point

>> 351/7 ans = 50.143

format long g

Best of 15 digit fixed or floating point

>> 351/7 ans = 50.1428571428571

format bank

Two decimal digits

>> 351/7 ans = 50.14

format compact

Eliminates empty lines to allow more lines with information displayed on the screen

format loose

Adds empty lines (opposite of compact)

10.4 ELEMENTARY MATH BUILT-IN FUNCTIONS MATLAB contains a number of functions for performing computations which require the use of logarithms, elementary math functions, and trigonometric math functions. A list of these commonly used elementary MATLAB mathematical built-in functions are given in Tables 10.3−10.8. TABLE 10.3  Common math functions. Function

Description

abs(x) sqrt(x) round(x) fix(x) floor(x) ceil(x) sign(x)

exp(x)

Computes the absolute value of x. Computes the square root of x. Rounds x to the nearest integer. Rounds (or truncates) x to the nearest integer toward 0. Rounds x to the nearest integer toward −∞. Rounds x to the nearest integer toward ∞. Returns a value of −1 if x is less than 0, a value of 0 if x equals 0, and a value of 1 otherwise. Returns the remainder of x/y. for example, rem(25, 4) is 1, and rem(100, 21) is 16. This function is also called a modulus function. Computes ex, where e is the base for natural logarithms, or approximately 2.718282.

log(x) log10(x)

Computes ln x, the natural logarithm of x to the base e. Computes log10 x, the common logarithm of x to the base 10.

rem(x,y)

ANMF.CH10_4PP.indd 761

3/1/2023 2:58:35 PM

762 • Applied Numerical Methods Using MATLAB

TABLE 10.4  Exponential functions. Function

Description

exp(x) log(x) log10(x) sqrt(x)

Exponential (ex) Natural logarithm Base 10 logarithm Square root

TABLE 10.5  Trigonometric and hyperbolic functions. Function

Description

sin(x)

Computes the sine of x, where x is in radians.

cos(x)

Computes the cosine of x, where x is in radians.

tan(x)

Computes the tangent of x, where x is in radians.

asin(x)

Computes the arcsine or inverse sine of x, where x must be between −1 and 1. The function returns an angle in radians between −p/2 and p/2.

acos(x)

Computes the arccosine or inverse cosine of x, where x must be between −1 and 1. The function returns an angle in radians between 0 and p.

atan(x)

Computes the arctangent or inverse tangent of x. The function returns an angle in radians between −p/2 and p/2.

atan2(y,x)

Computes the arctangent or inverse tangent of the value y/x. The function returns an angle in radians that will be between −p and p, depending on the signs of x and y.

sinh(x)

x −x Computes the hyperbolic sine of x, which is equal to e − e . 2

cosh(x)

x −x Computes the hyperbolic cosine of x, which is equal to e + e . 2

tanh(x) asinh(x)

acosh(x)

Computes the hyperbolic tangent of x, which is equal to

sinh x . cosh x

(

)

Computes the inverse hyperbolic sine of x, which is equal to ln x + x 2 + 1 . Computes the inverse hyperbolic cosine of x, which is equal to

(

)

ln x + x 2 − 1 . atanh(x)

ANMF.CH10_4PP.indd 762

1+ x Computes the inverse hyperbolic tangent of x, which is equal to ln 1−x for |x| ≤ 1.

3/1/2023 2:58:55 PM

MATLAB Basics • 763

TABLE 10.6  Round-off functions. Function

Description

Example

round(x)

Round to the nearest integer

>> round(20/6) ans = 3

fix(x)

Round towards zero

>> fix(13/6) ans = 2

ceil(x)

Round towards infinity

>> ceil(13/5) ans = 3

floor(x)

Round towards minus infinity

>> floor(−10/4) ans = −3

rem(x, y)

Returns the remainder after x is divided by y

>> rem(14,3) ans = 2

sign(x, y)

Signum function. Returns 1 if x > 0, −1 if x < 0, and 0 if x = 0.

>> sign(7) ans = 1

TABLE 10.7  Complex number functions. Function

Description

conj(x)

Computes the complex conjugate of the complex number x. Thus, if x is equal to a + i b, then conj(x) will be equal to a − i b. Computes the real portion of the complex number x. Computes the imaginary portion of the complex number x. Computes the absolute value of magnitude of the complex number x. Computes the angle using the value of atan2(imag(x), real(x)); thus, the angle value is between −p and p.

real(x) imag(x) abs(x) angle(x)

TABLE 10.8  Arithmetic operations with complex numbers. Operation

Result

c1 + c2

(a1 + a2) + i(b1 + b2)

c1 + c2

(a1 − a2) + i(b1 − b2)

c1 • c2

(a1a2 − b1b2) + i(a1b2 − a2b1)

c1 c2

 a1 a2 + b1 b2   a2 b1 − b2 a1   a2 + b2  + i  a2 + b2  2 2 2 2

|c1| c1∗

a12 + b12 (magnitude or absolute value of c1) a1 − ib1 (conjugate of c1)

(Assume that c1 = a1 + ib1 and c2 = a2 + ib2.)

ANMF.CH10_4PP.indd 763

3/1/2023 2:59:01 PM

764 • Applied Numerical Methods Using MATLAB

10.5 VARIABLE NAMES A variable is a name made of a letter or a combination of several letters and digits. Variable names can be up to 63 (in MATLAB 7) characters long (31 characters on MATLAB 6.0). MATLAB is case sensitive. For instance, XX, Xx, xX, and xx are the names of four different variables. It should be noted here that not to use the names of built-in functions for variables. For instance, avoid using: sin, cos, exp, sqrt, and so on. Once a function name is used to define a variable, the function cannot be used.

10.6 PREDEFINED VARIABLES MATLAB includes a number of predefined variables. Some of the predefined variables that are available to use in MATLAB programs are summarized in Table 10.9. TABLE 10.9  Predefined variables. Predefined variable in MATLAB ans pi eps inf i j NaN clock date

Description Represents a value computed by an expression, but not stored in variable name. Represents the number π. Represents the floating-point precision for the computer being used. This is the smallest difference between two numbers. Represents infinity, that for instance occurs as a result of a division by zero. A warning message will be displayed or the value will be printed as ∞. Defined as −1, that is: 0 + 1.0000i. Same as i. Stands for “Not a Number.” Typically occurs as a result of an expression being undefined, as in the case of division of zero by zero. Represents the current time in a six-element row vector containing year, month, day, hour, minute, and seconds. Represents the current date in a character string format.

10.7 COMMANDS FOR MANAGING VARIABLES Table 10.10 lists commands that can be used to eliminate variables or to obtain information about variables that have been created. The procedure is to enter the command in the Command Window and press the enter key.

ANMF.CH10_4PP.indd 764

3/1/2023 2:59:02 PM

MATLAB Basics • 765

TABLE 10.10  Commands for managing variables. Command

Description

clear clear x, y, z who whos

Removes all variables from the memory. Clears/removes only variables x, y, and z from the memory. Lists the variables currently in the workspace. Displays a list of the variables currently in the memory and their size together with information about their bytes and class.

10.8 GENERAL COMMANDS Tables 10.11 to 10.15 show useful general commands on online help, workspace information, directory information, and general information. TABLE 10.11  On-line help. Function

Description

help helpwin helpdesk help topic lookfor string demo

Lists topics on which help is available. Opens the interactive help window. Opens the web-browser-based help facility. Provides help on topic. Lists help topics containing string. Runs the demo program. TABLE 10.12  Workspace information.

ANMF.CH10_4PP.indd 765

Function

Description

who whos what clear clear x y z clear all mlock fun munlock fun clc home clf

Lists variables currently in the workspace. Lists variables currently in the workspace with their size. Lists m-, mat-, and mex-files on the disk. Clears the workspace; all variables are removed. Clears only variables x, y, and z. Clears all variables and functions from workspace. Locks function fun so that clear cannot remove it. Unlocks function fun so that clear can remove it. Clears command window, command history is lost. Same as clc. Clears figure window.

3/1/2023 2:59:02 PM

766 • Applied Numerical Methods Using MATLAB

TABLE 10.13  Directory information. Function

Description

pwd cd dir ls path editpath copyfile mkdir

Shows the current working directory. Changes the current working directory. Lists contents of the current directory. Lists contents of the current directory, same as dir. Gets or sets MATLAB search path. Modifies MATLAB search path. Copies a file. Creates a directory. TABLE 10.14  General information.

Function

Description

computer clock date more ver

Tells you the computer type being used. Gives wall-clock time and date as a vector. Displays the date as a string. Controls the paged output according to the screen size. Shows the license and the version information about MATLAB installed on your computer. Benchmarks your computer on running MATLAB compared to other computers.

bench

TABLE 10.15  Termination. Function

Description

c (Control-c) quit exit

Local abort, kills the current command execution. Quits MATLAB. Same as quit.

10.9 ARRAYS An array is a list of numbers arranged in rows and/or columns. A one-­ dimensional array is a row or a column of numbers, and a two-dimensional array has a set of numbers arranged in rows and columns. An array operation is performed element-by-element. 10.9.1

Row Vector

A vector is a row or column of elements.

ANMF.CH10_4PP.indd 766

3/1/2023 2:59:02 PM

MATLAB Basics • 767

In a row vector the elements are entered with a space or a comma between the elements inside the square brackets. For example, x = [7  −1 2 −5 8] 10.9.2

Column Vector

In a column vector the elements are entered with a semicolon between the elements inside the square brackets. For example, x = [7;  −1; 2; −5; 8] 10.9.3 Matrix A matrix is a two-dimensional array that has numbers in rows and columns. A matrix is entered row-wise with consecutive elements of a row separated by a space or a comma, and the rows separated by semicolons or carriage returns. The entire matrix is enclosed within square brackets. The elements of  the matrix may be real numbers or complex numbers. For example, to enter the matrix, 1 3 4  A    0 2 8  The MATLAB input command is A = [1  3  −4 ; 0  −2 8] Similarly for complex number elements of a matrix B

 −5 x ln 2 x + 7 sin 3 y B=   5 − 13 i  3i 

The MATLAB input command is B = [−5∗x log(2∗x) + 7∗sin(3∗y) ; 3i 5 − 13i] 10.9.4

Addressing Arrays

A colon can be used in MATLAB to address a range of elements in a vector or a matrix. 10.9.4.1 Colon for a vector Va(:) − refers to all the elements of the vector Va (either a row or a column vector).

ANMF.CH10_4PP.indd 767

3/1/2023 2:59:05 PM

768 • Applied Numerical Methods Using MATLAB Va(m:n) − refers to elements m through n of the vector Va. For instance >> V = [2  5  −1 11 8 4 7 −3 11] >> u = V (2:8) u = 5  −1 11 8 4 7 −3 11 10.9.4.2 Colon for a matrix Table 10.16 gives the use of a colon in addressing arrays in a matrix. TABLE 10.16  Colon use for a matrix.

10.9.5

Command

Description

A(:, n) A(n, :) A(:, m:n) A(m:n, :) A(m:n, p:q)

Refers to the elements in all the rows of a column n of the matrix A. Refers to the elements in all the columns of row n of the matrix A. Refers to the elements in all the rows between columns m and n of the matrix A. Refers to the elements in all the columns between rows m and n of the matrix A. Refers to the elements in rows m through n and columns p through q of the matrix A.

Adding Elements to a Vector or a Matrix

A variable that exists as a vector, or a matrix, can be changed by ding elements to it. Addition of elements is done by assigning values of the additional elements, or by appending existing variables. Rows and/or columns can be added to an existing matrix by assigning values to the new rows or columns. 10.9.6

Deleting Elements

An element, or a range of elements, of an existing variable can be deleted by reassigning blanks to these elements. This is done simply by the use of square brackets with nothing typed in between them. 10.9.7

Built-in Functions

Some of the built-in functions available in MATLAB for managing and handling arrays are listed in Table 10.17.

ANMF.CH10_4PP.indd 768

3/1/2023 2:59:05 PM

MATLAB Basics • 769

TABLE 10.17  Built-in functions for handling arrays.

ANMF.CH10_4PP.indd 769

Function

Description

Example

length(A)

Returns the number of elements in the vector A.

>> A = [5 9 2 4]; >> length(A) ans = 4

size(A)

Returns a row vector [m, n], where m and n are the size m × n of the array A.

>>A = [2 3 0 8 11 ; 6 17 5 7 1] A= 2 3 0 8 11 6 17 5  7  1 >> size(A) ans = 2 5

reshape (A, m, n)

Rearrange a matrix A that has r rows and s columns to have m rows and n columns. r times s must be equal to m times n.

>> A = [3 1 4 ; 9 0 7] A= 314 907 >> B = reshape(A, 3, 2) B= 3 0 9 4 1 7

diag(v)

When v is a vector, creates a square matrix with the elements of v in the diagonal

>> v = [3 2 1]; >> A = diag(v) A= 3 0 0 0 2 0 0 0 1

diag(A)

When A is a matrix, creates a vector from the diagonal elements of A.

>> A = [1 8 3 ; 4 2 6 ; 7 8 3] A= 1 8 3 4 2 6 7 8 3 >> vec = diag(A) vec = 1 2 3

3/1/2023 2:59:05 PM

770 • Applied Numerical Methods Using MATLAB

10.10 OPERATIONS WITH ARRAYS We consider here matrices that have more than one row and more than one column. 10.10.1 Addition and Subtraction of Matrices The addition (the sum) or the subtraction (the difference) of the two arrays is obtained by adding or subtracting their corresponding elements. These operations are performed with arrays of identical size (same number of rows and columns). For example, if A and B are two arrays (2 × 3 matrices):  a11 a12 a13   b11 b12 b13  A= and B =     a21 a22 a23   b21 b22 b23  Then, the matrix addition (A + B) is obtained by adding A and B is



 a11 + b11 a + b 21  21

a12 + b12 a22 + b22

a13 + b13  a23 + b23 

10.10.2 Dot Product The dot product is a scalar computed from two vectors of the same size. The scalar is the sum of the products of the values in corresponding positions in the vectors. For n elements in the vectors A and B:

dot product = A ⋅ B =

n

∑a i =1

i

bi

dot(A, B) Computes the dot product of A and B. If A and B are matrices, the dot product is a row vector containing the dot products for the corresponding columns of A and B. 10.10.3 Array Multiplication The value in position ci,j of the product C of two matrices, A and B, is the dot product of row i of the first matrix and column of the second matrix: n



ci, j = ∑ ai, k bk, j k =1

ANMF.CH10_4PP.indd 770

3/1/2023 2:59:16 PM

MATLAB Basics • 771

10.10.4 Array Division The division operation can be explained by means of the identity matrix and the inverse matrix operation. 10.10.5 Identity Matrix An identity matrix is a square matrix in which all the diagonal elements are 1’s, and the remaining elements are 0’s. If a matrix A is square, then it can be multiplied by the identity matrix, I, from the left or from the right: AI = IA = A 10.10.6 Inverse of a Matrix The matrix B is the inverse of the matrix A if when the two matrices are multiplied the product is the identity matrix. Both matrices A and B must be square and the order of multiplication can be AB or BA. AB = BA = I 10.10.7 Transpose The transpose of a matrix is a new matrix in which the rows of the original matrix are the columns of the new matrix. The transpose of a given matrix A is denoted by AT. In MATLAB, the transpose of the matrix A is denoted by A′. 10.10.8 Determinant A determinant is a scalar computed from the entries in a square matrix. For a 2 × 2 matrix A, the determinant is |A| = a11 a22 − a21 a12 MATLAB will compute the determinant of a matrix using the det function: det(A)  computes the determinant of a square matrix A. 10.10.9 Array Division MATLAB has two types of array division—the left division and the right division.

ANMF.CH10_4PP.indd 771

3/1/2023 2:59:16 PM

772 • Applied Numerical Methods Using MATLAB 10.10.10 Left Division The left division is used to solve the matrix equation Ax = B where x and B are column vectors. Multiplying both sides of this equation by the inverse of A, A−1, we have A−1Ax = A−1 B or

Ix = x = A−1 B

Hence

x = A−1 B

In MATLAB, the above equation is written by using the left division character: x = A \ B 10.10.11 Right Division The right division is used to solve the matrix equation xA = B where x and B are row vectors. Multiplying both sides of this equation by the inverse of A, A−1, we have xüAA−1 = BüA−1 or

x = BüA−1

In MATLAB, this equation is written by using the right division character: x = B / A

10.11 ELEMENT-BY-ELEMENT OPERATIONS Element-by-element operations can only be done with arrays of the same size. Element-by-element multiplication, division, and exponentiation of two vectors or matrices is entered in MATLAB by typing a period in front of the arithmetic operator. Table 10.18 lists these operations. TABLE 10.18  Element-by-element operations. Arithmetic operators

ANMF.CH10_4PP.indd 772

Matrix operators

Array operators

+ Addition − Subtraction ∗ Multiplication ^ Exponentiation / Left division \ Right division

+ Addition − Subtraction ü∗ Array multiplication ü^ Array exponentiation ü/ Array left division ü\ Array right division

3/1/2023 2:59:16 PM

MATLAB Basics • 773

10.11.1 Built-In Functions for Arrays Table 10.19 lists some of the many built-in functions available in MATLAB for analyzing arrays. TABLE 10.19  MATLAB built-in array functions. Function

Description

Example

mean(A)

If A is a vector, returns the mean value of the elements

>> A = [3 7 2 16]; >> mean(A) ans =    14

C = max(A)

If A is a vector, C is the largest element in A. If A is a matrix, C is a row vector containing the largest element of each column of A.

>> A = [3 7 2 16 9 5 18 13 0 4]; >> C = max(A) C=    18

[d, n] = max(A)

If A is a vector, d is the largest element in A, n is the position of the element (the first if several have the max value).

>> [d, n] = max(A) d=    18 n=    7

min(A)

The same as max(A), but for the smallest element.

[d, n] = min(A)

The same as [d, n] = max(A), but for the smallest element.

>> A = [3 7 2 16]; >> min(A) ans =    2

sum(A)

If A is a vector, returns the sum of the elements of the vector.

>> A = [3 7 2 16]; >> sum(A) ans =    28

sort(A)

If A is a vector, arranges the elements of the vector in ascending order.

>> A = [3 7 2 16]; >> sort(A) ans =    2 3 7 16

median(A)

If A is a vector, returns the median value of the elements of the vector.

>> A = [3 7 2 16]; >> median(A) ans =    5

std(A)

If A is a vector, returns the standard deviation of the elements of the vector.

>> A = [3 7 2 16]; >> std(A) ans =    6.3770 (continued)

ANMF.CH10_4PP.indd 773

3/1/2023 2:59:16 PM

774 • Applied Numerical Methods Using MATLAB

TABLE 10.19  MATLAB built-in array functions (continued) Function

Description

Example

det(A)

Returns the determinant of a square matrix A.

>> A = [1 2 ; 3 4]; >> det(A) ans = −2

dot(a, b)

Calculates the scalar (dot) product of two vectors a and b. The vector can each be row or column vectors.

>> a = [5 6 7];

cross(a, b)

Calculates the cross product of two vectors a and b, (a × b). The two vectors must have three elements

>> a = [5 6 7]; >> b = [4 3 2]; >> cross(a, b) ans =    −9  18  −9

inv(A)

Returns the inverse of a square matrix A.

>> a = [1 2 3; 4 6 8; −1 2 3]; >> inv(A) ans =    −0.5000  0.0000  −0.5000    −5.0000  1.5000   1.0000     3.5000 −1.0000  −0.5000

>> b = [4 3 2]; >> dot(a, b) ans =    52

10.12 RANDOM NUMBERS GENERATION There are many physical processes and engineering applications that require the use of random numbers in the development of a solution. MATLAB has two commands rand and rand n that can be used to assign random numbers to variables. The rand command: The rand command generates uniformly distributed over the interval [0, 1]. A seed value is used to initiate a random sequence of values. The seed value is initially set to zero. However, it can be changed with the seed function. The command can be used to assign these numbers to a scalar, a vector, or a matrix, as shown in Table 10.20.

ANMF.CH10_4PP.indd 774

3/1/2023 2:59:16 PM

MATLAB Basics • 775

TABLE 10.20  The rand command. Command

Description

Example

rand

Generates a single random number between 0 and 1.

>> rand ans =    0.9501

rand(1, n)

Generates an n elements row vector of random numbers between 0 and 1.

>> a = rand(1, 3)

rand(n)

Generates an n × n matrix with random numbers between 0 and 1.

>> b = rand(3) b=    0.7382 0.9355 0.8936    0.1763 0.9165 0.0579    0.4057 0.4103 0.3529

rand(m, n)

Generates an m × n matrix with random numbers between 0 and 1.

>> c = rand(2, 3) c=    0.2028 0.6038 0.1988    0.1987 0.2722 0.0153

randperm(n)

Generates a row vector with n elements that are random permutation of integers 1 through n.

>> randperm(7) ans =    5 2 4 7 1 6 3

a=    0.4565 0.0185 0.8214

10.12.1 The Random Command MATLAB will generate Gaussian values with a mean of zero and a variance of 1.0 if a normal distribution is specified. The MATLAB functions for generating Gaussian values are as follows: randn(n) Generates an n × n matrix containing Gaussian (or normal) random numbers with a mean of 0 and a variance of 1. Randn(m, n) Generates an m × n matrix containing Gaussian (or normal) random numbers with a mean of 0 and a variance of 1.

10.13 POLYNOMIALS A polynomial is a function of a single variable that can be expressed in the following form: f(x) = a0xn + a1xn−1 + a2xn−2 + … + an−1x1 + an

ANMF.CH10_4PP.indd 775

3/1/2023 2:59:16 PM

776 • Applied Numerical Methods Using MATLAB where the variable is x and the coefficients of the polynomial are represented by the values a0, a1, … and so on. The degree of a polynomial is equal to the largest value used as an exponent. A vector represents a polynomial in MATLAB. When entering the data in MATLAB, simply enter each coefficient of the polynomial into the vector in descending order. For example, consider the polynomial 5s5 + 7s4 + 2s2 − 6s + 10 To enter this into MATLAB, we enter this as a vector as >> x = [5 7 0  2 −6 10] x =

5 7 0 2 −6 10

It is necessary to enter the coefficients of all the terms. MATLAB contains functions that perform polynomial multiplication and division, which are listed below: conv(a, b)  Computes a coefficient vector that contains the coefficients of the product of polynomials represented by the coefficients in a and b. The vectors a and b do not have to be the same size. [q, r] = deconv(n, d) Returns two vectors. The first vector contains the coefficients of the quotient and the second vector contains the coefficients of the remainder polynomial. The MATLAB function for determining the roots of a polynomial is the roots function: root(a) Determines the roots of the polynomial represented by the coefficient vector a. The roots function returns a column vector containing the roots of the polynomial; the number of roots is equal to the degree of the polynomial. When the roots of a polynomial are known, the coefficients of the polynomial are determined when all the linear terms are multiplied, we can use the poly function: poly(r) Determines the coefficients of the polynomial whose roots are contained in the vector r.

ANMF.CH10_4PP.indd 776

3/1/2023 2:59:16 PM

MATLAB Basics • 777

The output of the function is a row vector containing the polynomial coefficients. The value of a polynomial can be computed using the polyval function, polyval (a, x). It evaluates a polynomial with coefficients a for the values in x. The result is a matrix the same size ad x. For instance, to find the value of the above polynomial at s = 2, >>x = polyval([5 7 0 2  −6  10], 2) x = 278 To find the roots of the above polynomial, we enter the command roots (a) which determines the roots of the polynomial represented by the coefficient vector a. >>roots([5 7 0 2 −6 10]) ans = −1.8652 −0.4641 + 1.0832i −0.4641 − 1.0832i 0.6967 + 0.5355i 0.6967 − 0.5355i % or >> x = [5 7 0 2 −6 10] x =

5 7 0 2 −6 10

>> r = roots(x) r = −1.8652 −0.4641 + 1.0832i −0.4641 − 1.0832i  0.6967 + 0.5355i  0.6967 − 0.5355i To multiply two polynomials together, we enter the command conv.

ANMF.CH10_4PP.indd 777

3/1/2023 2:59:16 PM

778 • Applied Numerical Methods Using MATLAB The polynomials are: x = 2x + 5 and y = x2 + 3x + 7 >> x = [2  5]; >> y = [1 3 7]; >> z = conv(x, y) z =

2 11 29 35

To divide two polynomials, we use the command deconv. z = [2 11 29 35]; x = [2 5] >> [g, t] = deconv (z, x) g = 1 3 7 t = 0 0 0 0

10.14 SYSTEM OF LINEAR EQUATIONS A system of equations is nonsingular if the matrix A containing the coefficients of the equations is nonsingular. A system of nonsingular simultaneous linear equations (AX = B) can be solved using two methods:

a) matrix division method



b) matrix inversion method

10.14.1 Matrix Division The solution to the matrix equation AX = B is obtained using matrix division, or X = A/B. The vector X then contains the values of x. 10.14.2 Matrix Inverse For the solution of the matrix equation AX = B, we pre-multiply both sides of the equation by A−1. A−1AX = A−1B or IX = A−1B where I is the identity matrix. Hence,

ANMF.CH10_4PP.indd 778

X = A−1B

3/1/2023 2:59:16 PM

MATLAB Basics • 779

In MATLAB, we use the command x = inv (A)∗B. Similarly, for XA = B, we use the command x = B∗inv (A). The basic computational unit in MATLAB is the matrix. A matrix expression is enclosed in square brackets, [ ]. Blanks or commas separate the column elements and semicolons or carriage returns separate the rows. >>A = [1 2 3 4 ; 5 6 7 8 ; 9 10 11 12] A = 1 2 3 4 5 6 7 8 9 10 11 12 The transpose of a simple matrix or a complex matrix is obtained by using the apostrophe key >>B = A′ B = 1 5 9 2 6 10 3 7 11 4 8 12 Matrix multiplication is accomplished as follows: >>C = A ∗ B C = 30 70 110 70 174 278 110 278 446 >>C = B ∗ A C =

ANMF.CH10_4PP.indd 779

107 122 137 152

122 140 158 176

137 158 179 200

152 176 200 224

3/1/2023 2:59:16 PM

780 • Applied Numerical Methods Using MATLAB The inverse of a matrix D is obtained as >>D = [1  2 ; 3  4] D = 1 2 3 4 >>E = inv (D) E = −2.0000 1.0000 1.5000   −0.5000 Similarly, its eigenvalue is >>eig (D) ans =   −0.3723   5.3723 Matrix operations require that the matrix dimensions be compatible. If A is an n × m and B is a p × r, then A ± B is allowed only if n = p and m = r. Similarly, matrix product A ∗ B is allowed only if m = p. EXAMPLE 10.1 Consider the two matrices:  1 0 1   A =  2 3 4  and B =  −1 6 7 

 7 4 2    3 5 6  −1 2 1 

Using MATLAB, determine the following: a) A + B b) AB c) A2 d) AT e) B−1 f) BTAT g) A2 + B2 − AB h) determinant of A, determinant of B, and determinant of AB.

ANMF.CH10_4PP.indd 780

3/1/2023 2:59:21 PM

MATLAB Basics • 781

Solution: >> A= [1 0 1; 2 3 4; −1 6 7]  A =  1  0  1  2  3  4 −1  6  7 >> B= [7 4 2; 3 5 6; −1 2 1]  B =  7  4  2  3  5  6 −1  2  1 a) >> C=A+B  C =  8  4  3  5  8  10 −2  8  8 b) >> D=A∗B  D =  6   6  3 19  31  26  4  40  41 c) >> E=A^2  E = 0   6   8 4  33  42 4  60  72 d) >> % Let F = transpose of A >> F=A’  F = 1  2  −1 0  3  6 1  4  7

ANMF.CH10_4PP.indd 781

3/1/2023 2:59:21 PM

782 • Applied Numerical Methods Using MATLAB e) >> H=inv (B)  H =  0.1111  0.0000  −0.2222  0.1429  −0.1429  0.5714 −0.1746  0.2857  −0.3651 f) >> J=B'∗A'  J = 6  19  4 6  31  40 3  26  41 g) >> K=A^2 + B^2 −A ∗ B  K = 53  52  45 15  51  58 −2  28  42 h) det (A) = 12 det (B) = −63 det (A∗B) = −756 EXAMPLE 10.2 Determine the eigenvalues and eigenvectors of A and B using MATLAB  4 2 −3    A =  −1 1 3   2 5 7 

1 2 3    B = 8 7 6  5 3 1 

Solution: % Determine the eigenvalues and eigenvectors >>A=[4 2 −3 ; −1 1 3 ; 2 5 7]  A =  4  2  −3 −1  1  3  2  5  7

ANMF.CH10_4PP.indd 782

3/1/2023 2:59:27 PM

MATLAB Basics • 783

eig(A)  ans = 0.5949 3.0000 8.4051 >>lamda=eig(A)  lamda = 0.5949 3.0000 8.4051 >>[V,D]=eig(A)  V = −0.6713  0.9163  −0.3905  0.6713  −0.3984  0.3905 −0.3144  0.0398  0.8337  D = 0.5949  0    0 0    3.0000  0 0    0    8.4051 EXAMPLE 10.3 Determine the values of x, y, and z for the following set of linear algebraic equations: x2 − 3x3 = −5 2x1 + 3x2 − x3 = 7 4x1 + 5x2 − 2x3 = 10 Solution: Here

 0 1 −3    A =  2 3 −1   B =  4 5 −2  or

ANMF.CH10_4PP.indd 783

5    7  and   X = 10 

 x1    x2   x 3 

AX = B A−1AX = A−1B IX = A−1B X = A−1B

3/1/2023 2:59:35 PM

784 • Applied Numerical Methods Using MATLAB >> A = [0 1 −3; 2 3 −1; 4 5 −2]; >> B = [−5; 7; 10] >> x = inv (A) ∗ B x = −1.0000 4.0000 3.0000 >> check = A ∗ x check = −5 7 10 % Alternative method >> x = A\B x = −1    4    3

10.15 SCRIPT FILES A script is a sequence of ordinary statements and functions used at the command prompt level. A script is invoked the command prompt level by typing the file name or by using the pull-down menu. Scripts can also invoke other scripts. The commands in the Command Window cannot be saved and executed again. Also, the Command Window is not interactive. To overcome these difficulties, the procedure is first to create a file with a list of commands, save it, and then run the file. In this way the commands contained are executed in the order they are listed when the file is run. In addition, as the need arises, one can change or modify the commands in the file, the file can be saved and run again. The files that are used in this fashion are known as script files. Thus, a script file is a text file that contains a sequence of MATLAB commands. A script file can be edited (corrected and/or changed) and executed many times. 10.15.1 Creating and Saving a Script File Any text editor can be used to create script files. In MATLAB, script files are created and edited in the Editor/Debugger Window. This window can be

ANMF.CH10_4PP.indd 784

3/1/2023 2:59:35 PM

MATLAB Basics • 785

opened from the Command Window. From the Command Window, select File, New, and then M-file. Once the window is open, the commands of the script file are typed line by line. The commands can also be typed in any text editor or word processor program and then copied and pasted in the Editor/ Debugger Window. The second type of M-files is the function file. Function file enables the user to extend the basic library functions by adding one’s own computational procedures. Function M-files are expected to return one or more results. Script files and function files may include reference to other MATLAB toolbox routines. MATLAB function file begins with a header statement of the form: function (name of result or results) = name (argument list) Before a script file can be executed it must be saved. All script files must be saved with the extension “.m”. MATLAB refers to them as m-files. When using MATLAB M-files editor, the files will automatically be saved with a “.m” extension. If any other text editor is used, the file must be saved with the “.m” extension, or MATLAB will not be able to find and run the script file. This is done by choosing Save As… from the File menu, selecting a location, and entering a name for the file. The names of user defined variables, predefined variables, MATLAB commands or functions should not be used to name script files. 10.15.2 Running a Script File A script file can be executed either by typing its name in the Command Window and then pressing the Enter key, directly from the Editor Window by clicking on the Run icon. The file is assumed to be in the current directory, or in the search path. 10.15.3 Input to a Script File There are three ways of assigning a value to a variable in a script file.

ANMF.CH10_4PP.indd 785

1.

The variable is defined and assigned value in the script file.

2.

The variable is defined and assigned value in the Command Window.

3.

The variable is defined in the script file, but a specified value is entered in the Command Window when the script file is executed.

3/1/2023 2:59:35 PM

786 • Applied Numerical Methods Using MATLAB 10.15.4 Output Commands There are two commands that are commonly used to generate output. They are the disp and fprintf commands. 1. The disp command The disp command displays the elements of a variable without displaying the name of the variable and displays text.

disp(name of a variable) or disp(‘text as string’)

>> A = [1 2 3 ; 4 5 6 ]; >> disp(A) 1 2 3 4 5 6 >> disp(‘Solution to the problem.’) Solution to the problem. 2. The fprintf command The fprintf command displays output (text and data) on the screen or saves it to a file. The output can be formatted using this command. EXAMPLE 10.4 Write a function file Veccrossprod to compute the cross product of two vectors a, and b, where a = (a1, a2, a3), b = (b1, b2, b3), and a × b = (a2b3 − a3b2, a3b1 − a1b3, a1b2 − a2b1). Verify the function by taking the cross products of pairs of unit vectors: (i, j), (j, k), etc. Solution: function c = Veccrossprod(a, b); % Veccrossprod : function to compute c = a × b where a and b are 3D vectors % call syntax: % c = Veccrossprod(a, b); c = [a(2) ∗ b(3) − a(3) ∗ b(2); a(3) ∗ b(1) − a(1) ∗ b(3); a(1) ∗ b(2) − a(2) ∗ b(1)];

ANMF.CH10_4PP.indd 786

3/1/2023 2:59:35 PM

MATLAB Basics • 787

10.16 PROGRAMMING IN MATLAB One most significant feature of MATLAB is its extendibility through userwritten programs such as the M-files. M-files are ordinary ASCII text files written in MATLAB language. A function file is a subprogram. 10.16.1 Relational and Logical Operators A relational operator compares two numbers by finding whether a comparison statement is true or false. A logical operator examines true/false statements and produces a result which is true or false according to the specific operator. Relational and logical operators are used in mathematical expressions and also in combination with other commands, to make decision that control the flow a computer program. MATLAB has six relational operators as shown in Table 10.21. TABLE 10.21  Relational operators. Relational operator

Interpretation


= == ~=

Less than Less than or equal Greater than Greater than or equal Equal Not equal

The logical operators in MATLAB are shown in Table 10.22. TABLE 10.22  Logical operators.

ANMF.CH10_4PP.indd 787

Logical operator

Name

Description

& Example: A&B

AND

Operates on two operands (A and B). If both are true, the result is true (1), otherwise the result is false (0).

| Example: A|B

OR

~ Example: ~A

NOT

Operates on two operands (A and B). If either or both are true, the result is true (1), otherwise (both are false) the result is false (0). Operates on one operand (A). Gives the opposite of the operand. True (1) if the operand is false, and false (0) if the operand is true.

3/1/2023 2:59:35 PM

788 • Applied Numerical Methods Using MATLAB 10.16.2 Order of Precedence The following Table 10.23 shows the order of precedence used by MATLAB. TABLE 10.23 Precedence

Operation

1 (highest) 2 3 4 5 6 7 8 (lowest)

Parentheses (If nested parentheses exist, inner have precedence). Exponentiation. Logical NOT (~). Multiplication, Division. Addition, Subtraction. Relational operators (>, =, >xor(8, −1) ans = 0 >>xor(8, 0) ans = 1

all(A)

Returns 1 (true) if all elements in a vector A are true (nonzero). Returns 0 (false) if one or more elements are false (zero). If A is a matrix, treats columns of A as vectors, returns a vector with 1’s and 0’s.

>>A = [5 3 11 7 8 15] >>all(A) ans = 1 >>B = [3 6 11 4 0 13] >>all(B) ans = 0 (continued)

ANMF.CH10_4PP.indd 788

3/1/2023 2:59:35 PM

MATLAB Basics • 789

TABLE 10.24  Additional logical built-in functions. (continued) Function

Description

Example

any(A)

Returns 1 (true) if any element in a vector A is true (nonzero). Returns 0 (false) if all elements are false (zero). If A is a matrix, treats columns of A as vectors, returns a vector with 1’s and 0’s.

>>A = [5 0 14 0 0 13] >>any(A) ans = 1 >>B = [0 0 0 0 0 0 ] >>any(B) ans = 0

find(A)

If A is a vector, returns the indices of the nonzero elements.

find(A>d)

If A is a vector, returns the address of the elements that are larger than d (any relational operator can be used).

>>A = [0 7 4 2 8 0 0 3 9] >>find(A) ans = 234589 >>find(A > 4) ans = 456

The truth table for the operation of the four logical operators, and, or, Xor, and not are summarized in Table 10.25. TABLE 10.25  Truth table. INPUT

OUTPUT

A

B

AND A&B

OR A|B

XOR (A,B)

NOT ~A

NOT ~B

false

false

false

false

false

true

true

false

true

false

true

true

true

false

true

false

false

true

true

false

true

true

true

true

true

false

false

false

10.16.4 Conditional Statements A conditional statement is a command that allows MATLAB to make a decision of whether to execute a group of commands that follow the conditional statement or to skip these commands. if conditional expression consists of relational and/or logical operators if

ANMF.CH10_4PP.indd 789

a < 30 count = count + 1 disp a end

3/1/2023 2:59:35 PM

790 • Applied Numerical Methods Using MATLAB The general form of a simple if statement is as follows: if logical expression statements end If the logical expression is true, the statements between the if statement and the end statement are executed. If the logical expression is false, then it goes to the statements following the end statement. 10.16.5 Nested if Statements Following is an example of nested if statements: if a < 30 count = count + 1; disp(a); if b > a b = 0; end end 10.16.6 else AND elseif Clauses The else clause allows us to execute one set of statements if a logical expression is true and a different set if the logical expression is false. % variable name inc if

inc < 1 x_inc = inc/10; else x_inc = 0.05; end

When several levels of if-else statements are nested, it may be difficult to find which logical expressions must be true (or false) to execute each set of statements. In such cases, the elseif clause is used to clarify the program logic. 10.16.7 MATLAB while Structures There is a structure in MATLAB that combines the for loop with the features of the if block. This is called the while loop and has the form:

ANMF.CH10_4PP.indd 790

3/1/2023 2:59:35 PM

MATLAB Basics • 791

while logical expression  his set of statements is executed repeatedly as long as the logical expresT sions remain true (equals +1) or if the expression is a matrix rather than a simple scalar variable, as long as all the elements of the matrix remain nonzero. end In addition to the normal termination of a loop by means of the end statement, there are additional MATLAB commands available to interrupt the calculations. These commands are listed in Table 10.26: TABLE 10.26 Command

Description

break

Terminates the execution of MATLAB for and while loops. In nested loops, break will terminate only the innermost loop in which it is placed. Primarily used in MATLAB functions, return will cause a normal return from a function from the point at which the return statement is executed. Terminates execution and displays the message contained in text on the screen. Note: The text must be enclosed in single quotes.

return error (‘text’)

The MATLAB functions used are summarized in Table 10.27: TABLE 10.27 Function

Description

Relational operators

A MATLAB logical relation is a comparison between two variables x and y of the same size effected by one of the six operators, =, = =, ~=. The comparison involves corresponding elements of x and y, and yields a matrix or scalar of the same size with values of “true” or “false” for each of its elements. In MATLAB, the value of “false” is zero, and “true” has a value of one. Any nonzero quantity is interpreted as “true”.

Combinatorial operators

The operators & (AND) and | (OR) may be used to combine two logical expressions.

all, any

If x is a vector, all(x) returns a value of one if all of the elements of x are nonzero, and a value of zero otherwise. When X is a matrix, all(X) returns a row vector of ones or zeros obtained by applying all to each of the columns of X. The function any operates similarly if any of the elements of x are nonzero.

find

If x is a vector, i = find(x) returns the indices of those elements of x that are nonzero (i.e., true). Thus, replacing all the negative elements of x by zero could be accomplished by i = find(x < 0); x(i) = zeros(size(i)); (continued)

ANMF.CH10_4PP.indd 791

3/1/2023 2:59:35 PM

792 • Applied Numerical Methods Using MATLAB

TABLE 10.27  (continued) Function

Description If X is a matrix, [i,j] = find(X) operates similarly and returns the row-column indices of nonzero elements.

if, else, elseif

The several forms of MATLAB if blocks are as follows: if variable if variable 1 if variable 1 block of statements block of statements block of statements executed if variable executed if variable 1 executed if variable 1 is “true”, i.e., nonzero is “true”, i.e., nonzero is “true”, end else elseif variable 2   block of statements block of statements   executed if variable 1 executed if variable 2 is “false”, i.e., zero is “true”, end else end   block of statements executed if neither variable is “true”

break

Terminates the execution of a for or while loop. Only the innermost loop in which break is encountered will be terminated.

return

Causes the function to return at that point to the calling routine. MATLAB M-file functions will return normally without this statement.

error (‘text’)

Within a loop or function, if the statement error(‘text’) is encountered, the loop or function is terminated, and the text is displayed.

while

The form of the MATLAB while loop is while variable block of statements executed as long as the value of variable is “true”; i.e., nonzero end Useful when a function F itself calls a second “dummy” function “f”. For example, the function F might find the root of an arbitrary function identified as a generic f(x). Then, the name of the actual M-file function, say fname, is passed as a character string to the function F either through its argument list or as a global variable, and the function is evaluated within F by means of feval. The use of feval(name, x1, x2, ..., xn), where fname is a variable containing the name of the function as a character string; i.e., enclosed in single quotes, and x1, x2, ..., xn are the variables needed in the argument list of function fname.

10.17 GRAPHICS MATLAB has many commands that can be used to create basic 2-D plots, overlay plots, specialized 2-D plots, 3-D plots, mesh, and surface plots. 10.17.1 Basic 2-D Plots The basic command for producing a simple 2-D plot is plot(x values, y values, ‘style option’)

ANMF.CH10_4PP.indd 792

3/1/2023 2:59:35 PM

MATLAB Basics • 793

where  x values and y values are vectors containing the x- and y-coordinates of points on the graph style option is an optional argument that specifies the color, line-style, and the point-marker style The style option in the plot command is a character string that consists of 1, 2, or 3 characters that specify the color and/or the line style. The different color, line-style, and marker-style options are summarized in Table 10.28. TABLE 10.28  Color, line-style, and marker-style options. Color style-option

Line style-option

Marker style-option

y yellow m magenta c cyan r red g green b blue w white k black

− solid − − dashed : dotted − . dash-dot

+ plus sign o circle ∗ asterisk x x-mark . point ^ up triangle s square d diamond, etc.

10.17.2 Specialized 2-D Plots There are several specialized graphics functions available in MATLAB for 2-D plots. The list of functions commonly used in MATLAB for plotting x-y data are given in Table 10.29. TABLE 10.29  List of functions for plotting x−y data. Function

Description

area bar barh comet compass contour contourf errorbar feather

Creates a filled area plot. Creates a bar graph. Creates a horizontal bar graph. Makes an animated 2-D plot. Creates arrow graph for complex numbers. Makes contour plots. Makes filled contour plots. Plots a graph and puts error bars. Makes a feather plot. (continued)

ANMF.CH10_4PP.indd 793

3/1/2023 2:59:35 PM

794 • Applied Numerical Methods Using MATLAB

TABLE 10.29  List of functions for plotting x−y data. (continued) Function

Description

fill fplot hist loglog pareto pcolor pie plotyy plotmatrix polar quiver rose scatter semilogx semilogy stairs stem

Draws filled polygons of specified color. Plots a function of a single variable. Makes histograms. Creates plot with log scale on both x and y axes. Makes pareto plots. Makes pseudo color plot of matrix. Creates a pie chart. Makes a double y-axis plot. Makes a scatter plot of a matrix. Plots curves in polar coordinates. Plots vector fields. Makes angled histograms. Creates a scatter plot. Makes semilog plot with log scale on the x-axis. Makes semilog plot with log scale on the y-axis. Plots a stair graph. Plots a stem graph.

10.17.2.1 Overlay plots There are three ways of generating overlay plots in MATLAB, they are: a) plot command b) hold command c) line command a) Plot command Example 10.7(a) shows the use of plot command used with matrix argument, each column of the second argument matrix plotted against the corresponding column of the first argument matrix. b) Hold command Invoking hold at any point during a session freezes the current plot in the graphics window. All the next plots generated by the plot command are added to the exiting plot. See Example 10.7(a).

ANMF.CH10_4PP.indd 794

3/1/2023 2:59:36 PM

MATLAB Basics • 795

c) Line command The line command takes a pair of vectors (or a triplet in 3-D) followed by a parameter name/parameter value pairs as argument. For instance, the command line (x data, y data, parameter name, parameter value) adds lines to the existing axes. See Example 10.7(a). 10.17.3 3-D Plots MATLAB provides various options for displaying three-dimensional data. They include line and wire, surface, mesh plots, among many others. More information can be found in the Help Window under Plotting and Data visualization. Table 10.30 lists commonly used functions. TABLE 10.30  Functions used for 3-D graphics. Command

Description

plot3

Plots three-dimensional graph of the trajectory of a set of three parametric equations x(t), y(t), and z(t) can be obtained using plot3(x,y,z). If x and y are two vectors containing a range of points for the evaluation of a function, [X,Y] = meshgrid(x, y) returns two rectangular matrices containing the x and y values at each point of a two-dimensional grid. If X and Y are rectangular arrays containing the values of the x and y coordinates at each point of a rectangular grid, and if z is the value of a function evaluated at each of these points, mesh(X,Y,z) will produce a three-dimensional perspective graph of the points. The same results can be obtained with mesh(x,y,z). If the xy grid is rectangular, these two functions are merely variations of the basic plotting program mesh, and they operate in an identical fashion. meshc will produce a corresponding contour plot drawn on the xy plane below the threedimensional figure, and meshz will add a vertical wall to the outside features of the figures drawn by mesh. Produces a three-dimensional perspective drawing. Its use is usually to draw surfaces, as opposed to plotting functions, although the actual tasks are quite similar. The output of surf will be a shaded figure. If row vectors of length n are defined by x =r cos q and y = r sin q, with 0 ≤ q ≤ 2p, they correspond to a circle of radius r. If r is a column vector equal to r = [0 1 2]’; then z = r∗ones(size(x)) will be a rectangular, 3 × n, arrays of 0’s and 2’s, and surf(x, y, z) will produce a shaded surface bounded by three circles; that is, a cone. This function is related to surf in the same way that meshc is related to mesh. Used to change the default coloring of a figure. See the MATLAB reference manual or the help file. Controls the type of color shading used in drawing figures. See the MATLAB reference manual or the help file. view(az,el) controls the perspective view of a three-dimensional plot. The view of the figure is from angle “el” above the xy plane with the coordinate axes (and the figure) rotated by an angle “az” in a clockwise direction about the z axis. Both angles are in degrees. The default values are az = 37½º and el = 30º.

meshgrid

mesh(X,Y,z)

meshc, meshz

surf

surfc colormap shading view

(continued)

ANMF.CH10_4PP.indd 795

3/1/2023 2:59:37 PM

796 • Applied Numerical Methods Using MATLAB

TABLE 10.30  Functions used for 3-D graphics. (continued) Command

Description

axis

Determines or changes the scaling of a plot. If the coordinate axis limits of a twodimensional or three-dimensional graph are contained in the row vector r = [xmin, xmax, ymin, ymax, zmin, zmax], axis will return the values in this vector, and axis(r) can be used to alter them. The coordinate axes can be turned on and off with axis(‘on’) and axis(‘off’). A few other string constant inputs to axis and their effects are given below: axis(‘equal’) x and y scaling are forced to be the same. axis(‘square’) The box formed by the axes is square. axis(‘auto’) Restores the scaling to default settings. axis(‘normal’)  Restoring the scaling to full size, removing any effects of square or equal settings. axis(‘image’)  Alters the aspect ratio and the scaling so the screen pixels are square shaped rather than rectangular. The use is contour(x,y,z). A default value of N = 10 contour lines will be drawn. An optional fourth argument can be used to control the number of contour lines that are drawn. contour(x,y,z,N), if N is a positive integer, will draw N contour lines, and contour(x,y,z,V), if V is a vector containing values in the range of z values, will draw contour lines at each value of z = V. Plots lines or curves in three dimensions. If x, y, and z are vectors of equal length, plot3(x,y,z) will draw, on a three-dimensional coordinate axis system, the lines connecting the points. A fourth argument, representing the color and symbols to be used at each point, can be added in exactly the same manner as with plot. grid on adds grid lines to a two-dimensional or three-dimensional graph; grid off removes them. Draws “slices” of a volume at a particular location within the volume.

contour

plot3

grid slice

EXAMPLE 10.5 a) Generate an overlay plot for plotting three lines y1 = sin t y2 = t 5 t7 t3 t y3 = t − + + 3! 5! 7!

0 ≤ t ≤ 2π

Use (i) the plot command (ii) the hold command (iii) the line command b) Use the functions for plotting x-y data for plotting the following functions. (i) f(t) = t cost 0 ≤ t ≤ 10π

ANMF.CH10_4PP.indd 796

3/1/2023 2:59:43 PM

MATLAB Basics • 797

(ii) x = et y = 100 + e3t 0 ≤ t ≤ 2π Solution: a) overlay plot i) % using the plot command >>t = linspace(0, 2∗pi, 100); >>y1 = sin (t); y2 = t; >>y3 = t − (t. ^3)/6 + (t. ^5)/120 − (t. ^7)/5040; >>plot (t, y1, t, y2, ‘−‘, t, y3, ‘o’) >>axis ([0 5 −1 5]) >>xlabel(‘t’) >>ylabel(‘sin(t) approximation’) >>title(‘sin(t) function’) >>text(3.5,0, ‘sin(t)’) >>gtext(‘Linear approximation’) >gtext(‘4-term approximation’) Output is shown in Figure 10.1.

FIGURE 10.1

ANMF.CH10_4PP.indd 797

3/1/2023 2:59:43 PM

798 • Applied Numerical Methods Using MATLAB ii) % using the hold command >>x = linspace(0, 2∗pi, 100); y1 = sin(x); >>plot(x, y1) >>hold on >>y2 = x; plot(x, y2, ‘−’) >>y3 = x − (x.^3)/6 + (x.^5)/120 − (t.^7)/5040; >>plot(x, y3, ‘o’) >>axis ([0 5 −1 5]) >>hold off Output is shown in Figure 10.2.

FIGURE 10.2

iii) % using the line command

ANMF.CH10_4PP.indd 798

>>t = linspace(0, 2∗pi, 100); >>y1 = sin (t); >>y2 = t; >>y3 = t − (t. ^3)/6 + (t. ^5)/120 − (t. ^7)/5040; >>plot (t, y1) >>line (t, y2, ‘linestyle’, ‘−’) >>line(t, y3, ‘marker’, ‘o’) >>axis([0 5 −1 5]) >>xlabel(‘t’) >>ylabel(‘sin(t) approximation’)

3/1/2023 2:59:43 PM

MATLAB Basics • 799



>>title(‘sin(t) function’) >>legend(‘sin(t)’, ‘linear approx’, ‘7th order approx’)



Output is shown in Figure 10.3.

FIGURE 10.3

b) Using Table 10.29 functions i) >>fplot(‘x.∗cos(x)’, [0  10∗pi]) This will result in Figure 10.4.

FIGURE 10.4

ANMF.CH10_4PP.indd 799

3/1/2023 2:59:44 PM

800 • Applied Numerical Methods Using MATLAB ii) >>t = linspace(0, 2∗pi, 200); >>x = exp(t); >>y = 100 + exp (3∗t); >>loglog(x, y), grid

FIGURE 10.5

EXAMPLE 10.6 a)

Plot the parametric space curve of x (t) = t y (t) = t2 z (t) = t3 0 ≤ t ≤ 1.0

b) z = − 7 / (1+ x2 + y2)

| x | ≤ 5, | y | ≤ 5

Solution: (a) >> t=linspace (0, 2,100); >> x=t; y=t. ^2; z=t. ^3; >> plot3(x, y, z), grid0

ANMF.CH10_4PP.indd 800

3/1/2023 2:59:44 PM

MATLAB Basics • 801

The plot is shown in Figure 10.6.

FIGURE 10.6

(b) >> t=linspace (0, 2,100); >> x=t; y=t. ^2; z=t. ^3; >> plot3(x, y, z), grid >> t=linspace (−5, 5,50); y=x; >> z=−7./(1+x.^2+y.^2); >> mesh(z) The plot is shown in Figure 10.7.

FIGURE 10.7

ANMF.CH10_4PP.indd 801

3/1/2023 2:59:44 PM

802 • Applied Numerical Methods Using MATLAB 10.17.4 Saving and Printing Graphs To obtain a hardcopy of a graph, type print in the Command Window after the graph appears in the figure Window. The figure can also be saved into a specified file in the PostScripter or Encapsulated PostScript (EPS) format. The command to save graphics to a file is print − d device type − options filename where device type for PostScript printers are listed in Table 10.31. TABLE 10.31  Device type for PostScript printers. Devicetype

Description

Devicetype

Description

ps psc ps2 psc2

Black and white PostScript Color PostScript Level 2 BW PostScript Level 2 color PostScript

eps epsc eps2 epsc2

Black and white EPSF Color EPSF Level 2 black and white EPSF Level 2 color EPSF

MATLAB can also generate a graphics file in the following popular formats among others.

−dill −djpeg −dtiff −dmfile

saves file in Adobe Illustrator format. saves file as a JPEG image. saves file as a compressed TIFF image. saves file as an M-file with graphics handles.

10.18 INPUT/OUTPUT IN MATLAB In this section, we present some of the many available commands in MATLAB for reading data from an external file into a MATLAB matrix, or writing the numbers computed in MATLAB into such an external file. 10.18.1 The fopen Statement To have the MATLAB read or write a separate data file of numerical values, we need to connect the file to the executing MATLAB program. The MATLAB functions used are summarized in Table 10.32.

ANMF.CH10_4PP.indd 802

3/1/2023 2:59:44 PM

MATLAB Basics • 803

TABLE 10.32  MATLAB functions used for input/output. Function

Description

fopen

Connects an existing file to MATLAB or to create a new file from MATLAB. fid = fopen(‘Filename’, permission code); where, if fopen is successful, fid will be returned as a positive integer greater than 2. When unsuccessful, a value of −1 is returned. Both the file name and the permission code are string constants enclosed in single quotes. The permission code can be a variety of flags that specify whether or not the file can be written to, read from, appended to, or a combination of these. Some common codes are: Code Meaning ‘r’ read only ‘w’ write only ‘r+’ read and write ‘a+’ read and append The fopen statement positions the file at the beginning. Disconnects a file from the operating MATLAB program. The use is fclose(fid), where fid is the file identification number of the file returned by fopen.fclose(‘all’) will close all files. Reads opened files. The use is A = fscanf(fid, FORMAT, SIZE) where FORMAT specifies the types of numbers (integers, reals with or without exponent, character strings) and their arrangement in the data file, and optional SIZE determines how many quantities are to be read and how they are to be arranged into the matrix A. If SIZE is omitted, the entire file is read. The FORMAT field is a string (enclosed in single quotes) specifying the form of the numbers in the file. The type of each number is characterized by a percent sign (%), followed by a letter (i or d for integers, e or f for floating-point numbers with or without exponents). Between the percent sign and the type code, one can insert an integer specifying the maximum width of the field. Writes files previously opened. fprintf(fid, FORMAT, A) where fid and FORMAT have the same meaning as for fscanf, with the exception that for output formats the string must end with \n, designating the end of a line of output.

fclose

fscanf

fprintf

10.19 SYMBOLIC MATHEMATICS In Sections 10.1 to 10.18, the capability of MATLAB for numerical computations have been described. In this section some of MATLAB’s capabilities for symbolic manipulations will be presented. Specifically, the symbolic expressions, symbolic algebra, simplification of mathematical expressions, operations on symbolic expressions, solution of a single equation or a set of linear algebraic equations, solutions to differential equations, differentiation and integration of functions using MATLAB are presented.

ANMF.CH10_4PP.indd 803

3/1/2023 2:59:44 PM

804 • Applied Numerical Methods Using MATLAB 10.19.1 Symbolic Expressions A symbolic expression is stored in MATLAB as a character string. Single quote marks are used to define the symbolic expression. For instance:

‘sin(y/x)’; ‘x^4 + 5∗x^3 + 7∗x^2 − 7’

The independent variable in many functions is specified as an additional function argument. If an independent variable is not specified, then MATLAB will pick one. When several variables exist, MATLAB will pick the one that is a single lowercase letter (except i and j), which is closest to x alphabetically. The independent variable is returned by the function symvar, symvar(s) Returns the independent variable for the symbolic expression s. For example:

Expression s symvar(s)

‘5 ∗ c ∗ d + 34’ d ‘sin (y/x)’ x In MATLAB, a number of functions are available to simplify mathematical expressions by expanding the terms, factoring expressions, collecting coefficients, or simplifying the expression. For instance: expand(s)

Performs an expansion of s.

A summary of these expressions is given in Table 10.33. A summary of basic operations is given in Table 10.34. The standard arithmetic operation (Table 10.35) is applied to symbolic expressions using symbolic functions. These symbolic expressions are summarized in Table 10.36. TABLE 10.33 Simplification

ANMF.CH10_4PP.indd 804

collect

Collect common terms

expand

Expand polynomials and elementary functions

factor

Factorization

horner

Nested polynomial representation

numden

Numerator and denominator

simple

Search for shortest form

3/1/2023 2:59:44 PM

MATLAB Basics • 805

TABLE 10.33  (Continued) Simplification simplify

Simplification

subexpr

Rewrite in terms of subexpressions TABLE 10.34 Basic Operations

ccode

C code representation of a symbolic expression

conj

Complex conjugate

findsym

Determine symbolic variables

fortran

Fortran representation of a symbolic expression

imag

Imaginary part of a complex number

latex

LaTeX representation of a symbolic expression

pretty

Pretty prints a symbolic expression

real

Real part of an imaginary number

sym

Create symbolic object

syms

Shortcut for creating multiple symbolic objects TABLE 10.35  Arithmetic operations.

ANMF.CH10_4PP.indd 805

+

Addition



Subtraction



Multiplication

.∗

Array multiplication

/

Right division

./

Array right division

\

Left division

.\

Array left division

^

Matrix or scalar raised to a power

.^

Array raised to a power



Complex conjugate transpose

.‘

Real transpose

3/1/2023 2:59:44 PM

806 • Applied Numerical Methods Using MATLAB

TABLE 10.36  Symbolic expressions. horner(S) Transposes S into its horner, or nested, representation. numden(S) Returns two symbolic expressions that represent, respectively, the numerator expression and the denominator expression for the rational representation of S. numeric(S) Converts s to a numeric form (S must not contain any symbolic variables). poly2sym(c) Converts a polynomial coefficient vector c to a symbolic polynomial. pretty(S) Prints S in an output form that resembles typeset mathematics. sym2poly(S) Converts S to a polynomial coefficient vector. ∗ symadd(A,B) Performs a symbolic addition, A + B. symdiv(A,B) Performs a symbolic division, A / B. symmul(A,B) Performs a symbolic multiplication, A ∗ B. sympow(S,p) Performs a symbolic power, S^p. symsub(A,B) Performs a symbolic subtraction, A − B.

10.19.2 Solution to Differential Equations Symbolic math functions can be used to solve a single equation, a system of equations, and differential equations. For example: solve(f) S  olves a symbolic equation f for its symbolic variable. If f is a symbolic expression, this function solves the equation f = 0 for its symbolic variable. solve(f1, … fn) Solves the system of equations represented by f1, …, fn. The symbolic function for solving ordinary differential equation is dsolve as shown below: dsolve(‘equation’, ‘condition’) Symbolically solves the ordinary differential equation specified by ‘equation’. The optional argument ‘condition’ specifies a boundary or initial condition. The symbolic equation uses the letter D to denote differentiation with respect to the independent variable. A D followed by a digit denotes repeated differentiation. Thus, Dy represents dy/dx, and D2y represents d2y/dx2. For example, given the ordinary second order differential equation:

dx d2 x +5 + 3x = 7 dt dt 2

with the initial conditions x(0) = 0 and x (0) = 1.

ANMF.CH10_4PP.indd 806

3/1/2023 2:59:52 PM

MATLAB Basics • 807

The MATLAB statement that determines the symbolic solution for the previous differential equation is the following: x = dsolve(‘D2x = −5∗Dx−3∗x+7’, ‘x(0)=0’, ‘Dx(0)=1’) The symbolic functions are summarized in Table 10.37. TABLE 10.37  Solution of equations. compose

Functional composition

dsolve

Solution of differential equations

finverse

Functional inverse

solve

Solution of algebraic equations

10.19.3 Calculus There are four forms by which the symbolic derivative of a symbolic expression is obtained in MATLAB. They are: diff(f)

Returns the derivative of the expression f with respect to the default independent variable. diff(f, ‘t’) Returns the derivative of the expression f with respect to the variable t. diff(f,n) Returns the nth derivative of the expression f with respect to the default independent variable. diff(f, ‘t’,n) Returns the nth derivative of the expression f with respect to the variable t. The various forms that are used in MATLAB to find the integral of a symbolic expression f are given below and summarized in Table 10.38. int(f)

 eturns the integral of the expression f with respect to the R default independent variable. int(f, ‘t’) Returns the integral of the expression f with respect to the variable t. int(f,a,b) Returns the integral of the expression f with respect to the default independent variable evaluated over the interval [a,b], where a and b are numeric expressions. int(f, ‘t’,a,b) Returns the integral of the expression f with respect to the variable t evaluated over the interval [a,b], where a and b are numeric expressions.

ANMF.CH10_4PP.indd 807

3/1/2023 2:59:52 PM

808 • Applied Numerical Methods Using MATLAB int(f, ‘m’, ‘n’) Returns the integral of the expression f with respect to the default independent variable evaluated over the interval [m, n], where m and n are numeric expressions. The other symbolic functions for pedagogical and graphical applications, conversions, integral transforms, and linear algebra are summarized in Tables 10.38 to 10.42. TABLE 10.38 Calculus diff

Differentiate

int

Integrate

jacobian

Jacobian matrix

limit

Limit of an expression

symsum

Summation of series

taylor

Taylor series expansion TABLE 10.39 Pedagogical and Graphical Applications

ANMF.CH10_4PP.indd 808

ezcontour

Contour plotter

ezcontourf

Filled contour plotter

ezmesh

Mesh plotter

ezmeshc

Combined mesh and contour plotter

ezplot

Function plotter

ezplot

Easy-to-use function plotter

ezplot3

Three-dimensional curve plotter

ezpolar

Polar coordinate plotter

ezsurf

Surface plotter

ezsurfc

Combined surface and contour plotter

funtool

Function calculator

rsums

Riemann sums

taylortool

Taylor series calculator

3/1/2023 2:59:52 PM

MATLAB Basics • 809

TABLE 10.40 Conversions char

Convert sym object to string

double

Convert symbolic matrix to double

poly2sym

Function calculator

sym2poly

Symbolic polynomial to coefficient vector TABLE 10.41 Integral Transforms

fourier

Fourier transform

ifourier

Inverse Fourier transform

ilaplace

Inverse Laplace transform

iztrans

Inverse Z-transform

laplace

Laplace transform

ztrans

Z-transform TABLE 10.42 Linear Algebra

ANMF.CH10_4PP.indd 809

colspace

Basis for column space

det

Determinant

diag

Create or extract diagonals

eig

Eigenvalues and eigenvectors

expm

Matrix exponential

inv

Matrix inverse

jordan

Jordan canonical form

null

Basis for null space

poly

Characteristic polynomial

rank

Matrix rank

rref

Reduced row echelon from

svd

Singular value decomposition

tril

Lower triangle

triu

Upper triangle

3/1/2023 2:59:52 PM

810 • Applied Numerical Methods Using MATLAB Example Problems and Solutions EXAMPLE 10.7 Consider the function H(s) =

n(s) d(s)

where n(s) = s4 + 6s3 + 5s2 + 4s + 3 d(s) = s5 + 7s4 + 6s3 + 5s2 + 4s + 7

a) Find n(−10), n(−5), n(−3) and n(−1)



b) Find d(−10), d(−5), d(−3) and d(−1)



c) Find H(−10), H(−5), H(−3) and H(−1)

Solution: a) >> n=[1 6 5 4 3]; % n=s^4+6s^3+5s^2+4s+3 >> d=[1 7 6 5 4 7]; % d=s^5+7s^4+6s^3+5s^2+4s+7 >> n2=polyval(n,[−10]) n2 = 4463 >> nn10=polyval(n,[−10]) nn10 = 4463 >> nn5=polyval(n,[−5]) nn5 = −17 >> nn3=polyval(n,[−3]) nn3 = −45 >> nn1=polyval(n,[−1]) nn1 = −1 b) >> dn10=polyval(d,[−10]) dn10 = −35533 >> dn5=polyval(d,[−5]) dn5 = 612 >> dn3=polyval(d,[−3]) dn3 = 202 >> dn1=polyval(d,[−1]) dn1 = 8

ANMF.CH10_4PP.indd 810

3/1/2023 2:59:56 PM

MATLAB Basics • 811

c) >> Hn10=nn10/dn10 Hn10 = −0.1256 >> Hn5=nn5/dn5 Hn5 = −0.0278 >> Hn3=nn3/dn3 Hn3 = −0.2228 >> Hn1=nn1/dn1 Hn1 = −0.1250 EXAMPLE 10.8 Generate a plot of y(x) = e−0.7x sin ωx where ω = 15 rad/s, and 0 ≤ x ≤ 15. Use the colon notation to generate the x vector in increments of 0.1 Solution:

>> x = [0: 0.1: 15]; >> w = 15; >> y = exp (−0.7∗x).∗sin (w∗x); >> plot(x, y) >> title (‘y(x) = e^−^0^. ^7^xsin \omegax’) >> xlabel (‘x’) >> ylabel (‘y’)

FIGURE 10.8

ANMF.CH10_4PP.indd 811

3/1/2023 2:59:56 PM

812 • Applied Numerical Methods Using MATLAB EXAMPLE 10.9 Generate a plot of y(x) = e−0.6x cos ωx where ω = 10 rad/s, and 0 ≤ x ≤ 15. Use the colon notation to generate the x vector in increments of 0.05. Solution:

>> x = [0 : 0.1 : 15]; >> w = 10; >> y = exp(−0.6∗x).∗cos(w∗x); >> plot(x, y) >> title (‘y(x) = e^−^0^. ^6^xcos \omegax’) >> xlabel (‘x’) >> ylabel (‘y’)

FIGURE 10.9

EXAMPLE 10.10 Using the functions for plotting x–y data given in Table 10.29 plot the ­following functions. a) r2 = 5 cos 3t

0 ≤ t ≤ 2π

2

b) r = 5 cos 3t 0 ≤ t ≤ 2π x = r cos t, y = r sin t

ANMF.CH10_4PP.indd 812

3/1/2023 2:59:56 PM

MATLAB Basics • 813

c) y1 = e−2x cos x 0 ≤ t ≤ 20 2x y2 = e cos(x) d) y = −5 ≤ x ≤ 5π x e) f = e−3t/5 cos t 0 ≤ t ≤ 2π 1 f) z = −  x2 + 2xy + y2 3 |x| ≤ 7, |y| ≤ 7 Solution: a) >>t = linspace(0, 2∗pi, 200); >>r =sqrt(abs(5∗cos(3∗t))); >>polar(t, r)

FIGURE 10.10

(b) >>t = linspace (0, 2∗pi, 200);

>>r =sqrt (abs (5∗cos (3∗t)));

>>x=r.∗cos (t); >>y=r.∗sin (t); >>fill(x,y,’k’), >>axis(‘square’)

ANMF.CH10_4PP.indd 813

3/1/2023 3:00:00 PM

814 • Applied Numerical Methods Using MATLAB

FIGURE 10.11

(c)

>>x=1:0.1:20;

>>y1=exp (−2∗x).∗cos(x); >>y2=exp (2∗x); >>Ax=plotyy(x, y1, x, y2); >>hy1=get (Ax (1),’ylabel’); >>hy2=get(Ax(2),’ylabel’); >>set(hy1,’string’,’exp(−2x).cos(x)’) >>set(hy2,’string’,’exp(−2x)’);

FIGURE 10.12

ANMF.CH10_4PP.indd 814

3/1/2023 3:00:00 PM

MATLAB Basics • 815

(d) >>x=linspace (−5∗pi, 5∗pi, 100); >>y=cos(x). /x; >>area(x, y); >>xlabel (‘x (rad)’), ylabel (‘cos(x)/x’) >>hold on

FIGURE 10.13

(e) >>t=linspace (0,2∗pi, 200); >>f=exp(−0.6∗t).∗sin(t); >>stem(t, f)

FIGURE 10.14

ANMF.CH10_4PP.indd 815

3/1/2023 3:00:01 PM

816 • Applied Numerical Methods Using MATLAB (f) >>r = −7:0.2:7; >> [X, Y] = meshgrid(r, r); >>Z = −0.333∗X. ^2+2∗X.∗Y+Y. ^2; >>cs=contour(X, Y, Z); >>label(cs)

FIGURE 10.15

EXAMPLE 10.11 Use the functions listed in Table 10.30 for plotting 3-D data for the following. a) z = cos x cos y e



x2 + y 2 5

|x| ≤ = 7, |y| ≤ 7 b) Discrete data plots with stems x = t, y = t cos (t) z = et/5 − 2 0 ≤ t ≤ 5π c) A cylinder generated by r = sin(5πz) + 3 0 ≤ z ≤ 1 0 ≤ θ ≤ 2π Solution: (a) >>u=−7:0.2:7; >> [X, Y]=meshgrid (u, u); >>Z=cos(X).∗cos (Y).∗exp (−sqrt (X. ^2+Y. ^2)/5); >>surf(X,Y,Z)

ANMF.CH10_4PP.indd 816

3/1/2023 3:00:03 PM

MATLAB Basics • 817

FIGURE 10.16

(b) >>t=linspace(0,5∗pi,200); >>x=t; y=t.∗cos(t); >>z=exp(t/5)−2; >>stem3(x,y,z,‘filled’); >>xlabel (‘t’), ylabel (‘tcos (t)’), zlabel (‘e^t/5−1’)

FIGURE 10.17

ANMF.CH10_4PP.indd 817

3/1/2023 3:00:04 PM

818 • Applied Numerical Methods Using MATLAB (c) >>z= [0:0.2:1]’; >>r=sin (5∗pi∗z)+3; >>cylinder(r)

FIGURE 10.18

EXAMPLE 10.12 Obtain the plot of the points for 0 ≤ t ≤ 6 π when the coordinates x, y, z are given as a function of the parameter t as follows:

x = t sin(3 t) y = t cos(3 t) z = 0.8 t

Solution: % Line plots >> t=[0:0.1:6∗pi]; >> x=sqrt(t).∗sin(3∗t); >> y=sqrt (t).∗cos (3∗t); >> z=0.8∗t; >> plot3(x,y,z,‘k’,‘linewidth’,1) >> grid on >> xlabel(‘x’);ylabel(‘y’);label(‘z’)

ANMF.CH10_4PP.indd 818

3/1/2023 3:00:08 PM

MATLAB Basics • 819

FIGURE 10.19

EXAMPLE 10.13

2 xy2

Obtain the mesh and surface plots for the function z = 2 over the 2 + x y domain −2 ≤ x ≤ 6 and 2 ≤ y ≤ 8. Solution: % Mesh and surface plots x=−2:0.1:6; >> y=2:0.1:8; >> [x, y]=meshgrid (x, y); >> z=2∗x.∗y.^2./ (x. ^2+y. ^2); >> mesh(x, y, z) >> xlabel (‘x’); ylabel (‘y’); zlabel (‘z’) >> surf(x, y, z) >> xlabel (‘x’); ylabel (‘y’); zlabel (‘z’)

ANMF.CH10_4PP.indd 819

3/1/2023 3:00:21 PM

820 • Applied Numerical Methods Using MATLAB

.

FIGURE 10.20

FIGURE 10.21

EXAMPLE 10.14

−1.5 x2 + y2

Plot the function z = 2 and −4 ≤ y ≤ 4 using Table 10.30

sin( x)cos(0.5 y) over the domain −4 ≤ x ≤ 4

(a) Mesh plot (b) Surface plot (c) Mesh curtain plot

ANMF.CH10_4PP.indd 820

3/1/2023 3:00:28 PM

MATLAB Basics • 821

(d) Mesh and contour plot (e) Surface and contour plot Solution a)

% Mesh Plot >> x = −4:0.25:4; >> y = −4:0.25:4; >> [x, y] = meshgrid(x, y); >> z=2. ^ (−1.5∗sqrt (x. ^2 + y. ^2)).∗cos (0.5∗y).∗sin(x); >> mesh(x, y, z) >> xlabel (‘x’); ylabel (‘y’) >> zlabel(‘z’)

FIGURE 10.22

b) % Surface Plot >> x=−4:0.25:4; >> y=−4:0.25:4;

ANMF.CH10_4PP.indd 821

3/1/2023 3:00:28 PM

822 • Applied Numerical Methods Using MATLAB

>> [x, y]=meshgrid(x, y); >> z = 2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x); >> surf(x, y, z) >> xlabel (‘x’); ylabel (‘y’) >> zlabel(‘z’)

FIGURE 10.23

c)

% Mesh Curtain Plot >> x=−4.0:0.25:4; >> y=−4.0:0.25:4; >> [x, y]=meshgrid(x, y); >> z=2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x); >> meshz(x, y, z) >> xlabel (‘x’); ylabel (‘y’) >> zlabel(‘z’)

d)

% Mesh and Contour Plot >> x=−4.0:0.25:4; >> y=−4.0:0.25:4; >> [x, y]=meshgrid(x, y); >> z=2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x);

ANMF.CH10_4PP.indd 822

3/1/2023 3:00:28 PM

MATLAB Basics • 823



>> meshc(x, y, z) >> xlabel (‘x’); ylabel (‘y’) >> zlabel(‘z’)

FIGURE 10.24

FIGURE 10.25

ANMF.CH10_4PP.indd 823

3/1/2023 3:00:29 PM

824 • Applied Numerical Methods Using MATLAB e)

% Surface and Contour Plot >> x=−4.0:0.25:4; >> y=−4.0:0.25:4; >> [x, y] =meshgrid(x, y); >> z=2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x); >> surfc(x, y, z) >> xlabel (‘x’); ylabel (‘y’) >> zlabel(‘z’)

FIGURE 10.26

EXAMPLE 10.15 −1.5 x2 + y2

Plot the function z = 2 and −4 ≤ y ≤ 4 using Table 10.30.

sin( x)cos(0.5 y) over the domain −4 ≤ x ≤ 4

(a) Surface plot with lighting (b) Waterfall plot (c) 3-D contour plot (d) 2-D contour plot

ANMF.CH10_4PP.indd 824

3/1/2023 3:00:39 PM

MATLAB Basics • 825

Solution: a) % Surface Plot with lighting >> x=−4.0:0.25:4; >> y=−4.0:0.25:4; >> [x, y]=meshgrid(x, y); >> z=2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x); >> surfl(x, y, z) >> xlabel (‘x’); ylabel (‘y’) >> zlabel(‘z’)

FIGURE 10.27

b)

ANMF.CH10_4PP.indd 825

% Waterfall Plot >> x=−4.0:0.25:4; >> y=−4.0:0.25:4; >> [x, y]=meshgrid(x, y); >> z=2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x); >> waterfall(x, y, z) >> xlabel (‘x’); ylabel (‘y’) >> zlabel (‘z’)

3/1/2023 3:00:39 PM

826 • Applied Numerical Methods Using MATLAB

FIGURE 10.28

c) % 3-D Contour Plot >> x=−4.0:0.25:4;

>> y=−4.0:0.25:4;



>> [x, y]=meshgrid(x, y);



>> z=2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x);



>> contour3(x, y, z, 15)



>> xlabel (‘x’); ylabel (‘y’)



>> zlabel(‘z’)

FIGURE 10.29

ANMF.CH10_4PP.indd 826

3/1/2023 3:00:39 PM

MATLAB Basics • 827

d)

% 2-D Contour Plot >> x=−4.0:0.25:4; >> y=−4.0:0.25:4; >> [x, y]=meshgrid(x, y); >> z=2.0. ^ (−1.5∗sqrt (x. ^2+y. ^2)).∗cos (0.5∗y).∗sin(x); >> contour(x, y, z, 15) >> xlabel (‘x’); ylabel (‘y’) >> zlabel(‘z’)

FIGURE 10.30

EXAMPLE 10.16 Using the functions given in Table 10.29 for plotting x-y data, plot the following functions: 0 ≤ t ≤ 10π

(a) f(t) = t cost

(b) x = e −2 t , y = t 0 ≤ t ≤ 2π (c) x = t, y = e2 t 0 ≤ t ≤ 2π (d) x = e t , y = 50 + e t 0 ≤ t ≤ 2π (e) (f)

r 2 = 3 sin 7 t y = r sin t r 2 = 3 sin 4 t

0 ≤ t ≤ 2π 0 ≤ t ≤ 2π

y = r sin t (g) y = t sin t

ANMF.CH10_4PP.indd 827

0 ≤ t ≤ 5π

3/1/2023 3:01:06 PM

828 • Applied Numerical Methods Using MATLAB Solution: a) % Use of plot command >> fplot(‘x.∗cos(x)’,[0,10∗pi])

FIGURE 10.31

b)

% Semilog x command >> t=linspace(0,2∗pi,200); >> x=exp(−2∗t);y=t; >> semilogx(x, y),grid

FIGURE 10.32

ANMF.CH10_4PP.indd 828

3/1/2023 3:01:06 PM

MATLAB Basics • 829

c) % Semilog y command t=linspace(0,2∗pi,200); >> semilogy(text(−2∗t)),grid

FIGURE 10.33

d)

% Use of loglog command >> t=linspace(0,2∗pi,200); >> x=exp(t); >> y=50+exp (t); >> loglog(x, y), grid

FIGURE 10.34

ANMF.CH10_4PP.indd 829

3/1/2023 3:01:06 PM

830 • Applied Numerical Methods Using MATLAB e)

%Use of stairs command >> t=linspace (0,2∗pi, 200); >> r=sqrt (abs (3∗sin (7∗t))); >> y=r.∗sin (t); >> stairs(t, y) >> axis([0 pi 0 inf]);

FIGURE 10.35

f)

% Use of bar command >> t=linspace (0,2∗pi, 200); >> r=sqrt (abs (3∗sin (4∗t))); >> y=r.∗sin (t); >> bar(t, y) >> axis([0 pi 0 inf]);

FIGURE 10.36

ANMF.CH10_4PP.indd 830

3/1/2023 3:01:07 PM

MATLAB Basics • 831

g)

%use of comet command >> q=linspace (0,5∗pi, 200); >> y=q.∗sin (q); >> comet(q, y) 15

10

5

0

-5

-10

-15

0

5

10

15

FIGURE 10.37

EXAMPLE 10.17 Consider the two matrices 2π  3 A =    5 j 10 + 2 j  7 j 15 j B =   2  18  Using MATLAB, determine the following: a) A + B b) AB c)

A2

d) AT e) B−1 f) BTAT g) A2 + B2 − AB

ANMF.CH10_4PP.indd 831

3/1/2023 3:01:14 PM

832 • Applied Numerical Methods Using MATLAB Solution: >> A = [3 2∗pi;5j 10+sqrt(2)∗j]; >> B = [7j −15j;2∗pi 18]; a) A + B ans = 3.0000 + 7.0000i 6.2832 + 5.0000i

6.2832 − 15.0000i 28.0000 + 1.4142i

b) >> A ∗ B ans = 1.0e+002 ∗ 0.3948 + 0.2100i 0.2783 + 0.0889i

1.1310 − 0.4500i 2.5500 + 0.2546i

c) >> A^2 ans = 9.0000 + 31.4159i 81.6814 + 8.8858i −7.0711 + 65.0000i 98.0000 + 59.7002i d) >> inv(A) ans = 0.1597 + 0.1917i 0.0829 − 0.0916i

−0.1150 − 0.1042i 0.0549 + 0.0498i

e) >> B^−1 ans = 0 − 0.0817i 0.0681 0 + 0.0285i 0.0318 f) >> inv(B) ∗ inv(A) ans = 0.0213 − 0.0193i −0.0028 + 0.0016i

−0.0048 + 0.0128i 0.0047 − 0.0017i

g) >> (A^2 + B^2) − (A ∗ B) ans = 1.0e + 002 ∗ −0.7948 − 0.8383i 0.7819 + 1.0010i

0.7358 − 2.1611i 1.6700 − 0.6000i

ANMF.CH10_4PP.indd 832

3/1/2023 3:01:14 PM

MATLAB Basics • 833

EXAMPLE 10.18 Use diff command for symbolic differentiation of the following functions: a) S1 = e x

8

b) S2 = 3 x3 e x c)

5

S3 = 5x3 − 7x2 + 3x + 6

Solution: (a) >> syms x >> S1=exp(x^8); >> diff (S1) ans = 8∗x^7∗exp(x^8) (b) >> S2=3∗x^3∗exp(x^5); >> diff (S2) ans = 9∗x^2∗exp(x^5) +15∗x^7∗exp(x^5) (c) >> S3=5∗x^3−7∗x^2+3∗x+6; >> diff (S3) ans = 15∗x^2−14∗x+3 EXAMPLE 10.19 Use MATLAB’s symbolic commands to find the values of the following integrals. a) b) c)

∫ ∫

0.7

0.2 π 0

| x | dx

(cos y + 7 y 2 ) dy

x

d) 7x5 − 6x4 + 11x3 + 4x2 + 8x + 9 e) cos a

ANMF.CH10_4PP.indd 833

3/1/2023 3:01:26 PM

834 • Applied Numerical Methods Using MATLAB Solution: (a) >>syms x, y, a, b >> S1=abs(x) >> int (S1, 0.2, 0.7) ans = 9/40 (b) >> S2=cos (y) +7∗y^2 >> int (S2, 0, pi) ans = 7/3∗pi^3 (c) >> S3=sqrt (x) >> int (S3) ans = 2/3∗x^ (3/2)

>> int (S3,‘a’,‘b’)

ans = 2/3∗b^ (3/2)−2/3∗a^ (3/2) >> int (S3, 0.4, 0.7) ans = 7/150∗70^ (1/2)−4/75∗10^ (1/2) (d) >> S4=7∗x^5−6∗x^4+11∗x^3+4∗x^2+8∗x−9 >> int (S4) ans = 7/6∗x^6−6/5∗x^5+11/4∗x^4+4/3∗x^3+4∗x^2−9∗x (e) >> S5=cos (a) >> int (S5) ans = sin (a)

ANMF.CH10_4PP.indd 834

3/1/2023 3:01:26 PM

MATLAB Basics • 835

EXAMPLE 10.20 Obtain the general solution of the following first-order differential equations: dy a) = 5t − 6y dt b)

d2 y dy +y=0 +3 2 dt dt

c)

ds = Ax3 dt

d)

ds = Ax3 dA

Solution: (a) >> solve (‘Dy=5∗t−6∗y’) ans = 5/6∗t−5/36+exp (−6∗t)∗C1 (b) >> dsolve (‘D2y+3∗Dy+y=0’) ans = C1∗exp (1/2∗(5^ (1/2)−3)∗t) +C2∗exp (−1/2∗(5^ (1/2) +3)∗t) (c) >> dsolve (‘Ds=A∗x^3’,‘x’) ans = 1/4∗A∗x^4+C1 (d) >> dsolve (‘Ds=A∗x^3’,‘A’) ans = 1/2∗A^2∗x^3+C1 EXAMPLE 10.21 Determine the solution of the following differential equations that satisfies the given initial conditions. a) b)

ANMF.CH10_4PP.indd 835

dy = −7x2 dx dy = 5x cos2 y dx

y (1) = 0.7 y (0) = p/4

3/1/2023 3:01:43 PM

836 • Applied Numerical Methods Using MATLAB

c) d)

dy = −y + e3x dx dy + 5y = 35 dt

y (0) = 2 y (0) = 4

Solution: (a) >> dsolve (‘Dy=−7∗x^2’,’y (1) =0.7’) ans = −7∗x^2∗t+7∗x^2+7/10 (b) >> dsolve (‘Dy=5∗x∗cos (y) ^2’,‘y (0) =πi/4’) ans = atan (5∗t∗x+1) (c) >> dsolve (‘Dy=−y+ exp (3∗x)’,‘y (0) =2’) ans = exp (3∗x) +exp (−t)∗(−exp (3∗x) +2) (d) >> dsolve (‘Dy+5∗y=35’,‘y (0) =4’) ans = 7−3∗exp (−5∗t) EXAMPLE 10.22 Given the differential equation

d2 x dx + 7 + 5x = 8 u (t) 2 dt dt

t≥0

Using MATLAB program, find a) x(t) when all the initial conditions are zero b) x(t) when x (0) = 1 and x (0) = 2 Solution: a) x (t) when all the initial conditions are zero >> x = dsolve (‘D2x = −7∗Dx − 5∗x +8’, ‘x (0) = 0’) x = 8/5+ (−8/5−C2)∗exp (1/2∗(−7+29^ (1/2))∗t) +C2∗exp (−1/2∗(7+29^ (1/2))∗t)

ANMF.CH10_4PP.indd 836

3/1/2023 3:01:53 PM

MATLAB Basics • 837

b) x (t) when x (0) = 1 and x (0) = 2 >> x = dsolve (‘D2x = −7∗Dx − 5∗x +8’, ‘x (0) = 1’, ‘Dx (0) = 2’) x = 8/5+ (−3/10−1/290∗29^ (1/2))∗exp (1/2∗(−7+29^ (1/2))∗t)−1/290∗(−1+3∗ 29^ (1/2))∗29^ (1/2)∗exp (−1/2∗(7+29^ (1/2))∗t) EXAMPLE 10.23 Given the differential equation

dx d2 x + 12 + 15x = 35 t ≥ 0 2 dt dt

Using MATLAB program, find a) x(t) when all the initial conditions are zero b) x(t) when x (0) = 0 and x (0) = 1 Solution: a) x (t) when all the initial conditions are zero >> x = dsolve (‘D2x = −12∗Dx − 15∗x +35’, ‘x (0) = 0’) x = 7/3+ (−7/3−C2)∗exp ((−6+21^ (1/2))∗t) +C2∗exp (−(6+21^ (1/2))∗t) b) x (t) when x (0) = 0 and x (0) = 1 >> x = dsolve (‘D2x = −12∗Dx − 15∗x + 35’, ‘x (0) = 0’, ‘Dx (0) = 1’) x = 7/3+ (−7/6−13/42∗21^ (1/2))∗exp ((−6+21^ (1/2))∗t)−1/126∗(−39+7∗21^ (1/2))∗21^ (1/2)∗exp (−(6+21^ (1/2))∗t) EXAMPLE 10.24 Find the inverse of the following matrix using MATLAB. s 2 0    A = 2 s −3  3 0 1 

ANMF.CH10_4PP.indd 837

3/1/2023 3:01:58 PM

838 • Applied Numerical Methods Using MATLAB Solution: >> A = [s 2 0; 2 s −3; 3 0 1]; >> inv (A) ans = [s/(s^2−22), −2/(s^2−22), −6/(s^2−22)] [−11/(s^2−22), s/(s^2−22), 3∗s/(s^2−22)] [−3∗s/(s^2−22), 6/(s^2−22), (s^2−4)/(s^2−22)] EXAMPLE 10.25 Expand the following function F(s) into partial fractions using MATLAB. Determine the inverse Laplace transform of F(s). 1 F(s) = 4 s + 5s3 + 7 s2 The MATLAB program for determining the partial-fraction expansion is given below: Solution:

>> b = [0 0 0 0 1];



>> a = [1 5 7 0 0];



>> [r, p, k] = residue (b, a)

r = p = k = [ ]

ANMF.CH10_4PP.indd 838

0.0510 − 0.0648i 0.0510 + 0.0648i −0.1020 0.1429 −2.5000 + 0.8660i −2.5000 − 0.8660i 0 0

3/1/2023 3:01:59 PM

MATLAB Basics • 839

% From the above MATLAB output, we have the following expression: F(s) =

r4 r3 r1 r2 + + +s−p = s − p1 s − p 2 s − p 3 4

0.0510 + 0.0648 i ) 0.0510 − 0.0648 i + ( s − ( −2.5000 + 0.8660 i ) s − ( −2.5000 − 0.8660 i ) −0.1020 0.1429 + + s−0 s−0 % Note that because the row vector k is zero it implies that there is no constant term in this example. F(s) =

% The MATLAB program for determining the inverse Laplace transform of F(s) is given below: >> syms s >> f = 1/(s^4 + 5∗s^3 + 7∗s^2); >> ilaplace (f) ans = 1/7∗t−5/49+5/49∗exp (−)∗cos (1/2∗3^ (1/2)∗t) +11/147∗exp (−5/2∗t)∗3^ (1/2)∗sin(1/2∗3^(1/2)∗t) EXAMPLE 10.26 Expand the following function F(s) into partial fractions using MATLAB. Determine the inverse Laplace transform of F(s). F(s) =

5s2 + 3s + 6 s4 + 3s3 + 7 s2 + 9s + 12

Solution: The MATLAB program for determining the partial-fraction expansion is given below: >> b = [0 0 5 3 6]; >> a = [1 3 7 9 12]; >> [r,p,k] = residue(b,a) r = −0.5357 − 1.0394i −0.5357 + 1.0394i 0.5357 − 0.1856i 0.5357 + 0.1856i

ANMF.CH10_4PP.indd 839

3/1/2023 3:02:26 PM

840 • Applied Numerical Methods Using MATLAB p = k = [ ]

−1.5000 + 1.3229i −1.5000 − 1.3229i −0.0000 + 1.7321i −0.0000 − 1.7321i

% From the above MATLAB output, we have the following expression: F(s) =

r3 r4 r2 r1 + + + = s − p1 s − p 2 s − p 3 s − p 4

F(s) =

−0.5357 − 1.0394 i ( −0.5357 + 1.0394i ) + s − ( −1.500 + 1.3229 i ) s − ( −1.5000 − 1.3229 i )

0.5357 − 0.1856 i 0.5357 − 0.1856 i + s − ( −0 + 1.7321i ) s − ( −0 − 1.7321i ) % Note that because the row vector k is zero it implies that there is no constant term in this example. +

% The MATLAB program for determining the inverse Laplace transform of F(s) is given below: >> syms s >> f = (5∗s^2 + 3∗s +6)/(s^4 + 3∗s^3 + 7∗s^2 + 9∗s +12); >> ilaplace(f) ans = 11/14∗exp(−3/2∗t)∗7^(1/2)∗sin(1/2∗7^(1/2)∗t)−15/14∗exp(−3/2∗t)∗ cos(1/2∗7^(1/2)∗t)+3/14∗3^(1/2)∗sin(3^(1/2)∗t)+15/14∗cos(3^(1/2)∗t) EXAMPLE 10.27 For the following function F(s):

s4 + 3 s3 + 5 s2 + 7 s + 25

F(s) = 4 s + 5 s3 + 20 s2 + 40 s + 45

Using MATLAB, find the partial-fraction expansion of F(s). Also, find the inverse Laplace transformation of F(s). Solution:

ANMF.CH10_4PP.indd 840

s4 + 3 s3 + 5 s2 + 7 s + 25

F(s) = 4 s + 5 s3 + 20 s2 + 40 s + 45

3/1/2023 3:02:54 PM

MATLAB Basics • 841

The partial-fraction expansion of F(s) using MATLAB program is given as follows: >>num = [1 3 5 7 25]; >>den = [1 5 20 40 45]; >>[r,p,k] = residue(num,den) r = −.3849 + 1.2313i −1.3849 − 1.2313i 0.3849 − 0.4702i 0.3849 + 0.4702i p = −0.8554 + 3.0054i −0.8554 − 3.0054i −1.6446 + 1.3799i −1.6446 − 1.3799i k = 1 From the MATLAB output, the partial-fraction expansion of F(s) can be written as follows:

F( s) = F( s) =



+

r3 r1 r2 r4 + + + +k ( s − p1 ) ( s − p2 ) ( s − p3 ) ( s − p4 ) (−1.3849 + j1.2313) ( s + 0.8554 − j3.005)

+

(0.3849 − j0.4702) ( s + 1.6446 − j1.3799)

(−1.3849 − j1.2313) ( s + 0.8554 + j3.005) +

(0.3849 + j0.4702) ( s + 1.6446 + j1.3779)

+1

EXAMPLE 10.28 Obtain the partial-fraction expansion of the following function using MATLAB:

F(s) =

8( s + 1)( s + 3)

( s + 2)( s + 4)( s + 6)2

Solution:

ANMF.CH10_4PP.indd 841

F(s) =

8( s + 1)( s + 3)

( s + 2)( s + 4)( s + 6)2

=

(8 s + 8)( s + 3) 2 ( s + 6 s + 8)( s2 + 12 s + 36)

3/1/2023 3:03:13 PM

842 • Applied Numerical Methods Using MATLAB The partial fraction expansion of F(s) using MATLAB program is given as follows: >> num=conv ([8 8], [1 3]); >> den=conv ([1 6 8], [1 12 36]); >> [r,p,k]=residue(num,den) r = p =

3.2500 15.0000 −3.0000 −0.2500 −6.0000 −6.0000 −4.0000 −2.0000

k = [ ] From the above MATLAB result, we have the following expansion:

F(s) =



F(s) =

r3 r1 r2 r4 + + + +k ( s − p1 ) ( s − p2 ) ( s − p3 ) ( s − p4 ) 3.25 ( s + 6)

+

15 ( s − 15)

+

−3 ( s + 3)

+

−0.25 ( s + 0.25)

+0

It should be noted here that the row vector k is zero because the degree of the numerator is lower than that of the denominator.

F(s) = 3.25 e −6 t + 15 e15 t − 3 e −3 t − 0.25 e −0.25 t

EXAMPLE 10.29 Generate partial-fraction expansion of the following function

F(s) =

10 5 ( s + 7)( s + 13) s( s + 25)( s + 55)( s2 + 7 s + 75)( s2 + 7 s + 45)

Solution: Generate the partial fraction expansion of the following function: >>numg=poly[−7 −13]; >>numg=poly([−7 −13]);

ANMF.CH10_4PP.indd 842

3/1/2023 3:03:24 PM

MATLAB Basics • 843

>>deng=poly([0 −25 −55 roots([1 7 75])’ roots([1 7 45])’]); >>[numg,deng]=zp2tf(numg’,deng’,1e5); >>Gtf=(numg,deng); >>Gtf=tf(numg,deng); >>G=zpk(Gtf); >>[r,p,k]=residue(numg,deng) r = 1.0e−017∗ 0.0000 −0.0014 0.0254 −0.1871 0.1621 −0.0001 0.0000 0.0011 p = 1.0e+006∗ 4.6406 1.4250 0.3029 0.0336 0.0027 0.0001 0.0000  0 k = [ ]

10.23 SUMMARY In this chapter, the MATLAB environment which is an interactive environment for numeric computation, data analysis, and graphics was presented. Arithmetic operations, display formats, elementary built-in functions, arrays, scalars, vectors or matrices, operations with arrays including dot product, array multiplication, array division, inverse and transpose of a matrix, determinants, element-by-element operations, eigenvalues and eigenvectors, random number generating functions, polynomials, system of linear equation, script files, programming in MATLAB, the commands used for printing information and generating 2-D and 3-D plots, input/output in MATLAB was presented with

ANMF.CH10_4PP.indd 843

3/1/2023 3:03:24 PM

844 • Applied Numerical Methods Using MATLAB illustrative examples. MATLAB’s functions for symbolic mathematics were introduced. These functions are useful in performing symbolic operations and developing closed-form expressions for solutions to linear algebraic equations, ordinary differential equations and systems of equations. Symbolic mathematics for determining analytical expressions for the derivative and integral of an expression was also presented.

REFERENCES Chapman, S.J., MATLAB Programming for Engineers, 2nd ed., Brooks/Cole, Thomson Learning, Pacific Grove, CA, 2002. Dukkipati, R. V., Analysis and Design of Control Systems Using MATLAB, New Age International Publishing, New Delhi, India, 2006. Dukkipati, R. V., and Shivakumar, M. R., MATLAB for Electrical Engineers, New Age International Publishing, New Delhi, India, 2007. Dukkipati, R. V., and Srinivas, J., Solving Engineering Mechanics Problems With MATLAB, New Age International Publishing, New Delhi, India, 2007. Dukkipati, R. V., MATLAB for Engineers, New Age International Publishing, New Delhi, India, 2006. Dukkipati, R. V., Solving Engineering System Dynamics Problems With MATLAB, New Age International Publishing, New Delhi, India, 2006. Dukkipati, R. V., Solving Vibration Analysis Problems With MATLAB, New Age International Publishing, New Delhi, India, 2006. Etter, D. M., Engineering Problem Solving With MATLAB, Prentice-Hall, Englewood Cliffs, NJ, 1993. Gilat, A., MATLAB: An Introduction With Applications, 2nd ed., Wiley, New York, 2005. Hanselman, D., and Littlefield, B. R., Mastering MATLAB 6, Prentice Hall, Upper Saddle River, NJ, NJ, 2001. Herniter, M. E., Programming in MATLAB, Brooks/Cole, Pacific Grove, CA, 2001. Magrab, E. B., An Engineers Guide to MATLAB, Prentice Hall, Upper Saddle River, NJ, 2001. Marchand, P., and Holland, O. T., Graphics and GUIs with MATLAB, 3rd ed., CRC Press, Boca Raton, FL, 2003. Moler, C., The Student Edition of MATLAB for MS-DOS Personal Computers With 3-1/2” Disks, MATLAB Curriculum Series, The MathWorks, Inc., Natick, MA, 2002.

ANMF.CH10_4PP.indd 844

3/1/2023 3:03:24 PM

MATLAB Basics • 845

Palm, W. J. III., Introduction to MATLAB 7 for Engineers, McGraw Hill, New York, 2005. Pratap, R., Getting Started With MATLAB: A Quick Introduction for Scientists and Engineers, Oxford University Press, New York, 2002. Sigman, K., and Davis, T. A., MATLAB Primer, 6th ed., Chapman & Hall/ CRCPress, Boca Raton, FL, 2002. The MathWorks, Inc., MATLAB: Application Program Interface Reference Version 6, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Creating Graphical User Interfaces, Version 1, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Function Reference, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Release Notes for Release 12, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Symbolic Math Toolbox User’s Guide, Version 2, The MathWorks, Inc., Natick, MA, 1993−1997. The MathWorks, Inc., MATLAB: Using MATLAB Graphics, Version 6, The MathWorks, Inc., Natick, MA, 2000.

EXERCISES NOTE

In addition to solving the following exercises through analytical solutions or long-hand numerical computations, try to solve them using user-defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 10.1. Compute the following quantity using MATLAB in the Command Window:

17  5  1



57 log10 (e3 )

 ln(e4 )  11 152  13 2   121 10.2. Compute the following quantity using MATLAB in the Command Window: tan x + sin 2 x + log x 5 − x2 + cosh x − 2 tanh x cos x for x = 5π/6.

ANMF.CH10_4PP.indd 845

B=

3/1/2023 3:03:33 PM

846 • Applied Numerical Methods Using MATLAB 10.3. Compute the following quantity using MATLAB in the Command Window: 14 b log10 c ab (a + b) a x= a+ + c + 3 c + ln(2) + e c | ab | log10 (a + b + c)   + 2 sinh a − 3 tanh b

for a = 1, b = 2 and c = 1.8.

10.4. Use MATLAB to create a) a row and column vectors that has the elements: 11, −3, e7.8, ln(59), tan(π/3), 5 log10(26) b) a row vector with 20 equally spaced elements in which the first element is 5 c) a column vector with 15 equally spaced elements in which the first element is −1 10.5. Enter the following matrix A in MATLAB and create:



1 9  A = 17   25 333

2 10 18 23 34

3 11 19 27 35

4 12 20 28 36

5 13 21 29 37

6 14 22 30 38

7 15 23 31 39

8 16   24   32  40 

(a) a 4x5 matrix B from the 1st, 3rd, and the 5th rows, and the 1st, 2nd, 4th, and 8th columns of the matrix A (b) a 16 element-row vector C from the elements of the 5th row, and the 4th and 6th columns of the matrix A

(

10.6. Given the function y = x

2 + 0.02

+ ex

)

1.8

ln x . Determine the value of

y for the following values of x: 2, 3, 8, 10, −1, −3, −5, −6.2. Solve the problem using MATLAB by first creating a vector x, and creating a vector y, using element-by-element calculations. 10.7. Define a and b as scalars, a = 0.75, and b = 11.3, and x, y and z as the vectors, x = 2, 5, 1, 9, y = 0.2, 1.1, 1.8, 2 and z = −3, 2, 5, 4. Use these variables to calculate A given below using element-by-element computations for the vectors with MATLAB.

ANMF.CH10_4PP.indd 846

3/1/2023 3:03:43 PM

MATLAB Basics • 847



 z y  +  x1.1 y−2 z5 x 2 A= + a (a + b)b / 3 za

10.8. Enter the following three matrices in MATLAB and show that

 1 2 3  7 13 4  12 5 4        A   8 5 7  B   7 11 6  C   2 8 5   8 4 6   1 8 13   9 6 11  a) A + B = B + A b) A + (B + C) = (A + B)+C c) 7(A + C) = 7(A) + 7(C) d) A ∗ (B + C) = A ∗ B + A ∗ C

10.9. Consider the polynomials p1(s) = s3 + 5s2 + 3s + 10 p2(s) = s4 + 7s3 + 5s2 + 8s + 15 p3(s) = s5 + 15s4 + 10s3 + 6s2 + 3s + 9

Determine p1(2), p2(2), and p3(3)

10.10. The following polynomials are given: p1(x) = x5 + 2x4 − 3x3 + 7x2 − 8x + 7 p2(x) = x4 + 3x3 − 5x2 + 9x + 11 p3(x) = x3 − 2x2 − 3x + 9 p4(x) = x2 − 5x + 13 p5(x) = x + 5

Use MATLAB functions with polynomial coefficient vectors to evaluate the expressions at x = 2.

10.11. Determine the roots of the following polynomials: a) p1(x) = x7 + 8x6 + 5x5 + 4x4 + 3x3 + 2x2 + x + 1 b) p2(x) = x6 − 7x6 + 7x5 + 15x4 − 10x3 − 8x2 + 7x + 15 c) p3(x) = x5 − 13x4 + 10x3 + 12x2 + 8x − 15

ANMF.CH10_4PP.indd 847

3/1/2023 3:03:51 PM

848 • Applied Numerical Methods Using MATLAB d) p4(x) = x4 + 7x3 + 12x2 − 25x + 8 e) p5(x) = x3 + 15x2 − 23x + 105 f) p6(x) = x2 − 18x + 23 g) p7(x) = x + 7 10.12. An aluminum thin-walled sphere is used as a marker buoy. The sphere has a radius of 65cm and a wall thickness of 10 mm. The density of aluminum is 2700 kg/m3. The buoy is placed in the ocean where the density of the water is 1050 kg/m3. Determine the height H between the top of the buoy and the surface of the water.

H

r

FIGURE 10.38

10.13. Write a simple script file to find (a) dot product (b) cross-product of 2 vectors: a = ˆj − kˆ and b = 3 iˆ − 2 ˆj 10.14. Write a function to the find gradient of f(x, y) =x2+y2−2xy+4 at (a) (1, 1) (b) (1,−2) and (c) (0,−3). Use the function name from the command prompt. 10.15. Write MATLAB functions f = x2 − 3x + 1 and g = ex − 4x + 6 and find the result f(127)/g(5) from a script file. 10.16. Plot the function y = |x| cos (x) for −200 ≤ x ≤ 200.

ANMF.CH10_4PP.indd 848

3/1/2023 3:03:59 PM

MATLAB Basics • 849

10.17. Plot the following functions on the same plot for 0 ≤ x ≤ 2π using the plot function: a) sin2(x) b) cos2x c) cos(x) 10.18. Plot a graph of the function y = 45 sin(0.4t) for t ∈[0, 3]. 10.19. Consider the function z = 0.56 cos(xy). Draw a surface plot showing variation of z with x and y. Given x∈[0, 10] and y∈[0,100] 10.20. Figure 10.39 shows two boats: boat A travels south at a speed of 10 mph, and boat B travels east at a speed of 19 mph. The figure shows the position of the ships at 8 a.m. Write a MATLAB program to plot the distance between the ships as a function of time for the next 5 hours.

y Boat A

16 miles

Boat B

x 30 miles

FIGURE 10.39

10.21. Consider the given symbolic expressions defined below: S1 = ‘2/(x − 5)’; S2 = ‘x ^ 5 + 9 ∗ x − 15’; S3 = ‘(x ^ 3 + 2 ∗ x +9) ∗ (x ∗ x − 5)’;

Perform the following symbolic operations using MATLAB. a) S1S2/S3 S1/S2S3 b) S1/(S2)2 d) S1S3/S2 c) (S2)2/(S1S3)

ANMF.CH10_4PP.indd 849

3/1/2023 3:04:00 PM

850 • Applied Numerical Methods Using MATLAB 10.22. Figure 10.40 shows a scale with two springs.

b

k

k

k

k

x

a

W FIGURE 10.40

The two springs are unstretched initially and will stretch when a mass is attached to the ring and the ring will displace downwards a distance of x. The weight W of the object is given by 2k ( −  0 )(b + x) W =  where l0 = initial length of a spring = a 2 + b2 and l = t he stretched length of the spring = a 2 + (b + x)2 . If k = spring constant, write a MATLAB program to determine the distance x when W = 350 N. Given: a = 0.16 m, b = 0.045 m, and the spring constant k = 3000 N/m. 10.23. Determine the solutions of the following first-order ordinary differential equations using MATLAB’s symbolic mathematics. a) y′ = 8x2 + 5 with initial condition y(2) = 0.5 b) y′ = 5x sin2(y) with initial condition y(0) = π/5 c) y′ = 7x cos2(y) with initial condition y(0) = 2 d) y′ = −5x + y with initial condition y(0) = 3 e) y′ = 3y + e−5x with initial conditiy (y (0) = 2 10.24. For the following differential equations, use MATLAB to find x (t) when (a) all the initial conditions are zero, (b) x (t) when x (0) = 1 and x (0) = −1. d2 x dx a) + 10 + 5 x = 11 2 dt dt b)

ANMF.CH10_4PP.indd 850

d2 x dx − 7 − 3x = 5 2 dt dt

3/1/2023 3:04:14 PM

MATLAB Basics • 851

c)

d2 x dx + 3 + 7 x = −15 2 dt dt

d)

d 2 x dx + + 7 x = 26 dt 2 dt

10.25. Figure 10.41 shows a water tank (shaped as an inverted frustum cone with a circular hole at the bottom on the side).

R=0.5m

3m y rh=0.025 R=2m FIGURE 10.41  Water tank.

The velocity of water discharged through the hole is given by v = 2gy where h = height of the water and g=acceleration due to gravity (9.81 m/s2). The rate of discharge of water in the tank as the water drains out through the hole is given by

2gyrh2 dy where y = height of water and rh =  2 dt 2 0 5 .  y  

radius of the hole. Write a MATLAB program to solve and plot the differential equation. Assume, that the initial height of the water is 2.5 m. 10.26. An airplane uses a parachute (see Figure 10.42) and other means of braking as it slows down on the runway after landing. The acceleration of the airplane is given by a = −0.005v2−4 m/s2

ANMF.CH10_4PP.indd 851

3/1/2023 3:04:23 PM

852 • Applied Numerical Methods Using MATLAB

v x

FIGURE 10.42



Consider the airplane with a velocity of 500 km/h opens its parachute and starts decelerating at t = 0 seconds and write a MATLAB program to solve the differential equation and plot the velocity from t = 0 seconds until the airplane stops.

10.27. Obtain the first and second derivatives of the following functions using MATLAB’s symbolic mathematics. a) F(x) = x5 − 8x4 + 5x3 − 7x2 + 11x − 9 b) F(x) = (x3 + 3x − 8)(x2 + 21) c) F(x) = (3x3 − 8x2 + 5x + 9)/(x + 2) d) F(x) = (x5 − 3x4 + 5x3 + 8x2 − 13)2 e) F(x) = (x2 + 8x −11)/(x7 − 7x6 + 5x3 + 9x − 17) 10.28 Determine the values of the following integrals using MATLAB’s symbolic functions. a) b) c) d) e)

∫ (5x

)

7 − x 5 + 3 x 3 − 8 x 2 + 7 dx

∫ x cos x ∫ x sin 2 x ∫ x sin x dx ∫ x dx 2/3

1.8

2

2

0.2 −0.2 −1

5

10.29 Use MATLAB to calculate the following integral: 10.30 Use MATLAB to calculate the following integral:

∫ 0.8 x 0

2

1 dx + 0.5 x + 2

10

∫ cos (0.5x)sin (0.5x)dx 2

4

0

ANMF.CH10_4PP.indd 852

3/1/2023 3:04:46 PM

MATLAB Basics • 853

10.31 The variation of gravitational acceleration g with altitude y is given by:

g=

R2 g o , where R = 6371 km is radius of the earth and (R + y )2

go = 9.81 m/s2 is gravitational acceleration at sea level. The change in the gravitational potential energy ΔU of an object that is raised up y



from the earth is given by: ∆U = ∫ mgdy. Determine the change in the 0



potential energy of a satellite with a mass of 500 kg that is raised from the surface of the earth to a height of 800 km.

10.32 Consider the two matrices  1 0 2 A =  2 5 4   and B =    −1 8 7 

 7 8 2  3 5 9    −1 3 1  Using MATLAB, determine the following: a) A + B b) AB c) A2 d) AT e) B−1 f) BTAT g) A2 + B2 − AB h) determinant of A, determinant of B and determinant of AB

10.33 Use MATLAB to define the following matrices:

ANMF.CH10_4PP.indd 853



2 1  A =  0 5  B =   7 4 



2 3 C =  −5 −2     0 3 

5 3  −2 −4   

D = [1  2]

3/1/2023 3:05:04 PM

854 • Applied Numerical Methods Using MATLAB

Compute matrices and determinants if they exist. a) (ACT)−1 b) |B| c) |ACT| d) (CTA)−1

10.34 Consider the two matrices

 1 0 1   A =  2 3 4   and B =  −1 6 7 



Using MATLAB, determine the following:

 7 4 2    3 5 6  −1 2 1 

a) A + B b) AB c) A2 d) AT e) B−1 f) BTAT g) A2 + B2 − AB h) det A, det B, and det of AB. 10.35 Find the inverse of the following Matrices: 3 2 1 a) A =  −1 5 4     5 7 −9   1 6 3 b) B =  −4 −5 7     8 4 2   −1 −2 5  c) C =  −4 7 2     7 −8 −1

ANMF.CH10_4PP.indd 854

3/1/2023 3:05:13 PM

MATLAB Basics • 855

10.36 Determine the inverse of the following matrix using MATLAB. 0  3 s 2  A = 7 s  s 5     3 0 3 s 

10.37 Expand the following function F(s) into partial fractions with MATLAB:

ANMF.CH10_4PP.indd 855

5 s3 + 7 s2 + 8 s + 30

F(s) = 4 s + 15 s3 + 62 s2 + 85 s + 25

3/1/2023 3:05:21 PM

ANMF.CH10_4PP.indd 856

3/1/2023 3:05:21 PM

CHAPTER

11

Optimization 11.1 INTRODUCTION Optimization is the term often used for minimizing or maximizing a function. It is sufficient to consider the problem of minimization only; maximization of F(x) is achieved by simply minimizing – F(x). In engineering, optimization is closely related to design. The function F(x), called the merit function or ­objective function, is the quantity that we wish to keep as small as possible, such as cost or weight. The components of x are known as the design variables. Optimization deals with finding the maxima and minima of a function that depends on one or more variables. The goal is to determine the values of the variables that yield maxima or minima for the function. These can be substituted back into the function to compute its optimal values.

f’(x) = 0 f”(x) < 0

f(x)

Maximum

f(x) = 0 0

Root Root

Minimum

Root

x

f’(x) = 0 f”(x) > 0

FIGURE 11.1 Difference between roots and optima.

ANMF.CH11_4PP.indd 857

3/1/2023 3:06:19 PM

858 • Applied Numerical Methods Using MATLAB As shown in Figure 11.1, the optimums are the points where the curve is flat. Root location involves searching for the location where the function equals zero. Optimization involves searching for the function’s extreme points. In analytical terms, the optimums correspond to the x value where the derivative f′(x) is equal to zero. In addition, the second derivative, f″(x), indicates whether the optimum is a minimum or maximum: if f″(x) < 0, the point is a maximum; if f″(x) > 0, the point is a minimum. Design decisions are quite often made by means, based on the past experience of the designer, in an attempt to produce optimal design. It should be possible, however, to rationalize a significant amount of this decision-making process by basing it on analytical techniques. For a given specification, there may be several possible design configurations or options. The choice of a particular design option can be done either by the past experience of the designer or by an analytical technique, where numbers are attached to each possible option which can be optimized. These numbers can be assigned for various aspects of a design option such as the aesthetic value, simplicity in the production process, availability of required materials, and so on. Once the design option is chosen, the detailed design can be carried out by specifying the design variables such as the diameter of a shaft, length and cross-sectional dimensions of a load-carrying member, or viscosity of a lubricant. An optimization or objective function is set up to define the total value in terms of the design variables given as U(x1, x2, …., xn) = maximum (or minimum)

(11.1)

In certain design situations, the design variables, xi, may not be completely free to be varied and may be subject to constraints, which may be an equality type or inequality type given by

yi(x1, x2, …., xn) = 0   i = 1, m

(11.2)



fj(x1, x2, …., xn) ≥ 0   j = 1, p

(11.3)

Numerical procedures from the mathematics of operations research can be used to adjust the design variables so that the constraints are satisfied.

ANMF.CH11_4PP.indd 858

3/1/2023 3:06:19 PM

Optimization • 859

In general, the expression for the objective function and the constraints in terms of the design variables are nonlinear in nature. We will discuss some techniques of obtaining the optimum value for the objective function. The methods discussed below are not exhaustive and books on optimization must be referred to for an in-depth study. An attempt is made in this chapter to give a glimpse of the optimization procedure. In this chapter, we present how optimization can be used to determine the minima and maxima of both one-dimensional and multi-dimensional functions. Specifically, we study the distinction between global and local optima, the golden ratio, locating the optimum of a single-variable function with the golden-section search, the unconstrained minimization of functions, minimization with constraints using Lagrange multipliers, numerical optimization, optimization involving single variables, gradient methods, Newton’s method, methods based on the concept of quadratic convergence, Powell’s method, the Fetcher-Reeves method, direct search methods such as the Hooke and Jeeves method, and method of successive approximation. The MATLAB-built in fminbnd function to determine the minimum of a onedimensional functions, and the fminsearch function to determine the minimum of a multidimensional function are described in Section 11.14.

11.2 UNCONSTRAINED MINIMIZATION OF FUNCTIONS Consider an unconstrained design problem, represented by U(x1, x2, …., xn) = minimum

(11.4)

The conditions of an extremum given by

dU = 0    i = 1, 2, …., n dx i

(11.5)

These are n number of simultaneous nonlinear algebraic equations which must be solved. The Newton’s method of finding the roots of equations can be employed here, however, obtaining a good initial approximation is a tedious task. Further, out of the n possible solutions, a particular solution yielding a minimum must be found. The sign of the second derivative must be found to ensure that the roots correspond to minima and not maxima or saddle points.

ANMF.CH11_4PP.indd 859

3/1/2023 3:06:20 PM

860 • Applied Numerical Methods Using MATLAB

11.3 MINIMIZATION WITH CONSTRAINTS USING LAGRANGE MULTIPLIERS Considering only equality constraints, to start with and assuming that they cannot be used to eliminate some of the variables, the design problem can be given as U(x1, x2, …., xn) = maximum (or minimum)

yj(x1, x2, …., xn) = 0   j = 1, 2, …., m

(11.6) (11.7)

The condition for the optima is given as

U m  j  j  0    i = 1, 2, …, n x i j1 x i

(11.8)

where lj are m number of Lagrange multipliers. Equation (11.8) may be expressed in an alternative form as  x i

m    i  j   0    i = 1, 2, …., n (11.9) U    i 1   There are n equations in either Equations (11.8) or (11.9) and along with the constraint equations. There are (n + m) equations in the n design variables and m Lagrange multipliers which must be solved simultaneously.



The inequality constraints can be handled by converting them into equality constraints by the use of slack variables. Let the inequality constraint be given by

fk(x1, x2, …., xn) ≥ 0

(11.10)

which can be expressed as an equality constraint in the form

k  a 2k  0 (11.11)

where ak is a slack variable introduced which must be adjusted for the solution so that Equation (11.11) is satisfied. The slack variables are squared to prevent it from dropping out when the derivative is taken and to maintain the slack positive even if ak goes negative. The set of simultaneous equations to be solved now have the form

ANMF.CH11_4PP.indd 860

3/1/2023 3:06:27 PM

Optimization • 861



p m    2 U    j j    k (k  a k )  0    i = 1, 2, …., n x i  j 1 k 1 

(11.12)



p m    2 U    j j    k (k  a k )  0   l = 1, 2, …., p a l  j 1 k 1 

(11.13)



yj = 0   j = 1, 2, …., m

(11.14)



k  a 2k  0    k = 1, 2, …., p

(11.15)

The negative sign in the third term of Equation (11.12) is to make lk positive. It may be noted that Equation (11.13) reduced to lk ak = 0   k = 1, 2, …., p



(11.16)

requiring that either lk or ak is zero for all k. EXAMPLE 11.1 Design a tablet containing 1 ml of medicine. It is desired to have maximum possible surface area for fast solubility. From manufacturing point of view no dimension should be less than 2 mm. For two possible shapes of the tablet, formulate the task as an optimization problem. What will be the basis for final design selection? Solution: Let A = area in square centimeters

h = height in cm



d = diameter in cm and



v = volume in cubic centimeters

Design 1: Let the shape be that of a cylinder. Then we can formulate the constrained optimization problem as follows: Maximize the surface area, A = pd (h + d/2) subject to

ANMF.CH11_4PP.indd 861

V = pd2h/4 = 1

(since 1 ml = 1 cubic cm)

3/1/2023 3:06:32 PM

862 • Applied Numerical Methods Using MATLAB The side constraints on the design variables are d ≥ 0.2 h ≥ 0.2 The Lagrange function L can be written as d  L  d  h    1 [d 2 h  4] 2 



where l1 is the Lagrange multiplier. Further, L  h  d  2 dh1  0 d Solving this equation for l1, 1 



(h  d) (E.1) 2dh

Similarly, we have L  d  1 d 2  0 h Solving this equation for l1, 1 1   (E.2) d

Similarly,

L  d 2 h  4  0 1

Solving this equation for h, 4 (E.3) d 2 From Equations (E.1) and (E.2), we get

h

h=d Substituting this in Equations (E.1), we get h3 = 4/p which gives h* = d* = 1.084 cm.

ANMF.CH11_4PP.indd 862

3/1/2023 3:06:52 PM

Optimization • 863

Design 2: Let the shape of the table be a sphere. Then the constrained optimization problem can be stated as follows:

Maximize A = pd2



subject to

3

V

4 d    1 3 2

The side constraint on the design variable is d ≥ 0.2 Now the Lagrange function L can be written as   L  d 2    d 3  1  0 6   L  3  d 1  0  6 Solving this equation gives d* = 1.241 cm. Design 3: Let the tablet has the shape of a cylinder with hemispherical ends. The constrained optimization problem can be stated as follows: A = pd(l – d) + pd2



Maximize



subject to V 

d 3 d 2 l d 3   1 6 4 4

where l is the total length from the end to end. The side constraints on the design variables are d ≥ 0.2 l ≥ 0.2 The Lagrange function L can be written as  d 3 d 2 l d 3  L  d(l  d)  d 2       1 4 4  6 

ANMF.CH11_4PP.indd 863

3/1/2023 3:07:03 PM

864 • Applied Numerical Methods Using MATLAB

Calculating

L  0 and simplifying, we get d l – l[0.25d2 – 0.5dl] = 0

Solving this equation, we get 



l (E.4) 0.25d  0.5dl 2

L d 2  d   0 4 l Solving this equation gives l = –4/d



(E.5)

From Equations (E.4) and (E.6), we get l = d



(E.6)

That means the design turns out to be a hollow sphere. There is no need to proceed further, since we have the optimum design (as per the design 2) as d* = 1.241 cm Now let us compare the two optimum designs: Design

Surface Area (CC)

Cylinder h = 1.084 cm; d = 1.084 cm

5.537

Sphere d = 1.241 cm

4.838

The basis for the best design should be maximum area for fast solubility. The cylindrical shaped tablet has more surface area than spherical shaped tablet, and hence the cylindrical shape is preferred.

11.4 NUMERICAL OPTIMIZATION In Sections 11.1 and 11.2, analytical methods of finding the minimum of a function with or without constraint were discussed. Vast majority of design optimization problems are quite difficult to solve using the above analytical

ANMF.CH11_4PP.indd 864

3/1/2023 3:07:10 PM

Optimization • 865

techniques. Numerical techniques using computers must be employed to solve many engineering optimization problems. In the following section, several such numerical methods will be developed. 11.4.1

Optimization Involving Single Variables

Even though problems involving optimization of a function with just one variable occur rarely in practice, the technique discussed here will apply in several strategies used for nonlinear programming involving optimization of function of several variables that will be discussed later. Let the function to be minimized be U(x). Initially, the minimum is bracketed in a small interval by using the incremental search technique. The function is evaluated at x0, x0 + h, x0 + 2h, …., and so on, where h is incremental step. If Uj–1 < Uj < Uj+1 where Uj = U(x0 + jh), then the minimum is bracketed between x0 + (j – 1) h and x0 + (j + 1) h. In doing this search, if U1 < U0, the search is continued with x = x0 + 2h, x0 + 3h, and so on in the same direction. Otherwise, the search must be done in the opposite direction with x = x0 – h, x0 – 2h, and so on. Once the region of the minimum is bracketed, the bounds of this region can be squeezed and narrowed down to pinpoint the minimum more accurately. This can be done in an iterative fashion by making two interior evaluations in the interval containing the minimum. This is shown in the Figure 11.2 which represents a plot of U(x) versus x in the interval. After i number of iterations the lower and upper bounds of the interval are designated as li and ui. Let ai and bi be the points within the interval symmetrically located so that ai – li = ui – bi (11.17) The three possible situations are distinguished by evaluating U(ai) and U(bi) and are shown by curves 1, 2, and 3 in Figure 11.2.

ANMF.CH11_4PP.indd 865



Case 1.  U(ai) < U(bi) : minimum is in the region li < x < bi



Case 2.  U(ai) > U(bi) : minimum is in the region ai < x < ui



Case 3.  U(ai) = U(bi) : minimum is in the region ai < x < bi

3/1/2023 3:07:10 PM

866 • Applied Numerical Methods Using MATLAB

U(x) 1 2

ai

l0

x

u0

bi

3

FIGURE 11.2 Plot of function over the established interval

The procedure to choose the interior points ai and bi is explained with the aid of Figure 11.3. Designating ui – li = di bi – li = ui – ai = di +1

ai – li = ui – bi = di + 2 (11.18)

i δi+2 l

i+1 3

1 i+1

2

u δi+2

FIGURE 11.3 A strategy for locating new interior points.

and noting that ai and bi are located symmetrically, we have

ANMF.CH11_4PP.indd 866

di+1 + di+2 = di (11.19)

3/1/2023 3:07:10 PM

Optimization • 867

Denoting 

 i 1   (11.20) i

and eliminating di between Equations (12.19) and (12.20), we obtain

i  2 1    (11.21)  i 1 

If case 1 holds good then the minimum is between li and bi and the point ai is located in this region, whereas if case 2 is true then the minimum is between ai and ui, and bi will be a point in the interior of this region. In either case, if we want to use one such interior point which is already available, say, ai for case 1 and bi for c 2, then we must have the relation

i  2   (11.22)  i 1

according to Equation (11.20). Hence, from Equations (11.21) and (11.22) we have

1   (11.23) 

Solving Equation (11.23) we get a = 0.618034. Hence, knowing the bounds of the region containing the minimum, li and ui, the interior point ai and bi can be obtained using a = 0.618034 in Equations (11.18) and (11.20) as b1 = l1 + ad1 (11.24) The other interior point is symmetrical and from Equation (11.17) or Equation (11.18) it is obtained as ai = li + (ui – bi) (11.25) Now, function evaluations are made at ai and bi to obtain U(ai) and U(bi). After determining which case is satisfied, we can identify the region of the minimum on either between li and bi for case 1 or between ai and ui for case 2 or between ai and bi for case 3. We can redesignate the new bounds as li+1 and ui+1. For case 1, a1 is one interior point which can be redesignated as ai+1 and bi+1 is obtained by symmetrically placing it as bi+1 = li+1 + (ui+1 – ai+1) (11.26)

ANMF.CH11_4PP.indd 867

3/1/2023 3:07:16 PM

868 • Applied Numerical Methods Using MATLAB For case 2, bi is one interior point which can be called bi+1 and ai+1 is obtained as ai+1 = li+1 + (ui+1 – bi+1) (11.27) For case 3, both interior points must be found out using Equations (11.24) and (11.25). A second approach is to use polynomial approximation. If we define x as the coordinate for the direction of search, then we can approximate the function to be minimized by n

U(x)   a i x i i 0

In order to evaluate the airs, it is necessary to evaluate U(x) at n +1 successive values of x. Substituting corresponding known values in Equation (11.28), we get n linear algebraic equations which can be solved for the ai’s. The minimum is then given by solving n

U(x)   a i x i (11.28)



i 0

n

a i x

which is

i 1

i

i 1

 0 (11.29)

Explicit solutions are easily obtained for n equal to 2 or 3, and these are the common polynomials used. If the minimum is not bracketed, it is desirable to test for a true minimum using the sign of the derivative of Equation (11.29) at the apparent minimum. U

x x1

x2

x3

x4

x

FIGURE 11.4 Points used for polynomial approximation of a minimum along a line.

ANMF.CH11_4PP.indd 868

3/1/2023 3:07:22 PM

Optimization • 869

If the polynomial has a degree higher than a cubic, Equation (11.29) could be solved by the Newton-Raphson method. There are many strategies for generating the n + 1 values of x required for the polynomial approximation. The simplest is to take equally spaced points inside the region containing the minimum. Alternatively, if the previously described iterative method is used up to the ith iteration, then the points li, ai, bi and ui can be used to obtain the cubic. Defining these points as x1, x2, x3 and x4 for simplicity, the Lagrangian polynomial of degree 3 (cubic) is given by

4

P3 (x)   L i (x) U(x i ) (11.30) i 1

where,

4

L i (x )   j 1 i j

(x  x i ) (11.31) (x i  x j )

where the p symbol indicates that the product of the quantities following the symbol must be taken. For example,

L1 (x ) 

(x  x 2 )(x  x 3 )(x  x 4 ) (11.32) (x 1  x 2 )(x 1  x 3 )(x 1  x 4 ) dP3 = 0 (11.33) dx

By putting

and solving we can obtain the estimated xmin where the function U(x) has the minimum. EXAMPLE 11.2 (Polynomial Approximations) Given the following information, fit a polynomial by quadratic approximation and find the value of X at which F(X) is minimum. X

F(X)

1 2 5

12 7 20

Solution: The polynomial passing through three points will be of the form

ANMF.CH11_4PP.indd 869

F(X) = a0 + a1X + a2X2

3/1/2023 3:07:31 PM

870 • Applied Numerical Methods Using MATLAB Substituting from the table

F (1) = a0 + a1 + a2 = 12



F (2) = a0 + 2a1 + 4a2 = 7



F (5) = a0 + 5a1 + 25a2 = 20

Solving we get a0 = 21.67 a1 = –12.00 a2 = 2.33 Hence,

F(X) = 21.67 – 12.0X + 2.33X2

When F(X) is minimum, we have dF  12  4.66 X  0 dX Hence, when F(X) is minimum



X = 2.5751

To verify whether F(X) is indeed minimum at X = 2.5751, we obtain the second derivative of F(X) with respect to X, which is d 2F = 4.66 dX 2 Since this value is positive F(X) is minimum at X = 2.5751.

EXAMPLE 11.3 Use MATLAB’s graphical capabilities to display the following function and visually estimate its minimum in the range 3  x  0 and 0 ≤ y ≤ 3 . f (x, y )  3  x  y  2 x 2  2 xy  y 2 Solution: The following script generates contour and mesh plots of the function: >> x=linspace(-3,0,40);y=linspace(0,3,40); >> [X,Y]=meshgrid(x, y);

ANMF.CH11_4PP.indd 870

3/1/2023 3:07:46 PM

Optimization • 871

>> Z=3+X-Y+2* X.^2+2* X.* Y+Y.^2; >> subplot(1,2,1); >> cs=contour(X,Y,Z);clabel(cs); >> xlabel(‘x’);ylabel(‘y’); >> title(‘(a) Contour plot’);grid; >> subplot(1,2,2);cs=surfc(X,Y,Z); >> zmin=floor(min(Z)); >> zmax=ceil(max(Z)); >> xlabel(‘x’);ylabel(‘y’);zlabel(‘f(x, y)’); >> title(‘(b) Mesh plot’); (a) Contour plot

3

4

2.5

20 15

2 f(x,y)

y

2

6

(b) Mesh plot

8

1.5 8 1

6

5 4 0 3

12 0.5 1614 18 0 -3

10

10

0

2

-1

1 -2

x

-1

0

y

0 -3

-2

x

FIGURE 11.5

Figure 11.5 shows that both plots clearly indicate that the function has a minimum value of f(x, y) = 0 to 1 located at about x = -1 and y = 1.5.

ANMF.CH11_4PP.indd 871

3/1/2023 3:07:47 PM

872 • Applied Numerical Methods Using MATLAB EXAMPLE 11.4 Fit a polynomial by quadratic approximation and determine the values of X at which F(X) is minimum. X

F(X)

1

8

2

3

3

17

Solution: X

F(X)

1

8

2

3

3

17

Let F(X) = a0 + a1X + a2X2 . From the given data, we have a0 + a1 + a2 = 8 a0 + 2a1 + 4a2 = 3 a0 + 3a1 + 9a2 = 17 Solving above equations using MATLAB give >> A=[1 1 1;1 2 4;1 3 9]; >> b=[8;3;17]; >> x=A\b x=   32.0000  -33.5000   9.5000 >> x=inv(A)* b x=   32.0000

ANMF.CH11_4PP.indd 872

3/1/2023 3:07:47 PM

Optimization • 873

 -33.5000   9.5000 a0 = 32 a1 = -33.5 a2 = 9.5 >> x=[1 2 3];y=[8 3 17]; a2a1a0=polyfit(x,y,2) a2a1a0 =  9.5  -33.5  32.0 and the function is F(X) = 32 – 33.5X + 9.5X2 For finding minimum of the function, we find the first derivative of the function dF(X)  33.5  19 X  0 ; for a minima or maxima dX Thus, X = 1.763 To check for minima or maxima, we find second derivative of the function d 2 F(X)  19  0 ; thus, it’s a minimum. dX 2 The minimum value of F(X) at X is F(X = 1.763) = 2.462 11.4.2

Local and Global Optima

As shown in Figure 11.6, one-dimensional optimization problems involve functions that depend on a single dependent variable. Multidimension optimization problems involve functions that depend on two or more dependent variables. The search for optimization in one-dimensional problems involve climbing or descending one-dimensional peaks and valleys as shown in Figure 11.6(a). Similarly, a two-dimensional optimization consists of searching out peaks and valleys as shown in Figure 11.6(b).

ANMF.CH11_4PP.indd 873

3/1/2023 3:07:50 PM

874 • Applied Numerical Methods Using MATLAB

f(x) f(x)

x*

Minimum f(x) Maximum –f(x)

Optimum f(x*,y*)

y f(x,y)

y* x

(a) One-dimensional optimization

(b) Two-dimensional optimization

FIGIURE 11.6 

The method of determining a maximum versus finding a minimum is basically identical since the same value of x* both minimizes f(x) and –f(x). Figure 11.6(a) shows how the minimization of f(x) is equivalent to the maximization of –f(x). Figure 11.6(b) illustrates either maximization with contours increase in elevation up to the maximum like a mountain or a minimization where contours decrease in elevation down to the minimum like a valley. It should be noted here that both local and global optima can happen in optimization process. A global optimum represents the most desirable solution whereas the local optimum is less desirable solution although it is better than its immediate neighboring solutions. Cases that consist of local optima are called multimodal where the objective is to seek the global optimum. Figure 11.7 shows a function that asymptotically approaches zero at plus and minus ∞ and has two maximum and two minimum points in the vicinity of the origin. The two points to the right are local optima, whereas the two to the left are global. f(x) Local maximum Global maximum

x

Global minimum

Local minimum

FIGURE 11.7 Local and global optima.

ANMF.CH11_4PP.indd 874

3/1/2023 3:07:51 PM

Optimization • 875

11.4.3 Bracketing One-dimensional optimization can be classified into bracketing and open methods. The bracketing method is very similar to the bisection method for root location discussed in Chapter 4. It is highly desirable to have the minimum point bracketed before a minimization algorithm can be considered. The procedure in bracketing consists of starting with an initial value of x0 and move downhill determining the functions at x1, x2, x3, … until we reach the point xn where the function f(x) increases for the first time. The minimum point is then bracketed in the interval (xn–2, xn). The step size is defined as hi = xi+1 – xi. If a constant step size hi is selected, then the process takes too many steps. A better and more efficient scheme is to have the size increased with each step so that the minimum can be reached faster, although the resulting bracket may be wide. Generally, the step size is increased by a constant factor, r, that is, hi+1 = rhi, where r > 1. 11.4.4

Golden-Section Search

Euclid’s definition of the golden ratio is based on dividing a line into two segments so that the ratio of the whole line to the larger segment is equal to the ratio of the larger segment to the smaller segment. l1

l1 l1 + l2

FIGURE. 11.8  Euclid’s definition of golden ratio.

The actual value of the golden ratio can be obtained by expressing Euclid’s definition from Figure 11.8 as l1  l 2 l1  (11.34) l1 l2



Multiplying Equation (11.34) by l1/l2 and collecting terms, we obtain r2 – r – 1 = 0 where

ANMF.CH11_4PP.indd 875

(11.35)

r = l1/l2.

3/1/2023 3:07:53 PM

876 • Applied Numerical Methods Using MATLAB The positive root of Equation (11.35) gives the golden ratio: r



1 5 or 1.618033989 … 2

(11.36)

The golden ratio, r, provides a basis for the golden-section search which is a general method for finding the optimum of a simple-variable function. It should be noted here that each telescoping decreases the interval containing the minimum by the factor r, which is not as good as the factor of 0.5 used in bisection method (Chapter 4 for root finding). However, the golden-section search method achieves this reduction with one function evaluation, whereas two function evaluations are needed in a bisection method. Suppose that the minimum of f(x) has been bracketed in the interval (a, b) of length h. The number of telescoping required reducing h from |b – a| to an error tolerance ∈ is given by |b – a| rn = ∈ (11.37)

which gives f(x)

   ln  | b  a |      2.078087 ln  n    (11.38) ln r | b  a | 

Eliminate Minimum

xa

x

x1

d x2

d

xb

(a) Initial interval

f(x)

Old x2

Old x1

xa

x2

x1 = xa + d (11.39) x2 = xb – d (11.40) where  d = (r – 1)(xb – xa) (11.41) x1

xb

New (b) New interval

FIGURE 11.9  Golden-section search method.

ANMF.CH11_4PP.indd 876

The objective of the golden-section search method is to minimize function evaluating by replacing old ones with new ones. Here, two intermediate points are chosen according to the golden ratio (from Figure 11.9):

x

The initial step of the algorithm consists of selecting two interior points according to the golden ratio as shown in Figure 11.9(a). The second step involves defining a new interval that encompasses the optimum as shown in Figure 11.9(b).

3/1/2023 3:07:58 PM

Optimization • 877

The function is evaluated at these two interior points. The results obtained are as follows: a) If f(x1) < f(x2), then f(x1) is the minimum. From Figure 11.9, the domain of x to the left of x2, from x1 to x2, can be eliminated since it does not contain the minimum. Therefore, here x2 becomes the new xa for the next round. b) If f(x2) < f(x1), then f(x2) is the minimum and the domain of x to the right of x1, from x1 to xb would be eliminated. Here x1 becomes the new xb for the next round. Now d can be computed using Equation (11.41) based on the new values of xa and xb. Similarly, Equation (11.40) is used to compute d when the optimum falls in the left subinterval in Figure 11.9. As the iterations are repeated, the interval containing the extremum is reduced very rapidly. An upper bound for the golden-section search method is obtained as follows: Once iteration is complete, the optimum will fall in one of two intervals: a) If the optimum function value is at x2, it will be in the lower interval (xa, x2, x1). b) If the optimum function value is at x1, it will be in the upper interval (x2, x1, xb). The error can be defined using either (a) or (b) above. This is due to the fact that the interior points are symmetrical. Now, consider the upper interval (x2, x1, xb). The maximum distance from the estimate when the true value was at the far left is given by

Dxa = x1 – x2 = xa + (r – 1)(xb – xa) – xb + (r – 1)(xb – xa) = (xb – xa) + 2(r – 1)(xb – xa) = (2r – 3)(xb – xa) (11.42)

or 0.2361(xb – xa) The maximum distance from the estimate, when the true value was at the far right is given by

Dxb = xb – x1 = xb – xa – (r – 1)(xb – x1) = (xb – xa) – (r – 1)(xb – x1) = (2 – r)(xb – xa) (11.43)

or 0.3820(xb – xa)

ANMF.CH11_4PP.indd 877

3/1/2023 3:07:58 PM

878 • Applied Numerical Methods Using MATLAB From Equations (11.42) and (11.43), we observe that Equation (11.43) represents the maximum error. The result in Equation (11.43) can be normalized to the optimum value for that iteration xopt to give a  (2  r)



xb  xa  100% (11.44) x opt

Equation (11.44) can be used as a basis for terminating the iterations in the golden-section search method. 11.4.5

MATLAB Program for Bracketing Method

function [a,b] = Goldbracket(func,x1,h) % Brackets the minimum point of f(x) % Usage: [a,b] = Goldbracket(func,xstart,h) % Input % func = Handle of function that returns the value of f(x) % x1 = Starting value of x % h = Initial step size for use in search % Output % a, b = Limits on x obtained at the minimum point c = 1.618033989; % Constant f1 = feval(func,x1); x2 = x1 + h; f2 = feval(func,x2); % Find the downhill direction and change sign of h if needed if f2 > f1 h = -h; x2 = x1 + h; f2 = feval(func,x2); % Check to see if the minimum lies between x1 - h and x1 + h

ANMF.CH11_4PP.indd 878

3/1/2023 3:08:01 PM

Optimization • 879

if f2 > f1 a = x2; b = x1 - h; return end end % Search loop begins here for i = 1:100 h = c*h; x3 = x2 + h; f3 = feval(func,x3); if f3 > f2 a = x1; b = x3; return end x1 = x2; f1 = f2; x2 = x3; f2 = f3; end error(‘Goldbracket method had failed to find the minimum’)

11.4.6

MATLAB Program for Golden-Section Search Method

function [xmin,fmin] = Goldsearch(func,a,b,tol) % Golden section search method for finding the minimum of f(x) % The minimum point must be bracketed in a > func=inline(‘-2*x*exp(-x*x)’,’x’); >> a=0;b=3; >> [xmin,fmin]=Goldsearch(func,a,b) xmin =  0.7071 fmin =  -0.8578 The result has converged to the true minimum at x = 0.7071 where the function has a value of y = -0.8578. EXAMPLE 11.6 Use the golden-section search to find the x that minimizes f(x) = x4 + 2.1x3 + 8.2x2 + 5.3x with xa = –2 and xb = 1. Compare the results with the analytical solution. Solution: >> func=inline (‘x^4+2.1*x^3+8.2*x^2+5.3*x’,’x’); >> a=-2;b=1; >> [xmin,fmin]=Goldsearch(func,a,b)

ANMF.CH11_4PP.indd 881

3/1/2023 3:08:07 PM

882 • Applied Numerical Methods Using MATLAB xmin =  -0.3619 fmin =  -0.9265 >> fd=[4 6.3 16.4 5.3]; >> roots(fd) ans =   -0.6065 + 1.8147i   -0.6065 - 1.8147i  -0.3619 Analytical Solution: df  4 x 3  6.3 x 2  16.4 x  5.3  0 dx The positive root of this equation is x = –0.3619, where we used the MATLAB program.

11.5 MULTIDIMENSIONAL OPTIMIZATION Multidimensional unconstrained optimization techniques are classified in a number of ways. They are often divided depending upon whether they need derivative evaluation. The methods that do not require derivative evaluation are called nongradient, or direct methods. Methods that do require derivatives are called gradient, or descent (or ascent) methods. An example of a direct method is the random search method or the brute force approach. Gradient methods explicitly use derivative information to generate efficient algorithms to locate optima. As described in Section 11.12, the built-in MATLAB function fminsearch is a direct method. As shown in the following example, MATLAB’s graphic capabilities provide a handy means to visualize multidimensional functions.

ANMF.CH11_4PP.indd 882

3/1/2023 3:08:09 PM

Optimization • 883

EXAMPLE 11.7 The Lennard-Jones potential between two molecules is given by   12   6  V  4         r    r  where ∈ and s are constants, and r is the distance between the molecules. Use the function Goldbracket and golden-section search to find s/r that minimize the potential and check the results analytically. Solution: Let x = s/r, then the function to be minimized is f(x) = x12 – x6 Starting at x=0 and choosing h = 0.01 for the first step size in Goldbracket (both choices being rather arbitrary), we arrive at the following program: >> func=inline(‘x^12-x^6’,’x’); >> x=0.0;h=0.01; >> [a,b]=Goldbracket(func,x,h); >> [xmin, fmin] =Goldsearch (func, a, b) The output of the program is xmin =  0.8909 fmin =  -0.2500 Analytical Solution:

or (2x6 – 1)x = 0

df  12 x 4  6 x 5  0 dx

or x = 2–1/6 = 0.890899

ANMF.CH11_4PP.indd 883

3/1/2023 3:08:16 PM

884 • Applied Numerical Methods Using MATLAB EXAMPLE 11.8 Find the minimum of the function F(X )  4 X12  3 X 22  5 X1 X 2  5 X1 X 2  8 X1 starting from (0, 0). Solution: The starting point is given as 0  X (0)    0  Newton’s method is the second-order method, meaning that second derivative (Hessian matrix) of the given function is required in each iteration. The gradient and Hessian matrix of F(X) are 8 X 1  5 X 2  8   8 5  F(X)    and H(X)     6 X 2  5X 1   5 6  8  0.2609 0.2174  F(X (0) )    and H1 (X)    0 0.2174 0.3478  0  0.2609 0.2174  8  2.0870  Now X (1)  X (0)  H1 (X (0) )F(X (0) )           0  0.2174 0.3478   0  1.7391 

Substituting this value in ∇F(X) we get  0.0005  F(X (1) )    0.0004 

2.0870  0.2609 0.2174   0.0005  2.0870  X (2)       1.7391  0.2174 0.3478  0.0004  1.7391 

Substituting this value in ∇F(X) we get  0.0005  F(X (2) )    0.0004  since ∇F(X(2)) = 0, X(2) is the optimum.

ANMF.CH11_4PP.indd 884

3/1/2023 3:08:39 PM

Optimization • 885

Hence,

2.0870  X    1.7391 

11.6 GRADIENT METHODS Gradient methods are not as efficient as some of the most recent methods discussed later, however, it forms a conceptual basis for many of them. The optimization follows the direction of the gradient in this method. Consider the optimization function, in two dimensions, representing a terrain as shown in Figure.11.10. The local direction of steepest descent is given by the negative of the gradient vector components, –∂U/∂x1, –∂U/∂x2. The gradient vector direction would be normal to the local contour line. x2

Global optimum

Lines of constant U Local or relative optimum

x1 Starting point FIGURE 11.10 The concept of the gradient method.

For numerical execution, the partial derivatives are obtained as where,

U Ui (11.45)  x i x i DUi = U(xi + Dxi) – U(xi) (11.46)

and Dxi is as small as possible without causing round-off errors.

ANMF.CH11_4PP.indd 885

3/1/2023 3:08:45 PM

886 • Applied Numerical Methods Using MATLAB Once the direction of the steepest gradient is found as shown in Figure 11.11, the search is continued in that direction choosing a step δr given by  r  x i  i x 2 (11.47)



as shown. The step components have arbitrary absolute values, illustrated in Figure 11.10. Their relative values are given by x 1 U / x 1  (11.48) x 2 U / x 2



 x 1 U /  x 1   x 2 U /  x 2 x2

P2 P1

x2

 xU

2

Negative gradient vector

P0

x1

 xU 1

x1 FIGURE 11.11 Direction of step in gradient method.

A parameter l can be conveniently used to establish the absolute value of the step size, as

x i  

U (11.49) x i

This same expression can be used in the n-dimensional case, for which we assume that the same strategy is applicable. Use of short step sizes makes the process very slow and large step sizes cause zig-zagging because the steps overshoot the range in the gradient vector direction. An improved version of the gradient strategy is to continue along the gradient direction until a minimum is found along the direction line, using one of

ANMF.CH11_4PP.indd 886

3/1/2023 3:08:55 PM

Optimization • 887

the methods of single variable minimization described in Section 11.3. The parameter l is now the variable adjusted. This is illustrated in Figure 11.12. The search has the tendency to zigzag badly, particularly when the contours are elliptical in shape, or the function U is near quadratic. This is the reason for the poor success of the method. x2

Starting point

x1

FIGURE 11.12 Gradient method using line searches.

The stopping criterion for the search could be when the partial derivatives become negligibly small or when successive changes in U become negligible. When an apparent optimum is reached, it is desirable to reduce the step size in the line search to ensure that the search is not stalled in the “valley.” This is one of the several modes by which searches stall.

11.7 NEWTON’S METHOD Newton’s method is a gradient method and can be conveniently used to optimize functions with several parameters. Let the function to be optimized be U( x ), where x is the vector of parameters x1, x2, …, xn. The function U( x ) can be expanded in the Taylor’s series about a point x ∗ as

n

U( x )  U( x  )   i 1

ANMF.CH11_4PP.indd 887

U( x  ) (x i  x i ) (11.50) X i

3/1/2023 3:09:07 PM

888 • Applied Numerical Methods Using MATLAB Newton’s method uses only two terms in the series. Expressing in concise form, the above series can be written as

U( x )  U( x  )  g  T ( x  )( x  x  ) (11.51)

where g ( x ∗ ) is the vector of first derivatives given by T



 U( x  ) U( x  )  g( x )   , ,.... (11.52) x 2  x 1  

The minimum of U( x ) is obtained by setting U  0 (11.53) x i



In Equation (11.51) which yields the set of equations g ( x  )  J ( x  )( x  x  )  0 (11.54)



where J ( x ∗ ) is the Jacobian matrix of second derivatives given by



J(x ) 

 2U( x  ) x 12

 2U( x  )  2U( x  )  x 1x 2 x 1 x n

 2U( x  ) x n x 1

 2U( x  )  x 2n



(11.55)

A set of linear algebraic equations in the unknown x ∗i s are given by Equation (11.54). If x ∗ is taken as xk , the kth point in the step by step search for the minimum, then solution of Equation (11.54) gives (k + 1)st approximation. Writing Equation (11.54) in the form

g ( xk )  J ( xk )( xk 1  xk )  0 (11.56)

it is possible to obtain an iterative form for the solution as

xk 1  xk  [ J ( xk )]1 g ( xk ) (11.57)

It is not actually necessary to obtain the inverse of the matrix J[ xk ] to solve for the new approximation xk . It is possible to write Equation (11.56) in the form

ANMF.CH11_4PP.indd 888

J ( xk )k   g ( xk ) (11.58)

3/1/2023 3:09:46 PM

Optimization • 889

where

k  xk 1  xk

and then solve Equation (11.58) for δk using Gaussian elimination. The major disadvantage of Newton’s method is the need for determining second-order partial derivatives, which makes it quite impractical for most engineering applications. 11.7.1

MATLAB Program for Newton’s Method

function [x,fx,xx]= Newtons(f,x0,tolx,maxiter,varargin) % Newtons method to solve a set of nonlinear equations % Input: f = A 1st-order vector function equivalent to a set of equations % x0 = Initial guess of the solution % tolx = Upper limit of |x(k)-x(k-1)| % maxiter= Maximum number of iterations % Output: % x = The point which the method has found % fx = f(x(last)) % xx = History of x eps=1e-6; h=1e-5; tolfun=eps; fx=feval(f,x0,varargin{:}); Nf=length(fx); Nx=length(x0); if Nf~=Nx, error(‘Incompatible dimensions of f and x0’); end if nargin [x0,g0,xx]=Newtons(gn,x0,tolx,maxiter) x0 =  -0.7500 1.0000 g0 =  1.0e-011 *  0.9095 -0.0500

ANMF.CH11_4PP.indd 891

3/1/2023 3:09:52 PM

892 • Applied Numerical Methods Using MATLAB xx =  0    0  -0.7500  1.0000 EXAMPLE 11.10 Use Newton’s method to find the maximum of f(x) = 5 sin x –

8 2 x with the 19

initial guess of x0 = 1, 8. Solution: >> fn=inline (‘5*sin(x)-8/19*x^2’,’x’); >> syms x >> diff (5*sin(x)-8/19*x^2) ans = 5*cos(x)-16/19*x >> gn=inline (‘5*cos(x)-16/19*x’,’x’); >> x0= [1.8]; Tolx=1e-4; Tolfun=1e-6; MaxIter=100; >> [x0, g0, xx] =Newtons (gn, x0, Tolx, MaxIter) x0 =  1.3427 g0 =  -1.3798e-011 xx =  1.8000  1.3357  1.3427  1.3427

ANMF.CH11_4PP.indd 892

3/1/2023 3:09:54 PM

Optimization • 893

20 0 -20 -40 -60 -80 -100 -120 -140 -160 -180 -10

-5

0

5

10

15

20

11.8 METHODS BASED ON THE CONCEPT OF QUADRATIC CONVERGENCE 11.8.1

Conjugate Directions for a Quadratic Function

The gradient method is expressed as

xk 1  xk   k g ( xk ) (11.59)

or in the form

k   k g ( xk ) (11.60)

which is similar in form to Equation (11.49). During each iteration lk is selected to minimize U( xk +1 ) in the gradient ­direction. As illustrated in Figure 11.12, a gradient search tends to zigzag quite badly, particularly for quadratic functions If it is possible to establish the best direction to take for a quadratic function, it would likely also be better for most other non-linear functions. This is called quadratic convergence and the approach is rather surprisingly successful. Equation (11.59) has the general form, in this case, given by

xk 1  xk   k C( xk ) (11.61)

where C( xk ) defines the conjugate direction vector at each step. The general quadratic form is

U( x ) 

1 T x Ax  BT x  d (11.62) 2

where A is a matrix and B is a vector.

ANMF.CH11_4PP.indd 893

3/1/2023 3:10:12 PM

894 • Applied Numerical Methods Using MATLAB It is required to determine a means of establishing conjugate directions for an optimization function of this form and show that they give convergence to the minimum. For this we require A to be a positive-definite matrix, which means it must be such that all quadratic terms of Equation (11.62) are positive. This is equivalent to requiring Equation (11.62) to be convex so that it has a minimum. A will also be symmetric since

 2U  2U  A ij   A ji (11.63) x i x j x jx i

and consequently A −1 is also symmetric. For convenience, let = g ( xk ) g= g ; C( xk ) = Ck k ; g( x ) Note that the gradient vector of the quadratic Equation (11.62) is

g  Ax  B (11.64)

Equation (11.61) is iterated for successive steps from i to (n – 1), to obtain

n 1

xn  xi    k Ck (11.65) k i

Premultiplying both sides by A and adding B, we get

n 1

Axn  B  Axi  B    k ACk (11.66) k 1

Using Equation (11.64) this becomes

n 1

gn  gi    k ACk (11.67) k 1

Finally, premultiplying by CTi −1 ,

n 1

CTi 1 gn  CTi 1 g1    k CiT1 ACk (11.68) k 1

It can be demonstrated intuitively that CTi −1 gi is zero by the following ­reasoning. In the i – 1 step, Ci–1 is followed to a minimum for U, which takes

ANMF.CH11_4PP.indd 894

3/1/2023 3:10:40 PM

Optimization • 895

us to xi . The search vector must be tangent to a contour line through xi and therefore the gradient gi is normal to Ci−1 . Consequently, CTi 1 gi  0 (11.69)



This can be demonstrated more rigorously as follows. Consider li–1 as a variable that is being adjusted to minimize U and bring the search to xi . Consequently, dU U dx 1 U dx 2 U dx n  (11.70)   ...  d j1 x 1 d i 1 x 2 d i 1 x n d i 1



Referring to Figure 11.12, the search vector Ci−1 at the origin has components C1, i–1, …, Cn,i–1, and the search moves along vector  i –1 Ci 1 from xi–1 to x1. In general, then, for any value of li–1′, x  xi 1   i 1 Ci 1 (11.71)



x2 i

C2,i – 1

x i –1

i –1C

Ci –1 C1,i – 1

x1

FIGURE 11.13 The (i – 1)th search step.

Taking the derivative with respect to li–1′, results in

dx dx  i 1  Ci 1 (11.72) d i 1 d i 1

Since xi−1 is constant at this stage in the search, we have

ANMF.CH11_4PP.indd 895

C i 1 

dx (11.73) d i 1

3/1/2023 3:11:12 PM

896 • Applied Numerical Methods Using MATLAB Thus Equation (11.70) can be written for any value of li–1′ as

dU  CTi 1 g (11.74) d i 1

Now at x1 , dU/dli–1 must be zero for a minimum U. Thus, we have

CTi 1 g  0 (11.75)

Consequently, Equation (11.68) reduces to

n 1

CTi 1 gn    k CTi 1 ACk (11.76) k 1

The conjugate vectors are defined as those satisfying

CTi AC j = 0 (11.77)

for i ≠ j. Since A must be a positive-definite matrix as defined above, the summation term of Equation (11.76) is zero so that

CTi 1 gn  0 (11.78)

The theory of n-dimensional vectors states that if we construct a set of n-­vectors all orthogonal or conjugate to each other, then any other vector can be written as a linear combination of these vectors. Therefore, no other vector can be orthogonal to all of the original n vectors other than the zero vector. Since Equation (11.78) is an expression of orthogonality of the nth gradient vector with all n conjugate vectors, then gn must be zero, which is the condition for the minimum of the quadratic. Thus, the minimum of the quadratic can be found in the n steps if the search directions are conjugate. The conjugate directions in two dimensions are shown in Figure 11.14. The first search direction is the gradient vector, which is chosen arbitrarily. The second direction is conjugate to the first one. Several methods have been proposed for generating the first search direction and these methods are applied to nonquadratic functions also. It should be noted that Newton’s method would go to the minimum in one step if the function to be minimized is quadratic.

ANMF.CH11_4PP.indd 896

3/1/2023 3:11:25 PM

Optimization • 897

x2 x1

Conjugate vector, C1

C0 x0

Gradient vectors x2

x1 FIGURE 11.14 Conjugate directions.

EXAMPLE 11.11 Find the minimum of the function F(X )  4 X12  3 X 22  5 X1 X 2  8 X1 starting from (0, 0). Solution: The starting point is given as 0  X (0)    0  By selecting the matrix 1 0  C  0 1  the initial search directions S(1) and S(2) become 1  0  S(1)    ;  S(2)    0  1  Now, we minimize F(X) along S(1) from X(0) 0  1  1  Then  X(1) = X(0) + a1S(1) =    1      0  0   0 

ANMF.CH11_4PP.indd 897

3/1/2023 3:11:37 PM

898 • Applied Numerical Methods Using MATLAB Substituting this value of X(1) in the given function, we get

F(X (1) )  412  81

To find the minimum step length 1 ,

dF(X (1) )  81  8  0 d1

which gives 1  1



1  X (1)    0 

\

Next, we minimize F(X) along S(2) form X(1), i.e.,

X(2) = X(1) + a2S(2)

Then

1  0   1  X (2)      2      0  1   2 

Substituting this value of X(2) in the given function, we get F(X (2) )  3 22  5 2  4 To find the minimum step length 2 , dF(X (2) )  61  5  0 d 2 which gives 2  0.83  1  therefore X (2)    0.83  Update the matrix C as follows: 0  1 C  0 0.83 

ANMF.CH11_4PP.indd 898

3/1/2023 3:12:00 PM

Optimization • 899

Hence,

 10   1  S(3)     0  0.83  0.83 

Minimize F(X) along S(3) from X(2)  1   1   1  3  Then X (3)     3    0.83  0.83  0.83(1   3 ) Substituting this value of X(3) in the given function, we get F(X (3) )  1.95(1   3 )3  8(1   3 ) To find the minimum step length 3 , dF(X (3) )  3.9(1   3 )  8  0 d 3 which gives 3  1.1 \ and

2.1 X (3)    1.7  (3) F(X ) = –8.3400

Again, update the matrix C as follows: 1.1   0 C  0.83 0.87  from which we get  0  1.1  1.1  S( 4)     0.83  0.87  1.7  Now minimize F(X) along S(4) from X(3) 2.1 1.1  2.1  1.1 4  Then  X ( 4)      4      1.7  1.7  1.7(1   4 )  Substituting this value of X(4) in the given function, we get F(X ( 4) )  4(2.1  1.1 4 )2  8.7(1   4 )2  8.5(1.1 24  3.2 4  2.1)  8(2.1  1.1 4 )

ANMF.CH11_4PP.indd 899

3/1/2023 3:12:24 PM

900 • Applied Numerical Methods Using MATLAB

To find the minimum step length 4 , find

dF(X ( 4) )  0 and solve for a4. Then d 4

we get 4 = 0.0143 Hence

 2.1  X (4)    1.72 

and

F(X(4)) = –8.3448

Now we can see that |F(X(4)) – F(X(3))| = 0.0048. If we need more accurate result, we need to minimize F(X) along S(5) from X(4) where S(5) is calculated as follows: Update the matrix C as follows:  1.1 0.016  C  0.87 0.02  from which we get 1.1  0.016  1.12  S( 5)     0.87  0.02  0.89  Now minimize F(X) along S(5) from X(4)  2.1  1.12   2.1  1.1 5  Then, X ( 5)     5    1.72  0.89  1.72  0.89 5  Substituting this value of X(5) in the given function, we get F(X)  4(2.1  1.12 5 )2  3(1.72  0.89 5 )2  5( 25  3.8 4  3.6)  8(2.1  1.2 5 )

To find the minimum step length 5 , find

dF(X ( 5) )  0 and solve for a5. d 5

5 = 0.0085 Hence and

ANMF.CH11_4PP.indd 900

2.11 X ( 5)    1.73  F(X(5)) = –8.3444

3/1/2023 3:12:45 PM

Optimization • 901

Since |F(X(5)) – F(X(4))| = 0.004, the iteration process can be stopped here and the optimum value of X is given by 2.11 X   . 1.73 

11.9  POWELL’S METHOD For an optimization problem involving n design variables, the basic algorithm is described below: a) select a point x0 in the design space b) select the starting vectors Vi, i = 1, 2, …, n c) do with i = 1, 2, …., n

minimize F(x) along th line thro xi–1 in the direction of Vi



assume the minimum point as xi



end do

d) Vn+1 ¬ x0 – xn

minimize F(x) along the line thro x0 in the direction of Vn+1



assume the minimum point as xn+1



if |xn+1 – x0| < ∈ exit loop



do with i = 1, 2, …., n

Vi ¬ Vi+1

end do



end cycle

The minimum point of a quadratic surface is reached in n cycles. 11.9.1

MATLAB Program for Powell’s Optimization Method

function [xmin,fmin,ncyc] = Powell(h,tol) % Powell’s optimization method % h = Initial search increment = 0.1 % tol = Error tolerance [default value = 1.0e-6]

ANMF.CH11_4PP.indd 901

3/1/2023 3:12:49 PM

902 • Applied Numerical Methods Using MATLAB % x = Starting point % FUNC = Function that returns f value % xmin = Minimum point % fmin = Minimum value of f % ncyc = Number of cycles for convergence global x FUNC v if nargin < 2; tol = 1.0e-6; end if nargin < 1; h = 0.1; end if size(x,2) > 1; x = x’; end n = length(x); df = zeros(n,1); u = eye(n); for j = 1:40 % Cycles xold = x; fold = feval(FUNC,xold); for i = 1:n v = u(1:n,i); [a,b] = Goldbracket(@fline,0.0,h); [s,fmin] = Goldsearch(@fline,a,b); df(i) = fold - fmin; fold = fmin; x = x + s*v; end v = x - xold; [a,b] = Goldbracket(@fline,0.0,h);

ANMF.CH11_4PP.indd 902

3/1/2023 3:12:49 PM

Optimization • 903

[s,fmin] = Goldsearch(@fline,a,b); x = x + s*v; % Convergence criterion if sqrt(dot(x-xold,x-xold)/n) < tol xmin = x; ncyc = j; return end imax = 1; dfmax = df(1); for i = 2:n if df(i) > dfmax imax = i; dfmax = df(i); end end for i = imax:n-1 u(1:n,i) = u(1:n,i+1); end u(1:n,n) = v; end error(‘Converge Failed’) function z = fline(s) global x FUNC v z = feval(FUNC,x+s*v); EXAMPLE 11.12 Find the minimum of the following function using Powell’s method: F(x)  4 x 12  3 x 22  5 x 1 x 2  8 x 1 Starting from (0, 0).

ANMF.CH11_4PP.indd 903

3/1/2023 3:12:52 PM

904 • Applied Numerical Methods Using MATLAB Solution: >> FUNC=inline(‘4*X(1)^2+3*X(2)^2-5*X(1)*X(2)-8*X(1)’,’X’); >> global X FUNC >> X=[0 0]; >> [xmin,fmin,numcycles]=Powell xmin =  2.0870  1.7391 fmin =  -8.3478 numcycles =  2

3

(b) Mesh plot

(a) Contour plot 8

2.5

2000 1500 f(x,y)

y

2

1.5

600

1000 500

1 1.2e+003 4

0.5

0 -3

0 3 0

2

-2

-1

1

1e+003 x

-1

0

y

-2 0 -3

x

FIGURE 11.15

ANMF.CH11_4PP.indd 904

3/1/2023 3:12:53 PM

Optimization • 905

EXAMPLE 11.13 Find the minimum of the following function using Powell’s method: F(x)  7 x 12  5 x 22  8 x 1 x 2  5 x 1 Starting from (0, 0). Solution: >> FUNC=inline (‘7*X (1) ^2+5*X (2) ^2-8*X (1)*X (2)-5*X (1)’,’X’); >> global X FUNC >> X= [0 0]; >> [xmin, fmin, numcycles] =Powell xmin =  0.6579  0.5263 fmin =  -1.6447 numcycles =  2 >> x=linspace (-3, 0, 40); y=linspace (0, 3, 40); >> [X, Y] =meshgrid(x, y); >> Z=7*X^2+5*Y^2-8*X*Y-5*X; >> subplot (1, 2, 1); >> cs=contour(X, Y, Z); clabel (cs); >> xlabel (‘x’); ylabel (‘y’); >> title (‘(a) Contour plot’); grid; >> subplot (1, 2, 2); cs=surfc(X, Y, Z); >> zmin=floor (min (Z)); >> zmax=ceil (max (Z));

ANMF.CH11_4PP.indd 905

3/1/2023 3:12:57 PM

906 • Applied Numerical Methods Using MATLAB >> xlabel (‘x’); ylabel (‘y’); zlabel (‘f(x, y)’); >> title (‘(b) Mesh plot’); (a) Contour plot 3 2.6e+003

2.5

(b) Mesh plot

2.4e+003 2.2e+003

3000 2500 2000

1.8e+003 2e+003 1.6e+003

1.5

1.2e+00 1.4e+003

f(x,y)

y

2

1500 1000 500

1 1e+003 0.5

6

0 3

800 0 -3

-2

x

-1

0

2

-1

1 0

y

-2 0 -3

x

FIGURE 11.16 

11.10  FLETCHER-REEVES METHOD This method [Fletcher & Reeves, 1964] is based on the assumption that conjugate directions can be generated by the following expression,

Ck 1   gk 1  k Ck (11.79)

where bk must be determined using the fact that Ck +1 is conjugate to Ck . Equation (11.79) is premultiplied by CTk A , to give

ANMF.CH11_4PP.indd 906

CTk ACk 1  CTk Agk 1  k CTk ACk (11.80)

3/1/2023 3:13:06 PM

Optimization • 907

By conjugacy the left-hand side is zero giving

CkT Agk 1 k  T (11.81) Ck ACk

In order to apply the method to nonquadratic functions we must eliminate A from Equation (11.81). We could develop the following special case:

gk 1  gk   k ACk (11.82)

Premultiplying both sides by CTk gives

CTk gk 1  CTk gk   k CTk ACk (11.83)

The left-hand side is zero by Equation (11.69), giving

CTk ACk  

1 T Ck gk (11.84) k

Returning to Equation (11.79), we premultiply it by gkT+1

gkT1 Ck 1   gk 1 gk 1k gkT1 Ck (11.85)

By Equation (11.69), again the last term is zero, and hence

gkT Ck  CTk gk   gkT gk (11.86)

Substituting this into Equation (11.84) results in

CTk ACk 

1 T gk gk (11.87) k

Premultiplying Equation (11.82) again by gkT+1 , we obtain

gkT1 gk 1  gkT1 gk   k gkT1 ACk (11.88)

This is rearranged in the form

gkT1 ACk 

1 T (gk 1 gk 1  gkT1 gk ) (11.89) k

Because A is a symmetric matrix, we can reorder the left-hand side as

ANMF.CH11_4PP.indd 907

CTk Agk 1 

1 T (gk 1 gk 1  gkT1 gk ) (11.90) k

3/1/2023 3:13:38 PM

908 • Applied Numerical Methods Using MATLAB

Premultiplying Equation (11.82) once again, this time by CTk 1 , we get

CTk 1 gk 1  CTk 1 gk  k CTk 1 ACk (11.91)

Due to Equation (11.69), the first term of Equation (11.91) on the right-hand side is zero, and the second term is zero due to the conjugate requirement. Thus,

CTk 1 gk 1  0 (11.92)

Equation (11.79) is used to get

C k 1 

1  k 1

(Ck  gk ) (11.93)

which we premultiply by gkT+1 and obtain

gkT1 Ck 1 

1 (gkT1 Ck  gkT1 gk ) (11.94)  k 1

The left-hand side is zero because of Equation (11.92) and the first term of the right-hand side is zero due to Equation (11.79). Thus,

gkT1 gk  0 (11.95)

We have thus verified the orthogonality of successive gradient vectors in the use of conjugate search directions. Returning to Equation (11.90), we get from Equation (11.95)

CTk Agk 1 

1 T gk 1 gk 1 (11.96) k

Finally returning to Equation (11.81), our original expression for bk, we use Equations (11.87) and (11.96) to get

k 

gkT1 gk 1 gk (11.97) gkT

So now we can use Equation (11.75) to get the final expression for generating each successive search direction.

ANMF.CH11_4PP.indd 908

C k 1   g k 1 

gkT1 gk 1 Ck (11.98) gkT gk

3/1/2023 3:14:02 PM

Optimization • 909

This is applied to the general algorithm expression in Equation (11.61). The search direction used is C0   g 0 (11.99)



This method is said to be difficult due to round off errors and a restart with Ck   gk after every n + 1 iterations is recommended. 11.10.1 MATLAB Program for Fletcher-Reeves Optimization Method function [xmin,fmin,ncyc] = Fletcherreeves(h,tol) % Fletcher-Reeves optimization method for minimizing f(x1,x2,...,xn) % Usage: [xmin,fmin,ncyc] = Fletcherreeves(h,tol) % Input % h = Initial search increment value [default value = 0.1] % tol = Error tolerance [default value = 1.0e-6] % GLOBALS: Must be declared GLOBAL in the main program % x = Starting point % FUNC = Handle of function that returns f % DFUNC = Handle of function that returns grad(f) % Output % xmin = Optimum or minimum point % fmin = Minimum value of function f % ncyc = Number of cycles for convergence global x FUNC DFUNC v if nargin < 2; tol = 1.0e-6; end if nargin < 1; h = 0.1; end if size(x,2) > 1; x = x’; end % x should be a column vector n = length(x); % Number of design variables g0 = -feval(DFUNC,x);

ANMF.CH11_4PP.indd 909

3/1/2023 3:14:08 PM

910 • Applied Numerical Methods Using MATLAB v = g0; for i = 1:50 [a,b] = Goldbracket(@fline,0.0,h); [s,fmin] = Goldsearch(@fline,a,b); x = x + s*v; g1 = -feval(DFUNC,x); if sqrt(dot(g1,g1)) > format short function y=func(X) y=4*X (1) ^2+3*X (2) ^2-5*X (1)*X (2)-8*X (1); function g=grad(X) g= [8*X (1)-5*X (2)-8  6*X (2)-5*X (1)]; >> global X FUNC DFUNC >> FUNC=@func;DFUNC=@grad; >> X= [0 0]; >> [x, fmin] =Fletcherreeves x=  2.0870  1.7391 fmin =  -8.3478 EXAMPLE 11.15 Minimize f(x1,x2) = x1 – x2 + 2x12 + 2x1x2 + x22 starting from the point (0, 0) using the Fletcher-Reeves method. Solution: func.m file function y=func(X) y= X (1)-X (2)+2*X (1) ^2+2*X (1)*X (2)+X (2) ^2; grad.m file

ANMF.CH11_4PP.indd 913

3/1/2023 3:14:50 PM

914 • Applied Numerical Methods Using MATLAB function g=grad(X) g=[1+4* X(1)+2*X(2)   -1+2*X(1)+2*X(2)]; >> format short >> global X FUNC DFUNC >> FUNC=@func;DFUNC=@grad; >> X=[0 0]; >> [x,fmin]=Fletcherreeves x=  -1.0000  1.5000 fmin =  -1.2500 >> x=linspace(-3,0,40);y=linspace(0,3,40); >> [X,Y]=meshgrid(x, y); >> Z=X-Y+2*X^2+2*X*Y+Y^2; >> subplot(1,2,1); >> cs=contour(X,Y,Z);clabel(cs); >> xlabel(‘x’);ylabel(‘y’); >> title(‘(a) Contour plot’);grid; >> subplot(1,2,2);cs=surfc(X,Y,Z); >> zmin=floor(min(Z)); >> zmax=ceil(max(Z)); >> xlabel(‘x’);ylabel(‘y’);zlabel(‘f(x, y)’); >> title(‘(b) Mesh plot’);

ANMF.CH11_4PP.indd 914

3/1/2023 3:14:50 PM

Optimization • 915

3

(b) Mesh plot

(a) Contour plot 400 350

2.5

600 250

2

200

400

100 f(x,y)

y

300 1.5

0 150

1

200 0

50 -200 3

0.5

-50 -

0 -3

-2

x

-1

0

0

2

-1

1 y

-2 0 -3

x

FIGURE 11.17 

EXAMPLE 11.16 Use the Fletcher-Reeves method to locate the minimum of function F(x) = 10 x 12  3 x 22  10 x 1 x 2  2 x 1 . Start with [0  0.05]T. Solution: function y=fex11_16(X) y=10*X(1)^2+3*X(2)^2-10*X(1)*X(2)+2*X(1); function g=dfex11_16(X) g=zeros(2,1); g(1)=20*X(1)-10*X(2)+2;

ANMF.CH11_4PP.indd 915

3/1/2023 3:14:54 PM

916 • Applied Numerical Methods Using MATLAB g(2)=6*X(2)-10*X(1); Global X FUNC DFUNC v >> FUNC=@fex11_16;DFUNC=@dfex11_16;X=[0, 0.5]; >> [xmin,fmin,ncyc]=Fletcherreeves xmin =  -0.6000  -1.0000 fmin =  -0.6000 ncyc =  3 EXAMPLE 11.17 Use the Fletcher-Reeves method to find the minimum of function F(x) = Start with [0  0]T. Solution: Global X FUNC DFUNC v function y=fex11_17(X) y=9*X(1)^2+4*X(2)^2-8*X(1)*X(2)+3*X(1); function g=dfex11_17(X) g=zeros(2,1); g(1)=18*X(1)-8*X(2)+3; g(2)=8*X(2)-8*X(1); >>X=[0,0]; >> FUNC=@fex11_17;DFUNC=@dfex11_17;X=[0, 0]; >> [xmin, fmin, ncyc] =Fletcherreeves

ANMF.CH11_4PP.indd 916

3/1/2023 3:14:54 PM

Optimization • 917

xmin =  -0.3000  -0.3000 fmin =  -0.4500 ncyc =  3

11.11  THE HOOKS AND JEEVES METHOD The Hooke and Jeeves method is a sequential technique each step of which consists of two kinds of moves, the exploratory move and the pattern move. General procedure:

 x1  x   2 1. Start with an arbitrarily chosen point X 1    , called the starting base    x n  point and prescribed step length Dxi in each of the coordinate directions ui, i = 1, 2, …., n. Set k = 1. 2. Compute fk = f(Xk). Set i = 1, Yk0 = Xk, and start the exploratory move as stated in step 3. 3. The variable xi is perturbed about the current temporary base point Yk,i–1 to obtain the new temporary base point as



Yk ,1

 Yk ,i 1  x i u i   Y  k ,i 1  x i u i      Yk ,i 1

if f   f (Yk ,i 1  x i u i )  f  f (Yk ,i 1 ) 

if f  f (Yk ,i 1  x i u i )  f  f (Yk ,i 1 )

(11.100)

 f   f (Yk ,i 1  x i u i ) if f  f (Yk ,i 1 )  min(f  , f  )

This process is continued for i = 1, 2, …., until xn is perturbed to find Yk,n.

ANMF.CH11_4PP.indd 917

3/1/2023 3:14:57 PM

918 • Applied Numerical Methods Using MATLAB 4. If the point Yk,n remains same as Xk, reduce the step lengths Dxi (say, by a factor of 2), set i = 1 and go to step 3. If Yk,n is different from Xk, obtain the new base point as Xk+1 = Yk,n and go to step 5. 5. With the help of the base points Xk and Xk+1, establish a pattern direction S as

S = Xk+1 – Xk (11.101)

and find a point Yk+1,0 as

Yk+1,0 = Xk+1 + lS (11.102)

where l is the step length, which can be taken as 1 for simplicity. Alternatively, we can solve a one-dimensional minimization problem in the direction S and use the optimum step length l* in place of l in Equation (11.102). 6. Set k = k + 1, fk = f(Yk0), i = 1, and repeat step 3. If at the end of step 3, f(Yk,n) < f(Xk), we take the new base point as Xk+1 = Yk,n and go to step 5. On the other hand, if f(Yk,n) ≥ f(Xk), set Xk+1 = Yk, reduce the step lengths Dxi, set k = k + 1 and go to step 2. 7. The process is assumed to have converged whenever the step lengths fall below a small quantity e. Thus, the process is terminated if max(Dxi) < e (11.103) The Hooks and Jeeves method makes local exploratory searches to determine the nature of the function and how to proceed in some accelerated fashion. This method avoids the use of derivatives and not only reduces the time of formulation, programming, and computation but also makes the strategy less sensitive to irregular or discontinuous functions. The Hooks and Jeeves method can get stuck on constraints because of the fixed orientation of its search coordinates as illustrated in Figure 11.18 for a two-dimensional case. If the contour lines and the inequality constraints line happen to have a certain orientation, and the search approaches from a certain direction, the search becomes stalled as shown in the figure. A new

ANMF.CH11_4PP.indd 918

3/1/2023 3:14:57 PM

Optimization • 919

starting point may lead to the right solution and hence a local random search may be added to the strategy when the process gets stuck at a constraint.

Lines of constant U x2

Infeasible search steps

Constrained optimum

Starting point x1 Inequality constraint zero line

Direct search is hung up at this point

FIGURE 11.18 Illustration of how a Hooke and Jeeves direct search can become stalled at constraint lines.

EXAMPLE 11.18 Find the minimum of the function F(X)  4 X 12  3 X 22  5 X 1 X 2  8 X 1 starting from (0, 0) using the Hooke and Jeeves direct search method. Solution: The starting point is given as 0  X (0)    0 

ANMF.CH11_4PP.indd 919

3/1/2023 3:15:01 PM

920 • Applied Numerical Methods Using MATLAB Let the initial step lengths be DX1 = 0.5 DX2 = 0.5 The value of the given function at X(0) is F(X(0)) = 0 Now

0  Y00  X (0)    0 

To find the new temporary base point, F = F(y00) = 0 F(1) = F(y00 + DX1u1) = F(0.5, 0) = –3 F(2) = F(y00 – DX1u1) = F(–0.5, 0) = –5 Since F(1) < F, 0.5  Y01    0  F = F(Y01) = –3 F(1) = F(Y01 + DX2u2) = F(0.5, 0.5) = –3.5 F(2) = F(0.5, –0.5) = –1 Since F(1) < F, 0.5  Y02    0.5  Since Y02 is different from X(0), the new base point is taken as 0.5  X (1)    0.5 

ANMF.CH11_4PP.indd 920

3/1/2023 3:15:13 PM

Optimization • 921

Now the pattern direction is calculated as 0.5  0  0.5  S  X (1)  X (0)          0.5  0  0.5  0.5  0.5  Y10  X (1)  S    0.5  0.5  Substituting this in the given function and simplifying, we get F(Y10) = 2(0.5 + 0.5a)2 – 8(0.5 + 0.5a) To find the minimum step length, dF(Y10 )  4(0.5  0.5)  4  0 d which gives a* = 3 2  Hence, Y10    2  Now F = F(Y10) = –8 F(1) = F(Y10 + DX1u1) = F(2.5, 2) = –8 F(2) = F(Y10 – DX1u1) = F(1.5, 2) = –6 Since F(Y10) = F(1), reduce the step length to 0.2. 0.5  Then, X (2)    0.5  0.5  Y(20)  X (2)    0.5  F = F(Y20) = –3.5 F(1) = F(Y20 + DXiui) = F(0.7, 0.5) = –4.64 F(2) = F(Y20 – DX1u1) = F(0.3, 0.5) = –3.64

ANMF.CH11_4PP.indd 921

3/1/2023 3:15:26 PM

922 • Applied Numerical Methods Using MATLAB Since F(1) < F, 0.7  then, Y(21)    0.5  F = F(Y21) = –4.64 F(1) = F(Y21 + DX2u2) = F(0.7, 0.7) = –4.62 F(2) = F(Y21 – DX2u2) = F(0.7, 0.3) = –4.42 Since F < min(F(1), F(2)), 0.7  Y22  Y21    then, 0.5  Since F(Y22) = –4.64 < F(X(2)) = –3.5, take the new base point as 0.7  X (3)    0.5  Now the new pattern direction is 0.7  0.5  0.2  S  X (3)  X (2)          0.5  0.5   0  0.7  0.2  0.7  0.2  Y30  X (3)  S            0.5   0   0.5  Substituting this in the given function and simplifying, we get F(Y30) = 4(0.7 + 0.2a)2 + 0.75 – 10.5(0.7 + 0.2a) To find the minimum step length, dF(Y30 )  8(0.7  0.2)(0.2)  2.1  0 d which gives a = 3.1 1.3125  Hence, Y30     0.5 

ANMF.CH11_4PP.indd 922

3/1/2023 3:15:39 PM

Optimization • 923

F = F(Y30) = –6.14 F(1) = F(Y30 + DX1u1) = F(1.5125, 0.5) = –5.98 F(2) = F(Y30 – DX1u1) = F(1.1125, 0.5) = –5.981 Since F < min(F(1), F(2)) 1.3125  Y31     0.5  F = F(Y31) = –6.14 F(1) = F(Y31 + DX2u2) = F(1.3125, 0.7) = –6.733 Since F(1) < F, 1.3125  Y32     0.7  Now the new direction is 1.3125  0.7  0.6125  S  X ( 4)  X (3)        0.7  0.5   0.2  1.3125  0.6125  Y40  X ( 4)  S     0.7  0.2  Substituting this in the given function and simplifying, we get F(Y40) = 4(1.3125 + 0.6125a)2 + 3(0.7 + 0.2a)2 – 5(1.3125   + 0.6125a)(0.7 + 0.2a)   – 8(1.3125 + 0.6125a) To find the minimum step length, find a* = 0.536

dF(Y40 )  0 and solving, we get d

1.64  Hence Y40    0.81

ANMF.CH11_4PP.indd 923

3/1/2023 3:15:58 PM

924 • Applied Numerical Methods Using MATLAB Similarly repeating the procedure, we get the optimum solution as 2.08696  X     1.7391 

11.12  METHOD OF SUCCESSIVE LINEAR APPROXIMATION It is possible to represent the nonlinear function by linear expressions, which are meaningful if they are restricted to a small local region about some given point. These expressions can be solved by linear programming. Another linearization is then done about the new position and the process is continued. Let the optimization problem be defined by U = U(x1, x2, …., xn) = minimum yj(x1, x2, …., xn) = 0  j = 1, m fs(x1, x2, …., xn) ≥ 0  s = 1, p

(11.104)

It is convenient to use vector notation and designate the kth position of the search as xk . The linearization is achieved by using a Taylor’s series expansion about xk , in which all terms above linear are dropped.

n

U  U(x k )   (x i  x ki ) i 1



U( x k )  minimum (11.105) x i

n

 j ( x k )

i 1

x i

 j (x k )   (x i  x ki ) n

s ( x k )   (x i  x ki ) i 1

 0   j = 1, m

(11.106)

s ( x k )  0 (11.107) x i

Using the following notation for convenience U( x k ) ci  x i

U  U( x k )

 kj   j ( x k )

U ji 

sk  s ( x k )

Vsi 

 j ( x k ) x i s ( x k ) x i

x ki  x i  x ki

ANMF.CH11_4PP.indd 924

3/1/2023 3:16:21 PM

Optimization • 925

Using this notation in Equation (11.105), Equation (11.107) gives

n

U  Uk   c i x ik = minimum

(11.108)

i 1



n

u i 1



ji

x kj   kj   j = 1, m

(11.109)

si

x ki  sk   s = 1, p

(11.110)

n

v i 1

Adding to these expressions the relation defining the limits on the x i s, designated mi, we have x ki  mi x ki  mi (11.111) The values of mi are based on judgment in order to adequately limit the linear approximation. This results in a linear programming form in terms of x i s. The updating of the x ′i s, given by

x ki 1  x ki  x ki (11.112)

is repeated until the x i s become negligibly small. The method has convergence problems, but performance can be improved by adjusting the variables after each iterations so that the equality constraints are satisfied. This can be done by using Newton’s method for solving simultaneous nonlinear equations. An initial feasible solution is required in this method.

11.13  INTERIOR PENALTY FUNCTION METHOD In the interior penalty function method, a new function (f function) is constructed by augmenting a penalty term to the objective function. The penalty term is chosen such that its value will be small at points away from the constraint boundaries and will tend to infinity as the constraint boundaries are approached. Thus, once the unconstrained minimization of f(X, rk) is started from any feasible point X1, the subsequent points generated will always lie within the feasible domain since the constraint boundaries act as barriers during the minimization process. The f function defined as

m

(X, rk )  f (X)  rk  j 1

ANMF.CH11_4PP.indd 925

1 (11.113) g j (X )

3/1/2023 3:16:37 PM

926 • Applied Numerical Methods Using MATLAB Since the above equation does not allow any constraint to be violated, it requires a feasible starting point for the search towards the optimum point. The iteration procedure of this method can be summarized as follows: Iterative process: 1. Start with an initial feasible point X1 satisfying all the constraints with strict inequality sign, that is, gj(X1) < 0 for j = 1, 2, …., m and an initial value for r1 > 0. Set k = 1. 2. Minimize f(X, rk) by using any of the unconstrained minimization methods and obtain the solution X*k. 3. Test whether X*k is the optimum solution of the original problem. If X*k is found to be optimum, terminate the process or else, go to the next step. 4. Find the value of the next penalty parameter, rk+1, as rk+1 = crk where c < 1. 5. Set the new value of k = k + 1, take the new starting point as X1 = X*k and go to step 2. All these aspects are discussed in the following paragraphs. Starting Feasible Point X1 1. Select an arbitrary point X1 and evaluate the constraints gj(X) at the point X1. Since the point X1 is arbitrary, it may not satisfy all the constraints with strict inequality sign. If r out of a total of m constraints are violated, renumber the constraints such that the last constraints will become the violated ones, that is, gj(X1) < 0,  j = 1, 2, …., m – r gj(X1) ≥ 0,  j = m – r + 1, m – r + 2, …

(11.114)

2. Identify the constraint that is violated most at the point X1, that is obtain the integer k such that gk(X1) = max[gj(X1)]  for j = m – r + 1, m – r + 2, …., m

ANMF.CH11_4PP.indd 926

(11.115)

3/1/2023 3:16:37 PM

Optimization • 927

3. Formulate a new optimization problem as Find X which minimizes gk(X) subject to gj(X) ≤ 0,  j = 1, 2, …., m – r gj(X) – gk(X1) ≤ 0 j = m – r + 1, m – r + 2, …., k – 1, k + 1, …., m (11.116) 4. Solve the optimization problem formulated in step 3 by taking the point X1 as a feasible starting point using the interior penalty function method. Note that this optimization method can be terminated whenever the value of the objective function gk(X) drops below zero. The solution obtained XM will satisfy at least one more constraint than did the original point X1. 5. If all the constraints are not satisfied at the point XM, set the new starting point as X1 = XM, and renumber the constraints such that the last r constraints will be unsatisfied ones (this value of r will be different from the previous value), and go to step 2. This procedure is repeated until all the constraints are satisfied and a point X1 = XM is obtained for which gj(X1) < 0, j = 1, 2, …., m. Initial Value of the Penalty Parameter (r1): Since the unconstrained minimization of f(X, rk) is to be carried out for a decreasing sequence of rk, it might appear that by choosing a very small value of r1, we can avoid an excessive number of minimizations of the function f. But from a computational point of view, it will be easier to minimize the unconstrained function f(X, rk) if rk is large. A moderate value has to be chosen for the initial penalty parameter (r1). In practice, a value of r1 that gives the value of f(X1, r1) approximately equal to 1.1 to 2.0 times the value of f(X1) has been found to be quite satisfactory in achieving quick convergence of the process. Hence, for any initial feasible starting point X1, the value of r1 can be taken as

ANMF.CH11_4PP.indd 927

r1  0.1 to 1.0

f ( X1 )

(11.117) m   1 / g j ( X1 ) j1

3/1/2023 3:16:39 PM

928 • Applied Numerical Methods Using MATLAB Subsequent Values of the Penalty Parameter: Once the initial value of rk is chosen, the subsequent values of rk is selected so that rk+1 < rk (11.118) The values of rk are chosen such that rk+1 < crk (11.119) where c < 1. The value of c can be taken as 0.1, 0.2, or 0.5. Convergence Criteria The process will be terminated whenever the following conditions are satisfied. The relative difference between the values of the objective function obtained at the end of any two consecutive unconstrained minimizations falls below a small number e1, that is,

f (X k )  f (X k 1 )  1 (11.120) f (X k )

The difference between the optimum points X ∗k and X k1 becomes very small. This can be judged in several ways. Some of them are given below: |(DX)i| ≤ e2 (11.121) where DX = X ∗k – X k1 and (DX)i is the ith component of the vector DX. max|(DX)i| ≤ e3 (11.122)

| X | [(X)12  (X)22  ....  (X)2n ]1 / 2   4 (11.123)

It is advisable to normalize the constraints so that they vary between –1 and 0 as far as possible. If the constraints are not normalized, the problem can still be solved effectively by defining different penalty parameters for different constraints as

ANMF.CH11_4PP.indd 928

m

Rj

j 1

g j (X )

(X, rk )  f (X)  rk 

(11.124)

3/1/2023 3:16:53 PM

Optimization • 929

where R1, R2, …., Rm are selected such that the contributions of different gj(X) to the f function will be approximately the same at the initial point X1. EXAMPLE 11.19 (Interior Penalty Function Method) Find the minimum of the function F(X) = (X1 – 1)3 + (X2 – 5)2 Subject to –X1 + 1 ≤ 0 –X2 + 5 ≤ 0 using the interior penalty function method. Solution: To find the minimum of a constrained function by penalty function approach, no initial starting point is required, since the method of calculus will be used to minimize the function. The pseudo-objective function f(X, r) is given by  1 1  (X, r)  (X 1  1)3  (X 2  5)2  r     X1  1 X 2  5  To find the minimum of the pseudo-objective function,   1  0  3(X 1  1)2  r  2  X 1  ( X 1  1)  Simplifying and solving this equation, we get 3(X1 – 1)2 (–X1 + 1)2 – r = 0 3(X1 – 1)4 – r = 0 (X1 – 1)4 = r/3 1

 r 4 X 1  1    (E.1) 3 Similarly, at the minimum of the pseudo-objective function,   1  0  2(X 2  5)  r  2  X 2  ( X 2  5) 

ANMF.CH11_4PP.indd 929

3/1/2023 3:16:59 PM

930 • Applied Numerical Methods Using MATLAB Simplifying and solving this equation, we get 2(X2 – 5)3 – r = 0 (X2 – 5)3 = r/2 1/3

r X 2  5    (E.2) 2 Rewriting the Equations (E.1) and (E.2), we get the optimum values as 1/ 4

r X 1  1    3

1/ 3

r X  5  2  2

Substituting these optimum values in the pseudo-objective function gives

1/2

r min (r)    3

r   2

2/3

  r 1 / 3  r 1 / 4        2 3  r    1 / 4  1/ 3   r r         3 2 

To find the minimum of the original objective function, we have to decrement the value of r, that is, Fmin (X)  lim min (r) r 0

X 1  lim X 1 (r) r 0

X 2  lim X 2 (r) r 0

In the interior penalty function method, we have to start initially with a high value of r (say 106) and decrement it until we get the optimum solution. The results of this iterative procedure are shown in the following table.

ANMF.CH11_4PP.indd 930

3/1/2023 3:17:09 PM

Optimization • 931

r

X1∗

X 2∗

fmin(r)

Fmin(X)

1000000.0 10000.0 100.0 1.0 1.0E-2 1.0E-4 1.0E-6 1.0E-8 1.0E-9

25.02811 8.598357 3.402811 1.759836 1.240281 1.075984 1.024028 1.007598 1.004273

80.97316 21.62111 8.636304 5.795537 5.174045 5.038077 5.008330 5.001822 5.000853

61430.98 2261.097 88.32125 3.781561 0.186458 0.011108 0.000804 0.000068 0.000020

19644.56 714.9528 27.09534 1.071570 0.044164 0.018885 0.000083 0.000003 0.000000

It is clear from the above table that when r = 1.0E-9, the function is minimum and the optimum values are X 1  1.004273 X 2  5.000853 Fmin(X*) = 0

11.14  MATLAB BUILT-IN FUNCTIONS 11.14.1 MATLAB Function: fminbnd The fminbnd combines the slow, dependable golden-search with the faster but perhaps unreliable, parabolic interpolation. fminbnd initially applies parabolic interpolation and continues to apply it as long as acceptable or satisfactory results are found. If acceptable results are not obtained, then fminbnd employs the golden-section search to obtain the results. The algorithm used is Brent’s method that combines golden-section search with quadratic interpolation. A simple expression of its syntax is [xmin, fval] = fminbnd (function, x1, x2) where x and fval are the location and value of the minimum. EXAMPLE 11.20 Minimize f(x1, x2) = 90 (x2 –x12) 2 + (1 – x1) 2 using the starting point  x1 = -1.1, x2 = 1.0 Use MATLAB built-in command fminsearch.

ANMF.CH11_4PP.indd 931

3/1/2023 3:17:14 PM

932 • Applied Numerical Methods Using MATLAB Solution: The minimum = 0 at x = 1, y = 1 EDU>> Run_EN_11_20 x=  1.0000  1.0000 fval =  7.2476e-010 Run_EN_11_20 y=@(x)90.* (x(2)-x(1)^2)^2 + (1.-x(1))^2; [x,fval] = fminsearch(y,[-1.1,1.0]) EXAMPLE 11.21 Determine the length of the shortest ladder that reaches from the ground over the face of the building’s wall using MATLAB function fminbnd. Given that h = d = 4.1 m. The length of the shortest ladder that reaches from the ground over the face of the building’s wall is given by l

2

h

 hx  l  x 2     x d

d

where l, x, y, h and d are shown in Figure 11.19.

y

x FIGURE 11.19 

Solution: The following MATLAB program is used to determine the value of x that minimizes the length.

>> fx=@(x) sqrt(x^2+(4.1* x/(x-4.1))^2); >> [xmin,fmin]=fminbnd(fx,1,10) xmin =  8.2000

ANMF.CH11_4PP.indd 932

3/1/2023 3:17:16 PM

Optimization • 933

fmin =  11.5966 Hence, the shortest ladder is l = 11.5966 m, x = 8.2 and hx 4.1(8.2) y   8.2 (x  d) (8.2  4.1) EXAMPLE 11.22 Use the MATLAB fminbnd function to find the maximum of y = (3* (sin(x)) - (x 2/12) Solution: The graph of this function is shown in Figure 11.20. By inspection, the maximum value of the function is close to 2.8 and occurs at approximately x = 1.5.

FIGURE 11.20

ANMF.CH11_4PP.indd 933

3/1/2023 3:17:18 PM

934 • Applied Numerical Methods Using MATLAB To compute the exact value of the maximum of the function, calculate the negative of the function (y). Use fminbnd on this new function (fun) to calculate the minimum value. The maximum of the original function occurs at the same x as the minimum of the new function. The maximum value of the original function is the negative of the new function. Run_EN_11_22 fun = @(x)(-1.0)* (3*(sin(x)) - (x.^2)/12) x = fminbnd(fun, 0,5.0) fun = -1.0 * (3*(sin(x)) - (x.^2)/12) EDU>> Run_EN_11_22 fun =  @(x)(-1.0)* (3*(sin(x)) - (x.^2)/12) x=  1.4880 fun =  -2.8052 Therefore, the maximum of the original function, f, is   y = 2.8052 at   x = 1.4880 EXAMPLE 11.23 Use MATLAB’s function fminbnd to find x that minimizes f (x)  x 4  2.1x 3  8.2 x 2  5.3 x In the interval (-2,2). Solution: The following script generates the required results: >> z=@(x) -(x^4+2.1*x^3-8.2*x^2+5.3*x); >> [x, f]=fminbnd(z,-2,2)

ANMF.CH11_4PP.indd 934

3/1/2023 3:17:19 PM

Optimization • 935

x=  0.4004 f=  -0.9680 EXAMPLE 11.24 The torque transmitted to an induction motor is a function of the slip between the rotation of the stator field and the rotor speed s. The torque and slip are related by 20s T 2 (5s  4s  5) Use MATLAB’s function fminbnd to determine the slip at which the maximum torque occurs. Solution: The following script generates the required results: >> T=@(s) -(20*s/(5*s^2-4*s+5)); >> [smin, Tmin]=fminbnd(T,0,5) smin =  1.0000 Tmin =  -3.3333 11.14.2 MATLAB Function: fminsearch MATLAB has a standard built-in function fminsearch that can be used to determine the minimum of a multidimensional optimization. fminsearch is based on the Nelder-Mead method. The Nelson-Mead method is a directsearch method that utilizes only function values (derivatives not required) and handles non-smooth objective functions. A simple expression of its syntax is [xmin, fval] = fminsearch(function, x1, x2)

ANMF.CH11_4PP.indd 935

3/1/2023 3:17:21 PM

936 • Applied Numerical Methods Using MATLAB where xmin and fval are the location and value of the minimum, function is the name of the function being evaluated, and x1 and x2 are the bounds of the interval being searched. The algorithm is based on the Nelder-Mead method, also known as the downhill simplex, which is reliable, but much less efficient than Powell’s method. EXAMPLE 11.25 The potential energy function for the system shown in Figure 11.21 is given by 1 1 PE  k 1 x 12  k 2 (x 1  x 2 )2  Fx 2 2 2 a) develop a contour and surface plots in MATLAB using the function fminsearch b) minimize the potential energy function to obtain the equilibrium displacements x1 and x2 Given the forcing function F = 120 N, k1 = 25 N/m and k2 = 20 N/m.

x1 k1

x2 k2

1

2

F

FIGURE 11.21 

Solution: a) The contour and surface plots are generated with the following script: >> k1=25;k2=20;F=120; >> x=linspace(0,20,40);y=linspace(0,30,50); >> [x1,x2]=meshgrid(x, y); >> z=0.5*k1*x1.^2+0.5*k2*(x2-x1).^2-F*x2; >> subplot(1,2,1); >> cs=contour(x1,x2,z);clabel(cs);

ANMF.CH11_4PP.indd 936

3/1/2023 3:17:23 PM

Optimization • 937

>> xlabel(‘x_1’);ylabel(‘x_2’); >> title(‘(a) Contour plot’);grid; >> subplot(1,2,2); >> cs=surfc(x1,x2,z); >> zmin=floor(min(z)); >> zmax=ceil(max(z)); >> xlabel(‘x_1’);ylabel(‘x_2’);zlabel(‘f(x_1,x_2)’); >> title(‘(b) Mesh plot’); (a) Contour plot 4e+003

30

(b) Mesh plot

25 3e+003

10000

2e+003

8000 6000 1 2

f(x ,x )

x

2

20

15

10

4e 5e

5 0 0

0

4000 2000 0 -2000 30 20

20

7 1e+003 2e+003 6e+0 3e+003 8e9 10 20 x1

10

10 x2

0 0

x1

FIGURE 11.22

ANMF.CH11_4PP.indd 937

3/1/2023 3:17:26 PM

938 • Applied Numerical Methods Using MATLAB b) The values of x1 and x2 that minimizes the PE function can be determined as >> PE=@(x) 0.5*k1*x(1).^2+0.5*k2*(x(2)-x(1)).^2-F*x(2); >> [xmin,Fmin]=fminsearch(PE,[5,5]) xmin =  4.8000 10.8000 Fmin =  -648.0000 EXAMPLE 11.26 Use the fminsearch function in MATLAB to determine the maximum of f(x, y) = 5x + 2y + x2 – 3x4 + 2xy – 4y2 Solution: The following MATLAB program can be used to determine the minimum of f(x, y): >> fxy=@(x) -(5*x(1)+2*x(2)+x(1)^2-3*x(1)^4+2*x(1)*x(2)-4*x(2)^2); >> [x,fmin]=fminsearch(fxy,[0,0]) x=  0.8607  0.4652 fmin =  -4.2635 EXAMPLE 11.27 A finite-element model of a cantilever beam subject to certain point loading and bending moments at the end of the beam is given by optimizing f(x, M) = 5x2 – 5xM + 3M2 – x – 2M where x = end displacement and M = end moment. Determine the values of x and M that minimizes f(x, M).

ANMF.CH11_4PP.indd 938

3/1/2023 3:17:26 PM

Optimization • 939

Solution: The following MATLAB program can be used to determine the values of x and M that minimizes f(x,M): >> fxM=@(x) 5*x(1)^2-5*x(1)*x(2)+3*x(2)^2-x(1)-2*x(2); >> [x,fval]=fminsearch(fxM,[0,0]) x=  0.4571 0.7143 fval =  -0.9429 EXAMPLE 11.28 Using the MATLAB function fminsearch, maximize the function f(x1, x2) = 3x1x2 + 5x1 –x12 – 3x22 with initial guess x1 = -1.0, x2 = 1.0 Solution: The minimum of –f occurs at x = 10.0 y = 5.0 and the minimum has a value of -25.0. The maximum of the original function, f, occurs at x = 10.0 y = 5.0 and has a value of 25.0. EDU>> Run_EN_11_28 x=  10.0000  5.0000 fval =  -25.0000 Run_EN_11_28 y=@(x)-1.0 * (3.*x(1).*x(2)+5.*x(1) -x(1).^2 - 3.*(x(2).^2)); [x,fval] = fminsearch(y,[-1.0,1.0])

ANMF.CH11_4PP.indd 939

3/1/2023 3:17:26 PM

940 • Applied Numerical Methods Using MATLAB It should be noted here that both of these functions fminbnd and fminsearch can be called with various control options that set optimization parameters (such as the tolerances) and control the display of results. There are also additional output parameters that may be used in the function call.

11.15  ADDITIONAL EXAMPLE PROBLEMS AND SOLUTIONS Examples 11.29 to 11.33 are based on tutorials and demos from MATLAB (MathWorks, Inc.). EXAMPLE 11.29 This is an unconstrained minimization example. Find the set of values [x1, x2] that solve min f (x)  e x1 (4 x 12  2 x 22  4 x 1 x 2  2 x 2  0.9) x

Starting guess: x0 = [–1, 1]. MATLAB Solution [Using built-in function] Write an m-file objfun.m function f=objfun(x) f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9); Solution: Write one of the unconstrained optimization routines >> x0=[-1,1]; % Starting guess at the solution >> options=optimset(‘LargeScale’,’off’); >> [x,fval,exitflag,output]=fminunc(@objfun,x0,options) Optimization terminated: relative infinity-norm of gradient less than options. TolFun. x=  0.5000  -1.0000

ANMF.CH11_4PP.indd 940

3/1/2023 3:17:28 PM

Optimization • 941

fval =  3.6609e-016 exitflag =  1 output =   iterations: 8   funcCount: 66   stepsize: 1   firstorderopt: 7.3704e-008    algorithm: ‘medium-scale: Quasi-Newton line search’     message: ‘Optimization terminated: relative infinity-norm of gradient less than options. TolFun.’ EXAMPLE 11.30 This is a nonlinear inequality constrained example. Find x that solves min f (x)  e x1 (4 x 12  2 x 22  4 x 1 x 2  2 x 2  0.9) x

Subject to the constraints x1x2 – x1 – x2 ≤ –2.0 x1x2 ≥ –10 Starting guess: x0 = [–1, 1]. Solution: The constraints are written in the form c(x) ≤ 0. x1x2 – x1 – x2 + 2 ≤ 0 –x1x2 – 10 ≤ 0 MATLAB Solution [Using built-in function]: Write an m-file objfun.m function f=objfun(x) f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9);

ANMF.CH11_4PP.indd 941

3/1/2023 3:17:29 PM

942 • Applied Numerical Methods Using MATLAB Write an m-file confun.m for the constraints function [c,ceq]=confun(x) %Nonlinear inequality constraints c=[2+x(1)*x(2)-x(1)-x(2);-x(1)*x(2)-10]; %Nonlinear inequality constraints ceq=[]; Invoke constrained optimization routine >> x0=[-1,1]; % Initial guess at the solution >> options=optimset(‘LargeScale’,’off’); >> [x,fval]=fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options) Optimization terminated: first-order optimality measure less   than options.TolFun and maximum constraint violation is less   than options.TolCon. Active inequalities (to within options.TolCon = 1e-006):  lower  upper  ineqlin  ineqnonlin   1          2 x=  -9.5474  1.0474 fval =  0.0236 >> [c,ceq]=confun(x) c=  1.0e-007 *  -0.9035  0.9035 ceq =  []

ANMF.CH11_4PP.indd 942

3/1/2023 3:17:29 PM

Optimization • 943

EXAMPLE 11.31 This is a constrained optimization example: inequalities and bounds. Minimize f (x)  e x1 (4 x 12  2 x 22  4 x 1 x 2  2 x 2  0.9)

Subject to 2 + x1x2 – x1 – x2 ≤ 0

–x1x2 ≤ 0 x1 ≥ 0 x2 ≥ 0 Initial values:

x0 = [–1, 1].

Solution: MATLAB Solution [Using built-in function] function f=objfun(x) f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9); function [c, ceq]=confun(x) %Nonlinear inequality constraints c=[2+x(1)*x(2)-x(1)-x(2);-x(1)*x(2)-10]; %Nonlinear inequality constraints ceq=[]; >> x0=[-1,1]; % Initial guess at the solution >> lb=[0,0]; % Lower bounds >> ub=[]; % Upper bounds >> options=optimset(‘LargeScale’,’off’); >> [x,fval]=fmincon(@objfun,x0,[],[],[],[],lb,ub,@confun,options) Optimization terminated: first-order optimality measure less than options. TolFun and maximum constraint violation is less than options.TolCon. Active inequalities (to within options.TolCon = 1e-006):  lower  upper  ineqlin  ineqnonlin  1            1

ANMF.CH11_4PP.indd 943

3/1/2023 3:17:31 PM

944 • Applied Numerical Methods Using MATLAB x=  0  1.5000 fval =  8.5000 >> [c,ceq]=confun(x) c=  

0

 -10 ceq =  [] EXAMPLE 11.32 This is a constrained example with gradients. Minimize f (x)  e x1 (4 x 12  2 x 22  4 x 1 x 2  2 x 2  0.9)

Subject to 2 + x1x2 – x1 – x2 ≤ 0

–x1x2 ≤ 0 Initial values:

x0 = [–1, 1].

Solution: MATLAB Solution [Using built-in function] Write an m-file for the objective function and gradient The objective function and its gradient are defined in the m-file objgrad.m as follows: function [f, G]=objfungrad(x) f=exp (x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9); t=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9); G=[t+exp(x(1))*(8*x(1)+4*x(2)),  exp(x(1))*(4*x(1)+4*x(2)+2)];

ANMF.CH11_4PP.indd 944

3/1/2023 3:17:32 PM

Optimization • 945

Write an m-file for the nonlinear constraints and the gradients of the nonlinear constraints: The constraints and their partial derivatives are contained in the m-file confungrad.m: function[c,ceq,dc,dceq]=confungrad(x) c=[2+x(1)*x(2)-x(1)-x(2);  -x(1)*x(2)-10]; dc=[x(2)-1,-x(2); x(1)-1,-x(1)]; ceq=[]; dceq=[]; Invoke constrained optimization routine: Define a guess at the solution: >> x0=[-1,1]; >> options=optimset(‘LargeScale’,’off’); >> options=optimset(options,’GradObj’,’on’,’GradConstr’,’on’); >> lb=[],ub=[], % no lower or upper bounds >> [x,fval]=fmincon(@objfungrad,x0,[],[],[],[],lb,ub,@confungrad,options) Optimization terminated: first-order optimality measure less than options.TolFun and maximum constraint violation is less than options. TolCon. Active inequalities (to within options.TolCon = 1e-006):  lower  upper  ineqlin  ineqnonlin  1            2 x=  -9.5474  1.0474 fval =  0.0236

ANMF.CH11_4PP.indd 945

3/1/2023 3:17:32 PM

946 • Applied Numerical Methods Using MATLAB >> [c,ceq]=confungrad(x) % check the constraint values at x c=  1.0e-007 *  -0.9032  0.9032 ceq =  [] EXAMPLE 11.33 Minimize f (x)  e x1 (4 x 12  2 x 22  4 x 1 x 2  2 x 2  0.9)

Subject to x 12  x 22  1

–x1x2 ≥ –10

Initial values:

x0 = [–1, 1].

Solution: MATLAB Solution [Using built-in function] The objective function and its gradient redefined in the m-file objfun.m as follows: function f=objfun(x) f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9); The m-file confuneq.m contains the equality and inequality constraints: function [c,ceq]=confuneq(x) c=-x(1)*x(2)-10; ceq=x(1)^2+x(2)-1; Define a guess at the solution: >> x0=[-1,1]; We will use the option as below: >> options=optimset(‘LargeScale’,’off’);

ANMF.CH11_4PP.indd 946

3/1/2023 3:17:35 PM

Optimization • 947

Call the optimization algorithm:. >> [x,fval,exitflag,output]=fmincon(@objfun,x0,[],[],[],[],[],[],@confuneq, options); Optimization terminated: first-order optimality measure less than options. TolFun and maximum constraint violation is less than options. TolCon. No active inequalities. >> x x=  -0.7488  0.4393 The function value at the solution is >> fval fval =  1.4621 The constraint values at the solution are >> [c,ceq]=confuneq(x) c=  -9.6710 ceq =  6.4376e-009 The total number of function evaluations was >> output.funcCount ans =  21 Changing the default termination tolerances Consider the original unconstrained problem solved first Minimize f(x)= exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9

ANMF.CH11_4PP.indd 947

3/1/2023 3:17:35 PM

948 • Applied Numerical Methods Using MATLAB This time we will solve it more accurately by overriding the default termination criteria (options. TolX and options. TolFun). Create an anonymous function of the objective to be minimized: >> fun=@(x) exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9); >> fun fun =   @(x) exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9) Make a guess at the solution as >> x0= [-1, 1]; Set the optimization options: turn off the large – scale algorithms (the default): >> options=optimset (‘LargeScale’,’off’); Override the default termination criteria: % Termination tolerances on X and f. >> options=optimset (options,’TolX’, 1e-3,’TolFun’, 1e-3); Call the optimization algorithm: >> [x, fval, exitflag, output] =fminunc (fun, x0, options); Optimization terminated: relative infinity-norm of gradient less than options. TolFun. The optimizer has found a solution at >> x x=  0.5246  -1.0248 The function value at the solution is >> fval fval =  -0.1669

ANMF.CH11_4PP.indd 948

3/1/2023 3:17:35 PM

Optimization • 949

The total number of function evaluations was >> output.funcCount ans =  48 Set optimization options: turn off the large scale algorithms (the default): >> options=optimset (options, ‘Display’,’iter’); >> [x, fval, exitflag, output] =fminunc (fun, x0, options); First-order ­optimality

Iteration

Func-count

f(x)

Step-size

0

3

1.80261

0.736

1

9

1.68824

0.377066

0.295

2

30

-0.124807

21.3836

0.964

3

36

-0.160866

0.239618

0.158

4

45

-0.166915

0.0531349

0.0219

5

48

-0.166933

1

0.00114

Optimization terminated: relative infinity-norm of gradient less than options. TolFun.

11.16 SUMMARY Optimization is the term used for minimizing or maximizing a function. Some strategies for optimizing a function subject to constraints have been discussed. The methods given in this chapter give only a glimpse of the optimization procedure and techniques used to achieve the optimum. Optimization deals with finding the maxima and minima of a function that depends on one or more variables. The goal is to determine the values of the variables that yield maxima or minima for the function. In this chapter, we presented the distinction between global and local optima, the golden ratio, locating the optimum of a single-variable function with the golden-section search, the unconstrained minimization of functions, minimization with constraints using Lagrange multipliers, numerical optimization, optimization involving single variables, gradient methods, Newton’s method, methods based on the concept of quadratic convergence, Powell’s method, the Fletcher-Reeves method, direct search

ANMF.CH11_4PP.indd 949

3/1/2023 3:17:36 PM

950 • Applied Numerical Methods Using MATLAB methods such as the Hooke and Jeeves method, the method of successive approximation, the MATLAB built-function fminbnd to determine the minimum of a one-dimensional functions and the fminsearch function to determine the minimum of a multidimensions function. Numerous illustrative example problems with solutions were presented.

REFERENCES Arora, J. S., Introduction to Optimum Design, McGraw Hill, New York, 1989. Fox, R. L., Optimization Methods for Engineering Design, Addison Wesley, Reading, MA, 1972. Papalambros, P. Y., and Wilde, D. J., Principles of Optimal Design, Cambridge University Press, Cambridge, UK, 1988. Rao, S. S., Optimization: Theory and Applications, 2nd ed., Wiley, New York, 1984. Recklaitis, G. V., Ravindran, A., and Ragsdell, K. M., Engineering Optimization: Methods and Applications, Wiley, New York, 1983. Shoup, T. E., and Mistree, F., Optimization Methods with Applications for Personal Computers, Prentice Hall, Englewood Cliffs, NJ, 1987. Siddall, J. N., Optimal Engineering Design: Principles and Applications, Marcel Dekker, New York, 1982. Vanderplatts, G. N., Numerical Optimization Techniques for Engineering Design with Applications, McGraw Hill, New York, 1984.

EXERCISES NOTE

In addition to solving the following exercises through analytical solutions or long-hand numerical computations, try to solve them using-user defined MATLAB functions or MATLAB script files and/or MATLAB built-in functions where possible or applicable. 11.1. A total of 360 linear feet of tubes must be installed in a heat exchanger in order to provide the necessary heat-transfer surface area. The total cost of the installation includes: Cost of the tubes = $900 Cost of the shell = 30 D2.5 L

ANMF.CH11_4PP.indd 950

3/1/2023 3:17:36 PM

Optimization • 951



Cost of the floor space occupied by the heat-exchanger = 25 DL The spacing of the tubes is such that 25 tubes will fit in a cross-sectional area of 1 ft2 inside the shell. Determine the diameter D and the length L of the heat exchanger to minimize the purchase cost using Lagrange multiplier method.

11.2. Fit a polynomial by quadratic approximation and determine the values of X at which F(X) is a minimum.

X 1 2 3

F(X) 20 –17 15

11.3. Fit a polynomial by quadratic approximation and determine the values of X at which F(X) is a minimum.

X 1 2 3

F(X) –7 5 14

11.4. Fit a polynomial by quadratic approximation and find the value of x at which F(x) is a minimum.

X 1 2 3

F(X) 12 7 20

11.5. Determine the value of insulation thickness for a furnace wall that will minimize the cost. Given, Wall temperature = 600ºF Outside air temperature = 70ºF Air film coefficient = 5Btu/ (h) (ft2) (ºf) Thermal conductivity of insulator = 0.04 btu/ (h) (ft) (ºf) Insulation cost/inch of thickness = $0.80 per ft2 Value of heat saved = $0.70 per million btu Fixed charges = 25 percent per year Hours of operation = 9000 h/year Surface of furnace wall = 125 ft2

ANMF.CH11_4PP.indd 951

3/1/2023 3:17:36 PM

952 • Applied Numerical Methods Using MATLAB 11.6. Use the golden-section search to find x that minimizes f(x) = 1.8x3 + 3x2 – 2x

subject to the constraint x ≥ 0. Compare the result with the analytical solution.

11.7. Use the golden-section search to find the minimum of x2 f (x )   2 sin x 10

with the interval from xa = - to xb = 4.

11.8. The trajectory of a ball is given by y  (tan 0 )x 

g x2  y 0 2 2 v cos 0 2 0

where y = the height (m), q0 = the initial angle (radians), v0 = the initial velocity (m/s), g = the gravitational constant = 9.81 m/2, and y0 = the initial height (m). Use the golden-search to determine the maximum height given y0 = 1 m, v0 = 30 m/s, and q0 = 55º. Use the initial guesses of xa = 0 and xb = 60 m. 11.9. Pressure measurements were taken at certain points behind an airfoil over time. The data best fits the curve y = 6 cos x – 2 sin x from x = 0 to 5 seconds. Determine the minimum pressure. 11.10. One wave function of the hydrogen atom is given by y = C (27 - 18s + 12s2) e σ / 3 where s = zr/a0 2/3



1  z    813   a 0  z = nuclear charge C=

a0 = Bohr radius

r = radial distance

Determine s where y is at a minimum. Check the results analytically.

ANMF.CH11_4PP.indd 952

3/1/2023 3:17:42 PM

Optimization • 953

11.11. Minimize f(x1, x2) = x1 – 0.5x2 + 2x 12 + 2x1x2 – x 22 using Newton’s method. Starting point: (0, 0). 8 2 11.12. Use Newton’s method to find the maximum of f(x) = 6 sin x – x 19 with the initial guess of x0 = 1, 8. 11.13. Minimize f(x1, x2) = x1 – 0.5x2 + 2x12 + 2x1x2 + x22 using Newton’s method. Starting point: (0, 0). 11.14. Find the minimum of the following function using Powell’s method: F(x)  7 x 12  5 x 22  8 x 1 x 2  5 x 1

Starting point: (0, 0).

11.15. Find the minimum of the function f = 90(y – x2)2 + (1 – x) 2

with Powell’s method. Starting point: (–1, 1).

11.16. Find the minimum of the function f(x, y) = 100(y – x2)2 + (1 – x) 2

with Powell’s method. Starting point: (–1, 1).

11.17. The cylindrical container has a conical bottom and an open top. If the volume V of the container is to be 2 m3, find the dimensions R, H, and B that minimize the surface area S. Note that B  V  R 2   H  3 



S  R 2H  B2  R 2

 R H

B

ANMF.CH11_4PP.indd 953

3/1/2023 3:17:51 PM

954 • Applied Numerical Methods Using MATLAB 11.18. Use the Fletcher-Reeves method to locate the minimum of f(x) =9 x 12  3 x 22  8 x 1 x 2  2 x 1 .

Start with X0 = [0 0]T.

11.19. Use the Fletcher-Reeves method to locate the minimum of f(x1, x2) = 10 x 12  3 x 22  10 x 1 x 2  2 x 1

starting at x0 = [0 0]T.

11.20. Find the minimum of the function F(x, y, z) = 3x2 + 3y2 + z2 + xy + xz – 2y

using the Fletcher-Reeves method.

11.21. Minimize (x1 – x2)2 + (x2 – x3)4

Initial guess: (–2.5, 2, 2)

11.22. Minimize x1x4(x1 – x2+ x3) + x3

Initial guess: (1, 4, 5, 1)

11.23. Minimize x1 + 2x2+ 3x3 + exp(x1x4)

Initial guess: (1, 2, 1, 2)

11.24. Minimize x1 + 2x2+ 5x3 + exp(x1x4)

Initial guess: (1, - 2, -1, 2)

11.25. Minimize x1 + 2x2- 3x3 + exp(x1x4)

Initial guess: (1, 2, 1, 2)

11.26. Minimize f(x1, x2) = (x1 – 1)2 + (x2 – 1)2 Subject to g1(x1, x2) = –x1 ≤ 0 g2(x1, x2) = –x2 ≤ 0 g3(x1, x2) = x 12 + x 22 –1 ≤ 0 11.27. Minimize f(x1, x2) = (x1 – 1)2 + x 22 Subject to g2(x1, x2) = –x2 ≤ 0 g3(x1, x2) = x2 – (1 – x1)2 ≤ 0

ANMF.CH11_4PP.indd 954

3/1/2023 3:17:57 PM

Optimization • 955

11.28. Minimize

f(x) = x 12 + x 22 – 6x1 – 8x2 + 10



4x 12 + x 22 ≤ 0

Subject to

3x1 + 5x2 ≤ 0 using the penalty function method. 11.29. Minimize the following function f(x) by using the penalty function method. f(x) = [(x1 + 2)2 + 5(x2 – 2)2] [x1 – 1.5)2 + 0.5(x2 – 0.5)2]   0  x1     x 2   0  3 x1  x1 x 2  4 x 2  6   0      2 x1  x 2  5   0      2  3 x1  4 x 2  4 x 2  0 



Subject to



Starting values: [0.4, 0.5].

11.30. Minimize f(x) = x1 + 6x2 Subject to x1 + x2 ≤ 3 –x1 + x2 ≤ 2 Initial guess: x0 = (0, 0) 11.31. Minimize f(x) = 2x1 – 3(x2 – 4)2

Subject to



x 12 + x 22 – 8 ≤ 0 x 12 + (x2 – 4)2 ≤ 0

Initial guess: [–1, 1]

11.32. Minimize f(x) = x1 + 5x2+ 3x3 Subject to x1 + 5x2 – x3 ≥ 4 –x1 + x2 + 2x3 ≥ 1 –x1 + 3x2 + 3x3 ≥ 5 –3x1 + 8x2 – 5x3 ≥ 3 Initial guess: x0 = (0, 1, 1) 11.33. Solve Exercise 11.32 with x1, x2, x3 ≥ 0. 11.34. Solve Exercise 11.26 with x1, x2 ≥ 0.

ANMF.CH11_4PP.indd 955

3/1/2023 3:18:05 PM

956 • Applied Numerical Methods Using MATLAB 11.35 Minimize f(x) = (x1 – x2)2 -x2 Subject to 1 – x1 + x2 ≤ 2 x1 + 2x2 ≤ 3 Initial guess: [2, 0] 11.36. Roller bearings are subject to fatigue failure caused by large contact loads F acting along the x-axis. The location of the maximum stress along the x-axis is equivalent to maximizing the function 0.5    1  x 2 1  x 2  1  x  1 x Determine the value of x that minimizes f(x) using the MATLAB function fminbnd.

f (x) 

0.5

2

11.37. Use MATLAB function fminbnd to find the minimum of the function: (x 2  5)2 f (x )   ex  5 15 11.38. Minimize f = 0.65 – 0.75/(1 + x2) – 0.5 * tan–1(1/x) using the MATLAB function fminbnd. 11.39. Use the MATLAB fminbnd function to find the maximum of y = 3 sin x – x2/12

with the interval of x = [0,7].

11.40. Use the MATLAB fminsearch function to find the maximum of f(x, y) = 3xy + 9x – x2 – 9y2

using the initial guesses, x = –1 and y = 1.

11.41. Find the minimum point of the following objective function f(x) using the MATLAB built-in function fminbnd: (x 2  3.2)2

f (x) 

11

 e x  6.9

11.42. Given the following function f(x, y) = –9x + x2 + 12y + 4y2 – 2xy

ANMF.CH11_4PP.indd 956

Determine the minimum (a) graphically, (b) numerically with the fminsearch function.

3/1/2023 3:18:10 PM

Optimization • 957

11.43. The two-dimensional distribution of pollutant concentration in a channel is given by c(x, y) = 8 + 0.1x + 0.2y – 0.05x2 – 0.015y2 – 0.008xy

Determine the exact location of the peak concentration using the MATLAB fminsearch function.

11.44. A two-spring vibrating system can move both horizontally and vertically. The potential energy of the system is given by PE(x 1 , x 2 )  0.5k a



 0.5k b

x 12  (L a  x 2 )2  L a





2

x 12  (L b  x 2 )2  L b



2

 F1 x 1  F2 x 2

If the parameters are ka = 10 N/cm, kb = 2 N/cm, La = 10 cm, Lb = 10 cm, F1 = 3 N and F2 = 5 N. Use MATLAB fminsearch function to solve for the displacements and the potential energy.

11.45. Find the minimum of the function f (x 1 , x 2 )  4 x 1 x 2  8 x 1  x 1 2  9 x 2 2

starting from (0, 0). Use the MATLAB fminsearch function.

11.46. Using the MATLAB function fminsearch minimize the function f (x 1 , x 2 )  3.1x 1 x 2  5.1x 1  x 12  3 x 22

with initial guess x1 = –1.0, x2 = 1.0.

11.47. Find the minimum point of the following objective function f(x) using quadratic approximation method. (x 2  5)2 f (x )  1 8 11.48. Find the minimum point of the following objective function f(x) using quadratic approximation method. (x 2  3)2 f (x )  1 7 11.49. Find the minimum point of the following objective function f(x) using the MATLAB built-in function fminbnd. (x 2  3.2)2 f (x )   ex  9 10

ANMF.CH11_4PP.indd 957

3/1/2023 3:18:19 PM

958 • Applied Numerical Methods Using MATLAB 11.50. Find the minimum point of the following objective function f(x) using quadratic approximation method. (x 2  3)2 f (x )   ex  8 8 11.51. This is an unconstrained minimization example. Find the set of values [x1, x2] that solve min f (x)  e x1 (3 x 12  4 x 22  6 x 1 x 2  2 x 2  1.0) x



Starting guess: x0 = [0, 1].

11.52. This is a nonlinear inequality constrained example. Find x that solves min f (x)  e x1 (3 x 12  4 x 22  6 x 1 x 2  2 x 2  1.0) x

Subject to the constraints 2x1x2 – x1 – x2 ≤ 0 x1x2 ≥ –10 Starting guess: x0 = [0, 1]. 11.53. This is a constrained optimization example: inequalities and bounds.

Minimize

min f (x)  e x1 (3 x 12  4 x 22  6 x 1 x 2  2 x 2  1.0) x

Subject to 2 + x1x2 – x1 – x2 ≤ 0 –x1x2 ≤10 x1 ≥ 0 x2 ≥ 0 Starting guess: x0 = [0, 1]. 11.54. This is a constrained example with gradients.

Minimize

min f (x)  e x1 (3 x 12  4 x 22  6 x 1 x 2  2 x 2  1.0) x

Subject to 2 + x1x2 – x1 – x2 ≤ 0 –x1x2 ≤ 0 Starting guess: x0 = [0, 1]. 11.55. Minimize

min f (x)  e x1 (3 x 12  4 x 22  6 x 1 x 2  2 x 2  1.0)



x  x 22  1

Subject to

x

2 1

–x1x2 ≥ –10 Starting guess: x0 = [0, 1].

ANMF.CH11_4PP.indd 958

3/1/2023 3:18:29 PM

CHAPTER

12

Partial Differential Equations Phenomena that vary either with more than one space dimension in both time and space are often modeled as partial differential equations, which contain partial derivatives of one or more dependent variables. Partial differential equations are equations satisfied by partial derivatives of functions of two or more independent variables. They describe many types of physical phenomena in engineering and science. They include transient heat conductions, vibrations of strings and plates, fluid flow, and behavior of electric and magnetic fields. In this chapter, we consider some of the important partial differential equations occurring in engineering applications. A partial differential equation (PDE) contains more than one independent variable, for example, time t and positions x, y, and z. Such an equation contains partial derivatives. Consider the unsteady state heat condition equation, well-known in chemical and mechanical engineering:  2 T 2 T 2 T  T   2  2  2  y z  t  x where a is thermal diffusivity, T is temperature, and x, y, and z are the rectangular coordinates. A few of the well-known examples of partial differential equations are:

• heat-conduction equation • Navier-Stokes equation in fluid flow • Poisson equations (in elasticity, electrostatics, and fluid dynamics) • telephone equation • wave equation

ANMF.CH12_4PP.indd 959

3/1/2023 3:26:46 PM

960 • Applied Numerical Methods Using MATLAB The partial differential equations are usually more difficult to solve ­analytically than the ordinary differential equations. Most of the engineering problems involving partial differential equations require numerical solution. A body is isotropic if the thermal conductivity at each point in the body is independent of the direction of heat flow through the point. The temperature, u ≡ u(x, y, z, t), in an isotropic body can be found by solving the partial differential equation

u   u    u    u   k    k    k   c t x  x  y  y  z  z 

where k, c, and r are functions of (x, y, z) and represent, respectively, the thermal conductivity, specific heat, and density of the body at the point (x, y, z). When k, c, and r are constants, the equation is known as the simple threedimensional heat equation and is expressed as

 2 u  2 u  2 u c u    x 2 y 2 z 2 k t

In most situations were k, c, and r are not constant or when the boundary is irregular, the solution to the partial differential equation must be obtained by approximations techniques. This chapter is intended to provide an introduction to methods for partial differential equations and will concentrate on the classical problems of parabolic, elliptic, and hyperbolic equations with relatively simple domains. The methods that are presented are finite difference methods in which derivatives of functions are replaced by difference equations. We develop methods for solving initial and boundary value problems consisting of equation of a model of physical system. We will primarily deal with the phenomena involving radiation or conduction, and wave motion. Models of both kinds of phenomena involve partial differential equations, called, respectively, the heat equation and the wave equations.

12.1 INTRODUCTION A differential equation in which partial derivatives occur is called a partial differential equation. It is an equation containing an unknown function of two or more variables and its partial derivatives with respect to these variables. Mathematical models of physical phenomena involving more than one

ANMF.CH12_4PP.indd 960

3/1/2023 3:26:50 PM

Partial Differential Equations • 961

independent variable often include partial differential equations. For instance, a model of heat conduction in a region will normally involve three space variables for coordinates of points in the region and one variable for time. The order of a partial differential equation is that of the highest ordered derivative present. A partial differential equation is linear if it is of the first degree in the dependent variable (the unknown function) and its partial derivatives. If each term of such an equation contains either the independent variable or one of the derivatives, the equation is said to be homogeneous, otherwise it is said to be nonhomogeneous. A solution of a partial differential equation is any function which satisfies the equation identically. The general solution is a solution which contains a number of arbitrary independent functions equal to the order of the equation. A particular solution is one which can be obtained from the general solution by particular choice of the arbitrary functions. A singular solution is one which cannot be obtained from the general solution by particular choice of arbitrary function. A boundary-value problem involving a partial differential equation seeks all solutions of a partial differential equation which satisfy conditions called boundary conditions. Existence and uniqueness theorems are the theorems relating to the existence and uniqueness of such solutions.

12.2 CLASSIFICATION OF LINEAR SECOND-ORDER PARTIAL DIFFERENTIAL EQUATION The general linear partial differential equation of order two in two independent variables has the form

A

2u 2u 2u u u    D  E  Fu  G (12.1) B C x 2 xy y 2 x y

where A, B, C, D, E, F, and G are functions of x, y, or are real constants but not dependent on u. A second-order equation with independent variables x and y that does not have the form of Equation (12.1) is called nonlinear. If G = 0, the equation is called homogeneous, while if G ≠ 0, it is called nonhomogeneous. Because of the nature of the solution of Equation (12.1), the equation is often classified as follows:

if B2 – 4AC = 0 2

if B – 4AC > 0

and if  B2 – 4AC < 0

ANMF.CH12_4PP.indd 961

parabolic equation hyperbolic equation

(12.2)

elliptic equation

3/1/2023 3:26:53 PM

962 • Applied Numerical Methods Using MATLAB The above classification in Equation (12.2) governs the number and type of conditions to be prescribed in order that the problem is well posed and has a unique solution. It should be noted here that the lower-order terms (firstorder terms) in Equation (12.1) do not play any role in the classification of the equation. Some important linear partial differential equations of the second order are given below:

2 2u 2  u  c t 2 x 2

One-dimensional wave equation

(12.3)



u 2u  c 2 2 t x

One-dimensional heat equation

(12.4)



2u 2u   0 x 2 y 2

Two-dimensional Laplace equation (12.5)



2u 2u   f (x, y ) x 2 y 2

Two-dimensional Poisson equation

(12.6)



 2u 2u  2u  c2  2  2  2 t y   x

Two-dimensional wave equation

(12.7)



2u 2u 2u    0 x 2 y 2 z 2

Three-dimensional Laplace equation (12.8)

In Equations (12.3) to (12.8), c is a constant, t is time, x, y, z are Cartesian coordinates, and dimension is the number of these coordinates in the equation. Equation (12.6) with f(x, y) ≠ 0 is nonhomogeneous, while the other equations are homogeneous. Applying the conditions given by Equation (12.2) to Equation (12.8), we obtain the following simple examples of the classification of these equations: a) Parabolic equation:

u 2u  c 2 2 t x



 2u 2u  u  c 2  2  2  (Two-dimensional heat equation) t y   x

ANMF.CH12_4PP.indd 962

(One-dimensional heat equation)

3/1/2023 3:27:13 PM

Partial Differential Equations • 963

b) Hyperbolic equation:

2 2u 2  u  c t 2 x 2

(One-dimensional wave equation)



2 2u 2u  2 u  c    2 t 2 y 2   x

(Two-dimensional wave equation)

c) Elliptic equation: 2u 2u 2  2  0 x y

(Two-dimensional Laplace equation)

EXAMPLE 12.1 Classify each of the following equations as elliptic, hyperbolic, or parabolic. a)

2 2  0 x 2 y 2

b)

u 2u k 2 t x

c)

2 2 y 2  y  a t 2 x 2

d)

2u 2u 2u u u 2 3    4  2  4u  3 x  5 y 2 2 x xy y x y

e) x

2u 2u u   4y 2 0 y 2 2 x y x

Solution:

ANMF.CH12_4PP.indd 963

a)

2 2   0 x 2 y 2



The B2 – 4AC = –4 < 0 and the equation is elliptic.

u = ϕ, A = 1, B = 0, C = 1

3/1/2023 3:27:32 PM

964 • Applied Numerical Methods Using MATLAB

u 2u  k 2 y = t, A = k, B = 0, C = 0 t x Then B2 – 4AC = 0 and the equation is parabolic. b)

c)

2 2 y 2  y  a t 2 x 2

y = t, u = Y, A = a2, B = 0, C = –1

Then B2 – 4AC = 4a2 > 0 and the equation is hyperbolic. d)

2u 2u 2u u u 2 3    4  2  4u  3 x  5 y x 2 xy y 2 x y

A = 1, B = 2, C = 3

The B2 – 4AC = –8 < 0 and the equation is elliptic. e) x

2u 2u u   4y 2  0 y 2 2 x y x

A = x, B = 0, C = y

Then B2 – 4AC = –4xy In the region xy > 0 the equation is elliptic. In the region xy < 0 the equation is hyperbolic. If xy = 0, the equation is parabolic. Elliptic equations The simplest examples of this type of PDEs are Poisson’s equation

2u 2u   g( x , y ) x 2 y 2

and the Laplace equation

2u 2u 2   0   or  ∇ u = 0 x 2 y 2

(12.9)

These equations are generally associated with equilibrium or steady-state problems. The analytic solution of an elliptic equation is a function of x and y which satisfies the PDE at every point of the region S which is bounded by a plane closed curve C and satisfies some conditions at every point on C. The condition that the dependent variable satisfies along the boundary curve C is known as boundary condition.

ANMF.CH12_4PP.indd 964

3/1/2023 3:27:46 PM

Partial Differential Equations • 965

Parabolic equation In a parabolic equation, time t is involved as an independent variable. The simplest example of a parabolic equation is the heat conduction equation

u 2u   2 (12.10) t x

The solution u is the temperature at a distance x units of length from one end of a thermally insulated bar after t seconds of heat conduction. Here, the temperature at the ends of a bar is known for all time, that is, the boundary conditions are known. Hyperbolic equation In a hyperbolic equation time t appears as an independent variable. The simplest example of hyperbolic equation is the one-dimensional wave equation

2 2u 2  u  c (12.11) t 2 x 2

Here u is the transverse displacement at a distance x from one end of a vibrating string of length l after a time t. Hyperbolic equations generally originate from vibration problems or from problems where discontinuities can persist in time. The values of u at the ends of the string are usually known for all time (i.e., boundary conditions are known) and the shape and velocity of the string are given at initial time (i.e., initial conditions are known).

12.3 TYPES OF PROBLEMS Generally speaking, there are three types of problems encountered in partial differentiate equations. They are 1. Dirichlet’s problem Let R be a region bounded by a closed curve C and f be a continuous function on C.

ANMF.CH12_4PP.indd 965

3/1/2023 3:27:51 PM

966 • Applied Numerical Methods Using MATLAB Now the problem is to find u satisfying Laplace’s equation

2u 2u   0 in R(12.12) x 2 y 2

and

u = f(x, y) on C.

2. Cauchy’s problem

2u 2u  t 2 x 2



for t > 0 (12.13)



u(x, 0) = f(x)

and ut = (x, 0) = g(x) where f(x) and g(x) are arbitrary. 3. and

u  2 u  for t > 0 t x 2

(12.14)

u(x, 0) = f(x)

The above three problems given by Equations (12.12) to (12.14) are all welldefined (i.e., well-posed) and it can be shown that each of the above problems has unique solution. However, the problem of Laplace’s equation with Cauchy boundary conditions, namely

2u 2u   0 (12.15) x 2 y 2



u(x, 0) = f(x)

and uy(x, 0) = g(x) is deemed an ill-posed problem.

ANMF.CH12_4PP.indd 966

3/1/2023 3:28:02 PM

Partial Differential Equations • 967

12.4 FINITE-DIFFERENCE APPROXIMATION TO PARTIAL DERIVATIVES Consider the xy plane divided into sets of equal rectangles of sides Dx = h and Dy = k by having the equally spaced grid lines parallel to the coordinate axes, defined by xi = ih, i = 0, ±1, ±2, …. yj = jh, j = 0, ±1, ±2, …. The value of u at a mesh point (intersection of horizontal and vertical lines) P(xi, yj) or at P(ih, jk) is denoted by ui,j, i.e., ui,j = u(xi, yj) = u(ih, jk). Now, u x (ih, jk) 

u i 1, j  u i , j

 O(h)   (forward difference approximation) h (12.16) u i , j  u i 1 , j   O(h)   (backward difference approximation) (12.17) h



u i  1 , j  u i 1 , j 2h

Similarly,  u y (ih, jk) 

 O(h 2 )  (central difference approximation)

(12.18)

u i , j1  u i , j

 O(k)   (forward difference approximation) k (12.19) u i , j  u i , j 1   O(k)  (backward difference approximation) k (12.20) u i , j  1  u i , j 1   O(k 2 )   (central difference approximation) 2k (12.21)

ANMF.CH12_4PP.indd 967



u xx (ih, jk) 



u yy (ih, jk) 

u i 1 , j  2 u i , j  u i  1 , j h2 u i , j 1  2 u i , j  u i , j  1 k2

 O(h 2 ) (12.22)  O(k 2 ) (12.23)

3/1/2023 3:28:21 PM

968 • Applied Numerical Methods Using MATLAB

12.5 PHYSICAL PHENOMENA Most physical phenomena can be modeled by using the partial differentiate equations. In this section, we review a few of such physical phenomena. 12.5.1

Laplace’s Equation

Figure 12.1 shows the modeling of the steady-state distribution of temperature in the rectangular metal plate of width a and length b. It also shows that u(x, y) denotes the temperature at each point with the temperature along the boundary of the plate being held constant. It can be shown here that the steady-state temperature distribution is a solution of the PDE.

2u 2u   0 (12.24) x 2 y 2

No forcing term, f(x, y) is present since there are no heat sources or sinks within the plate. Equation (12.24) is known as Laplace’s equation. y g4(x)

b

u(x,y)

g1(y)

0

0

g3(x)

g2(y)

a

x

FIGURE 12.1  Rectangular plate temperature distribution.

We use the following operator, called the two-dimensional Laplacian operator.

2 

2 2  2 (12.25) 2 x y

We can define a three-dimensional Laplacian operator involving x, y, and z in a similar manner. Using the Laplacian operator, Equation (12.24) can be written as

ANMF.CH12_4PP.indd 968

∇2u = 0

(12.26)

3/1/2023 3:28:28 PM

Partial Differential Equations • 969

There are several possible solutions to Laplace’s equation. For example, it is easy to verify using direct substitution that both u(x,y) = x2 – y2 and u(x,y) = exp(x) sin(y) are solutions. To obtain a unique solution, we must impose additional constraints in the form of boundary conditions. For instance, suppose the temperature along the boundary of the plate in Figure 12.1 is as follows.

u(0, y) = g1(y)



u(a, y) = g2(y)



u(x, 0) = g3(x)



u(x, b) = g4(x)(12.27)

Boundary conditions of this type are referred to as Dirichlet boundary conditions. It is also possible to instead specify the first derivative of u along all or part of the boundary. Derivative conditions are referred to as Neumann boundary conditions. 12.5.2

Heat Equation

Consider the thin metal rod of length a shown in Figure 12.2. Suppose the rod is made of a homogeneous material and is enclosed in an insulating jacket except at its end points. Let u(t,x) denote the temperature at time t and point x. It can be shown that u(t,x) is a solution to the following partial differential equation known as the heat equation.

u 2u   2 (12.28) t x

The parameter b is a positive constant that depends on the properties of the material, specifically the thermal conductivity, the specific heat, and the density. The heat conduction equation can be generalized to two dimensions by replacing ∂2u/∂x2 in Equation (12.25) with ∇2u where ∇2 is the Laplacian operator. The heat equation can also be used to model the diffusion process and is therefore sometimes referred to as the diffusion equation. hot

u(t,x)

cold x

0

a FIGURE 12.2  Heat flow along a thin rod.

ANMF.CH12_4PP.indd 969

3/1/2023 3:28:32 PM

970 • Applied Numerical Methods Using MATLAB There are several solutions to the heat equation. To obtain the solution unique, we must impose both an initial condition and boundary conditions. The initial condition specifies the distribution of temperature along the rod at time zero.

u(0, x) = f(x)

(12.29)

The boundary conditions at the two ends of the rod can be of different types. When the temperature at each end of the rod is held constant, it corresponds to two Dirichlet boundary condition.

u(t, 0) = b1



u(t, a) = b2(12.30)

For these boundary conditions, the temperature approaches the following steady-state condition after all transients have died out.

x u(t, x)  b1    (b2  b1 ) as t → ∞(12.31) a

Also, one or both ends of the bar may be insulated, in which case ∂u/∂x = 0. There may be a heat source or a heat sink on either end, which can be modeled with nonzero derivatives. When the heat flow or the diffusion process reaches the steady-state condition, u is no longer changing with time in which case ∂u/∂t = 0. Hence, the two-dimensional version of the heat equation reduces to Laplace’s equation, ∇2u = 0, in the steady state. The steady-state solution to the two-dimensional heat equation is the solution to the Laplace’s equation. 12.5.3

Wave Equation

Figure 12.3 shows a taut elastic string of length a fixed at both ends. Considering the string is homogeneous and of uniform diameter. Let u(t,x) denote the vertical displacement of the string at time t and point x. Also, assume that the initial displacement is small and that the effects of gravity can be ignored. It can be shown that the string displacement is a solution to the following partial differential equation called the wave equation.

ANMF.CH12_4PP.indd 970

2u 2u   (12.32) t 2 x 2

3/1/2023 3:28:38 PM

Partial Differential Equations • 971

u

x 0

a FIGURE 12.3  Vibrating string.

The parameter b is a positive constant that depends on the string tension and density. It has units of (m/s)2, with β being the velocity of wave propagation. The wave equation can be generalized to two dimensions by replacing ∂2u/∂x2 in Equation (12.32) with ∇2u, where ∇2 is the Laplacian operator in Equation (12.25). To obtain unique solutions, we must again impose both initial conditions and boundary conditions. Since the wave equation is second order in time, two initial conditions are required as shown below:

u(0, x) = f(x)

ut(0, x) = g(x)

(12.33)

We use the notation

u t (t, x) 

u(t, x) (12.34) t

Hence, an independent variable used as a subscript denotes partial differentiation with respect to that variable. If the string is fixed at both ends, then the two Dirichlet boundary conditions are simply written as u(t, 0) = 0 u(t, a) = 0

(12.35)

In general, we can impose a constraint on a linear combination of u and ux = ∂u/∂x at each end of the string. 12.5.4

Equation Classification

In general, partial differential equations are classified into three groups. A general linear second-order partial differential equation involving the variables t and x can be written as follows.

ANMF.CH12_4PP.indd 971

3/1/2023 3:28:40 PM

972 • Applied Numerical Methods Using MATLAB



c1

2u 2u 2u u u    d1u  d 2  d 3 (12.36) c c 3 2 t 2 t x x 2 t x

The equations are classified based on the relative values of the coefficients ck as summarized in Table 12.1. In the case of elliptic equations, the variable t can be replaced by the variable y, which makes Laplace’s equation an example of an elliptic partial differential equation. Since neither the c1 nor the c2 term are present in the heat equation, it is a parabolic partial differential equation. The wave equation is an example of a hyperbolic partial differential equation. TABLE 12.1  Linear partial differential equations. Condition

Equation type

Example

2 2

Elliptic

Laplace’s equation

2 2

c = 4c1c3

Parabolic

Heat equation

c 22 > 4c1c3

Hyperbolic

Wave equation

c < 4c1c3

12.6 ELLIPTIC EQUATIONS Here, we present numerical techniques for finding a solution to a partial differential equation of the following general form, which is called Poisson’s equation.

2u 2u   f (12.37) x 2 y 2

The forcing term, f, depends on x and y. When f = 0, Poisson’s equation reduces to Laplace’s equation, also called the potential equation. Here, we solve Equation (12.37) over the following rectangular region R in the xy plane.

R  {(x, y) | 0 < x < a, 0 < y < b} (12.38)

Further, we also put constraints on the solution along the boundary dR or R. Suppose the following Dirichlet conditions are applied here. 12.6.1

u(x, y) = g(x, y)   (x, y) ∈ δR(12.39) Central Difference Method

We convert Equation (12.37) to an equivalent system of difference equations using a finite difference approximation. We are interested in estimating the

ANMF.CH12_4PP.indd 972

3/1/2023 3:28:50 PM

Partial Differential Equations • 973

solution u(x, y) on a uniform grid consisting of m + 2 values of x and n + 2 values of y as shown in Figure 12.4. Let Dx and Dy denote the step size in the x and y directions, respectively.

a (12.40) m1 b y  (12.41) n 1 x 

let us denote xk and yj as the values of x and y at the grid points. Hence, xk = k Dx,  0 ≤ k ≤ (m + 1) yj = j Dx,  0 ≤ j ≤ (n + 1)

(12.42) (12.43)

y ∆x

yn+1

∆y

y1 y0

x0

xm+1

x1

x

FIGURE 12.4  Uniform solution grid.

Here, x0 and xm+1 correspond to the boundaries in the x direction, and y0 and yn+1 correspond to the boundaries in the y direction. The boundary points are indicated with empty circles and the interior points with filled circles in Figure 12.4. Let ukj denote the computed value of u(xk, yj). We use finite-­difference approximations for the second derivative terms in Equation (12.37). The three-point central difference approximations for the second derivative are given by

ANMF.CH12_4PP.indd 973



u xx (x k , y j ) 



u yy (x k , y j ) 

u k  1 , j  2 u k , j  u k 1 , j x 2 u k , j  1  2 u k , j  u k , j 1 y 2

 O(x 2 ) (12.44)  O(y 2 ) (12.45)

3/1/2023 3:29:00 PM

974 • Applied Numerical Methods Using MATLAB Extending the single-subscript notation for partial derivatives to a ­double-subscript notation for second-order partial derivatives, we obtain

u xx (x, y ) 

 2 u(x, y ) (12.46) x 2



u yy (x, y ) 

 2 u(x, y ) (12.47) y 2

The mixed partial derivatives such as uxy = ∂2u/∂x∂y are also taken care of here. Let fkj = f(xk, yj). Substituting Equation (12.44), (12.45), into (12.37), and ignoring the higher-order terms, we obtain the following system of difference equations for the interior grid points, 1 ≤ k ≤ m and 1 ≤ j ≤ n.

u k  1 , j  2 u k , j  u k 1 , j x

2

0

1 x 2 0



u k , j  1  2 u k , j  u k , j 1 y 2

1 y 2  1 1  2  2  2  y   x 1 y 2

 f kj (12.48)

0

1 x 2 0

FIGURE 12.5  Interior calculation template.

Equation (12.48) is known as the central difference method. In order to evaluate Equation (12.48) at the interior points of the grid, values for ukj along the grid boundary are required. Let gkj = g(xk, yj) where g(x, y) is the boundary value function in Equation (12.39). Then, ukj = gkj along the boundary, which corresponds to k = 0, k = m + 1, j = 0, and j = n + 1. The calculation on the left-hand side of Equation (12.48) can be thought of as an operator acting on point ukj and its neighbors. The operator forms a linear combination of ukj and its four neighbors to the north, south, east, and west using the 3 × 3 weighting coefficient template shown in Figure 12.5. The five-point computation in Figure 12.5 has a truncation error of order O(Dx2 + Dy2), assuming that u is sufficient smooth.

ANMF.CH12_4PP.indd 974

3/1/2023 3:29:07 PM

Partial Differential Equations • 975

The grid element can often be taken to be square with Dy = Dx = h. This results in the following simplification of the central difference equations in (12.48). uk+1,j + uk–1,j – 4ukj + uk,j+1 + uk,j–1 = h2fkj Assuming that the values ukj are arranged as an mn × 1 vector v = [u11, u12, …, umn]T constructed by placing the rows of u end to end. The difference equations can then be cast as a linear algebraic system of dimension mn in the unknown vector v. For the case m = 3 and n = 3, the following system is obtained.  h 2 f  g 01  g10  0 1 0 0 0 0 0  u11   11  4 1  2  1 4 1 0 1 0 0 0 0  u12   h f12  g 02     2   0 1 4 0 0 1 0 0 0  u13   h f13  g 03  g14     2 0 0 4 1 0 1 0 0  u 21   h f21  g 20  1   0 1 0 1 4 1 0 1 0  u 22    h 2 f22       2 0 0 1 0 1  4 0 0 1 u    23   h f22  g 24  0 0 0 1 0 0 4 1 0  u 31   h 2 f  g  g  41 30      31  0 0 0 1 0 1 4 1  u 32   h 2 f  g 0  32 42 0    0 0 0 0 1 0 1  4 u 2    33   h f  g  g  43 34   33 (12.49) Here, the mn × mn coefficient matrix is symmetric and that the boundary values appear in the right-hand-side vector. For modest values of m and n such as mn ≤ 100, the linear algebraic system in Equation (12.49) can be solved by the direct elimination methods. 12.6.2

Boundary Conditions

A common type of boundary condition involves constraints on the value of the derivative of the solution at the boundary. For the case of a rectangular region R, it is helpful to consider each edge separately. –ux(0, y) = g1(y)  ux(a, y) = g2(y) –uy(x, 0) = g3(x)  uy(x, b) = g4(x)(12.50)

ANMF.CH12_4PP.indd 975

3/1/2023 3:29:10 PM

976 • Applied Numerical Methods Using MATLAB The negative signs in Equation (12.50) are included so that in each case the partial derivative is in the direction of the outward normal to the boundary. The constraints of this form are called Neumann boundary conditions. Suppose the left edge (x = 0), and the right edge (x = 1) must satisfy Neumann boundary conditions. First, we compute Equation (12.48) along the two edges. This gives the following supplementary difference equations for 1 ≤ j ≤ n.

u ij  2u 0 j  u n 1, j x

2



u n  2, j  2u n 1, j  u nj x 2

u n , j  1  2 u 0 j  u 0 , j 1 y 2 

 f0 j (12.51)

u n  1 , j  1  2 u n 1 , j  u n  1 , j  1 y 2

 f n 1, j (12.52)

Equations (12.51) and (12.52) contain some new functions points u1,j and un+2,j that lie just outside the boundary. To determine estimated values for these points, we approximate the first derivatives in Equation (12.50) using threepoint central difference approximations. This gives us the following difference equations for 1 ≤ j ≤ n.

(u1 j  u1, j )

 g1 (y j ) (12.53)

2x (u n  2, j  u nj ) 2x

 g 2 (y j ) (12.54)

Equations (12.53) and (12.54) can be solved for the fictitious points u–1,j and un+2,j. Substituting the results into Equation (12.51) and Equation (12.52) gives the following equations along the left and right edges of the region R.

2u1 j  2u 0 j x

2



u 0 , j  1  2 u 0 j  u 0 , j 1

2u n 1, j  2u nj x

2

y 

2

 f0 j 

2 g 1 (y j )

u n  1 , j  1  2 u n  1 , j  u n  1 , j 1 y

2

x

(12.55)

 f n  1, j 

2 g 2 (y j ) x

(12.56)

Now, along the boundaries we now use a four-point computation rather than a five-point computation because one neighbor is not present. There is also an extra term on the right-hand side due to the presence of a nonzero derivative. By applying the same analysis to the bottom and top edges of R using the boundary conditions in Equation (12.50), we arrive at the following additional difference equations.

ANMF.CH12_4PP.indd 976

3/1/2023 3:29:30 PM

Partial Differential Equations • 977



u k  1 , 0  2 u k 0  u k 1 , 0 x

2



2 g (x ) 2u k 1  2u k 0  f k 0  3 k (12.57) 2 y y

u k  1 , m  1  2 u k , m  1  u k 1, m  1 x

2



2u k ,m 1  2u km y

2

 f k ,m 1 

2 g 4 (x k ) (12.58) y

Each of the four corner points of the solution grid is at the intersection of two edges. The four-point computations along the boundary are conveniently summarized with the computational templates shown in Figure 12.6. When derivative or Neumann boundary conditions are applied to the edges, the number of equations that must be solved increases. There can be a combination of Dirichlet conditions along some edges and Neumann conditions along the remaining edges. 12.6.3

Iterative Solution Methods

The iterative methods are ideally suited for the solution of finite difference equations. For the iterative methods, approximately mn elements must be stored for large values of m and n. There are a large number of iterative methods available with varied rates of convergence. When first approximate value of u is available, then those values can be further improved by applying an appropriate iterative method. A few methods are described below:  1 1  2  2  2  y   x 2 y 2 0

1 x 2 0 0

1 y 2  1 1  2  2  2  y   x 1 y 2

1 x 2 0 0

1 y 2

0

0

0

0

2 x 2

0

0

2 x 2

0

0

0

0 0

1 x 2

0 2 y 2

 1 1  2  2  2  y   x

 1 1  2  2  2  y   x 1 y 2 0 0

1 x 2

FIGURE 12.6  Template for Neumann boundary conditions.

ANMF.CH12_4PP.indd 977

3/1/2023 3:29:35 PM

978 • Applied Numerical Methods Using MATLAB EXAMPLE 12.2 A metal plate (1 m × 1 m) is placed such that its four edges are kept at d ­ ifferent temperatures as shown in Figure 12.7(a). The steady state temperature distribution in the plate is given by the Laplace equation

2 T 2 T  0 x 2 y 2 T = 70ºC

T = 60ºC

T = 100ºC

T = 35ºC FIGURE 12.7(a)  Find the temperature distribution in the plate.

Solution: Let us use square grid such that Dx = Dy = 0.25 (see Figure 12.7(b)). Applying central difference approximation to the second derivatives, we obtain  

T( i 1) j  2 Tij  T( i 1) j (x)

2



Ti ( j1)  2 Tij  Ti ( j1) j (y )2

 0   I = 1, 2, 3; j = 1, 2, 3 (E.1)

T = 70ºC (1,4) (2,4) (3,4) y=1

(0,2) (0,1)

(1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1)

(4,3) (4,2)

T = 100ºC

T = 60ºC

(0,3)

(4,1)

y=0 x = 0 (1,0) (2,0) (3,0) x = 1 T = 35ºC FIGURE 12.7(b)  Arrangement of grid points.

ANMF.CH12_4PP.indd 978

3/1/2023 3:29:40 PM

Partial Differential Equations • 979

Since Dx = Dy, we have T(i+1)j + T(i–1)j + Ti(j+1) + Ti(j–1) – 4Tij = 0

(E.2)

For i = 1, j = 1: T21 + T01 + T12 + T10 – 4T11 = 0 Putting T01 = 60 and T10 = 35 we obtain 4T11 – T21 – T12 = 95

(E.3)

For i = 2, j = 1: T31 + T11 + T22 + T20 – 4T21 = 0 Putting T20 = 25 we get –T11 + 4T21 – T31 – T22 = 35

(E.4)

For i = 3, j = 1: T41 + T21 + T32 + T30 – 4T31 = 0 Putting T30 = 25 and T41 = ?? we have –T21 + 4T31 – T32 = 135

(E.5)

For i = 1, j = 2: T22 + T02 + T13 + T11 – 4T12 = 0 Now, T02 = 60. Therefore, the above equation becomes –T11 + 4T12 – T22 – T13 = 60

(E.6)

For i = 2, j = 2: T32 + T12 + T23 + T21 – 4T22 = 0 This equation can be rewritten as –T21 – T12 + 4T22 – T32 – T23 = 0

(E.7)

For i = 3, j = 2: T42 + T22 + T33 + T31 – 4T32 = 0

ANMF.CH12_4PP.indd 979

3/1/2023 3:29:40 PM

980 • Applied Numerical Methods Using MATLAB Putting T42 = 100, the above equation becomes –T31 + T22 + 4T32 – T33 = 100

(E.8)

For i = 1, j = 3: T23 + T03 + T14 + T12 – 4T13 = 0 Putting T03 = 60 and T14 = 70, we have –T12 + 4T13 – T23 = 120

(E.9)

For i = 2, j = 3: T33 + T13 + T24 + T22 – 4T23 = 0 Putting T24 = 70, we obtain –T22 – T13 + 4T23 – T33 = 70

(E.10)

For i = 3, j = 3: T43 + T23 + T34 + T32 – 4T33 = 0 Putting T43 = 100 and T34 = 70, the above equation becomes –T32 – T23 + 4T33 = 17

(E.11)

Hence, we have nine equations, (E.3) to (E.11), and nine unknowns. These equations are presented in matrix form as (E.12):



ANMF.CH12_4PP.indd 980

 4 1 0 1  1 4 1 0   0 1 4 0   1 0 0 4  0 1 0 1   0 0 1 0  0 0 0 1  0 0 0 0 0 0 0 0 

0 0 0 0 0   T11   95  1 0 0 0 0   T21   35   0 1 0 0 0   T31  135      1 0 1 0 0   T12   60  4 1 0 1 0   T22    0  (E.12)     1 4 0 0 1  T32  100  0 0 4 1 0   T13  120      1 0 1 4 1  T23   70    0 1 0 1 4   T33  170 

3/1/2023 3:29:42 PM

Partial Differential Equations • 981

The above set of equations are solved using MATLAB. The results are T11 = 52.5446

T21 = 60.1339

T31 = 62.3661

T12 = 55.0446

T22 = 65.6250

T32 = 69.3304

T13 = 67.0089

T23 = 77.9911

T33 = 79.3304

EXAMPLE 12.3 Determine the temperatures at the inside nodes of the following L-shaped Figure 12.8, made up of twelve equally spaced/sized squares, whereby the temperatures along the boundaries are known. T = 30 T = 40 T = 30

T = 20

T = 40 T=0 FIGURE 12.8 

Solution: This is a boundary value problem for which Equation (E.1) can be used.  2u 2u  1    2  2   2 [u i 1, j  u i , j1  4u i , j  u i 1, j  u i , j1  O(h 2 )]  0 (E.1) y  i , j h  x

T = 30 T = 30

T = 40 T = 40

1 2 3

4

5

T = 50

T=0

ANMF.CH12_4PP.indd 981

 2u  1 [u i 1, j  2u i , j  u i 1, j  O(x)2 ] (E.2)  2  2 (  )  x x  i , j

3/1/2023 3:29:46 PM

982 • Applied Numerical Methods Using MATLAB Using Equation (E.2) for each of the nodes 1 to 5 we obtain

Node 1:

20 + 30 – 4T1 + 40 + T2 = 0



Node 2:

20 + T1 – 4T2 + 40 + T3 = 0



Node 3:

20 + T2 – 4T3 + T4 + 0 = 0



Node 4:

T3 + 40 – 4T4 + T5 + 0 = 0



Node 5:

T4 + 40 – 4T5 + 50 + 0 = 0

(E.3)

These are five equations with five unknowns, which can also be written as:



 4 1 0 0 0   T1  90   1 4 1 0 0   T  60    2    0 1 4 1 0   T3   20  (E.4)       0 0 1 4 1  T4   40   0 0 0 1 4   T5  90     

Solving above system of Equations (E.4) using MATLAB, we obtain T1 = 29.1282,  T2 = 26.5128,  T3 = 16.9231,  T4 = 21.1795 and T5 = 27.7949 MATLAB Solution: >> A= [4 -1 0 0;-1 4 -1 0 0; 0 -1 4 -1 0; 0 0 -1 4 -1; 0 0 0 -1 4]; >> b= [90; 60; 20; 40; 90]; >> x=inv (A)*b x=  29.1282  26.5128  16.9231  21.1795  27.7949

ANMF.CH12_4PP.indd 982

3/1/2023 3:29:48 PM

Partial Differential Equations • 983

EXAMPLE 12.4 Consider the problem of finding the electrostatic potential, υ(x, y), over a plate of width a = 4 and length b = 4. If er denotes the permittivity of the material and r(x, y) denotes the charge density, then Poisson’s equation can be used to model the distribution of electrostatic potential as follows.

 2   2     x 2 y 2 

Suppose the plate is free of charge, which means r = 0. Let the boundary conditions be the following voltages along the edges.

υ(0, y) = 0 y υ(a, y) =   b υ(x, 0) = 0

2

x υ(x, b) =   a Write a MATLAB program to solve this system on a 30 × 30 grid. Plot the solution as a surface. Print the number of iterations, the optimal relaxation factor, and the maximum potential.

Solution: function u=fung124(x, y) global a, b if (x=a) u=(y/a) ^2; elseif(y=b) u=sqrt(x/a) end

ANMF.CH12_4PP.indd 983

3/1/2023 3:29:52 PM

984 • Applied Numerical Methods Using MATLAB >> m=30; n=30; q=600; a=4; b=4; tol=1.e-5; >>x=zeros (m, 1); y=zeros (n, 1); U=zeros (m, n); >>[alpha,r,x,y,U]=Poisson(a,b,m,n,q,tol,’’,’fung124’); >> show (‘Number of iterations’, r) Number of iterations = 72 >> show (‘Optimal relaxation factor’, alpha) Optimal relaxation factor = 1.811 >> umax=max (max (U)); >> show (‘Maximum potential’, umax) Maximum potential = 0.9244 >> plotxyz (x, y, U,’Electrostatic Potential’,’x’,’y’,’u (V)’) When this script is executed, it requires 72 iterations to converge. The optimal value for the relaxation factor for the SR method is 1.811, and the maximum potential (over the interior points) is 0.9244. A plot of the electrostatic potential surface is shown in Figure 12.9. Electrostatic Potential

1 0.8

u(V)

0.6 0.4 0.2 0 4 3 2 1 y

0

0

2

1

3

4

x

FIGURE 12.9 

ANMF.CH12_4PP.indd 984

3/1/2023 3:29:53 PM

Partial Differential Equations • 985

EXAMPLE.12.5 Approximate the solution to the following elliptic partial differential equation:

2u 2u   0 , x 2 y 2

1 < x < 2,

0 < y < 1;



u(x, 0) = 2 ln x,

u(x, 1) = ln(x2 + 1),

1 ≤ x ≤ 2;



u(1, y) = ln(y2 + 1),

u(2, y) = ln(y2 + 4),

0 ≤ y ≤ 1.

Use h = k = 1/3, and compare the results to the analytical solution u(x, y) = ln(x2 + y2). Solution: The Poisson equation finite-difference algorithm gives the following results. TABLE 12.2 

12.6.4

i

j

xi

yj

wij

u(xi, yi)

1 1 2 2

1 2 1 2

1.3333 1.3333 1.6666 1.6666

0.3333 0.6666 0.3333 0.6666

0.63480 0.79850 1.05999 1.16982

0.63598 0.79850 1.06872 1.17007

The Jacobi Method

The finite difference method for the Poisson equation uxx + uyy = g(x, y)

(12.59)

When discritized using a standard five-point formula is given by

1 u i , j  (u i 1, j  u i 1, j  u i , j1  u i , j1  h2 g i , j ) (12.60) 4

Let u(i r,j) denote the rth iterative value of ui,j. The iterative scheme to compute Equation (12.60) for the interior mesh points is given by

u(i ,rj1) 

1 (r ) u i 1, j  u(i r)1, ju(i ,rj)1  u(i ,rj)1  h 2 g i , j  (12.61)  4

This is called the point Jacobi’s method.

ANMF.CH12_4PP.indd 985

3/1/2023 3:30:02 PM

986 • Applied Numerical Methods Using MATLAB 12.6.5

Gauss-Seidel Method

In the Gauss-Seidel method, the most recently computed values are used as soon as they are available, and the values of u along each row are computed systematically from left to right. The iterative formula is given by

u(i ,rj1) 

1 ( r  1) u i 1, j  u(i r)1, j  u(i ,rj11)  u(i ,rj)1  h 2 g i , j  (12.62)  4

The rate of convergence of this method is twice as fast as Jacobi’s method. 12.6.6

Successive Over-Relaxation or S.O.R. Method

In S.O.R. method, the rate of convergence of an iterative method is accelerated by making corrections on u(i r, j1)  u(i r, j)  . If u(i r,j+1) is the value obtained from a basic iterative (such as Jacobi’s or Gauss-Seidel’s) method then the value at the next iteration is given by

u(i ,rj1)  wu(i ,rj1)  (1  w )u(i ,rj) (12.63)

where w is the over-relaxation factor. Hence, Jacobi’s method compared to over-relaxation method for the Poisson equation is given by

u(i ,rj1) 

1 w u(i r)1, j  u(i r)1, j  u(i ,rj)1  u(i ,rj)1  h 2 g i , j   (1  w )u(i ,rj) (12.64) 4 

and the Gauss-Seidel over-relaxation method for that is given by

u(i ,rj1) 

1 w u(i r11, j)  u(i r)1, j  u(i ,rj11)  u(i ,rj)1  h 2 g i , j   (1  w )u(i r, j) (12.65) 4 

The rate of convergence of Equation (12.64) and Equation (12.65) depends on the value of w and its value lies between 1 and 2. However, the choice of w is a difficult one. It may be noted that w = 1 gives the corresponding basic iteration formula.

ANMF.CH12_4PP.indd 986

3/1/2023 3:30:15 PM

Partial Differential Equations • 987

EXAMPLE 12.6 Solve the elliptical partial differential equation

2u 2u   0 using the S.O.R. x 2 y 2

iterative method by writing a MATLAB program for determining the steadystate temperature over a square plate 5 units on a side. The steady-state temperature u(x, y) is a solution of Laplace’s equation, which is a Poisson’s equation with f(x, y) = 0. Take the size of the grid as m = n = 29. Use the Dirichlet boundary conditions u(x, y) = g(x, y), (x, y) ∈ dR. The boundary value function g(x,y) = eycos x – e cos y. Determine the maximum number of iterations, optimal relaxation factor, and maximum error. Plot the surface of the steady-state temperature distribution. Solution: clc  clear   m = 29; n = 29; q = 600;   a = 5; b = 5;

tol = 1.e-5;

  x = zeros (m,1); y = zeros (n,1); U = zeros (m,n);   g = inline (‘exp(y)*cos(x) - exp(x)*cos(y)’,’x’,’y’);   [alpha,r,x,y,U] = Poisson (a,b,m,n,q,tol,’’,g);   show (‘Number of iterations’,r)   show (‘Optimal relaxation parameter’,alpha)   du = 0;   for i = 1 : m   for j = 1 : n   du = max(du,abs(U(i,j) - g(x(i),y(j))));  end  end   show (‘maximum error’,du)   plotxyz (x,y,U,’Steady-State Temperature’,’x’,’y’,’u’) When the script is executed, it produces the following result and the surface plot is shown in Figure 12.10.

ANMF.CH12_4PP.indd 987

3/1/2023 3:30:16 PM

988 • Applied Numerical Methods Using MATLAB Number of iterations = 91 Optimal relaxation parameter = 1.805 maximum error = 0.07079 Steady-State Temperature

150 100

u

50 0 -50 -100 -150 6 4 2 y

0

0

1

2

3

4

5

x

FIGURE 12.10 

12.7 ONE-DIMENSIONAL PARABOLIC EQUATIONS A partial differential equation that is first order in time, t, and second order in a spatial coordinate, x, is a parabolic equation of the following form.

u 2u   2 (12.66) t x

Equation (12.66) is the one-dimensional heat conduction equation, also called the diffusion equation. The coefficient b is a positive constant. Since u(t, x) depends on both time and space, we must specify both an initial condition and boundary conditions. We solve the heat equation over the interval 0 < x < a. The initial condition will be of the following form for some initial value function f(x).

ANMF.CH12_4PP.indd 988

u(0, x) = f(x),   0 < x < a

(12.67)

3/1/2023 3:30:20 PM

Partial Differential Equations • 989

The boundary conditions can be formulated in a general manner by placing constraints on the following linear combination of u and ux = ∂u/∂x.

(1 – c1)u(t, 0) – c1ux(t, 0) = g1(t)(12.68)



(1 – c2)u(t, a) – c2ux(t, a) = g2(t)(12.69)

Here, the components of the 2 × 1 coefficient vector c take on values in the range 0 ≤ ck ≤ 1. If c1 = 0, then Equation (12.68) becomes the Dirichlet boundary condition u(t, 0) = g1(t). If c2 = 1, then (12.69) reduces to the Neumann boundary condition ux(t, a) = g2(t). In general, 0 < ck < 1 produces a mixed boundary condition. 12.7.1

Explicit Forward Euler Method

Here, we are interested in solving the heat equation over the time interval 0 ≤ t ≤ T. We calculate a numerical solution by estimating u(t, x) over a uniform grid consisting of m + 1 values of t and n + 2 values of x as shown in Figure  12.11. Let Dt and Dx denote the step sizes of the variables t and x, respectively. Then  t 

T (12.70) m

x 

a (12.71) n 1



x ∆t

xn+1

∆x

x1 x0

t t0

t1

tm

FIGURE 12.11  Uniform solution grid for heat equations.

ANMF.CH12_4PP.indd 989

3/1/2023 3:30:24 PM

990 • Applied Numerical Methods Using MATLAB To simplify the final equations, let tk and xj denote the values of t and x at the grid points. Hence, tk = kDt, 0 ≤ k ≤ m

(12.72)

xj = jDx, 0 ≤ k ≤ (n + 1)

(12.73)

Let u kj denote the computed value of u(tk, xj). Here, the superscript specifies the time step, while the subscript specifies the space step. To convert the heat Equation (12.66) to a difference equation, we again use finite difference approximations for the derivative terms. The first-order time derivative is approximated with a two-point forward Euler difference, and the secondorder space derivative can be approximated with a three-point central difference. u kj 1  u kj



u t (t k , x j ) 



u xx (t k , x j ) 

t

 O(t) (12.74)

u kj1  2u kj  u kj1 x 2

 O(x 2 ) (12.75)

The approximation of ut = ∂u/∂t in Equation (12.74) has a truncation error of order O(Dt), while the approximation of uxx = ∂2u/∂x2 in Equation (12.75) is more accurate for the same step size because it has a truncation error of order O(Dx2). If we substitute Equations (12.74) and (12.75) into Equation (12.66), and ignore the higher-order terms, this yields the following system of difference equations for the interior grid points, 0 ≤ k ≤ m and 1 ≤ j ≤ n.  u kj1  2u kj1     (12.76)  t x 2   The formulation in Equation (12.76) is referred to as the explicit forward Euler method since the time derivative is represented with a forward Euler approximation, and the solution at time tk+1 can be solved for explicitly as follows: u kj 1  u kj



u kj 1  u kj1  (1  2 )u kj  u kj1 (12.77)

where the gain parameter g > 0 is defined as

ANMF.CH12_4PP.indd 990



t (12.78) x 2

3/1/2023 3:30:36 PM

Partial Differential Equations • 991

To compute Equation (12.77) at j = 1 and j = n, the boundary values u 0k and u kn +1 are needed. We use a two-point forward difference for the derivative in (12.68) and a two-point backward difference for the derivative in (12.69). This converts the boundary condition constraints into the following difference equations.

 u k  u 0k (1  c1 )u 0k  c1  1  x



 u k  u kn (1  c 2 )u kn 1  c 2  n 1  x

   g1 (t k ) (12.79)     g 2 (t k ) (12.80) 

Solving Equations (12.79) and (12.80) for the boundary values, we obtain g1 (t k )x  c1 u1k (12.81) c1  (1  c )x



u 0k 



u kn 1 

g 2 (t k )x  c 2 u nk (12.82) c 2  (1  c 2 )x

Comparing the Neumann boundary conditions for the Poisson equation, the formulations in Equations (12.81) and (12.82) for the heat equation do not require increasing the dimension of the system. The use of two-point forward and backward differences in place of three-point central differences means that the order of the truncation error at the boundary is only O(Dx), rather than O(Dx2), except when Dirichlet boundary conditions are used. The forward EulerT equations can be expressed in vector form by letting u k  u1k , u 2k ,...., u kn  denote the solution at time tk for 0 ≤ k ≤ m. Assume the lower boundary has a Dirichlet constraint (c1 = 0), while the upper boundary has a Neumann constraint (c2 = 1). For the case when n = 5, the following explicit solution is obtained:

  u k 1

ANMF.CH12_4PP.indd 991

 0 0 1  2    g1 (t k )       1  2  0 0 0      (12.83)  1  2  0  uk   0  0     0  1  2   0  0    0  xg 2 (t k ) 0 0  1      

3/1/2023 3:30:50 PM

992 • Applied Numerical Methods Using MATLAB The amplitude factor is given by

l = 1 + 2g[cos(p/r) – 1]

(12.84)

To ensure stability, we must constrain g such that |l| ≤ 1. Since |cos(p/r)| ≤ 1, it follows that (1 – 4g) ≤ g ≤ 1. Therefore, setting 1 – 4g ³ –1 and solving for g yields g ≤ ½. This gives the following stability criterion for the explicit forward Euler method.

t  

x 2 (12.85) 2

From Equation (12.85), we see that as the space step Dx gets smaller, so must the time step Dt in order to maintain stability. This is a drawback of the forward Euler method. It is a very efficient method because an explicit formula is available for uk+1, but the two step sizes can not be chosen independent of one another. 12.7.2

Implicit Backward Euler Method

To improve the stability characteristics of the forward Euler method, we evaluate the three-point central difference approximation for uxx at tk+1 rather than tk,

u xx (t k 1, x , x j ) 

u kj11  2u kj 1  u kj11 x

2

 O(x 2 ) (12.86)

Using tk+1 as the reference time in place of tk has the effect of making the Euler approximation of ui in Equation (12.74) a backward difference, rather than a forward difference. Substituting Equations (12.74) and (12.86) into Equation (12.66), and ignoring the higher-order terms, we obtain the following alternative system of difference equations for the interior grid points, 0 ≤ k ≤ m and 1 ≤ j ≤ n.

u kj 1  u kj t

 u kj11  2u kj 1  u kj11     (12.87) 2   x  

Equation (12.87) cannot be solved explicitly for u kj +1 . There are three terms that involve the solution at time tk+1. Grouping them together on the left-hand side and recalling the definition of the gain constant g in Equation (12.78) gives

ANMF.CH12_4PP.indd 992

3/1/2023 3:30:58 PM

Partial Differential Equations • 993



 u kj11  (1  2 )u kj 1  u kj11  u kj (12.88)

The formulation in Equation (12.88) is referred to as the implicit backward Euler method. At each time step, a tridiagonal linear algebraic system of n equations must be solved. Note that to solve Equation (12.88), the boundary values u 0k and u kn +1 in Equations (12.81) and (12.82) must be utilized. The backward Euler equations can be expressed in compact vector form. Let T u k  u1k , u 2k ,...., u kn  denote the solution at time tk for 0 ≤ k ≤ m. Assume the lower boundary has a Neumann constraint (c1 = 1), while the upper boundary has a Dirichlet constraint (c2 = 0). For the case n = 5, this results in the following implicit linear algebraic system:  0 0 0  1    xg1 (t k 1 )   1  2     0 0 0      (12.89)  1  2  0  u k 1  u k   0     0     0  1  2   0  0    0  g 2 (t k 1 )  0 0  1  2      The amplitude factor is given by



1 (12.90) 1  2 [1  cos( / )]

Since 1 – cos(p/r)³ 0 and g > 0, it follows that |l| ≤ 1 for all values of g. Therefore, the backward Euler method is unconditionally stable. Unlike the forward Euler method, there is no constraint similar to (12.85) on the relative values of Dt and Dx. 12.7.3

The Crank-Nicolson Implicit Method

In the Crank-Nicolson implicit method, the equation is approximated by replacing both space and time derivatives by their central difference approximations at a point (ih, (j + 1/2)k), which is the midpoint of the points (ih, jk) u 2u and (ih, (j + 1)k). Thus, the equation   2 can be written as t t

ANMF.CH12_4PP.indd 993

 2u    2u     2 u   u        2   (12.91)    2  2  t  i , j1 / 2  x  i , j1 / 2 2  x  i , j  x  i , j1 

3/1/2023 3:31:17 PM

994 • Applied Numerical Methods Using MATLAB Using the central difference approximation, Equation (12.91) reduces to

u i , j1  u i , j k



  u i  1, j  2 u i , j  u i 1, j u i  1, j  1  2 u i , j  1  u i 1 , j  1     2 k2 k2 

Simplifying Equation (12.91), we obtain –rui–1,j+1 + (2 + 2r)ui,j+1 – rui+1,j+1 = rui–1,j + (2 – 2r)ui,j + rui+1,j(12.92) where  r = ak/h2. The left-hand side of Equation (12.92) contains three unknowns and the right-hand side has three known values of u. For j = 0 and i = 1, 2, …., N – 1, Equation (12.92) generates N simultaneous equations for N – 1 unknown u1,1, u2,1, …., uN–1,1 (of first row) in terms of known initial and boundary values u0,0, u1,0, u2,0, …., uN,0; u0,0 and uN,0 are the boundary values and u1,0, u2,0, …., uN–1,0 are the initial values. Similarly, for j = 1 and i = 1, 2, …., N – 1 we obtain another set of unknown values u1,2, u2,2, …., uN–1,2 in terms of calculated values for j = 0, and so on. t

Left boundary values

Right boundary values

j+1 j

Unknown values Known values i=0

i–1

x i

i+1

i=N

FIGURE 12.12  Meshes of the Crank-Nicolson implicit method.

In this method, the value of ui,j+1 is not given directly in terms of known ui,j at one time step earlier but is also a function of unknown values at previous time step, and hence the method is called an implicit method.

ANMF.CH12_4PP.indd 994

3/1/2023 3:31:20 PM

Partial Differential Equations • 995

The system of equations (12.92) can be written in the following matrix notation:  2  2 r r   u 1, j  1   d 1, j       u d r 2  2r r    2 , j1   2 , j          u 3 , j1    d 3 , j             r 2  2 r  r              r 2  2r  u N1, j1  d N1, j      (12.93) where d1,j = ru0,j + (2 – 2r)u1,j + ru2,j + ru0,j+1 di,j = rui–1,j + (2 – 2r)ui,j + ruj+1,j; i = 2, 3, …., N – 2 dN–1,j = ruN–2,j + (2 – 2r)uN–1,j + ruN,j + ruN,j+1(12.94) The right-hand side of Equation (12.93) is known. The tridiagonal system can be solved easily. The Crank-Nicolson equations can be expressed in vector form by letting T u k  u1k , u 2k ,...., u kn  denote the solution at time tk for 0 ≤ k ≤ m. Suppose both boundary conditions are of the Dirichlet type, which means c = 0. For the case n =5, this results in the following implicit linear algebraic system.  0 0 0  2(1  )   2(1  )  0 0     0  2(1  )  0  u k 1    0  2(1  )    0  0 0 0  2(1  )    0 0 0   1 (t k )  g1 (t k 1 )] 2(1  )     2(1  )  0 0  0       0  2(1  )  0  uk   0     0  2(1  )   0    0   0  0 0  2(1  )  (t )  g 2 (t k 1 )]    2 k  (12.95)

ANMF.CH12_4PP.indd 995

3/1/2023 3:31:27 PM

996 • Applied Numerical Methods Using MATLAB The amplitude factor is given by



1  [1  cos( / p)] (12.96) 1  [1  cos( / p)]

Since 1 – cos(p/p) ≥ 0 and g > 0, it follows that |l| ≤ 1 for all values of g. The Crank-Nicolson method is also unconditionally stable. However, because the Crank-Nicolson method has a second-order truncation error in both time and space, it tends to be more accurate than the backward Euler method using identical step sizes. 12.7.4

function [t,x,U] =Heatone(T,a,m,n,beta,c,f,g)

% Usage:

[t, x, U] = Heatone (T, a, m, n, beta, c, f, g)

% Uses the Crank-Nicolson method to solve the PDE; 1-D heat % equation (Diffusion equation) % (d/dt) u (t, x) = beta*(d/dx)*(d/dx) u (t, x) % The heat equation is to be solved over the region: 0 0)

%

m = Number steps in t (m >= 1)

%

n = Number of steps in x (n >= 1)

%

beta = Heat equation coefficient (beta > 0)

%

c = 1 by 2 vector specifying boundary conditions

%

0 = Dirichlet, 1 = Neumann

%f

= String containing name of [user-supplied] initial condition function.

% g = String containing name of [user-supplied] boundary condition function % The functions f and g are of the form: Function y = f(x); Function y = g (t, k)

ANMF.CH12_4PP.indd 996

3/1/2023 3:31:31 PM

Partial Differential Equations • 997

% When f is called, it must return the initial value u (0, x). When g is called, it must return the % boundary value g (t, k). % Output:

t = (m+1) by 1 vector of t grid points

%

t (k) = (k-1)*T/m for 1 = 1)

%

p

= Number of steps in y (p >= 1)

%

beta = Heat equation coefficient (beta > 0)

%

f = String containing name of user-supplied

% %

ANMF.CH12_4PP.indd 999

T

Initial condition function g = String containing name of user-supplied

3/1/2023 3:31:31 PM

1000 • Applied Numerical Methods Using MATLAB %

boundary condition function

%

The functions f and g are of the form: function z = f(x, y)

%

function z = g (t, x, y)

% Output: x = n by 1 vector of x grid points %

y = p by 1 vector of y grid points

%

U = n by p matrix containing final solution estimates

%

U (i, j) = u (T, x (i), y (j))

% Check arguments   T = args (T, eps, T, 1,’Heattwo’);   a = args (a, eps, a, 2,’Heattwo’);   b = args (b, eps, b, 3,’Heattwo’);   m = args (m, 1, m, 4,’Heattwo’);   n = args (n, 1, n, 5,’Heattwo’);   p = args (p, 1, p, 6,’Heattwo’);   beta = args (beta, eps, beta, 7,’Heattwo’);   funf = getfun (f);   fung = getfun (g); % Initialize   A1 = zeros (3, p);   A2 = zeros (3, n);   Q1 = zeros (3, p);   Q2 = zeros (3, n);   W = zeros (n, p);   c1 = zeros (p, 1);   c2 = zeros (n, 1);   v2 = zeros (p, 1);   x = zeros (n, 1);

ANMF.CH12_4PP.indd 1000

3/1/2023 3:31:31 PM

Partial Differential Equations • 1001

  y = zeros (p, 1);   U = zeros (n, p);   dt = T/m;   dx = a/ (n+1);   dy = b/ (p+1);   gx = beta*(dt/2)/ (dx*dx);   gy = beta*(dt/2)/ (dy*dy);   for i = 1: n   x (i) = i*dx;  end   for j = 1: p   y (j) = j*dy;  end % Coefficient matrices   g0 = 1 + 2*gy;   for i = 1: p   A1 (1, i) = -gy;   A1 (2, i) = g0;   A1 (3, i) = -gy;  end   [Q1, delta] = trifac (A1);   if abs (delta) < eps/100   disp (‘Tridiagonal coefficient matrix in heattwo is singular.’)  return  end   g0 = 1 + 2*gx;   for i = 1: n

ANMF.CH12_4PP.indd 1001

3/1/2023 3:31:31 PM

1002 • Applied Numerical Methods Using MATLAB   A2 (1, i) = -gx;   A2 (2, i) = g0;   A2 (3, i) = -gx;  end   [Q2, delta] = trifac (A2);   if abs (delta) < eps/100   disp (‘Tridiagonal coefficient matrix in heattwo is singular.’)  return  end % Initialize solution   if funf   for i = 1: n   for j = 1: p   U (i, j) = feval (f, x (i), y (j));  end  end  end % Solve the heat equation   hwbar = waitbar (0,’Solving Heat Equation: Heattwo’);   for k = 0: m-1   waitbar (k/ (m-1))   t0 = k*dt;   t1 = (k+1)*dt;   tm = (t0 + t1)/2;   g0 = 1 - 2*gx;   for j = 1: p   c1 (j) = g0*U (1, j) + gx*U (2, j);

ANMF.CH12_4PP.indd 1002

3/1/2023 3:31:31 PM

Partial Differential Equations • 1003

  if fung   c1 (j) = c1 (j) + gx*feval (g, t0, 0, y (j));  end  end   if fung   c1 (1) = c1 (1) + gy*feval (g, tm, x (1), 0);   c1 (p) = c1 (p) + gy*feval (g, tm, x (1), b);  end   [v2, delta] = trisub (Q1, c1);   W (1, 1: p) = v2’;   for i = 2: n-1   for j = 1: p   c1 (j) = gx*U (i-1, j) + g0*U (i, j) + gx*U (i+1, j);  end   if fung   c1 (1) = c1 (1) + gy*feval (g, tm, x (i), 0);   c1 (p) = c1 (p) + gy*feval (g, tm, x (i), b);  end   [v2, delta] = trisub (Q1, c1);   W (i, 1: p) = v2’;  end   for j = 1: p   c1 (j) = gx*U (n-1, j) + g0*U (n, j);   if fung   c1 (j) = c1 (j) + gx*feval (g, t0, a, y (j));  end  end

ANMF.CH12_4PP.indd 1003

3/1/2023 3:31:31 PM

1004 • Applied Numerical Methods Using MATLAB   if fung   c1 (1) = c1 (1) + gy*feval (g, tm, x (n), 0);   c1 (p) = c1 (p) + gy*feval (g, tm, x (n), b);  end   [v2, delta] = trisub (Q1, c1);   W (n, 1: p) = v2’;   g0 = 1 - 2*gy;   for i = 1: n   c2 (i) = g0*W (i, 1) + gx*W (i, 2);   if fung   c2 (i) = c2 (i) + gy*feval (g, t0, x (i), 0);  end  end   if fung   c2 (1) = c2 (1) + gx*feval (g, t1, 0, y (1));   c2 (n) = c2 (n) + gx*feval (g, t1, a, y (1));  end   [U (1: n, 1), delta] = trisub (Q2, c2);   for j = 2: p-1   for i = 1: n   c2 (i) = gy*W (i, j-1) + g0*W (i, j) + gy*W (i, j+1);  end   if fung   c2 (1) = c2 (1) + gx*feval (g, t1, 0, y (j));   c2 (n) = c2 (n) + gx*feval (g, t1, a, y (j));  end   [U (1: n, j), delta] = trisub (Q2, c2);

ANMF.CH12_4PP.indd 1004

3/1/2023 3:31:31 PM

Partial Differential Equations • 1005

 end   for i = 1: n   c2 (i) = gy*W (i, p-1) + g0*W (i, p);   if fung   c2 (i) = c2 (i) + gx*feval (g, tm, x (i), b);  end  end   if fung   c2 (1) = c2 (1) + gx*feval (g, t1, 0, y (p));   c2 (n) = c2 (n) + gx*feval (g, t1, a, y (p));  end   [U (1: n, p), delta] = trisub (Q2, c2);  end   forint (‘\n’)   close (hwbar); 12.7.6

function [t,x,U] = Waveone(T,a,m,n,beta,f,g)

% Usage:

[t, x, U] = Waveone (T, a, m, n, beta, f, g);

% Description: Use the explicit central difference method to solve the % PDE - 1D wave equation:(d/dt)(d/dt)u(t,x) = beta*(d/dx)(d/dx)u(t,x) % Over the region: 0 0)

%

m

= Number steps of t (m >= 1)

%

n

= Number of steps of x (n >= 1)

3/1/2023 3:31:31 PM

1006 • Applied Numerical Methods Using MATLAB %

beta = Wave equation coefficient (beta > 0)

%

f

% %

= String containing name of user-supplied initial value function

g

%

= String containing name of user-supplied initial derivative function

% The functions f and g are of the form: Function y = f(x); Function y = g(x) % Output: t = (m+1) by 1 vector containing t grid %

x = n by 1 vector containing x grid

%

U = (m+1) by n matrix containing the estimated solution

%

with U (k, j) = u (t (k), x (j).

% Check arguments   T = args (T, eps, T, 1,’Waveone’);   a = args (a, eps, a, 2,’Waveone’);   m = args (m, 1, m, 3,’Waveone’);   n = args (n, 1, n, 4,’Waveone’);   beta = args (beta, eps, beta, 5,’Waveone’);   funf = getfun (f);   fung = getfun (g); % Initialize   dt = T/m;   dx = a/ (n+1);   t = [0: dt: T]’;   x = [dx: dx: n*dx]’;   gamma = beta*(dt/dx) ^2;   tmax = m*dx/sqrt (beta);   if T > tmax

ANMF.CH12_4PP.indd 1006

3/1/2023 3:31:31 PM

Partial Differential Equations • 1007

 wait  return;  end % Initialize U (1, :) and U (2, :) using initial condition functions   g0 = 2*(1 - gamma);   if funf   for i = 1: n   U (1, i) = feval (f, x (i));  end   U (2, 1) = (g0*feval (f, x (1)) + gamma*feval (f, x (2)))/2;   for i = 2: n-1 U(2,i) = (gamma*feval(f,x(i-1)) + g0*feval(f,x(i)) + gamma*feval(f,x(i+1)))/2;  end   U (2, n) = (gamma*feval (f, x (n-1)) + g0*feval (f, x (n)))/2;  else   U (1:2, 1: n) = 0;  end   if fung   for i = 1: n   U (2, i) = U (2, i) + dt*feval (g, x (i));  end  end   % Compute remaining rows of U   for i = 2: m   U (i+1, 1) = g0*U (i, 1) + gamma*U (i, 2) - U (i-1, 1);   for j = 2: n-1

ANMF.CH12_4PP.indd 1007

3/1/2023 3:31:31 PM

1008 • Applied Numerical Methods Using MATLAB   U(i+1,j) = gamma*U(i,j-1) + g0*U(i,j) + gamma*U (i, j+1) - U (i-1, j);  end   U (i+1, n) = gamma*U (i, n-1) + g0*U (i, n) - U (i-1, n);  end 12.7.7

function [x,y,U] = Wavetwo (T,a,b,m,n,p,beta,f,g)

% Usage:

[x,y,U] = Wavetwo(T,a,b,m,n,p,beta,f,g)

% Description: Use the explicit central difference method to solve the % PDE -2D wave equation:(d/dt)(d/dt)u(t,x,y) = beta*[(d/dx)(d/dx)u(t,x,y) + % (d/dy) (d/dy) u (t, x, y)] % The wave equation is to be solved over the region: 0 < t 0)

%

m

= Number of steps in t (m >= 1)

%

n

= Number of steps in x (n >= 1)

%

p

= Number of steps in y (p >= 1)

%

beta = Wave equation coefficient (beta > 0)

%

f

% %

= String containing name of user-supplied initial value function

g

%

= String containing name of user-supplied initial derivative function

% The functions f and g are of the form: function z = f(x, y); function z = g(x, y) % Output:

ANMF.CH12_4PP.indd 1008

x = n by 1 vector of x grid points

3/1/2023 3:31:31 PM

Partial Differential Equations • 1009

%

y = p by 1 vector of y grid points

%

U = n by p matrix containing final solution estimate

%

with U(i,j) = u(T,x(i),y(j)).

  T = args (T, eps, T, 1,’Wavetwo’);   a = args (a, eps, a, 2,’Wavetwo’);   b = args (b, eps, b, 3,’Wavetwo’);   m = args (m, 1, m, 4,’Wavetwo’);   n = args (n, 1, n, 5,’Wavetwo’);   p = args (p, 1, p, 6,’Wavetwo’);   beta = args (beta, eps, beta, 7,’Wavetwo’);   funf = getfun (f);   fung = getfun (g); % Check for stability   dt = T/m;   dx = a/ (n+1);   dy = b/ (p+1);   U = zeros (n, p);   Dt = sqrt ((dx*dx + dy*dy)/ (4*beta));   if dt > Dt  wait  return  end % Compute x, y, U0   x = [dx: dx: n*dx]’;   y = [dy: dy: p*dy]’;   U0 = zeros (n, p);

ANMF.CH12_4PP.indd 1009

3/1/2023 3:31:31 PM

1010 • Applied Numerical Methods Using MATLAB   U1 = zeros (n, p);   if funf   for i = 1: n   for j = 1: p   U0 (i, j) = feval (f, x (i), y (j));  end  end  end   % Compute: U1 (1)   hwbar = waitbar (0,’Solving Wave Equation: Wavetwo’);   gx = beta*(dt/dx) ^2;   gy = beta*(dt/dy) ^2;   gz = 2*(1 - gx - gy);   if funf   U1(1,1) = (gx*feval(f,x(2),y(1)) + gz*feval(f,x(1),y(1)) ... + gy*feval (f, x (1), y (2)))/2;   for j = 2: p-1 U1(1,j) = (gx*feval(f,x(2),y(j)) + gz*feval(f,x(1),y(j)) + gy*(feval(f,x(1),y(j-1)) + feval(f,x(1),y(j+1))))/2; end   U1(1,p) = (gx*feval(f,x(2),y(p)) + gz*feval(f,x(1),y(p)) + gy*feval(f,x(1), y(p-1)))/2; else U1 (1, 1: p) = 0; end if fung for j = 1: p

ANMF.CH12_4PP.indd 1010

3/1/2023 3:31:31 PM

Partial Differential Equations • 1011

U1(1,j) = U(1,j) + dt*feval(g,x(1),y(j)); end end % Compute U1 (i), 1 < i < n for i = 2: n-1 if funf U1(i,1) = (gx*(feval(f,x(i-1),y(1)) + feval(f,x(i+1),y(1)))+ gz*feval(f,x(i),y(1)) + gy*feval(f,x(i),y(2)))/2; for j = 2: p-1 U1(i,j) = (gx*(feval(f,x(i-1),y(j)) + feval(f,x(i+1),y(j)))+ gz*feval(f,x(i),y(j)) + gy*(feval(f,x(i),y(j-1)) + feval (f, x (i), y (j+1))))/2; end U1(i,p) = (gx*(feval(f,x(i-1),y(p)) + feval(f,x(i+1),y(p)))+ gz*feval(f,x(i),y(p)) + gy*feval(f,x(i),y(p-1)))/2; else U1 (i, 1: p) = 0; end if fung for j = 1: p U1(i,j) = U1(i,j) + dt*feval(g,x(i),y(j)); end end end % Compute: U1 (n) if funf U1(n,1) = (gx*feval(f,x(n-1),y(1)) + gz*feval(f,x(n),y(1))+ gy*feval(f,x(n), y(2)))/2; for j = 2: p-1

ANMF.CH12_4PP.indd 1011

3/1/2023 3:31:31 PM

1012 • Applied Numerical Methods Using MATLAB U1(n,j) = (gx*feval(f,x(n-1),y(j)) + gz*feval(f,x(n),y(j)) + gy*(feval(f,x(n), y(j-1)) + feval (f, x (n), y (j+1))))/2; end U1(n,p) = (gx*feval(f,x(n-1),y(p)) + gz*feval(f,x(n),y(p)) + gy*feval(f,x(n), y(p-1)))/2; else U1 (n, 1: p) = 0; end if fung for j = 1: p U1(n,j) = U1(n,j) + dt*feval(g,x(n),y(j)); end end for k = 1: m-1 end waitbar (k/m) % Compute: U (1) U(1,1) = gx*U1(2,1) + gz*U1(1,1) + gy*U1(1,2) - U0(1,1); for j = 2: p-1 U(1,j) = gx*U1(2,j) + gz*U1(1,j) + gy*(U1(1,j-1) + U1 (1, j+1)) - U0 (1, j); end U(1,p) = gx*U1(2,p) + gz*U1(1,p) + gy*U1(1,p-1) - U0(1,p); % Compute U (i), 1 < i < n for i = 2: n-1 U(i,1) = gx*(U1(i-1,1) + U1(i+1,1)) + gz*U1(i,1) + gy*U1(i,2) - U0(i,1); for j = 2: p-1

ANMF.CH12_4PP.indd 1012

3/1/2023 3:31:31 PM

Partial Differential Equations • 1013

U(i,j) = gx*(U1(i-1,j) + U1(i+1,j))+ gz*U1(i,j)+ gy*(U1(i,j-1) + U1(i,j+1)) - U0(i,j); end U(i,p) = gx*(U1(i-1,p) + U1(i+1,p)) + gz*U1(i,p) + gy*U1(i,p-1) - U0(i,p); end % Compute: U (n) U(n,1) = gx*U1(n-1,1) + gz*U1(n,1) + gy*U1(n,2) - U0(n,1); for j = 2: p-1 U (n, j) = gx*U1 (n-1, j) + gz*U1 (n, j) + gy*(U1 (n, j-1) +U1 (n, j+1)) - U0 (n, j); end U(n,p) = gx*U1(n-1,p) + gz*U1(n,p) + gy*U1(n,p-1) - U0(n,p); U0 = U1; U1 = U; close (hwbar); 12.7.8

function [alpha,r,x,y,U] = Poisson (a,b,m,n,q,tol,f,g)

% Usage:

[alpha,r,x,y,U] = Poisson (a,b,m,n,q,tol,f,g)

% Description: Uses SOR method and 3-point central differences to solve the % PDE - Poisson equation :( d/dx) (d/dx) (x, y) + (d/dy) (d/dy) u(x, y) = f(x, y) % in the rectangular region: R = {(x, y): 0 < x < a, 0 < y < b} % with the Dirichlet type boundary conditions: u(x, y) = g(x, y) % Input:

ANMF.CH12_4PP.indd 1013

a = Upper limit of x (a > 0)

%

b = Upper limit of y (b > 0)

%

m = Number of steps in x (m >= 1)

%

n = Number of steps in y (n >= 1)

%

q = Upper bound on the number of SOR iterations (q >= 1)

3/1/2023 3:31:31 PM

1014 • Applied Numerical Methods Using MATLAB %

tol = Upper bound on the magnitude of the difference

%

between successive solution estimates (tol >= 0)

%

f = String containing name of right-hand side function

%

g = String containing name of boundary condition function.

% The functions f and g are of the form: function z = f(x, y); functin z = g(x, y) % Output:

alpha = Optimal relaxation parameter

%

r

= Number of SOR iterations performed

%

x

= m by 1 vector of x grid points:

% %

x (k) = k*a/ (m+1) for 1 >clear   >>global a   m = 40;   n = 29;   c = [0 0];   a = 1;   T = 5.0e3;   beta = 1.0e-6;   dt = T/m;   dx = a/(n+1);   gamma = beta*dt/(dx*dx);   show (‘gamma’,gamma)   [t,x,U] = Heatone (T,a,m,n,beta,c,’funf128’,’fung128’);   plotxyz ’x’,’u’)

(t,x,U,’Heat

Flow

along

a

Thin

Rod’,’t’,

function y = funf128 (x)   y = x + sin(2*pi*x); function y = fung128(t,k)   global a   y = funf128((k-1)*a); When the script is executed, it produces the following result and the surface plot is shown in Figure 12.13. gamma = 0.1125

ANMF.CH12_4PP.indd 1021

3/1/2023 3:31:35 PM

1022 • Applied Numerical Methods Using MATLAB

Heat Flow along a Thin Rod

2

u

1.5 1 0.5 0 1 0.5 0

x

0

1000

2000

3000

4000

5000

t

FIGURE 12.13 

EXAMPLE 12.9 Solve Laplace’s equation uxx + uyy = 0 in the domain shown in Figure 12.14 by (a) Gauss-Seidel’s method, (b) Jacobi’s method, and (c) the Gauss-Seidel S.O.R. method.

u0,3

u1,3 20

u2,3 20

u3,3

u0,2 0

u1,2

u2,2

0 u3,2

u0,1 0

u1,1

u2,1

0 u3,1

u0,0

0 u1,0

0 u2,0

u3,0

FIGURE 12.14  Boundary conditions of the Laplace equation.

Solution: a) Gauss-Seidel’s method:

ANMF.CH12_4PP.indd 1022

let u2,1 = u1,2 = u2,2 = u1,1 = 0 at the beginning.

3/1/2023 3:31:37 PM

Partial Differential Equations • 1023



ANMF.CH12_4PP.indd 1023

The Gauss-Seidel iteration scheme is



u1(r,11) 

1 (r ) u 2,1  u1(r,2)  4



u(2r,11) 

1 ( r  1) u1,1  u(2r,)2  4



u1(r,21) 

1 ( r  1) u1,1  u(2r,)2  20  4



u(2r,2 1) 

1 ( r  1) u1,2  u(2r,1)  20  4

For r = 0

1 u1(1,1)  [0  0]  0 4



1 u(21,1)  [0  0]  0 4



1 u1(1,2)  [0  0  20]  5 4



1 u(21,)2  [5  0  20]  6.25 4 u1,1

u2,1

u1,2

u2,2

0.00000 1.25000 1.87500 2.34375 2.46094 2.49023 2.49756 2.49939 2.49985 2.49996 2.49999 2.50000

0.00000 1.25000 2.18750 2.42188 2.48047 2.49512 2.49878 2.49969 2.49992 2.49998 2.50000 2.50000

5.00000 6.25000 7.18750 7.42188 7.48047 7.49512 7.49878 7.49969 7.49992 7.49998 7.50000 7.50000

3.75000 6.87500 7.34375 7.46094 7.49023 7.49756 7.49939 7.49985 7.49996 7.49999 7.50000 7.50000

3/1/2023 3:31:45 PM

1024 • Applied Numerical Methods Using MATLAB b) Jacobi’s method:

Initially we take u2,1 = u1,2 = u2,2 = u1,1 = 0.



The Jacobi iteration scheme is



u1(r,11) 

1 (r ) 1 u 2,1  u1(r,2)  0  0   u(2r,1)  u1(r,2)  4 4



u(2r,11) 

1 (r ) 1 u1,1  u(2r,)2  0  0   u1(r,1)  u(2r,)2  4 4



u1(r,21) 

1 (r ) 1 u1,1  u(2r,)2  0  20   u1(r,1)  u(2r,)2  20  4 4



u(2r,2 1) 

1 (r ) 1 u1,2  u(2r,1)  20  0   u1(r,2)  u(2r,1)  20  4 4

For

r = 0, u1(1,1) = 0, u(21,1) = 0, u1(1,2) = 5, u(21,)2 = 5

The successive iterations are given below:

ANMF.CH12_4PP.indd 1024

u1,1

u2,1

u1,2

u2,2

0.00000 1.25000 1.87500 2.18750 2.34375 2.42188 2.46094 2.48047 2.49023 2.49512 2.49756 2.49878 2.49939 2.49969 2.49985 2.49992

0.00000 1.25000 1.87500 2.18750 2.34375 2.42188 2.46094 2.48047 2.49023 2.49512 2.49756 2.49878 2.49939 2.49969 2.49985 2.49992

5.00000 6.25000 6.87500 7.18750 7.34375 7.42188 7.46094 7.48047 7.49023 7.49512 7.49756 7.49878 7.49939 7.49969 7.49985 7.49992

5.00000 6.25000 6.87500 7.18750 7.34375 7.42188 7.46094 7.48047 7.49023 7.49512 7.49756 7.49878 7.49939 7.49969 7.49985 7.49992

2.49996 2.49998 2.50000

2.49996 2.49998 2.50000

7.49996 7.49998 7.50000

7.49996 7.49998 7.50000

3/1/2023 3:31:53 PM

Partial Differential Equations • 1025

c) Gauss-Seidel’s S.O.R. method:

Let u2,1 = u1,2 = u2,2 = u1,1 = 0 at the beginning



The Gauss-Seidel S.O.R. scheme for interior mesh points are



u(i ,rj1) 

w ( r  1) u i 1, j  u(i r)1, j  u(i ,rj11)  u(i ,rj)1   (1  w )u(i r, j)  4

For j = 1, 2 and i = 1, 2, the formulae are



u1(r,11) 

w (r ) u 2,1  u1(r,2)   (1  w )u1(r,2) 4



u(2r,11) 

w ( r  1) u1,1  u(2r,)2   (1  w )u(2r,1) 4



u1(r,21) 

w (r ) u 2,2  u1(r,11)  20   (1  w )u1(r,2) 4



u(2r,2 1) 

w ( r  1) u1,2  u(3r,)2  u(2r,11)  20   (1  w )u(2r,)2 4

For w = 1.3, the values are listed below:

ANMF.CH12_4PP.indd 1025

r

u1,1

u2,1

u1,2

u2,2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

0.00000 1.25000 2.18750 2.42188 2.48047 2.49512 2.49878 2.49969 2.49992 2.49998 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000

0.00000 1.87500 2.34375 2.46094 2.49023 2.49756 2.49939 2.49985 2.49996 2.49999 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000 2.50000

5.00000 6.87500 7.34375 7.46094 7.49023 7.49756 7.49939 7.49985 7.49996 7.49999 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000

6.25000 7.18750 7.42188 7.48047 7.49512 7.49878 7.49969 7.49992 7.49998 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000 7.50000

3/1/2023 3:31:58 PM

1026 • Applied Numerical Methods Using MATLAB EXAMPLE 12.10 Solve the Poisson equation uxx + uyy = 64x2y2 for the square region 0 ≤ x ≤ 1, 0 ≤ y ≤ 1 with h = 1/3 and the values of u on the boundary are zero everywhere. Use Gauss-Seidel’s method. Solution: In this problem g(x, y) = 4x2y2, h = 1/3 and the boundary conditions are u0,0 = u1,0 = u2,0 = u3,0 = 0, u0,1 = u0,2 = u0,3 = 0, u1,3 = u2,3 = u3,3 = 0, u3,1 = u3,2 = 0. a) The Gauss-Seidel iteration scheme is

u(i ,rj1) 

1 ( r  1) u i 1, j  u(i r)1, j  u(i ,rj11)  u(i ,rj)1  h 2 g(ih, jk)  4

Now, g(ih, jk) = 8h4i2j2 =

64 2 2 i j . Thus 81

1  ( r  1) 1 88  u 0,1  u(2r,1)  u1(r,01)  u1(r,2)  . .1.1  4 9 81  8  1  (r ) 1 88  u 2,1  u1(r,2)   0  u(2r,1)  0  u1(r,2)   4 729  4  729 

u1(r,11) 

1  ( r  1) 1 88 2 2 u1,1  u(3r,1)  u(2r,0 1)  u(2r,)2  . . 2 .1   4 9 81  1 8  32   u1(r,11)  u(2r,)2  4 729 

u(2r,11) 

1  ( r  1) 1 8  u 0,2  u(2r,)2  u1(r,11)  u1(r,3)  . .12.2 2   4 9 81  1 8  32   u(2r,)2  u1(r,11)  4 729 

u1(r,21) 

1  ( r  1) 1 88 2 2 u1,2  u(3r,)2  u(2r,11)  u(2r,)3  . .2 .2   4 9 81  1 8  128   u1(r,21)  u(2r,11)  4 729 

u(2r,2 1) 

ANMF.CH12_4PP.indd 1026

3/1/2023 3:32:03 PM

Partial Differential Equations • 1027

Let

(0) (0) u= u= u1(0,2) = 0 . 2 ,1 2 ,2

All the values are shown in the following table. r

u1,1

u2,1

u1,2

u2,2

1 2 3 4 5 6 7 8 9 10

0.00000 –0.02195 –0.06859 –0.12414 –0.13803 –0.14150 –0.14237 –0.14259 –0.14264 –0.14266

0.00000 –0.09328 –0.20439 –0.23217 –0.23911 –0.24085 –0.24128 –0.24139 –0.24142 –0.24142

0.00000 –0.02195 –0.09328 –0.20439 –0.23217 –0.24085 –0.24128 –0.24139 –0.24142 –0.24142

0.00000 –0.39781 –0.45336 –0.46725 –0.47072 –0.47159 –0.47181 –0.47186 –0.47187 –0.47188

EXAMPLE 12.11 Random motion of particles at the molecular level causes movement of matter from region of high concentration to regions of low concentration through the process of diffusion. Suppose a substance is constrained to move in one dimension. If c(t, x) denotes the concentration of the substance at time t and position x, then using Fick’s law can model the change in concentration due to diffusion as follows:

c 2c D 2 t x

where D > 0 is the diffusion coefficient. When the substance is oxygen and the medium is air then the diffusion coefficient at 20ºC = 0.201. Solve this system for 0 ≤ t ≤ 30 and 0 ≤ x ≤ 5. The initial and boundary conditions are given as follows:

u(0, x) = (x – 3)2



u(t, 0) = 1

ux(t, 5) = 0 Write a MATLAB program to solve this system. Print g and plot the surface.

ANMF.CH12_4PP.indd 1027

3/1/2023 3:32:05 PM

1028 • Applied Numerical Methods Using MATLAB Solution: clc  clear   % t Precision   m = 50;   % x Precision   n = 39;   % Boundary condition type   c = [0 1];   % Maximum x   a = 5;   % Maximum t   T = 30;   % Diffusion coefficient   D = 0.201;   f = inline (‘(x - 3.0) ^2’,’x’);   g = inline (‘1*(k - 2) + 0*(k-1)’,’t’,’k’); % Find gamma   dt = T/m;   dx = a/ (n+1);   gamma = D*dt/ (dx*dx); % Solve diffusion equation   [t, x, U] = Heatone (T, a, m, n, D, c, f, g);   plotxyz (t, x, U,’Plot of Diffusion Equation Solution’,’t’,’x’,’u’) >> gamma gamma =  7.7184

ANMF.CH12_4PP.indd 1028

3/1/2023 3:32:05 PM

Partial Differential Equations • 1029

Plot of Diffusion Equation Solution

10

u

5

0

-5 6 30

4

20

2 x

10 0

0

t

FIGURE 12.15 

The value of gamma, g = 7.7184. EXAMPLE 12.12 Use the forward difference method to approximate the solution to the following parabolic equation.

u 4  2 u   0 , 0 < x < 4, 0 < t, t 2 x 2



u(0, t) = u(4, t) = 0,



    u(x, 0)  sin x  1  2 cos x  , 0 ≤ x ≤ 4. 4  4 

0 < t,

Use h = 0.2, and k = 0.04, and compare your results at t = 0.4 to the analytical solution

ANMF.CH12_4PP.indd 1029

  u(x, t)  e t sin x  e t / 4 sin x . 2 4

3/1/2023 3:32:10 PM

1030 • Applied Numerical Methods Using MATLAB Solution: For h = 0.2 and k = 0.04, the forward-difference method gives the following results: TABLE 12.4 i

j

xi

tj

wi,j

u(xi, tj)

4 8 12 16

10 10 10 10

0.8 1.6 2.4 3.2

0.4 0.4 0.4 0.4

1.16614 1.25241 0.46818 –0.10276

1.16936 1.25455 0.46654 –0.10566

EXAMPLE 12.13 Repeat Example 12.12 using the Crank-Nicolson algorithm. Solution: For h = 0.2 and k = 0.04, the Crank-Nicolson algorithm gives the following results: TABLE 12.5 i

j

xi

tj

wi,j

u(xi, tj)

4 8 12 16

10 10 10 10

0.8 1.6 2.4 3.2

0.4 0.4 0.4 0.4

1.17153 1.25600 0.46544 –0.10762

1.16936 1.25456 0.46655 –0.10566

12.8 TWO-DIMENSIONAL PARABOLIC EQUATIONS Consider the following two-dimensional heat equations:

 2u 2u  u    2  2  (12.97) t y   x

Here, the solution, u(t, x, y), depends on three independent variables. In the steady-state when ut = 0, the two-dimensional heat equation reduces to Laplace’s equation. If only the steady-state solution is of interest, then it may be more efficient to solve Laplace’s equation directly.

ANMF.CH12_4PP.indd 1030

3/1/2023 3:32:11 PM

Partial Differential Equations • 1031

Here, we consider solving Equation (12.97) over the following rectangular region in the x, y plane.

R = {(x, y) | 0 < x < a, 0 < y < b}

(12.98)

To obtain a unique solution, we need to specify both an initial condition and boundary conditions. The initial condition will be of the following form for some initial condition function f(x, y).

u(0, x, y) = f(x, y), (x, y) ∈ R

(12.99)

We consider only the Dirichlet boundary conditions. In particular, if dR denotes the boundary of the region R, then we can place the following constraints on the solution along the boundary as

u(t, x, y) = g(t, x, y),

(x, y) ∈ dR(12.100)

Here, we consider an alternative finite difference method specifically suitable to the two-dimensional case. We solve the heat equation over the time interval 0 ≤ t ≤ T. We compute numerical estimates of u(t, x, y) over a uniform grid consisting of m + 1 values of t, n + 2 values of x, and p + 2 values of y. If Dt, Dx, and Dy denote the step sizes of the variables t, x, and y, respectively, then

t 

T (12.101) m



x 

a (12.102) n 1



y 

b (12.103) p 1

Let tk, xi, and yj denote the values of t, x, and y at the grid points. Hence, tk = kDt,  0 ≤ k ≤ m

(12.104)

xi = iDx, 0 ≤ i ≤ (n + 1)

(12.105)

yj = jDy, 0 ≤ j ≤ (p + 1)

(12.106)

Let u kij denote the computed values of u(tk, xi, yj). The superscript specifies the time step, while the subscripts specify the space steps. To convert the twodimensional heat equation in Equation (12.97) to a difference equation, we

ANMF.CH12_4PP.indd 1031

3/1/2023 3:32:15 PM

1032 • Applied Numerical Methods Using MATLAB use finite difference approximations for the derivative terms. The first-order time derivative can be approximated with a three-point central difference about the midpoint, (tk + tk+1)/2. We evaluate one of the second-order derivative terms, uxx, at time tk, and the other second-order derivative term, uyy, at time tk+1. This gives the following system of difference equations at the i­ nterior points 0 ≤ k ≤ m, 1 ≤ i ≤ n, and 1 ≤ j ≤ p. u kij 1  u kij



t

 u ki 1, j  2u kij  u ki 1, j u ki ,j11  2u kij 1  u ki ,j11      (12.107) 2 2    x y  

In Equation (12.107), the coefficient matrix of uk+1 is tridiagonal. However, this is achieved by adding some bias, which evaluates uxx at an earlier time than uyy. This bias can be compensated for by adding a second equation for uk+2, which reverses the roles of x and y by evaluating uxx at time tk+2 and uyy at time tk+1. u kij  2  u kij 1



t

 u ki 12, j  2u kij  2  u ki 12, j u ki ,j11  2u kij 1  u ki ,j11      (12.108)  y 2 x 2  

When Equations (12.107) and (12.108) are solved together, we see that the derivatives with respect to x are computed at the beginning of the interval [tk, tk+1] and then at the end of the interval [tk+1, tk+2], while the derivatives with respect to y are computed at the end of the interval [tk, tk+1] and then at the beginning of interval [tk+1, tk+2]. The formulation in Equations (12.107) and (12.108) is referred to as the alternating direction implicit method or the ADI method. The ADI formulas can be simplified by introducing the following gain parameters.

x 

t (12.109) x 2



y 

t (12.110) y 2

Using gx and gy, Equations (12.107) and (12.108) can be written as

















    y u ki ,j11  u ki ,j11  (1  2  y )u ki ,j 1   x u ik1, j  u ki 1, j  (1  2 x )u kij (12.111)     x u ki 12, j  u ki 12, j  (1  2  x )u ki ,j 2   y u ik,j11  u ik,j11  (1  2  y )u kij 1 (12.112)

ANMF.CH12_4PP.indd 1032

3/1/2023 3:32:20 PM

Partial Differential Equations • 1033

Equation (12.111) is solved for the ith row of the n × p matrix uk+1 for 1 ≤ i ≤ n. This involves solving n tridiagonal systems, each of dimension p. Equation (12.112) is solved for the jth column of the n × p matrix uk+2 for 1 ≤ j ≤ p. This involves solving p tridiagonal systems, each of dimension n. EXAMPLE 12.14 Consider the problem of determining the displacement of a thin elastic membrane stretched over a square opening of width a = 2 and length b = 2. Suppose the tension and density of the membrane are such that the wave equation coefficient is b = 0.6. Hence, the membrane displacement is a solution of

 2u 2u  2u 0 . 6   2  2 t 2 y   x

The membrane is fixed along the edges, and the initial conditions are

u(0, x, y) = 0

ut(0, x, y) = [1 cos(px)][1 – cos(xy)] Write a MATLAB program to find the membrane displacement u(t, x, y). Plot the membrane displacement as a surface on a 9 × 9 grid at t = 2.5. Solution: >> % Problem E12.14 >> a=2; b=2; m=20; n=19; p=9; T=4; beta=0.6; >> g=inline (‘(1-cos (pi*x))*(1-cos (pi*y))’,’x’,’y’); >> T=prompt (‘enter final time’, 0, T, T/2); >> T=2.5 >> [x,y,U]=Wavetwo(T,a,b,m,n,p,beta,’’,g); T=  2.5000 >> [x,y,U]=Wavetwo(T,a,b,m,n,p,beta,’’,g); >> caption=sprintf (‘Membrane Displacement: t = %.3f’, T); >> plotxyz(x, y, U, caption,’x’,’y’,’u’);

ANMF.CH12_4PP.indd 1033

3/1/2023 3:32:21 PM

1034 • Applied Numerical Methods Using MATLAB When the script is executed, it produces the plot shown in Figure 12.16 where it is evident that the membrane is vibrating. Membrane Displacement: t = 2.500

1 0.8

u

0.6 0.4 0.2 0 2 1.5 1 0.5 y

0

0

1

0.5

1.5

2

x

FIGURE 12.16 

EXAMPLE 12.15 Write a MATLAB program and use the ADI method to solve a two-dimensional parabolic partial differential equation for determining the temperature over a square plate that is 5 units on a side so that a = b = 5. The thermal diffusivity is b = 10–5. The temperature u(t, x, y) at time t and position (x, y) is a solution of the following two-dimensional heat equation:

 2u 2u  u  10 5  2  2  t y   x

The time interval is T = 5000. Let the size of the grid be m = 20 and n = 19, and p = 19. Determine the gain parameters gx and gy. The initial and boundary conditions are

ANMF.CH12_4PP.indd 1034

3/1/2023 3:32:22 PM

Partial Differential Equations • 1035



u(0, x, y) = 0

ut(t, x, y) = ey cos x – ex cos y. Solution: clc  clear   a = 5; b = 5; m = 20; n = 19; p = 19; T = 1.e4; beta = 1.0e-5;   g = inline (‘exp(y)*cos(x) - exp(x)*cos(y)’,’t’,’x’,’y’);   T = prompt (‘Enter final time’,0,T,T/2);   dt = T/m;   dx = a/(n+1);   dy = b/(p+1);   gx = beta*dt/(dx*dx);   gy = beta*dt/(dy*dy);   show (‘gamma_x’,gx);   show (‘gamma_y’,gy);   [x,y,U] =Heattwo (T,a,b,m,n,p,beta,’’,g);   caption = sprintf (‘Plate Temperature Distribution: t = %g’,T);   plotxyz (x,y,U,caption,’x’,’y’,’u’) When the script is executed, it produces the following result and the surface plot is shown in Figure 12.17. Enter final time (0 to 10000, default: 5000): 5000 gamma_x = 0.04 1 => Save, 2 => Append, Enter => continue: gamma_y = 0.04 1 => Save, 2 => Append, Enter => continue:

ANMF.CH12_4PP.indd 1035

3/1/2023 3:32:22 PM

1036 • Applied Numerical Methods Using MATLAB

Plate Temperature Distribution: t = 5000

100

u

50 0 -50 -100 6 4 2 y

0

0

2

1

3

5

4

x

FIGURE 12.17 

EXAMPLE 12.16 Approximate the solution to the following elliptic partial differential equation using the Gauss-Seidel iteration method.

2u 2u   (cos(x  y )  cos(x  y )), x 2 y 2

0 < x < p, 0 < y
> f=inline (‘sin (pi*x)/ (1+x)’,’x’); g=inline (‘0’,’t’,’k’); >> dt=T/m; dx=a/ (n+1); gamma=beta*dt/ (dx*dx); >>show (‘gamma’, gamma) gamma = 2.25 >> [t,x,U]=Heatone(T,a,m,n,beta,c,f,g); >> plotxyz (t, x, U,’Heated Rod’,’t’,’x’,’u’) When the script is executed, it produces gamma = 2.5 and the plot shown in Figure 12.20 where it is evident that since the rod is insulated, the steady state value of the temperature is constant and is equal to the average of the initial temperature (no heat loss). Heated Rod

1

u

0.5

0

-0.5 2 1.5

1.5 1

1 0.5

x

0.5 0

0

t

FIGURE 12.20 

ANMF.CH12_4PP.indd 1054

3/1/2023 3:33:28 PM

Partial Differential Equations • 1055

EXAMPLE 12.24 Write a MATLAB program to find the electrostatic potential of the plate in Example E12.4 using b = 1. The initial potential is v(0, x, y) = 0. Plot the solution surface on a 30 × 30 grid at t = 0.1 and t = 1.0. Print gx and gy in each case. Solution: function u=fung1224 (t, x, y) global a b if (x=a) u=(y/a) ^2; elseif(y=b) u=sqrt(x/a) end >> T=prompt (‘Enter final time’, 0, T, T/2); >> dt=T/m; dx=a/ (n+1); dy=b/ (p+1); >> gx=beta*dt/ (dx*dx); gy=beta*dt/ (dy*dy); >> show (‘gamma_x’, gx); show (‘gamma_y’, gy); >> gx gx =  6.0063 >> gy gy =  6.0063

ANMF.CH12_4PP.indd 1055

3/1/2023 3:33:28 PM

1056 • Applied Numerical Methods Using MATLAB >>[x, y, U]=Heattwo (T, a, b, m, n, p, beta,’’,’fung1224’); >> caption=sprintf (‘Electrostatic Potential: t = %g’, T); >> plotxyz(x, y, U, caption,’x’,’y’,’u’) >> gx gx =  6.0063 >> gy gy =  6.0063 When the script is executed, with t =2.0 it produces gamma x = Gamma y = 6.0063and the surface plot is shown in Figure 12.21(a). Electrostatic Potential: t = 2

1 0.8

u

0.6 0.4 0.2 0 4 3 2 1 y

0

0

2

1

3

4

x

FIGURE 12.21(a) 

ANMF.CH12_4PP.indd 1056

3/1/2023 3:33:28 PM

Partial Differential Equations • 1057

EXAMPLE 12.25 Write a MATLAB program and use the two-dimensional central difference method to solve a hyperbolic partial differential equation for determining the displacement of a thin elastic membrane stretched over a rectangular opening of width a = 5 and b = 5. The membrane tension and density are such that the wave equation coefficient is b = 1/2. The membrane displacement u(t, x, y) at time t and coordinates (x, y) is a solution of the following wave equation:

2u 1  2u 2u      t 2 2  x 2 y 2 

The time interval T = 50, and the size of the grid is m = 40 and n = p = 39. The membrane is fixed along the rectangular boundary and the boundary conditions are

u(t, 0, y) = 0



u(t, 5, y) = 0



u(t, x, 0) = 0



u(t, x, 5) = 0

The following initial conditions are given  y  u(0, x, y) = 0.2 sin (px) sin    2  ut(0, x, y) = 0.

Solution: clc  clear   a = 5; b = 5; m = 40; beta = 0.5;

n = 39;

p = 39; T = 5.0;

  f = inline (‘0.2*sin(pi*x)*sin(pi*y/2)’,’x’,’y’);   T = prompt (‘Enter Final Time’,0,T,T);   [x,y,U] = wavetwo(T,a,b,m,n,p,beta,f,’’);

ANMF.CH12_4PP.indd 1057

3/1/2023 3:33:31 PM

1058 • Applied Numerical Methods Using MATLAB   caption = sprintf (‘Membrane Displacement: t = %.3f’,T);   plotxyz (x,y,U,caption,’x’,’y’,’u’); When the script is executed, it produces the following result and the surface plot is shown in Figure 12.21(b). Membrane Displacement: t = 5.000

0.2

u

0.1 0 -0.1 -0.2 6 4 2 y

0

0

1

2

3

4

5

x

FIGURE 12.21(b) 

Example 12.26 Write a MATLAB program for analyzing the torsion in a square prismatic bar that is subjected to twisting. The torsion function ϕ for the bar is a solution to the following partial differential equation (Poisson equation):

2 2   5 x 2 y 2

The cross-section of the bar is a = 5 cm wide by b = 5 cm deep. Use the grid as m = 30, and n = 30. Maximum number of iterations: 100 and error tolerance = 5 × 10–6. Plot the torsional function of the twisted bar as a surface plot. Determine the relaxation parameter a and the number of iterations.

ANMF.CH12_4PP.indd 1058

3/1/2023 3:33:32 PM

Partial Differential Equations • 1059

Solution: clc clear a = 5; b = 5; m = 30; n = 30; q = 100; tol = 5.e-6; f = inline (‘-5’,’x’,’y’);   fprintf (‘Example 9.7.4: Twisted Bar\n’);   [alpha,r,x,y,U] = Poisson (a,b,m,n,q,tol,f,’’);   show (‘Relaxation parameter alpha’,alpha)   show (‘Number of iterations’,r)   plotxyz (x,y,U,’Twisted Bar Twisting’,’x (cm)’,’y (cm)’,’u (cm^2)’) When the script is executed, it produces the following result and the surface plot is shown in Figure 12.22. Relaxation parameter alpha = 1.811 Number of iterations = 93 Twisted Bar Twisting

10

u (cm2)

8 6 4 2 0 6 4 2 y (cm)

0

0

1

2

3

4

5

x (cm)

FIGURE 12.22 

ANMF.CH12_4PP.indd 1059

3/1/2023 3:33:33 PM

1060 • Applied Numerical Methods Using MATLAB

12.11 MATLAB BUILT-IN FUNCTION: PDEPE [Source: MATLAB website product help] PDE Solver pdepe Solve initial-boundary value problems for parabolic-­ elliptic PDEs in 1-D Syntax : Sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan) sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,options) Arguments: m

A parameter corresponding to the symmetry of the problem. m can be slab = 0, cylindrical = 1, or spherical = 2.

pdefun

A handle to a function that defines the components of the PDE.

icfun

A handle to a function that defines the initial conditions.

bcfun

A handle to a function that defines the boundary conditions.

xmesh

A vector [x0, x1, ..., xn] specifying the points at which a numerical solution is requested for every value in tspan. The elements of xmesh must satisfy x0  0, it is not necessary to use a fine mesh near x = 0 to account for the coordinate singularity. The time integration is done with ode15s. pdepe exploits the capabilities of ode15s for solving the differential-algebraic equations that arise when Equation (E.1) contains elliptic equations, and for handling Jacobians with a specified sparsity pattern. After discretization, elliptic equations give rise to algebraic equations. If the elements of the initial conditions vector that correspond to elliptic equations are not “consistent” with the discretization, pdepe tries to adjust them before beginning the time integration. For this reason, the solution returned for the initial time may have a discretization error comparable to that at any other time. If the mesh is sufficiently fine, pdepe can find consistent initial conditions close to the given ones. If pdepe displays a message that it has difficulty finding consistent initial conditions, try refining the mesh. No adjustment is necessary for elements of the initial conditions vector that correspond to parabolic equations. EXAMPLE 12.27 This illustrates the straightforward formulation, computation, and plotting of the solution of a single PDE.

2

 u   u    t  x  x 

This equation holds on an interval 0 ≤ x ≤ 1 for times t³ 0. The PDE satisfies the initial condition u(x, 0) = sin px

ANMF.CH12_4PP.indd 1063

3/1/2023 3:33:37 PM

1064 • Applied Numerical Methods Using MATLAB and boundary conditions

u(0, t)  0 u  e t  (1, t)  0 x

MATLAB Solution: It is convenient to use subfunctions to place all the functions required by pdepe in a single M-file. function pdex1 m = 0; x = linspace(0,1,20); t = linspace(0,2,5); sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t); % Extract the first solution component as u. u = sol(:,:,1); % A surface plot is often a good way to study a solution. surf(x,t,u) title(‘Numerical solution computed with 20 mesh points.’) xlabel(‘Distance x’) ylabel(‘Time t’) % A solution profile can also be illuminating. figure plot(x,u(end,:)) title(‘Solution at t = 2’) xlabel(‘Distance x’) ylabel(‘u(x,2)’) % -------------------------------------------------------------function [c,f,s] = pdex1pde(x,t,u,DuDx)

ANMF.CH12_4PP.indd 1064

3/1/2023 3:33:39 PM

Partial Differential Equations • 1065

c = pi^2; f = DuDx; s = 0; % -------------------------------------------------------------function u0 = pdex1ic(x) u0 = sin(pi*x); % -------------------------------------------------------------function [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t) pl = ul; ql = 0; pr = pi * exp(-t); qr = 1; In this example, the PDE, initial condition, and boundary conditions are coded in subfunctions pdex1pde, pdex1ic, and pdex1bc. The surface plot (Figure 12.23) shows the behavior of the solution.

FIGURE 12.23 

ANMF.CH12_4PP.indd 1065

3/1/2023 3:33:39 PM

1066 • Applied Numerical Methods Using MATLAB The following plot (Figure 12.24) shows the solution profile at the final value of t (i.e., t = 2).

FIGURE 12.24 

EXAMPLE 12.28 This example illustrates the solution of a system of PDEs. The problem has boundary layers at both ends of the interval. The solution changes rapidly for small t. The PDEs are



 u1  2 u1  0.024  F  u1  u2  t  x2  u2  2 u2  0.170  F  u1  u2  t  x2

where F(y) = exp(5.73y) – exp(–11.46y). This equation holds on an interval 0 ≤ x ≤ 1 for times t ≥ 0.

ANMF.CH12_4PP.indd 1066

3/1/2023 3:33:40 PM

Partial Differential Equations • 1067

The PDE satisfies the initial conditions

u1 ( x, 0) ≡ 1 u2 ( x, 0) ≡ 0

and boundary conditions u1 (0, t)  0 x u2 (0, t)  0 u1 (1, t)  1

u2 (1, t)  0 x

MATLAB Solution: In the form expected by pdepe, the equations are



1   u1   0.024   u1 /  x     F  u1  u2      1 .   u      t  2   x 0.170   u2 /  x    F  u1  u2  

The boundary conditions on the partial derivatives of u have to be written in terms of the flux. In the form expected by pdepe, the left boundary condition is



 0  1  0.024   u1 /  x   0   u   0  .  0.170  u /  x   0   2     2   

and the right boundary condition is



 u1  1 0  0.024   u1 /  x   0   0   1   0.170  u /  x   0   2        

The solution changes rapidly for small t. The program selects the step size in time to resolve this sharp change, but to see this behavior in the plots, the example must select the output times accordingly. There are boundary layers in the solution at both ends of [0,1], so the example places mesh points near 0 and 1 to resolve these sharp changes. Often some experimentation is needed to select a mesh that reveals the behavior of the solution.

ANMF.CH12_4PP.indd 1067

3/1/2023 3:33:48 PM

1068 • Applied Numerical Methods Using MATLAB function pdex4 m = 0; x = [0 0.005 0.01 0.05 0.1 0.2 0.5 0.7 0.9 0.95 0.99 0.995 1]; t = [0 0.005 0.01 0.05 0.1 0.5 1 1.5 2]; sol = pdepe(m,@pdex4pde,@pdex4ic,@pdex4bc,x,t); u1 = sol(:,:,1); u2 = sol(:,:,2); figure surf(x,t,u1) title(‘u1(x,t)’) xlabel(‘Distance x’) ylabel(‘Time t’) figure surf(x,t,u2) title(‘u2(x,t)’) xlabel(‘Distance x’) ylabel(‘Time t’) % -------------------------------------------------------------function [c,f,s] = pdex4pde(x,t,u,DuDx) c = [1; 1]; f = [0.024; 0.17] .* DuDx; y = u(1) - u(2); F = exp(5.73*y)-exp(-11.47*y); s = [-F; F]; % --------------------------------------------------------------

ANMF.CH12_4PP.indd 1068

3/1/2023 3:33:48 PM

Partial Differential Equations • 1069

function u0 = pdex4ic(x); u0 = [1; 0]; % -------------------------------------------------------------function [pl,ql,pr,qr] = pdex4bc(xl,ul,xr,ur,t) pl = [0; ul(2)]; ql = [1; 0]; pr = [ur(1)-1; 0]; qr = [0; 1]; In this example, the PDEs, initial conditions, and boundary conditions are coded in subfunctions pdex4pde, pdex4ic, and pdex4bc. The surface plots (Figure 12.25 and Figure 12.26) show the behavior of the solution components.

FIGURE 12.25 

ANMF.CH12_4PP.indd 1069

3/1/2023 3:33:48 PM

1070 • Applied Numerical Methods Using MATLAB

FIGURE 12.26 

System of PDEs EXAMPLE 12.29 This example illustrates the solution of a system of partial differential equations. The problem is taken from electrodynamics. It has boundary layers at both ends of the interval, and the solution changes rapidly for small t. The PDEs are



u1 2 u  0.024 21  F  u1  u2  t x u2  2 u2  0.170 2  F  u1  u2  t x

where F(y) = exp(5.73y) – exp(–11.46y). The equations hold on an interval 0 less than or equal to x less than or equal to 1 0 ≤ x ≤ 1 for times t ≥ 0.

ANMF.CH12_4PP.indd 1070

3/1/2023 3:33:50 PM

Partial Differential Equations • 1071

MATLAB Solution: The solution u satisfies the initial conditions

u1(x, 0) ≡ 1 u2(x, 0) ≡ 0

and boundary conditions u1 (0, t)  0 x u2 (0, t)  0 u1 (1, t)  1 NOTE

u2 (1, t)  0 x

The demo pdex4 contains the complete code for this example. The demo uses subfunctions to place all required functions in a single M-file. To run this example, type pdex4 at the command line. Rewrite the PDE. In the form expected by pdepe, the equations are



1   u1   0.024   u1 /  x     F  u1  u2      1   u      t  2   x 0.170   u2 /  x    F  u1  u2  

The boundary conditions on the partial derivatives of u have to be written in terms of the flux. In the form expected by pdepe, the left boundary condition is



 0  1  0.024   u1 /  x   0   u   0  .  0.170  u /  x   0   2     2   

and the right boundary condition is



 u1  1 0  0.024   u1 /  x   0   0   1  .  0.170  u /  x   0   2        

Code the PDE. After you rewrite the PDE in the form shown above, you can code it as a function that pdepe can use. The function must be of the form

ANMF.CH12_4PP.indd 1071

3/1/2023 3:33:57 PM

1072 • Applied Numerical Methods Using MATLAB [c,f,s] = pdefun(x,t,u,dudx), where c, f, and s correspond to the c, f, and s terms in Equation (E.1). function [c,f,s] = pdex4pde(x,t,u,DuDx) c = [1; 1]; f = [0.024; 0.17] .* DuDx; y = u(1) - u(2); F = exp(5.73*y)-exp(-11.47*y); s = [-F; F]; Code the initial conditions function. The initial conditions function must be of the form u = icfun(x) The code below represents the initial conditions in the function pdex4ic. function u0 = pdex4ic(x); u0 = [1; 0]; Code the boundary conditions function. The boundary conditions functions must be of the form [pl,ql,pr,qr] = bcfun(xl,ul,xr,ur,t) The code below evaluates the components p(x, t, u) and q(x, t) (Equation (E.3) of the boundary conditions in the function pdex4bc. function [pl,ql,pr,qr] = pdex4bc(xl,ul,xr,ur,t) pl = [0; ul(2)]; ql = [1; 0]; pr = [ur(1)-1; 0]; qr = [0; 1]; Select mesh points for the solution. The solution changes rapidly for small t. The program selects the step size in time to resolve this sharp change, but to see this behavior in the plots, output times must be selected accordingly. There are boundary layers in the solution at both ends of [0,1], so mesh points must be placed there to resolve these sharp changes. Often some experimentation is needed to select the mesh that reveals the behavior of the solution.

ANMF.CH12_4PP.indd 1072

3/1/2023 3:33:57 PM

Partial Differential Equations • 1073

x = [0 0.005 0.01 0.05 0.1 0.2 0.5 0.7 0.9 0.95 0.99 0.995 1]; t = [0 0.005 0.01 0.05 0.1 0.5 1 1.5 2]; Apply the PDE solver. The example calls pdepe with m = 0, the functions pdex4pde, pdex4ic, and pdex4bc, and the mesh defined by x and t at which pdepe is to evaluate the solution. The pdepe function returns the numerical solution in a three-dimensional array sol, where sol(i,j,k) approximates the kth component of the solution, uk, evaluated at t(i) and x(j). m = 0; sol = pdepe(m,@pdex4pde,@pdex4ic,@pdex4bc,x,t); View the results. The surface plots show the behavior of the solution components. u1 = sol(:,:,1); u2 = sol(:,:,2); figure surf(x,t,u1) title(‘u1(x,t)’) xlabel(‘Distance x’) ylabel(‘Time t’) ylabel('Time t')

FIGURE 12.27 

ANMF.CH12_4PP.indd 1073

3/1/2023 3:33:57 PM

1074 • Applied Numerical Methods Using MATLAB figure surf(x,t,u2) title(‘u2(x,t)’) xlabel(‘Distance x’) ylabel(‘Time t’)

FIGURE 12.28 

Additional Examples: The following additional examples are available from the MATLAB website. Example Name

Description

pdex1

Simple PDE that illustrates the straightforward formulation, computation, and plotting of the solution

pdex2

Problem that involves discontinuities

pdex3

Problem that requires computing values of the partial derivative

pdex4

System of two PDEs whose solution has boundary layers at both ends of the interval and changes rapidly for small t

pdex5

System of PDEs with step functions as initial conditions

ANMF.CH12_4PP.indd 1074

3/1/2023 3:33:57 PM

Partial Differential Equations • 1075

m

Symmetry of the problem: slab = 0, cylindrical = 1, spherical = 2. This is the first input argument used in the call to pdepe.

xmesh

A vector [x0, x1, ..., xn] specifying the points at which the elements of ui were computed. This is the same vector with which pdepe was called.

ui

A vector sol(j,:,i) that approximates component i of the solution at time tf and mesh points xmesh, where sol is the solution returned by pdepe.

xout

A vector of points from the interval [x0,xn] at which the interpolated solution is requested.

Description: [uout,duoutdx] = pdeval(m,x,ui,xout) approximates the solution ui and its partial derivative ∂ui/∂x at points from the interval [x0,xn]. The pdeval function returns the computed values in uout and duoutdx, respectively. NOTE

pdeval evaluates the partial derivative ∂ui/∂x rather than the flux f. Although the flux is continuous, the partial derivative may have a jump at a material interface Initial Value Problems: pdepe solves systems of parabolic and elliptic PDEs in one spatial variable x and time t, of the form

 u u   m   u    u  c  x, t, u, x f  x, t, u,  x m     s  x, t, u,  (E.1)  xt  x  x   x  

The PDEs hold for t0 ≤ t ≤ tf and a ≤ x ≤ b. The interval [a, b] must be finite. m can be 0, 1, or 2, corresponding to slab, cylindrical, or spherical symmetry, respectively. If m > 0, then a ≥ 0 must also hold. In Equation (E.1), f(x, t, u, ∂u/∂x) is a flux term and s(x, t, u, ∂u/∂x) is a source term. The flux term must depend on ∂u/∂x. The coupling of the partial derivatives with respect to time is restricted to multiplication by a diagonal matrix c(x, t, u, ∂u/∂x). The diagonal elements of this matrix are either identically zero or positive. An element that is identically zero corresponds to an elliptic equation and otherwise to a parabolic equation. There must be at least one parabolic equation. An element of c that corresponds to a parabolic equation can vanish at isolated values of x if they are mesh points. Discontinuities in c and/or s due to material interfaces are permitted provided that a mesh point is placed at each interface.

ANMF.CH12_4PP.indd 1075

3/1/2023 3:33:58 PM

1076 • Applied Numerical Methods Using MATLAB At the initial time t = t0, for all x the solution components satisfy initial conditions of the form

u(x, t0) = u0(x)(E.2)

At the boundary x = a or x = b, for all t the solution components satisfy a boundary condition of the form

 u  p( x, t, u)  q( x, t) f  x, t, u,   0 (E.3) x 

q(x, t) is a diagonal matrix with elements that are either identically zero or never zero. Note that the boundary conditions are expressed in terms of the f rather than partial derivative of u with respect to x ∂u/∂x. Also, of the two coefficients, only p can depend on u. The MATLAB PDE solver, pdepe, solves initial-boundary value problems for systems of parabolic and elliptic PDEs in the one space variable x and time t. There must be at least one parabolic equation in the system. The pdepe solver converts the PDEs to ODEs using a second-order accurate spatial discretization based on a fixed set of user-specified nodes. The discretization method has been described. The time integration is done with ode15s. The pdepe solver exploits the capabilities of ode15s for solving the differential-algebraic equations that arise when Equation (E.1) contains elliptic equations, and for handling Jacobians with a specified sparsity pattern. ode15s changes both the time step and the formula dynamically. After discretization, elliptic equations give rise to algebraic equations. If the elements of the initial conditions vector that correspond to elliptic equations are not “consistent” with the discretization, pdepe tries to adjust them before beginning the time integration. For this reason, the solution returned for the initial time may have a discretization error comparable to that at any other time. If the mesh is sufficiently fine, pdepe can find consistent initial conditions close to the given ones. If pdepe displays a message that it has difficulty finding consistent initial conditions, try refining the mesh. No adjustment is necessary for elements of the initial conditions vector that correspond to parabolic equations.

ANMF.CH12_4PP.indd 1076

3/1/2023 3:34:00 PM

Partial Differential Equations • 1077

PDE Solver Syntax: The basic syntax of the solver is: sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan) NOTE

Correspondences given are to terms used in Initial Value Problems. The input arguments are m pdefun

Specifies the symmetry of the problem. m can be 0 = slab, 1 = cylindrical, or 2 = spherical. It corresponds to m in Equation (E.1). Function that defines the components of the PDE. It computes the terms c, f, and s in Equation (E.1), and has the form [c,f,s] = pdefun(x,t,u,dudx)

icfun

where x and t are scalars, and u and dudx are vectors that approximate the solution u and its partial derivative with respect to x. c, f, and s are column vectors. c stores the diagonal elements of the matrix c. Function that evaluates the initial conditions. It has the form u = icfun(x)

bcfun

When called with an argument x, icfun evaluates and returns the initial values of the solution components at x in the column vector u. Function that evaluates the terms p and q of the boundary conditions. It has the form [pl,ql,pr,qr] = bcfun(xl,ul,xr,ur,t) where ul is the approximate solution at the left boundary xl = a and ur is the approximate solution at the right boundary xr = b. pl and ql are column vectors corresponding to p and the diagonal of q evaluated at xl. Similarly, pr and qr correspond to xr. When m> 0 and a = 0, boundedness of the solution near x = 0 requires that the f vanish at a = 0. pdepe imposes this boundary condition automatically and it ignores values returned in pl and ql.

ANMF.CH12_4PP.indd 1077

3/1/2023 3:34:00 PM

1078 • Applied Numerical Methods Using MATLAB xmesh

Vector [x0, x1, ..., xn] specifying the points at which a numerical solution is requested for every value in tspan. x0 and xn correspond to a and b, respectively. Second-order approximation to the solution is made on the mesh specified in xmesh. Generally, it is best to use closely spaced mesh points where the solution changes rapidly. pdepe does not select the mesh in x automatically. You must provide an appropriate fixed mesh in xmesh. The cost depends strongly on the length of xmesh. When m > 0, it is not necessary to use a fine mesh near x = 0 to account for the coordinate singularity.

tspan

The elements of xmesh must satisfy x0 0 sech 1 u   2 dx 1  u dx



d 1 du coth 1 u  dx 1  u 2 dx



du d 1   1 1      csc u   , 0  csc u  csc 1 u   2 dx 2 2 u u  1 dx

ANMF.CH14_App_B_1PP.indd 1110

2/6/2023 5:28:53 PM

Basic Engineering Mathematics • 1111



du d 1        sec 1 u   , 0  sec 1 u  sec 1 u  dx 2 2 u u 2  1 dx



d 1 du cot 1 u  dx 1  u 2 dx



du d 1   0  vers1 u   vers1 u  2 dx dx 2u  u



d du sinh u = cosh u dx dx



d du cosh u = sinh u dx dx



d du tanh u = sec h 2 u dx dx



d du csc h u   csc h u coth u dx dx



d du sec h u   sec h u tanh u dx dx



d du coth u   csc h 2 u dx dx

B.3.2

Expansion in Series Exponential and Logarithmic Series x x2 x3 x4     .... [–∞ < x < +∞] 1! 2 ! 3 ! 4 !



ex  1 



a x  emx  1 

m m2 2 m3 3 x x  x  ...  [a > 0, –∞ < x < +∞] 1! 2! 3!

where   m = ln a = (2.302 6 × log10 a) x2 x3 x4 x5     ...   [–1 < x < +1] 2 3 4 5



ln(1  x)  x 



ln(1  x)   x 

ANMF.CH14_App_B_1PP.indd 1111

x2 x3 x4 x5     ...   [–1 < x < +1] 2 3 4 5

2/6/2023 5:29:21 PM

1112 • Applied Numerical Methods Using MATLAB



  x3 x 5 x7 1 x  ln    2  x     ...    [–1 < x < +1] 3 5 7 1x    1 1 1 1  x 1  ln    2   3  5  7  ...    [x < –1 or +1 < x]  x 1 x 3 x 5 x 7 x     n(n  1) 2 n(n  1)(n  2) 3 x  x  ..., | x |  1 2! 3!



(1  x)n  1  nx 



(1  x) n  1  nx 

n(n  1) 2 n(n  1)(n  3) 3 x  x  ..., | x |  1 2! 3!



(1  x) n  1  nx 

n(n  1) 2 n(n  1)(n  3) 3 x  x  ..., | x |  1 2! 3!



(1 + x)–1 = 1 – x + x2 – x3 + …. ∞, |x| < 1



(1 + x)–2 = 1 – 2x + 3x2 – 4x3 + …. ∞, |x| < 1



(1 – x)–1 = 1 + x + x2 + x3 + …. ∞, |x| < 1



(1 – x)–2 = 1 + 2x + 3x3 + 4x2 + …. ∞, |x| < 1



(1 – x)–1/2 = 1 +



1 1 1 2    ...   12 2 2 3 2 6



1 1 1 2    ...   12 3 2 52 8



1 1 1 4    ...   14 2 4 3 4 90



1 1 1 4    ...   14 3 4 5 4 96



 x  1 1  x  1 3 1  x  1  5  ln x  2         ....   [0 < x 1 1 2 x 1



x



 a  2bx  cx

2

dx

1

2

1



ac  b

2

tan 1

b2  ac  b  cx









a  bx dx 





dx 2 a  bx  C  a  bx b





b  cx ac  b

2

1

tanh 1

b  cx

 C 2 b  ac b  ac  b  cx b  ac b2  ac [b2 – ac > 0]



ln

2

(m  nx) dx a  bx



2

2 3b



a  bx



3

2

C

2 (3mb  2an  nbx) a  bx  C 3 b2 b  cx a  2 bx  cx 2 2c dx C a  2 bx  cx 2

a  2 bx  cx 2 dx  



ac  b2 2c



ax C ln a



x  a dx 



n ax  x e dx 



 ln x dx  x ln x  x  C







(ln x)n 1 n 1  x dx  n  1 (ln x)  C



 sin

ANMF.CH14_App_B_1PP.indd 1116

C

 C   [ac – b2 > 0]

x n eax a

n n(n  1) n!   1  ax  a 2 x 2  ....  a n x n   C

ln x ln x 1 dx    C 2 x x x

2

1 1 1 1 x dx   sin 2 x  x  C   sin x cos x  x  C 4 2 2 2

2/6/2023 5:31:44 PM

Basic Engineering Mathematics • 1117

1 1 1 1 x dx  sin 2 x  x  C  sin x cos x  x  C 4 2 2 2



 cos



 sin mx dx  



 cos mx dx 



 sin mx cos nx dx 

cos(m  n)x cos(m  n)x  C 2(m  n) 2(m  n)



 sin mx sin nx dx 

sin(m  n)x sin(m  n)x  C 2(m  n) 2(m  n)



 cos mx cos nx dx 

sin(m  n)x sin(m  n)x  C 2(m  n) 2(m  n)



 tan x dx   ln cos x  C



 cot x dx  ln sin x  C



 sin x  ln tan 2  C



 cos x  ln tan  4  2   C



 1  cos x  tan 2  C



 1  cos x   cot 2  C



 sin x cos x dx  2 sin



 sin x cos x  ln tan x  C



 a  b cos x  b  b  a  b cos x  C

ANMF.CH14_App_B_1PP.indd 1117

2

cos mx C m

sin mx C m

dx

x

dx



dx



x

dx

x

1

2

xC

dx

cos x dx

x

a

dx

2/6/2023 5:32:13 PM

1118 • Applied Numerical Methods Using MATLAB

sin x dx

1



 a  b cos x   b ln(a  b cos x)  C







(B sin u  C cos u)



where b – p cos u, c = p sin u and x – u = y.



e

ax

sin bx dx 

a sin bx  b cos bx ax e C a 2  b2



e

ax

cos bx dx 

a cos bx  b sin bx ax e C a 2  b2



 sinh x dx  cosh x  C



 tanh x dx  ln cosh x  C



 cosh x dx  sinh x  C



 coth x dx  ln sinh x  C

ANMF.CH14_App_B_1PP.indd 1118

dy cos y dy A  B cos x  C sin x dx  A   (B cos u  C sin u) a  b cos x  c sin x a  p cos y a  p cos y sin y dy , a  p cos y

2/6/2023 5:32:37 PM

APPENDIX

C

Cramer’s Rule Cramer’s Rule: Cramer’s rule can be used to solve a system of simultaneous linear algebraic equations. Consider a general system of n linear equations in n unknowns: a11x1 + a12x2 + …. + a1nxn = b1 a21x1 + a22x2 + …. + a2nxn = b2 





an1x1 + an2x2 + …. + annxn = bn

(C.1)

and define the determinant of such a system. We begin by defining the determinant of a 3 × 3 system:

a 11 C  a 21 a 31

a 12 a 22 a 32

a 13 a 22 a 23  a11 a 32 a 33

a 23 a 21  a12 a 33 a 31

a 23 a 21  a 13 a 33 a 31

a 22 (C.2) a 32

The general definition of the determinant of the n × n system of Equation (C.1) is simply an extension of the procedure (C.2).



ANMF.CH15_App_C_1PP.indd 1119

a 11 a 21 C=  a n1

a 12 a 22  an2

 a1n  a2n = a11A11 – a12A12 + …. + (–1) n+1a1nA1n(C.3)    a nn

2/10/2023 1:00:27 PM

1120 • Applied Numerical Methods Using MATLAB where A1j is the (n – 1) × (n – 1) determinant obtained by crossing out the first row and jth column of the original n × n determinant. Hence an n × 1 determinant can be obtained by calculating n (n – 1) (n – 1) determinants. We shall conclude this appendix by introducing a method for obtaining solutions of the system (C.1). We define the determinants:



b1 b2 C1 =  bn

a12 a 22  an2

 a1n  a2n ,    a nn

a 11 a 21

b1 b2



C2 =

 a n1

 bn

a 13  a 1 n a 23  a 2 n , ….,    a n 3  a nn



a11 a 21 Ck   a n1

a12 a 22  a n2

 a 1 , k  1 b1  a 2 , k 1 b 2     a n , k 1 b k



a 11 a 21 Cn   a n1

a 12 a 22  an2

 a 1 , n 1  a 2 , n 1    a n , n 1

a 1, k  1 a 2 , k 1  a n , k 1

 a1n  a2n , ….,    a nn

b1 b2 ,(C.4)  bn

obtained by replacing the kth column of C by the column



 b1  b   2      bn 

Then we have the following theorem, known as Cramer’s rule.

ANMF.CH15_App_C_1PP.indd 1120

2/10/2023 1:00:33 PM

Cramer’s Rule • 1121

Cramer’s: Let C and Ck, k = 1, 2, …., n, be given as in (C.3). If C ≠ 0, then the unique solution to the system (C.1) is given by the values

= x1

C2 C C1 = , x2 , ...., x n = n (C.5) C C C

EXAMPLE C.1 Obtain the solution of the following simultaneous linear equations by Cramer’s rule. 1 3   x1   5  a)        4 1  x 2  12   1 3 2   x 1  8  4 1   x 2   5 b)  3      4 2 9   x 3  2  Solution: a) x i =

ANMF.CH15_App_C_1PP.indd 1121

| Ci | |C|



5 3 12 1 5  36 x1    3.15 1 3 13 4 1



1 5 4 12 12  20 x2    0.62 1 3 13 4 1

2/10/2023 1:00:45 PM

1122 • Applied Numerical Methods Using MATLAB

b) x i =

| Ci | |C|



8 5 2 x1  1 3 4

3 4 2 3 4 2

2 1 9 441  7 2 63 1 9



1 3 4 x2  1 3 4

8 5 2 3 4 2

2 1 9 189   3 2 63 1 9



1 3 4 x3  1 3 4

3 4 2 3 4 2

8 5 2 252   4 2 63 1 9

EXAMPLE C.2 Consider the system 2x1 + 4x2 – x3 = –5 –4x1 + 3x2 + 5x3 = 14 6x1 + 3x2 – 2x3 = 5

ANMF.CH15_App_C_1PP.indd 1122

2/10/2023 1:01:05 PM

Cramer’s Rule • 1123

We have

2 4 1 5  112 C  4 3 6 3 2

5 4 1 5  224 C1  14 3 5 3 2



2 5 1 C2  4 14 5  112 6 5 2

2 4 5 C3  4 3 14  560 6 3 5

Therefore x 1 

C1 2 C

x2 

C2  1 C

x3 

C3 5 C

EXERCISES Solve the following system of linear equations using Cramer’s rule. 1.

x + 2y + z = 0



3x + y – z = 0



x – y + 4z = 3

2.

2x + y – 3z = 11



4x – 2y + 3z = 8



–2x + 2y – z = –6

3.

x + 3y – z = –10



–x + 4y + 2z = –4



2x – 2y + 5z = 35

4.

y – 3z = –5



2x + 3y – z = 7



4x + 5y – 2z = 10

ANMF.CH15_App_C_1PP.indd 1123

2/10/2023 1:01:25 PM

1124 • Applied Numerical Methods Using MATLAB

5.

 1 1 1 1   x 1   2   1 1 1 2   x   0     2      4 4 1 1   x 3  11    2 1 2 2   x 4   2 

6.

10 7  8  7

ANMF.CH15_App_C_1PP.indd 1124

7 8 7   x 1  32  5 6 5   x 2  23       6 10 9   x 3  33   5 9 10   x 4  31 

2/10/2023 1:01:32 PM

APPENDIX

D

MATLAB Built-In m-File Functions No. m-file name Description

Chapter/ Section

1.

chol

chol generates the Cholesky factorization. It has the general syntax U = chol(X) where U is an upper triangular matrix so that U´ ∗U = X.

9/

2.

trapz

trapz evaluates integral for the data. It has the general syntax z = trapz(x, y) where the two vectors x and y hold the independent and dependent variables, respectively.

7/7.3.2

3.

cumtrapz

cumtrapz computes the cumulative integral. It has the general syntax z = cumtrapz(x, y) where the vectors, x and y, hold the independent and dependent variables, respectively.

7/7.32

4.

fminbnd

fminbnd returns x that minimizes the function of a simple variable. A simple 11/11.14.1 expression of its syntax is (xmin, fval) = fminbnd (function, x1, x2) where x and fval are the locations and the value of the minimum, function is the name of the function being evaluated, and x1 and x2 are the bounds of the interval being searched.

5.

Fminsearch

fminsearch is used to determine the minimum of a multidimensional function. 11/11.14.2 It is based on the Nelder-Mead method which is a direct search method that uses only function values (derivatives not required) and handles nonsmooth objective function. Its syntax is [xmin, fval] = fminsearch(function, x1, x2) where xmin and fval are the location and the value of the minimum, function is the name of the function being evaluated, and x1 and x2 are the bounds of the interval being searched.

6.

interpol

Interpl provides a handy means to implement a number of different types of piecewise one-dimensional interpolation. It has the general syntax yi = interpl(x, y, xi, ‘method’) where x and y = vectors containing values that are to be interpolated, yi = a vector containing the results of the interpolation as evaluated at the point in the vector xi, and ‘method’ = the desired method. The different methods are: ‘nearest’: nearest neighbor interpolation. This method sets the values of an interpolated point to the value of the nearest existing data point. ‘linear’: linear interpolation. This method uses straight lines to connect the points. ‘spline’: piecewise cubic spline interpolations. This is exactly the same as the spline function. ‘pchip’ and ‘cubic’: piecewise cubic Hermite interpolation. The default is linear interpolation when the ‘method’ argument is omitted.

ANMF.CH16_App-D_1PP.indd 1125

5/5.8.8

2/15/2023 6:51:22 PM

1126 • Applied Numerical Methods Using MATLAB

No. m-file name Description

Chapter/ Section

7.

interpol2 interpol3

These functions interpol2 and interpol3 operate in a similar fashion to interpl. The syntax of interpol2 is zi = interpol2(x, y, z, xi, yi, ‘method’) where x and y = matrices containing the coordinates of the points at which the values in the matrix z are given, zi = a matrix containing the results of the interpolation as evaluated at the points in the matrices xi and yi, and ‘method’ = the desired method. The methods are identical to those used in interpl, that is, linear, nearest, spline and cubic.

6/6.11

8.

polyfit

Polyfit fits a least-squares nth-order polynomial to data. It can be applied as in p = polyfit(x, y, n) where x and y are the vectors of the independent and dependent variables, respectively, and n = the order of the polynomial. The function returns a vector p containing the polynomial’s coefficients.

6/6.21

9.

polyval

Polyval can be used to compute a value using the coefficient. It has the general format: y = polyval(p, x) where p = the polynomial coefficients, and y = the best-fit value at x.

6/6.21

10.

ode23

The ode23 function uses the algorithm (Bogacki and Shampine, 1989, Shampine, 1994), which simultaneously uses second- and third-order RK formulas to solve the ODE and make error estimates for step-size adjustment.

8/8.4

11.

ode45

The ode45 functions uses an algorithm developed by Dormand and Prince (1990), which simultaneously uses fourth- and fifth-order RK formulas to solve ODE and make error estimates for step-size adjustment.

8/8.4

12.

ode113

The ode113 function uses a variable order Adams-Bashforth-Moulton solver. It is useful for stringent error tolerances or computationally intensive ODE functions. This is a multi-step method.

8/8.4

13.

roots

The roots function has the syntax, x = roots(c) where x is a column vector containing the roots and c is a row vector containing the polynomial’s coefficients.

3/3.13

14.

lu

The function lu generates the LU factorization. It has the general syntax: [L, U] = lu(X) where L and U are the lower and upper triangular matrices, respectively, derived from the LU factorization of the matrix X.

2/2.7

15.

‘leftdivision’ MATLAB provides two direct ways to solve systems of linear algebraic operations. The most efficient way is to employ the backslash, or ‘left-division,’ operator as the x = A\b. The second is to use matrix inversion x = inv(A)*b, where [A] is the matrix of coefficients and {b} is the column vector of constants.

2/2.4

16.

norm and cond

MATLAB has built-in functions to compute both norms and condition numbers: norm (X, p) and cond(X, p) where X is the vector or matrix and p designates the type of norm or condition number (1, 2, inf, or ‘fro’). The cond function is equivalent to norm(X, p)∗norm(inv(X), p).

2/2.3

17.

spline

Cubic splines are computed with the built-in MATLAB function, spline. It has the general syntax, yy = spline(x, y, xx) where x and y = vectors containing the values that are to be interpolated, and yy = a vector containing the results of the spline interpolation as evaluated at the points in the vector xx. By default, spline uses the not-a-knot condition. Although, if y contains two or more values than x has entries, then the first and last value in y are used as the derivatives at the end points. Consequently, this option provides the means to implement the clamped-end - condition.

5/5.8.6

18.

diff

When it is passed a one-dimensional vector of length n, the diff function returns a vector of length n – 1 containing the difference between adjacent elements. These can be employed to determine finite-difference approximations of derivatives.

4/4.9

ANMF.CH16_App-D_1PP.indd 1126

2/15/2023 6:51:23 PM

MATLAB Built-In m-File Functions • 1127

No. m-file name Description

Chapter/ Section

19.

gradient

The gradient function also returns differences. However, it does so in a fashion that is more compatible with evaluating derivatives at the values themselves rather than in the intervals between values. A simple representation of its syntax is fx = gradient(f) where f = a one-dimensional vector of length n, and fx is a vector of length n containing differences based on f. Just like with the diff function, the first value returned is the difference between the first and second value. If the vector represents equally spaced data, the following version divides all the results by the interval and hence returns the actual values of the derivatives. fx = gradient(f, h) where h = the spacing between points.

4/4.9

20.

eig

The function eig can be used to generate a vector of the eigenvalues in e = eig(A) where e is a vector containing the eigenvalues of a square matrix A. Alternatively, it can be invoked as [V, D] = eig(A) where D is a diagonal matrix of the eigenvalues and V is a full matrix whose columns are the corresponding eigenvectors.

3/

21.

quad quad1

The quad and quad1 commands are used for the integration when f(x) is a function, while the trapz and cumtrapz are used when f(x) is given by data points. The form of the quad command which uses the adaptive Simpson’s method of integration is given by q = quad(function, a, b) where q= the value of the integral, function = the function to be integrated, a, b = the integration limits. The function can be entered as a string expression, or as a function handle. The function f(x) must be written for argument x that is a vector (use element-by-element operations), so that it computes the value of the function for each element of x. quad calculates the integral with an absolute error that is smaller than 1.0e–6. This number can be changed by adding an optional tol argument to the command. q = quad(‘function,’ a, b, tol), tol is a number that defines the maximum error. With larger tol the integral is computed less accurately, but faster. The form of the quad1 command is exactly the same as the quad command: q = quad1(function, a, b) where q = the value of the integral, function = the function to be integrated, a, b = the integration limits. The only difference between the two commands is in the numerical method that is used for computing the integration. The quad command uses the adaptive Simpson method whereas quad1 command uses the adaptive Lobatto method, which can be more efficient for high accuracies and smooth integrals.

7/7.4.3

22.

fzero

In MATLAB, a zero of a function can be determined with the command (built-in function) fzero that has the form: x = fzero(function , x0), where x is the solution, and function is the function to be solved. It can be entered in several different ways: enter the mathematical expression as a string; the function is first created as a user defined function in a function file and then the function handle is entered; the function is first created as an anonymous (which is the name of the handle) is entered. The function has to be written in a standard form. When a function is entered as an expression (string), it cannot include predefined variables. x0 = a value of x near to where the function crosses the x axis. x0 can be a scalar or a two-element vector. If it is entered as a scalar, it has to be a value of x near the point where the function crosses the x axis. If x0 is entered as a vector, the two elements have to be points on opposite sides of the solution that f(x0(1)) has a different sign than f(x0(2)). When a function has more than one solution, each solution can be determined separately by using the fzero function and entering values for x0 that are near each of the solutions.

3/3.13

ANMF.CH16_App-D_1PP.indd 1127

2/15/2023 6:51:23 PM

1128 • Applied Numerical Methods Using MATLAB

No. m-file name Description

Chapter/ Section

23.

Poisson’s equation: [alpha, r, x, y, U] = poisson(a, b, m, n, q, tol, f, g) (float a, float b, int m, int n, int q, float tol, float ∗alpha, vector x, vector y, matrix U, float(∗f)(float x, float y)) One-dimensional heat equation: (t, x, U) = heat1(T, a, m, n, beta, f, g) (float T, float a, int m, int n, float beta, float c1, float c2, vector x, vector y, matrix U, float(∗f)(float x, float(∗f), float x)) Two-dimensional heat equation: (x, y, U) = heat2(T, a, b, m, n, p, beta, f, g) (float T, float a, float b, int m, int n, int p, float beta, vector x, vector y, matrix U, float(∗f)(float x, float y), float(∗g)(float t, float x, float y)) One-dimensional wave equation: (t, x, U) = wave1(T, a, m, n, beta, f, g) (float T, float a, int m, int n, float beta, vector t, vector x, matrix U, float(∗f)(float x), float(∗g), float x)) Two-dimensional wave equation: (x, y, U) = wave2(T, a, b, m, n, p, beta, f, g) (float T, float a, float b, int m, int n, int p, float beta, vector x, vector y, matrix U, float(∗f)(float x, float y), float(∗g)(float x, float y))

12/12.7

PDE Solver pdepe - Solve initial-boundary value problems for parabolic-elliptic PDEs in 1-D. Syntax: Sol = pdepe(m, pdefun, icfun, befun, xmesh, tspan) sol = pdepe(m, pdefun, icfun, befun, xmesh, tspan, options) Arguments: m A parameter corresponding to the symmetry of the problem. m can be slab = 0, cylindrical = 1, or spherical = 2. pdefun A handle to a function that defines the components of the PDE. icfun A handle to a function that defines the initial conditions. befun A handle to a function that defines the boundary conditions. xmesh A vector [x0, x1, …, xn] specifying the points at which a numerical solution is requested for every value in tspan. The elements of xmesh must satisfy x0 < x1 < … < xn. The length of xmesh must be >= 3. tspan A vector [t0, t1, …, tf] specifying the points at which a solution is requested for every value in xmesh. The elements of tspan must satisfy t0 < t1 < … < tf. The length of tspan must be >= 3. options Some options of the underlying ODE solver are available in pdepe: RelTol, AbsTol, NormControl, InitialStep, and MaxStep. In most cases, default values for these options provide satisfactory solutions. See odeset for details.

12/12.11

poisson

heatone

heattwo

waveone

24.

pdepe

ANMF.CH16_App-D_1PP.indd 1128

2/15/2023 6:51:23 PM

APPENDIX

E

MATLAB Programs The following table is a summary of the major MATLAB programs (script files and user-defined functions) that are listed and used in the book. Program number

Program name or title

File type

Description

Chapter/ Section

2-1

[x, det] = Gauss(A, b)

Script file

Solves a system of linear simultaneous algebraic equations of the form Ax = b by using the Gauss elimination method and also computes the determinant of matrix A.

2/2.5.1

2-2

x = Gaussjordan(A, b)

Script file

Solves a system of linear simultaneous algebraic equations of the form Ax = b by using the Gauss Jordon method.

2/2.6.1

2-3

[L, U] = Crout(A)

Script file

Uses Crout’s method of decomposition such that A = LU, where L = lower triangular matrix and U = upper triangle matrix.

2/2.8.1

2-4

x = Thomas(a, d, b, c)

Script file

Solves the matrix equation Ax = c, where A is a tridiagonal matrix, a is upper diagonal of matrix A, b is lower diagonal of A, c is right hand side of equation Ax = c, and d is diagonal of A.

2/2.9.1

2-5

[x, k, diff] = Jacobi (A, b, x0, tol, kmax)

Script file

Jacobi iteration method to solve a system of linear equations of the form Ax = b.

2/2.10.1

2-6

[y, iter] = Gausseidel (A, r, yguess, tol)

Script file

Gauss-Seidel iteration method to solve a system of linear equations of the form Ay = r.

2/2.10.1

2-7

[evals, evecs] = Jacobie (A, tol)

Script file

Jacobi method for finding the eigenvalues and eivenvectors of a symmetric matrix A. tol is the error tolerance (default value is 1.0 × 10− 9).

2/2.12.2

ANMF.CH17_App-E_3PP.indd 1129

2/15/2023 5:49:05 PM

1130 • Applied Numerical Methods Using MATLAB

Program number

Program name or title

File type

Description

Chapter/ Section

2-8

A = Householdereigen(A)

Script file

Householder method of reduction of matrix A to tridiagonal form A = [c\d\c]; extracts c and d with d = diag (A) and c = diag (A, 1).

2/2.12.2

2-9

p = Strunsequence (c, d, lambda)

Script file

Returns strum sequence p associated with the tridiagonal matrix A = [c\d\c] and lambda. |A − lambda ∗ I| = p (n); n = length (d) + 1.

2/2.12.2

2-10

numevals = Countlambda (c, d, lambda)

Script file

Counts eigenvalues smaller than lambda of matrix A = [c\d\c]. Uses strum sequence.

2/2.12.2

2-11

[evalmin, evalmax] = Gerschgorin (c, d)

Script file

Evaluates the global bounds on the eigenvalues of A = [c\d\c] using Gerschgorin’s theorem

2/2.12.2

2-12

evals = Eigenbrakets (C, D, m)

Script file

Computes the smallest m eigenvalues of A = [C\D\C]

2/2.12.2

2-13

f = func(eval)

Userdefined function

Returns |A − eval ∗ I|; the last element of strun sequence

2/2.12.2

2-14

root = Brent(func, a, b, tol)

Script file

Finds a root of f(x) = 0 by combining quadratic interpolation with Brent’s method

2/2.12.2

2-15

[eval, evec] = Invpowerthree (c, d, s, maxiter, tol)

Script file

Finds the eigenvalue of A = [c\d\c] closest to s and the associated eigenvector by using inverse power method.

2/2.12.2

2-16

[evals, evecs] = Sorteigenvalues (evals, evecs)

Userdefined function

Sorts eigenvalues and eigenvectors in an ascending order of eigenvalues.

2/2.12.2

2-17

[H, T] = Standform(A, B)

Userdefined function

Transforms Ax = lBx to Hz = lz and finds the transformation matrix T in x = Tz

2/2.12.2

2-18

[eval, evec] = Invpower (A, s, maxiter, tol)

Script file

Inverse power method of finding the eigenvalues of A and the corresponding eigenvectors.

2/2.12.2

3-1

root = Bisection (func, x1, x2, filter, tol)

Script file

Finds a bracketed root of f(x) = 0 by the Bisection method

3/3.2.2

3-2

[root, ea, iter] = Falsep (Func, xl, xu, es, maxit, varargin)

Script file

False position method to find a root of a given function f(x) = 0. func is the function name, xl, xu are the lower and upper guesses, es is the desired relative error (0.00001%), maxit is the maximum allowable iterations (60).

3/3.3.1

ANMF.CH17_App-E_3PP.indd 1130

2/15/2023 5:49:05 PM

MATLAB Programs • 1131

Program number

Program name or title

File type

Description

Chapter/ Section

3-3

root = NewRap(func, dfunc, a, b, tol)

Script file

Newton-Raphson method of finding a root of f(x) = 0. func is the handle of the function that returns f(x), dfunc is the handle of function that returns f´(x), a and b are the brackets (limits) of the root, tol is the error tolerance (1.0 × 10− 6).

3/3.4.3

3-4

[x1, x2] = Rootsearch (func, a, b, dx)

Userdefined function

Successive root search method for a root of f(x). func is the handle of function, a and b are the limits of search, dx is the search increment.

3/3.6.2

3-5

root = Secant(func, xold, x, es, maxiter)

Script file

Secant method to find the root of a function f(x) = 0. func is the name of function, xold, x are the initial guesses, es is the stopping criterion, and maxiter is the maximum allowable iterations.

3/3.6.3

3-6

[x, k] = Muller (x0, x1, x2, tol, maxiter, f, dm)

Script file

Muller’s method to find a root of f(x) = 0. x0 is the first initial guess, x1 is the second guess, x2 is the third guess, tol is the error tolerance to terminate the search, maxiter is the maximum number of iterations, f is the usersupplied function y = f(x), and dm is the optional display mode. If input, intermediate results are displayed.

3/3.7.1

3-7

root = Newton2(func, x, tol)

Script file

Newton’s method for finding a root of simultaneous equations fi(x1, x2, x3, …, xn) = 0, i = 1, 2, …, n. func is the handle of function that returns [f1, f2, …, fn], x is the initial solution vector(x1, x2, …, xn], tol is the error tolerance (1 × 10− 4), and root is the solution vector.

3/3.10.1

4-1

[dydx, d2ydx2] = Diffeq (x, y)

Script file

MATLAB program for derivatives based on Newton’s forward integration formula-equally spaced points.

4/4.2.1

5-1

yint = Newtonint (x, y, xx)

Script file

Newton’s interpolation method based on n data points of (x, y). x and y are the independent and dependent variables, xx is the independent variable value at which the interpolated value is determined.

5/5.3.3.1

5-2

yint = Lagrangeint (x, y, xx)

Script file

Lagrange’s interpolation method based on n data points (x, y). x and y are the independent and dependent variables, xx is the independent variable value at which the interpolated value is determined.

5/5.4

7-1

intf = Simpsonint (f, a, b, n, varargin)

Script file

Integral of f(x) over [a, b] by Simpson rule with n segments. eps = 1.0 × 10− 10.

7/7.4.2

ANMF.CH17_App-E_3PP.indd 1131

2/15/2023 5:49:06 PM

1132 • Applied Numerical Methods Using MATLAB

Program number

Program name or title

File type

Description

Chapter/ Section

7-2

[Int, neval] = Romberg (Func, a, b, tol, kmax)

Script file

Romberg integration of f(x) over [a, b] with tol = 1 × 10− 8, and kmax is the max number of panel doublings (default is 20), Func is the handle of function.

7/7.7.3

8-1

y = Euler (a, b, y0, n, func)

Script file

Euler’s method to solve the first-order ordinary differential equation y´ = func(x, y). a and b are the input values of x(a ≤ x ≤ b), y0 is the initial value of y, n is the number of steps, and func is the handle of the function.

8/8.3.2

8-2

y = ModEuler(dydx, n, xf, x0, y0)

Script file

Modified Euler method integrates a set of first-order differential equations of the form y´ = dydx(x, y) with initial conditions y(x0) = y0. The equations are integrated in n steps from x0 to xf.

8/8.3.3

8-3

[xsolution, ysolution] = RK4(eqn, x,y, xf, h)

Script file

Fourth-order Runge-Kutta integration method. eqn is the handle of the function that specifies the first-order differential equations; x, y are the initial values and y must be a row vector. xf is the terminal value of x and h is the increment of x used in integration.

8/8.3.3.3

11-1

[a, b] = Goldbracket (func, x1, h)

Script file

Brackets the minimum point of f(x); func is the handle of function that returns the value of f(x), x1 is the starting value of x, h is the initial step size for use in search. a and b are the limits on x obtained at minimum point.

11/11.4.5

11-2

[xmin, fmin] = Goldsearch (func, a, b, tol)

Script file

Golden section second method for finding the minimum of f(x). The minimum point is bracketed in (a ≤ x ≤ b); func is the handle of the function that returns the value of f(x), (a, b) are the limits of the interval containing the minimum point, and tol is the error tolerance (1 × 10− 6); fmin is the minimum value of f(x); xmin is the value of x obtained at the minimum point.

11/11.4.6

11-3

[x, fx, xx] = Newtons (f, x0, tolx, maxiter, varargin)

Script file

Newton’s method to solve a set of nonlinear equations; f is the first-order vector function equivalent to a set of equations; x0 is the initial guess of the solution; tolx is the upper limit of |x(k) − x(k − 1)|; maxiter is the maximum number of iterations; x is the point which the method has found fx = f[x(last)]; xx is the history of x.

11/11.7.1

ANMF.CH17_App-E_3PP.indd 1132

2/15/2023 5:49:06 PM

MATLAB Programs • 1133

Program number

Program name or title

File type

Description

Chapter/ Section

11-4

g = Jacob(f, x, h, varargin)

Userdefined function

Finds the Jacobian of the function f(x).

11/11.7.1

11-5

[xmin, fmin, ncyc] = Powell (h, tol)

Script file

Powell’s optimization method. h is the initial search increment (0.1) tol is the error tolerance (1 × 10− 6); x is the starting point; func is the function that returns f value; xmin is the minimum point; fmin is the minimum value of f; ncyc is the number of cycles for convergence.

11/11.9.1

Script file

Fletcher-Reeves optimization method for minimizing f(x1, x2, …., xn); h is the initial search increment value (0.1); tol is the error tolerance (1 × 10− 6); x is the starting point; FUNC is the handle of function that returns f; DFUNC is the handle of function that returns grad(f); xmin is the optimum or minimum point; fmin is the minimum value of function f; ncyc is the number of cycles for convergence.

11/11.10.1

Global func v, x

[xmin, fmin, ncyc] = Fletcherreeves(h, tol) Golbal x, FUNC, DFUNC V

11-6

NOTE

In addition to MATLAB programs listed and described in Appendix E table, there are several user-defined functions and/or script files written in Chapter 9, “Direct Numerical Integration Methods” for solving a set of differential equations. They are listed below:

a. Central difference method for single degree of freedom system b. Fourth-order Runge-Kutta method for a single degree of freedom system c. Central difference method for a two degree of freedom system d. Trapezoidal rule for a two degree of freedom system e. Fourth-order Runge-Kutta method for a two degree of freedom system f. Houbolt method for a two degree of freedom system g. Wilson-Theta method for a two degree of freedom system h. Newmark-Beta method for a two degree of freedom system i. Park stiffly stable method for two degree of freedom system

ANMF.CH17_App-E_3PP.indd 1133

2/15/2023 5:49:06 PM

ANMF.CH17_App-E_3PP.indd 1134

2/15/2023 5:49:06 PM

APPENDIX

F

Answers to Odd Numbered Exercises CHAPTER 1: NUMERICAL COMPUTATIONS 1.1 (a)  1.0215  (b)  1.405 1.3 ƒ (1.2) = 1.44 1.5 50824.6135 1.7 (75)10 = (1001 1011)2 1.9 (1235)10 1.11 Erxy = Erx + Ery 1.13 (a) Absolute error, ξa = 0.666…×10−6; relative error, ξr = 1 × 10−6 (b)  Absolute error, ξa = 0.33 × 10−7; relative error, ξr = 5 × 10−7 1.15 (a) Absolute error, EA = 0.004, relative error ER = 1.1312 × 10−3 (b)  Absolute error, EA = 0.006, relative error ER = 1.6968 × 10−3 1.17 Absolute error = 0.002857; Relative error = 0.0009, Percentage relative error = 9% 1.19 Percentage relative error = 9.02%; Approximate estimate of error = 33.3% With 6 terms included approximate error falls below Es = 0.05%

ANMF.CH18_App-F_1PP.indd 1135

2/24/2023 5:36:29 PM

1136 • Applied Numerical Methods Using MATLAB

x 3 x 5 x7    . 3 ! 5! 7 ! (b)  Relative error, r6 = 0.005129% 1.21 (a)  f ( x)  x 

(c)  Upper bound on the truncation error = 0.02305% 1.23 Upper bound on the error |R3(x)| ≤ 0.0625 for all x ∈ [0,1) 1.25 Order, n

F(n)(x)

F(π/3)

εt

0

cos x

0.707106781

−41.4

1

−sin x

0.521986659

−4.4

2

−cos x

0.497754491

0.449

3

sin x

0.499869147

2.62 × 10−2

4

cos x

0.500007551

−1.51 × 10−3

5

−sin x

0.500000304

−6.08 × 10−5

6

−cos x

0.499999988

2.4 × 10−6

1.27 (a) Condition number = −1 (b)  Condition number = −0.994975 (c)  Condition number = −0.00314161 1.29 (a) to (d) will not converge quadratically

CHAPTER 2: LINEAR SYSTEM OF EQUATIONS 0.2 0.3   0.7   0.7  2.1 (a) A   1.3 0.2  0.8 0.2 0.2  1

0.3529 0.2353  0.1765   (b)  A  0.4118 0.1765 0.1176  0.0588 0.1176 0.4118  1

0.0496 0.0583  0.0136   (c)  A  0.1886 0.2233 0.0124  0.0298 0.0174 0.0546  1

ANMF.CH18_App-F_1PP.indd 1136

2/24/2023 5:36:45 PM

Answers to Odd Numbered Exercises • 1137

0 0.1429   0   (d)  A   2 1 0.2857  1.6667 0.6667 0.1429  1

1 0   2   (e)  A   0.3333 0.6667 0.3333   0.6667 0.3333 0.3333  1

0 3 3  1  (f)  A  3 2 7  9 3 1 1  1

2.3 (a) x = 3, y = −1, z = −2 (b)  x = 1, y = 2, z = 3 (c)  x = 1, y = 1, z = 1 (d)  x1 = 2.7869, x2 = 4.4918, x3 = 2.1311, x4 = −2.5410 (e)  x1 = 25.3587, x2 = −19.6143, x3 = −28.9058, x4 = −7.8027 (f)  x1 = −1, x2 = 1, x3 = 2, x4 = 1 2.5 (a) x = 2, y = 1, z = 3 (b)  x = 3, y = 2, z = 2 (c)  x = 1, y = 0, z = 3 (d)  x = 35/18, y = 29/18, z = 5/18 (e)  x = 1, y = 1, z = 2 (f)  x1 = 2.7778, x2 = 4.2222, x3 = −0.5556, x4 = 6.8889 2.7 (a) x1 = 1, x2 = 2, x3 = 3 (b)  x1 = 1, x2 = 1, x3 = 1, x4 = 2 (c)  x1 = 1, x2 = 1, x3 = 1, x4 = 2 (d)  x1 = 1, x2 = 1, x3 = 1, x4 = 2 (e)  x1 = 1, x2 = −1, x3 = 1, x4 = −1 (f)  x1 = −1, x2 = 1, x3 = 1, x4 = −1

ANMF.CH18_App-F_1PP.indd 1137

2/24/2023 5:36:55 PM

1138 • Applied Numerical Methods Using MATLAB 2.9 (a) x = 4, y = −1, z = 3 (b)  x = 1, y = 2, z = 3 (c)  x = 5, y = 4, z = 1 (d)  x1 = 0.999, x2 = 1.9999, x3 = 2.9999, x4 = −0.0001 (e)  x1 = 1.155, x2 = −0.311, x3 = 0.088, x4 = −0.044 (f)  x1 = 1, x2 = 0, x3 = −1, x4 = 4 2.11 λ1 = 3 and λ2 = 2

Eigenvectors:





x1  0.4472 0.8944 



and

x2 0.8944 0.4472 

T

T

2.13 λ1 = 2.6916, λ2 = 6.6956 and λ3 = −1.3872

Eigenvectors:

0.211  0.764 0.610   0.592 0.518  0.617 0.191 0.526 0.829 

3.606 0   1   2.15 A  PAP  3.606 1.692 0.5385   0 0.5385 4.308  2.17 d  7 10.643 10.594 5.763 

c  3.742 9.131 4.772 



0 0 0  1 0 0.5345 0.2551 0.8057   P 0 0.8018 0.484 0.5789    0 0.2673 0.99555 0.1212 

ANMF.CH18_App-F_1PP.indd 1138

2/24/2023 5:37:10 PM

Answers to Odd Numbered Exercises • 1139

2.19 λ1 = 2.6916, λ2 = 6.6956 and λ3 = −1.3872

Eigenvectors: 0.211  0.764 0.610   0.592 0.518  0.617 0.191 0.526 0.829 

2.21 λ1 = 73.15 MPa, λ2 = 80 MPa and λ3 = 106.85 MPa 2.23 (a)   (b)

–32 –24

–16

–8

8

16

24

32

–32

–24

–16

–8

8

16

24

32



FIGURE P2.23(a) 

FIGURE P2.23(b) 

2.25 Eigenvectors:

Y1   0.5001  0.8018  0.3229  0.05242 T Y2   10.5003 0.0004696 0.6884 0.5252 T Y3   0.4996 0.2671 0.2385  0.7888 T Y4   0.5 0.5345  0.6042 0.3150 T



2.27 λ1 = 9, λ2 = −9 and λ3 = 18 2.29 70.944 0    2.31 λ = 2.00 with vector x  0  1  2.33 70.944

ANMF.CH18_App-F_1PP.indd 1139

2/24/2023 5:37:15 PM

1140 • Applied Numerical Methods Using MATLAB

CHAPTER 3: SOLUTION OF ALGEBRAIC AND TRANSDENTAL EQUATIONS 3.1 4.4932

3.37 −0.70346

3.3 1.51092

3.39 2.0

3.5 2.7119

3.41 3.051

3.7 0.3807

3.43 −1.3247

3.9 7.7253

3.45 4.217163

3.11 0.73909

3.47 4.730041

3.13 2.7392

3.49 1.13472

3.15 2.798

3.51 7.06858

3.17 0.5177

3.53 2.85234

3.19 1.488

3.55 2.95

3.21 0.7346

3.57 2.1080

3.23 2.1163

3.59 0.68

3.25 With x0 = 0, the root is 0.96434

3.61 0.7710

3.27 4.4934

3.63 2.0739

3.29 0.56714

3.65 7.000

3.31 0.6071

3.67 (a) 4.75

3.33 3.7892

(b) 4.73

3.35 1.172

ANMF.CH18_App-F_1PP.indd 1140

2/24/2023 5:37:16 PM

Answers to Odd Numbered Exercises • 1141

CHAPTER 4: NUMERICAL DIFFERENTIATION 4.1 (a)  y′(1.0) = 3.0; y″(1.0) = 6.0 (b)  y′(1.0) = 1.0; y″(1.0) = 4 (c)  y′(1.0) = 5.0004; y″(1.0) = 16 (d)  y′(1.0) = −8.2002; y″(1.0) = −23.6 (e)  y′(1.0) = −7.6009; y″(1.0) = −22.4 (f)  y′(1.0) = 1.6002; y″(1.0) = 5.2 (g)  y′(1.0) = 7.7021; y″(1.0) = 19.4040 4.3 (a) y′(6.0) = −1.68; y″(6.0) = −0.3520 (b)  y′(6.0) = −2.0537; y″(6.0) = −0.7126 4.5 y′(4) = 12.75; y″(4) = 9.75 4.7 (a) y′(3) = 1.7263; y″(3) = 4.6309 (b)  y′(3) = 0.0958; y″(3) = 0.0367 4.9 (a) y′(1.2) = 0.7917; y″(1.2) = 6.9667 (b)  y′(1.2) = 0.1193; y″(1.2) = −0.7767 (c)  y′(1.2) = 2.8964; y″(1.2) = −18.3050 4.11 ƒ′(3.4) = 0.12864; ƒ′(5.2) = −0.00836; ƒ′(5.6) = 3.930432 4.13 ƒ′(2) = 14; ƒ′(2.5) = 20.75 4.15 y′(2) = 2.4667; y″(2) = 6.9667 4.17 y′(1.2) = 0.1193; y″(1.2) = −0.7767 4.19 110.13334 4.21 −52, 500

ANMF.CH18_App-F_1PP.indd 1141

2/24/2023 5:37:16 PM

1142 • Applied Numerical Methods Using MATLAB 4.23 16 14

12

10

df/dx

8

6

4

2

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

x

FIGURE P4.23 

4.25

FIGURE P4.25 

ANMF.CH18_App-F_1PP.indd 1142

2/24/2023 5:37:16 PM

Answers to Odd Numbered Exercises • 1143

4.27 –3.9 –4 –4.1 –4.2 –4.3 df/dx –4.4 –4.5 –4.6 –4.7 –4.8 –4.9

0

0.1

0.2

0.3

0.4

0.5 x

0.6

0.7

0.8

0.9

1

FIGURE P4.27 

CHAPTER 5: FINITE DIFFERENCES AND INTERPOLATION 5.3 (a) (b) x

y

45

20

Δy

Δ2y

Δ3y

x

y

40

204

40 55

60

20

120

50

224

–20

180

Δ3y

Δ4y

0

2 22

60

246

60 75

Δ2y

20

60 65

Δy

0 2

24 70

270

2 26

80

0 0

296

0 0

2 28

90

ANMF.CH18_App-F_1PP.indd 1143

324

2/24/2023 5:37:16 PM

1144 • Applied Numerical Methods Using MATLAB 5.5 −49

5.33 −4

5.7 (a) 35

5.35 3.54672

(b)  77

5.37 2.5

(c)  6

5.39 38

5.9 (a)  ƒ (x) = 3x (3) + 10x (2) + 5x (1) + 1

5.41 32495.1328



(4)

(b)  ƒ (x) = x + x − x (1) + 4

(3)

− 8x

5.43 36.05469 (2)

5.45 24.46875 5.47 36.625

5.11 (a) 11

5.49 478.375

(b)  37

5.51 y1.6 = 0.36663

(c)  −33

5.53 1.11864

(d)  −25

5.55 31846.8528

(e)  38

5.57 1.54397

5.13 112.32097

5.59 46614.25781

5.15 0.66913

5.61 −5.83

5.17 48

5.63 1.6778 2

5.19 y = ƒ(x) = x + 3x + 1

5.65 5.528961

5.21 8.875

5.67 ƒ(1.5) = 0.7679

5.23 ƒ(0.12) = 0.1197; ƒ(0.26) = 0.2571

5.69 ƒ2, 3(0) = 0.7212

5.25 0.0000392 5.27 y = x3 − 8x2 − 20 5.29 y = x3 − x2 + 3x + 2 5.31 200

ANMF.CH18_App-F_1PP.indd 1144

5.71 ƒ (3.4) = −0.02856

ƒ′(5.2) = −0.0927



ƒ(5.6) = 3.9065

5.73 y (2.6) = 0.18714

2/24/2023 5:37:17 PM

Answers to Odd Numbered Exercises • 1145

5.75 (a) 

FIGURE P5.75 (a)

(b)

FIGURE P5.75(b)

ANMF.CH18_App-F_1PP.indd 1145

2/24/2023 5:37:17 PM

1146 • Applied Numerical Methods Using MATLAB (c)

FIGURE P5.75(c)

5.77 (a) 1

0.5

0

–0.5 –1

–0.8

–0.6

–0.4

–0.2

0

0.2

0.4

0.6

0.8

1

FIGURE P5.77(a)

ANMF.CH18_App-F_1PP.indd 1146

2/24/2023 5:37:17 PM

Answers to Odd Numbered Exercises • 1147

(b)

FIGURE P5.77(b)

(c)

FIGURE P5.77(c)

ANMF.CH18_App-F_1PP.indd 1147

2/24/2023 5:37:18 PM

1148 • Applied Numerical Methods Using MATLAB 5.79 (a)

Figure P5.79(a)

(b) 1.2 1 0.8 0.6 0.4 0.2 0 –0.2 –1

–0.8

–0.6

–0.4

–0.2

0

0.2

0.4

0.6

0.8

1

Figure P5.79(b)

ANMF.CH18_App-F_1PP.indd 1148

2/24/2023 5:37:18 PM

Answers to Odd Numbered Exercises • 1149

(c)

FIGURE P5.79(c)

5.81 not-a-knot

1 0.5

0

00

2

1

4 clamped

6

8

–0.05

0

2

4

6

8

6

8

6

8

error

0.05

0.5 0

error

0.05

0 0

2

4

6

8

0

2

pchip

1

4 error

0.2

0.5 00

–0.05

0 2

4

6

8

–0.2

0

2

4

Figure P5.81(c)

ANMF.CH18_App-F_1PP.indd 1149

2/24/2023 5:37:19 PM

1150 • Applied Numerical Methods Using MATLAB

CHAPTER 6: CURVE FITTING, REGRESSION AND CORRELATION 6.1 a = −22.0007, b = 3.7484 6.3 a = 0.4449, b = 0.2747 6.5 a = 22.917, b = 3.3883 6.7 a = 80.7777, b = 1.138 6.9 a = 1.1574, b = 0.2566 6.11 (a) a = −22.0007 and b = 3.7484

6.15 (a) a = 22.917 and b = 3.3883 (b)  se = 5.6669 (c)  SSE = 256.9085 (d)  SST = 4573.6 (e)  SSR = 4316.6915 (f)  r2 = 0.9438

(b)  se = 30.8722

(g)  r = 0.9715

(c)  SSE = 7624.7219



(d)  SST = 23973.6 (e)  SSR = 16348.8781 (f)  r2 = 0.682 (g)  r = 0.8258

   when x = 42, y = 135.4307

6.13 (a) a = 0.4449 and b = 0.2747

   when x = 15, y = 73.7415

6.17 (a) a = 80.7777 and b = 1.138 (b)  se = 7.0176 (c)  SSE = 492.4669 (d)  SST = 2500.6667 (e)  SSR = 2008.1998 (f)  r2 = 0.8031

(b)  se = 1.0550

(g)  r = 0.8961

(c)  SSE = 5.5648



(d)  SST = 66 (e)  SSR = 60.4352 (f)  r2 = 0.9157 (g)  r = 0.9569

   when x = 42, y = 11.9825

ANMF.CH18_App-F_1PP.indd 1150

   when x = 45, y = 131.988

6.19 (a) a = 1.1574 and b = 0.2556 (b)  se = 1.0438 (c)  SSE = 5.4473 (d)  SST = 58.2143 (e)  SSR = 52.767

2/24/2023 5:37:19 PM

Answers to Odd Numbered Exercises • 1151

(f)  r2 = 0.9064

6.21 b = 0.8671 and m = 1.3471

(g)  r = 0.9521

6.23 b = 0.3990 and m = 0.9057



   when x = 41, y = 11.6778

6.25 b = 18.9926 and m = 0.4983

6.27 a = −291.9285, b = 417.3647 and c = −146.3105; St = 8.2232;

Sr = 0.0406; r = 0.9975; Sy/x = 0.1163

6.29 a = 2.5095, b = 1.2, c = 0.7333, Sy/x = 0.2024, St = 332.0571,

Sr = 0.1638, r2 = 0.9995, r = 0.9998

6.31 a = 14.4217, b = 8.9904, c = −5.6087, St = 1051.3356, Sr = 4.3466,

Sy/x = 0.7244, r2 = 0.9959, r = 0.9979

6.33 a = 2.2561, b = 3.8171, c = 5.3333, St = 577.84, Sr = 4.2007,

Sy/x = 0.6001, r2 = 0.9927, r = 0.9964

6.35 a = 29.4487, b = −5.7449, c = 19.8449, St = 3463.61, Sr = 2.0574,

Sy/x = 0.2939, r2 = 0.9994, r = 0.9997

6.37 a = 2.7077 and b = 0.0369 6.39 a = 63.5770 and b = 0.3548 6.41 a = 4.0672 and b = 0.7823 6.43 a = 13.2275 and b = 37.5036 6.45 a = −0.0359 and b = 7.6495; r = 0.9998 6.47 a = 3.3385; b = 2.9169; Sum of squares of errors = 7.8421 6.49 a = 1.2045; b = 0.2599; Sum of squares of errors = 1.6846 6.51 a = 21.5949; b = 3.5681; Sum of squares of errors = 271.3282 6.53 a = 5.2116; b = 2.8682; Sum of squares of errors = 5.6054 6.55 a = −1.2576; b = 0.3166; Sum of squares of errors = 1.1108 6.57 a = 18.6429; b = 3.4286; Sum of squares of errors = 14.1888 6.67 k = 1.0317 × 105 N/m, Sr = 4.5673 × 103, St= 1.6 × 106, r2 = 0.9971 (Good fit)

ANMF.CH18_App-F_1PP.indd 1151

2/24/2023 5:37:19 PM

1152 • Applied Numerical Methods Using MATLAB 6.69 p = 1.0 × 104[4.1742, 0, 0.1485, 0]

k1 = 1485 and k2 = 4.1742 × 104

6.71 y = −43.2979 + 9.2461x −0.1115x2

CHAPTER 7: NUMERICAL INTEGRATION 7.1 I = 0.9023

7.31 I = 0.52359895

7.3 (a)  I = 1.8591, rel error = 0.0819

7.33 1.8278



(b)  I = 1.7539, rel error = 0.0207



(c)  I = 1.7272, rel error = 0.0052



(d)  I = 1.7205, rel error = 0.0052

7.5 (a)  I = 2.5268, rel error = 0.2

(b)  I = 2.1122, rel error = 0.0086

7.7 I = −1.81285 7.9 I = 0.9045 7.11 I = 1.718 7.13 I = 1.187 7.15 I = −2.56622 7.17 I = 1.3571 7.19 I = 0.7853959 7.21 I = 3.1832 7.23 I = 0.091111 7.25 I = 1.30859 7.27 I = 0.78535 7.29 I = 1.827858

ANMF.CH18_App-F_1PP.indd 1152

7.35 0.264241 7.37 18.6667 7.39 (a) 0.16061 (b)  0.19226 (c)  0.64269 (d)  2.58797 7.41 R3, 3 = 11.5246 7.43 0.4 7.45 0.49999 for n = 4 7.47 0.786885 for n = 1 and 0.785403 for n = 4 7.49 −0.656963 7.51 For n = 1, error = 1.481%; for n = 3, error = −0.01044% h h = 0.5, c = 0.4202; = 1, c b b h = 0.6063; = 2, c = 1.0589 b 7.55 60.5730 7.53

7.57 0.0250881 7.59 0.23129 7.61 2.9545

2/24/2023 5:37:22 PM

Answers to Odd Numbered Exercises • 1153

CHAPTER 8: NUMERICAL SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS 8.1 y (0.1) = 1.1053 8.3 y (0.2) = 0.83746 8.5 y (0.1) = 0.9828 8.7 1.1053 8.9 y (0.2) = 0.80227 8.11 y (0.1) = 1.1057, y (0.3) = 1.3694 8.13 y (1) = 3.18 8.15 y (1.25) = 0.34565 with h = 0.5, y (1.175) = 0.1897 with h = 0.025 8.17 y (0.5) = 0.2836165, y (1) = 3.2190993 8.19 y (0.3) = 1.2432 8.21 y (0.1) = 1.1055 8.23 y (0.075) = 1.076 8.25 y (0.1) = 2.2050, y (0.2) = 2.421, y (0.3) = 2.6492, y (0.4) = 2.8909 8.27 [t, y] = [0.1 1.00025, 0.2 1.00243, 0.3 1.00825, 0.4 1.01926, 0.5 1.03688] 8.29 y (0.1) = 2.2050 8.31 y (0.8) = 0.84899 8.33 y (0.2) = 1.196, y (0.4) = 1.3752 8.35 y (0.5) = 3.21875 8.37 y (0.2) = 1.00265 8.39 yp (0.8) = 1.02337, yuck (0.8) = 1.0296 8.41 y (1.4) = 2.575 8.43 y (0.8) = 2.4366 8.45 y (0.4) = 1.8392 8.47 y (0.8) = 1.0294, y (1.0) = 1.5549 8.49 (a) y (0) = 0, y (1.8) = 1.0795, Error at x = 1.8 is −0.8926 (b)  y (0) = 0, y (1.8) = 0.2008, Error at x = 1.8 is −0.0139

ANMF.CH18_App-F_1PP.indd 1153

2/24/2023 5:37:22 PM

1154 • Applied Numerical Methods Using MATLAB

 x2  x  2  8.53 (a) Analytical method:  y    2   (b)  Euler’s method: with h = 0.5



2

(c)  Fourth-order RungeKutta method:

x

y

x

y

0.00

1

0.00

1

0.25

1.3369

0.25

1.3369

0.50

1.8906

0.50

1.8906

0.75

2.7431

0.75

2.7431

1.00

4

1.00

3.9998

  Euler’s method: x

Y

0.00

1

0.25

1.25

0.50

1.6692

0.75

2.3153

1.00

3.2663

8.55 (a) y (1.5) = 0.75, Error at x = 1.5 is 0.1963 (b)  y (1.5) = 0.9468, Error at x = 1.5 is −0.5303 × 10−3 8.57 y(2) = 0.54345 and y′(2) = −1.0543 8.59 y1(2) = 0.54345 and y2(2) = −1.0543 8.61 t 0

ANMF.CH18_App-F_1PP.indd 1154

true 2

yEuler

|Et|%

2



5

19.28

1

6.19463

2

14.84392

11.40216

23.19

3

33.67717

25.51321

24.24

4

75.33896

56.84931

24.54

2/24/2023 5:37:23 PM

Answers to Odd Numbered Exercises • 1155

8.63 x

0

y(computed)

3

0.5 4.069

1.0 4.32

1.5 4.167

y(exact)

3

4.072

4.323

4.17

Error

0

0.003

0.003

0.003

8.65 t

1

2

3

4

5

6

x

0

0.5

1.0

1.5

2.0

2.5

y(computed)

3

4.7

4.893

4.55

4.052

3.542

y(exact)

3

4.072

4.323

4.17

Error

0

−0.6277

−0.5696

0.3803

3.835

3.436

−0.2165

−0.1054

8.67 y = 0.9801 and y′ = −0.1970 at x = 0.2 8.69 x5 = 2.0, y5 = 3.763 8.71 y (0.25) = 0.1047; y (0.5) = 0.1403; y (0.75) = 0.1047 8.73 (a) Euler’s method: t

yEuler

yanalytical

0.0

1

1

0.1

1

0.955

0.2

0.91

0.825

0.3

0.73

0.622

0.4

0.468

0.362

(b)  Runge-Kutta method of order four:

ANMF.CH18_App-F_1PP.indd 1155

t

y

yanalytical

0.0

1

1

0.1

0.9553

0.9553

0.2

0.8253

0.8253

0.3

0.6216

0.6216

0.4

0.3624

0.3623

0.5

0.0708

0.0707

2/24/2023 5:37:23 PM

1156 • Applied Numerical Methods Using MATLAB

1 2 8.75 y    0.6154; y    1.6923; y(1)  2.8462 3 3 8.77 y (1.0748) = −0.9303; y (3.1416) = 0; y (0) = 1.5; y (1.3228) = −0.8901;

y (0.4134) = 0.3089; y (2.322) = −0.1438

CHAPTER 9: DIRECT NUMERICAL INTEGRATION METHODS 9.1 The output of the program is shown below for various values of Δt.

FIGURE P9.1 

9.3 The results for x−1 = −0.5 and x0 = 0 are shown in Table P9.3 below: TABLE P9.3 Time(t) 0

0

0.5

0.5

1

0.875

1.5

1.0313

2.0

0.9297

2.5

0.5957

3 3.5

ANMF.CH18_App-F_1PP.indd 1156

x(t)

0.1128 -0.3983

2/24/2023 5:37:25 PM

Answers to Odd Numbered Exercises • 1157

9.5 TABLE P9.5 i+1

ti

Fi = F(ti)

xi

xi-1

xi+1

1

0

200

0

0.0625

0.0625

2

0.05

200

0.0625

0

0.3111

3

0.1

200

0.3111

0.0625

0.07869

4

0.15

200

0.07869

0.1311

0.00523

5

0.2

200

0.00523

0.07869

0.04735

6

0.25

175

0.04735

0.00523

0.1087

7

0.30

150

0.1087

0.04735

0.05983

8

0.35

125

0.05983

0.1087

−0.02152

9

0.40

100

−0.0215

0.05983

0.00071

10

0.45

75

0.00071

−0.02153

0.06738

11

0.50

50

0.06738

0.000711

0.03848

12

0.55

25

0.03848

0.06738

−0.04553

13

0.6

−0.00003

−0.0455

0.03848

−0.04316

1 [0 + 2 × (−0.00125) + 2 × −(0.00125) 6

9.7 Hence x (0.05) = 1 + − 0.002485] = 0.9991

y = 1 +

1 [−0.05 + 2 × −(0.05) + 2 × −(0.0497) − 0.4993] = 0.8752 6

9.9 TABLE P9.9  Response of the system obtained using the central difference method with dt = 0.05 s and F = 10 N Time (ti)

ANMF.CH18_App-F_1PP.indd 1157

Displacement (Xi)

Velocity (Vi)

Acceleration (ai)

0.000000

0.000000

0.000000

0.500000

1.250000

3.677614

10.000000 4.710456

1.000000

3.677614

−0.827391

−22.730475

1.500000

0.422609

−5.731781

3.112915

2.000000

−2.054167

−3.908362

4.180763

2.500000

−3.485752

1.155199

16.073482

3.000000

−0.898967

2.679238

−9.977329

3.500000

−0.806515

−1.679139

−7.456177

4.000000

−2.578107

−2.125155

5.672112

4.500000

−2.931670

2.354134

12.245043

5.000000

−0.223973

5.495064

0.318680

2/24/2023 5:37:28 PM

1158 • Applied Numerical Methods Using MATLAB 9.11 TABLE P9.11 Time ti = i.Δt

X x1

x2

0

0

0

0.2421

0

0.1466

0.4842

0.0172

0.552

0.7263

0.0931

1.122

0.9684

0.267

1.727

1.2105

0.551

2.237

1.4526

0.9027

2.547

1.6947

1.235

2.605

1.9368

1.439

2.418

2.1789

1.4202

2.042

2.421

1.141

1.563

2.6631

0.6437

1.0773

2.9053

0.0463

0.669

9.13 TABLE P9.13  Time ti = i.Δt

ANMF.CH18_App-F_1PP.indd 1158

X x1

x2

0

1

0

0.25

0.9397

−0.0566

0.5

0.771

−0.204

0.75

0.526

−0.374

1.0

0.268

−0.512

1.25

0.052

−0.512

1.5

−0.092

−0.381

1.75

−0.235

−0.112

2.00

−0.352

0.237

2.25

−0.492

0.259

2/24/2023 5:37:29 PM

Answers to Odd Numbered Exercises • 1159

9.15 TABLE P9.15  Time ti = i.Δt

X x1

x2

0

1

0.25

0.939

−0.056

0

0.5

0.7699

−0.202

0.75

0.529

−0.375

1.0

0.264

−0.497

1.25

0.0217

−0.504

1.5

0.136

−0.363

1.75

−0.243

−0.107

2.00

−0.373

0.242

2.25

−0.499

0.271

9.17 TABLE P9.17  t

ANMF.CH18_App-F_1PP.indd 1159

x1

x2

0

0

0.1

1

0.4550

2

0.5470

3 4

x 1

x 2

1

0

0.2500

0.0790

0.4250

0.9850

−0.0118

0.6490

0.3490

0.9050

−0.2770

0.6510

0.0705

0.0604

−0.1710

−0.7250

5

0.0888

−0.0464

6

−0.0183

−0.0985

−0.1950

−0.2790

7

−0.2330

−0.5300

−0.0880

−0.2770

8

−0.0785

−0.1860

0.2250

0.6550

9

0.0408

0.2270

−0.00143

0.0825

10

−0.0839

−0.1090

−0.0781

−0.4010

0.03210

0.1990

2/24/2023 5:37:31 PM

1160 • Applied Numerical Methods Using MATLAB 9.19 TABLE P9.19  Time

Δt

2Δt

3Δt

4Δt

5Δt

X1

0.113

0.423

0.849

1.290

1.651

X2

0.109

0.382

0.677

0.822

0.681

Time

6Δt

7Δt

8Δt

9Δt

10Δt

X1

1.882

1.980

1.992

1.986

2.022

X2

0.212

−0.509

−1.310

−1.979

−2.336

9.21 TABLE P9.21 

4

Time ti = i.Δt

X x1

x2

0.000000

0.00000

0.00000

0.250000

0.07812

0.00000

0.500000

0.29785

1.19599

0.750000

0.69273

2.87831

DOF–1 DOF–2

3

dis pla c e me nt(m)

2

1

0

–1

–2 0

0.5

1

1.5

2

2.5 time(s)

3

3.5

4

4.5

FIGURE P9.21

ANMF.CH18_App-F_1PP.indd 1160

2/24/2023 5:37:32 PM

Answers to Odd Numbered Exercises • 1161

9.23 3

DOF–1 DOF–2

2.5

dis pla c e me nt(m)

2 1.5 1 0.5 0 –0.5 –1

0

1

2

3

4

5 time(s)

6

7

8

9

10

FIGURE P9.23 TABLE P9.23  Time ti = i.Δt

X x1

x2

0.000000

0.00000

0.00000

0.050000

0.00000

0.00623

0.100000

0.00006

0.02486

0.150000

0.00027

0.05569

CHAPTER 10: MATLAB BASICS 10.1 2.9532e + 003 10.3 x = 5.7170 10.5 (a)  B = 9 2 34 28 24   17 10 4 36 32   25 18 12 8 40 (b)  C = Columns 1 through 12    33 34 35 36 37 38 39 40 4 12 20 28

ANMF.CH18_App-F_1PP.indd 1161



    Columns 13 through 16    6 14 22 30

2/24/2023 5:37:32 PM

1162 • Applied Numerical Methods Using MATLAB 10.7 A = −0.7783 + 0.3257i



0.4368





1.4052





0.6263

10.9   Output is ans =44

  Output is ans =123



  Output is ans =391

10.11 Output is a = − 7.3898

   −0.6570 + 0.3613i



  −0.6570 − 0.3613i



    0.4268 + 0.5473i



   0.4268 − 0.5473i



  −0.0749 + 0.7030i



  −0.0749 − 0.7030i



 Output is b = 5.1625



  2.4906



   0.8961 + 0.6912i



  0.8961 − 0.6912i



  −1.2191



  −0.6131 + 0.6094i



  −0.6131 − 0.6094i



 Output is c = 12.0867



  1.4875



  −0.7057 + 0.7061i



  −0.7057 − 0.7061i



  0.8371



 Output is d = −4.1331 + 2.2411i



  −4.1331 − 2.2411i

ANMF.CH18_App-F_1PP.indd 1162

2/24/2023 5:37:32 PM

Answers to Odd Numbered Exercises • 1163



  0.8305



  0.4358



 Output is e = 16.7477



     0.8738 + 2.3465i



      0.8738 − 2.3465i



 Output is ƒ =



  16.6158



  1.3842



 Output is g = −7

10.13 [0.666667 3.000000 3.333333] 10.15 ans =



0







ans =





6





−6



ans =





6





−6

0

10.17 |x| cos(x) for –200

200

 x  200

150 100 50 y

0 –50

–100 –150 –200 –200

–150

–100

–50

0 x

50

100

150

200

FIGURE 10.17 

ANMF.CH18_App-F_1PP.indd 1163

2/24/2023 5:37:32 PM

1164 • Applied Numerical Methods Using MATLAB 10.19 45 40 35 30 y(t)

25 20 15 10 5 0

0

0.5

1

1.5 t

2

2.5

3

FIGURE 10.19 

10.21

FIGURE 10.21 

ANMF.CH18_App-F_1PP.indd 1164

2/24/2023 5:37:33 PM

Answers to Odd Numbered Exercises • 1165

10.23 (a) ans =

    [ 3*i]



    [ −3*i]

(b)  ans =

    [ −5/2 + 1/2*57^(1/2)]



    [ −5/2 − 1/2*57^(1/2)]

(c)  ans =

    −11.6592



      0.3296 − 0.7599i



      0.3296 + 0.7599i

(d)  ans =

    0.7152 + 0.8217i



    0.7152 − 0.8217i



    −2.3378



    −10.0927

(e)  ans =

    −3.1362



    −0.8007 − 0.6944i



    −0.8007 + 0.6944i



    0.6139 − 0.6513i



    0.6139 + 0.6513i



    1.7549 − 0.3308i



    1.7549 + 0.3308i

10.25 x = 0.13864416954 m

ANMF.CH18_App-F_1PP.indd 1165

2/24/2023 5:37:33 PM

1166 • Applied Numerical Methods Using MATLAB 10.27 (a) x = 11/5 + (− 11/10 − 11/20*5^(1/2))*exp((− 5 + 2*5^(1/2))*t) − 11/100*(− 5 + 2*5^(1/2)) *5^(1/2) *exp(− (5 + 2*5^(1/2))*t)



y = 11/5 + (− 11/10 − 3/5*5^(1/2))*exp((− 5 + 2*5^(1/2))*t) − 1/50*(− 30 + 11*5^(1/2))*5^(1/2) *exp(− (5 + 2*5^(1/2))*t)



  (b)  x = − 5/3 + (5/6 + 35/366*61^(1/2))*exp(− 1/2*(− 7 + 61^(1/2))*t) + 5/366*(− 7 + 61^(1/2))*61^(1/2)*exp(1/2*(7 + 61^(1/2))*t)







  (c)  x = − 15/7 + 15/7*exp(− 3/2*t)*cos(1/2*19^(1/2)*t) + 45/133*19^(1/2)*exp(− 3/2*t)*sin(1/2*19^(1/2)*t)







  (d)  x = 26/7 − 26/7*exp(− 1/2*t)*cos(3/2*3^(1/2)*t) − 26/63*3^(1/2)*exp(− 1/2*t)*sin(3/2*3^(1/2)*t)





y = − 5/3 + (5/6 + 41/366*61^(1/2)) *exp(− 1/2*(− 7 + 61^(1/2))*t) + 1/366 *(− 41 + 5*61^(1/2))*61^(1/2) *exp(1/2*(7 + 61^(1/2))*t)

y = − 15/7 + 15/7*exp(− 3/2*t)*cos(1/2*19^(1/2)*t) + 31/133*19^(1/2)*exp(− 3/2*t)*sin(1/2*19^(1/2)*t)

y = 26/7 − 40/63*3^(1/2)*exp(− 1/2*t)*sin(3/2*3^(1/2)*t) − 26/7*exp(− 1/2*t)*cos(3/2*3^(1/2)*t)

10.29

FIGURE 10.29

ANMF.CH18_App-F_1PP.indd 1166

2/24/2023 5:37:33 PM

Answers to Odd Numbered Exercises • 1167

10.31 (a) 5/8*x^8 − 1/6*x^6 + 3/4*x^4 − 8/3*x^3 + 7*x

(b)  sin(x)*x^(1/2) − 1/2*2^(1/2)*pi^(1/2)*FreznelS(2^(1/2) /pi^(1/2)*x^(1/2))



(c)  ans = 12/11*x^(11/3)*hypergeom([1, 11/6],[2, 3/2, 17/6], − 4*x^2)



(d)  ans = − 31/25*cos(9/5) + 18/5*sin(9/5) − 49/25*cos(1/5) − 2/5*sin(1/5) eval(ans)







(e)  ans = 12/25

10.33 10.35





ans = 1.7872 ans = 0.603179 84

s

s2

 25 

2

 168

1

2

3



s  52 3

s

3

1

2

s

21

2

 25 

2

  336

1

2

s  52 3

s

2

1

2

 25 

s

3

4

s

 253 

10.37 (a) ans =

 5 10 13



 −2  11  8

(b)  ans =

25 53 53



10 53 77

(c)  ans =

8 57 52



8 96 79

(d)  ans =

0 5 8



2 4 7

ANMF.CH18_App-F_1PP.indd 1167

2/24/2023 5:37:39 PM

1168 • Applied Numerical Methods Using MATLAB (e)  ans =

  0.0541 −0.0405  0.2568





−0.0631  0.1306 −0.0495

(f)  ans =

14 53 53



 4 53 77

(g)  w = 10 80 59

  −1 53 28

(h)  ans = 45

    ans = −222



    ans = −9990

CHAPTER 11: OPTIMIZATION 11.1 D = 1.4085 ft; L = 9.442 ft; cost = $1878.43 11.3 F(X) = −22 + 16.5X − 1.5X2

X = 5.5

11.5 x = 2.74 inches for minimum cost 11.7 xmin = 1.4497; ƒmin = −1.8102 11.9 xmin = 2.8198; ƒmin = −6.3246 11.11 (0.25, −1.0) 11.13 x = (−0.75, 1.0) ƒmin = −0.625 11.15 xmin = (1, 1); ƒmin = 6.874 × 10−23 11.17 R = 0.7531; B = 0.6736; H = 0.3368; S = 3.9838 11.19 x = (−0.6, −1.0); ƒmin = −0.6 11.21 x = (0.4965, 0.4965, 0.5069);ƒmin = 1.1597 × 10−8 11.23 x = 1015(−2.8149, −2.1201, −3.1801, 0.0025); ƒval = −1.6595 × 1016 11.25 x = 1015(−1.0571, −0.7962, 1.1943, 0.0009); ƒval = −6.2322 × 1015

ANMF.CH18_App-F_1PP.indd 1168

2/24/2023 5:37:39 PM

Answers to Odd Numbered Exercises • 1169

11.27 x = (1.0023, 0); ƒval = 5.3613 × 10−6 11.29 x = (1.4421, 0.654); ƒval = 0.3178 11.31 x = (−0.2118, 2.8205); ƒval = −0.3930 11.33 x = 1014(0, 0, −3.9251); ƒval = −1.1775 × 1015 11.35 x = (0.8889, 1.0556); ƒval = −1.0278 11.37 x = 4.352 × 10−5; ƒval = −2.3333 11.39 x = 1.488; ƒval = −2.8052 11.41 x = 5.4646 × 10−5; ƒval = −4.9690 11.43 x = (0.4768, 6.5395); ƒval = −8.6778 11.45 x = (7.2, −1.6); ƒval = −28.8 11.47 x = 2.2361; ƒval = −1 11.49 x = 5.5632 × 10−5; ƒval = −6.9759 11.51 x = (1, −1); ƒval = 1.6052 × 10−12 11.53 x = (0, 2); ƒval = 21 11.55 x = (−0.7981, 0.368); ƒval= 1.0920

CHAPTER 12: PARTIAL DIFFERENTIAL EQUATIONS 12.1 (a) Linear;  order 2;  dependent variable u,  independent variables: x, (b)  Linear; 3;

R;

x,

(c)  Nonlinear; 2;

W;

x, s,

(d)  Linear; 3;

φ;

x, y,

(e)  Nonlinear; 1;

z;

u, v

12.3 (a) Elliptic, (b)  Hyperbolic, (c)  Parabolic

ANMF.CH18_App-F_1PP.indd 1169

2/24/2023 5:37:39 PM

1170 • Applied Numerical Methods Using MATLAB 12.5 t

T0

T1j+1

T2j+1

T3j+1

T4

F1 − T0

F2

F3 − T4

0.0

0

1.0000

2.0000

1.5000

1

−1.2500

−1.0000

−2.3750

0.1

0

0.6496

0.8613

1.1496

1

−0.3741

−1.1533

−1.9991

0.2

0

0.3351

0.7149

0.8351

1

−0.4636

−0.6340

−2.0886

0.3

0

0.3084

0.5387

0.8084

1

−0.3074

−0.7128

−1.9324

0.4

0

0.2583

0.5321

0.7583

1

−0.3384

−0.6175

−1.9634

0.5

0

0.2590

0.5032

0.7590

1

−0.3090

−0.6405

−1.9340

0.6

0

0.2505

0.5051

0.7505

1

−0.3172

−0.6222

−1.9422

0.7

0

0.2514

0.5000

0.7515

1

0

0.2500

0.5000

0.7500

1

 ∞

12.9 i

j

xi

yj

wi,j

u(xi, yj)

2

2

1.2

1.2

0.52515

0.52508

4

4

1.4

1.4

1.31908

1.31897

6

6

1.6

1.6

2.40651

2.40641

8

8

1.8

1.8

3.80889

3.80885

12.11 T1 = 19.385, T2 = 17.538, T3 = 10.769, T4 = 15.538, and T5 = 21.385 12.13 (a)  Gauss-Seidel method:

ANMF.CH18_App-F_1PP.indd 1170

r

u1,1

u2,1

u1,2

u2,2

1

−0.00274

−0.01166

−0.01166

−0.04973

2

−0.00857

−0.02555

−0.02555

−0.05667

3

−0.01552

−0.02902

−0.02902

−0.05841

4

−0.01725

−0.02989

−0.02989

−0.05884

5

−0.01769

−0.03011

−0.03011

−0.05895

6

−0.01780

−0.03016

−0.03016

−0.05898

7

−0.01782

−0.03017

−0.03017

−0.05898

8

−0.01783

−0.03018

−0.03018

−0.05898

2/24/2023 5:37:39 PM

Answers to Odd Numbered Exercises • 1171

(b)  S.O.R. method: r

u1,1

u2,1

u1,2

u2,2

1

−0.00302

−0.01299

−0.01290

−0.05538

2

−0.00981

−0.02871

−0.02871

−0.05854

3

−0.01783

−0.03020

−0.03020

−0.05904

4

−0.01785

−0.03020

−0.03020

−0.05899

5

−0.01784

−0.03018

−0.03018

−0.05899

6

−0.01783

−0.03018

−0.03018

−0.05898

12.15 (a) Gauzz-Seidel’s method:



ANMF.CH18_App-F_1PP.indd 1171

u1,1

u2,1

u1,2

u2,2

0.00000

0.00000

2.50000

3.12500

0.62500

0.93750

3.43750

3.59375

1.09375

1.17188

3.67188

3.71094

1.21094

1.23047

3.73047

3.74023

1.24023

1.24512

3.74512

3.74756

1.24756

1.24878

3.74878

3.74939

1.24939

1.24969

3.74969

3.74985

1.24985

1.24992

3.74992

3.74996

1.24996

1.24998

3.74998

3.74999

1.24999

1.25000

3.75000

3.75000

(b)  Jacobi’s method: u1,1

u2,1

u1,2

u2,2

0.00000

0.00000

2.50000

2.50000

0.62500

0.62500

3.12500

3.12500

0.93750

0.93750

3.43750

3.43750

1.09375

1.09375

3.59375

3.59375

1.17188

1.17188

3.67188

3.67188

1.21094

1.21094

3.71094

3.71094

1.23047

1.23047

3.73047

3.73047

1.24023

1.24023

3.74023

3.74023

2/24/2023 5:37:39 PM

1172 • Applied Numerical Methods Using MATLAB

u1,1

u2,1

u1,2

u2,2

1.24512

1.24512

3.74512

3.74512

1.24756

1.24756

3.74756

3.74756

1.24878

1.24878

3.74878

3.74878

1.24939

1.24939

3.7499

3.74939

1.24969

1.24969

3.74969

3.74969

1.24985

1.24985

3.74985

3.74985

1.24992

1.24992

3.74992

3.74992

(c)  S.O.R. method: r

u1,1

u2,1

u1,2

u2,2

1

0.00000

0.00000

2.75000

3.50625

2

0.75625

1.17219

3.64719

3.72470

3

1.24970

1.25074

3.75324

3.75363

4

1.25113

1.25123

3.75098

3.75025

5

1.25050

1.25008

3.75011

3.75003

6

1.25000

1.25000

3.75000

3.75000

xi

wi,50

u(xi, 0.5)

0.0

0

0

0.1

0.00230

0.00222

8.271 × 10−5

0.2

0.00438

0.00422

1.573 × 10−4

0.3

0.00603

0.00581

2.165 × 10−4

0.4

0.00709

0.00689

2.546 × 10−4

0.5

0.00745

0.00719

2.677 × 10−4

0.6

0.00709

0.00683

2.546 × 10−4

0.7

0.00603

0.00581

2.165 × 10−4

0.8

0.00438

0.00422

1.573 × 10−4

0.9

0.00230

0.00222

8.271 × 10−5

1.0

0

0

12.21

ANMF.CH18_App-F_1PP.indd 1172

|wi,50 − u(xi, 0.5)|

2/24/2023 5:37:40 PM

Answers to Odd Numbered Exercises • 1173

12.23 xi

u(xi, 0.5)

wi,1000 k = 0.0005

|u(xi, 0.5) − wi,1000 |

wi,50 k = 0.01

|u(xi, 0.5) − wi,50 |

0.0

0

0

0.1

0.00222

0.00228

6.411 × 10−5

8.1987 × 10−7

8.199 × 10−7

0.2

0.00422

0.00434

1.219 × 10−4

−1.5571 × 10−8

1.557 × 10−8

−4

0

−8

0.3

0.00581

0.00598

1.678 × 10

2.1383 × 10

2.138 × 10−8

0.4

0.00683

0.00703

1.973 × 10−4

−2.5064 × 10−8

2.506 × 10−8

−4

−8

0.5

0.00719

0.00739

2.075 × 10

2.6268 × 10

2.627 × 10−8

0.6

0.00683

0.00703

1.973 × 10−4

−2.4901 × 10−8

2.490 × 10−8

−4

0.7

0.00581

0.00598

1.678 × 10

2.1120 × 10

2.112 × 10−8

0.8

0.00422

0.00434

1.219 × 10−4

−1.5308 × 10−8

1.531 × 10−8

−5

0.9

0.00222

0.00228

1.0

0

0

−8

−7

6.511 × 10

8.0360 × 10

8.036 × 10−7

0

12.25 (a) and (b) TABLE P12.25(a) i

j

xi

tj

wi,j

u(xi, tj)

2

5

0.8

0.5

3.03563

0.49069

3

5

1.2

0.5

−3.03563

0.57684

4

5

1.6

0.5

1.87612

0.18742

For h = 0.4 and k = 0.05. TABLE P12.25(b) i

ANMF.CH18_App-F_1PP.indd 1173

j

xi

tj

wi,j

u(xi, tj)

2

10

0.8

0.5

0

0

3

10

1.2

0.5

0

0

4

10

1.6

0.5

0

0

2/24/2023 5:37:40 PM

1174 • Applied Numerical Methods Using MATLAB 12.27 X u(x)

0.0 0.0

0.2 0.5878

0.4 0.9511

0.6 0.9511

0.8 0.5878

1.0 0.1

n=0

0.0

0.5878

0.9511

0.9511

0.5878

0.0

n=1

0.0

0.5129

0.8176

0.8078

0.4890

0.0

n=2

0.0

0.4907

0.7900

0.7868

0.4855

0.0

n=3

0.0

0.4861

0.7858

0.7855

0.4853

0.0

n=4

0.0

0.4854

0.7854

0.7854

0.4883

0.0

n=5

0.0

0.4853

0.7854

0.7854

0.4853

0.0

i=0 x=0

i=1 x = 0.2

i=2 x = 0.4

i=3 x=6

i=4 x=8

i=5 x = 1.0

j = 0, t = 0.0

0

0.2938

0.4755

0.4755

0.2938

0

j = 1, t = 0.1

0

0.2798

0.4528

0.4528

0.2798

0

j = 2, t = 0.2

0

0.2391

0.3868

0.3868

0.2391

0

j = 3, t = 0.3

0

0.1755

0.2839

0.2839

0.1755

0

j = 4, t = 0.4

0

0.0951

0.1539

0.1539

0.0951

0

j = 5, t = 0.5

0

0.0057

0.0092

0.0092

0.0057

0

12.31

12.33

ANMF.CH18_App-F_1PP.indd 1174

j

u0j

u1j

u2j

u3j

u4j

0

0

0.1875

0.25

0.1875

0

1

0

0.1775

0.24

0.1775

0

2

0

0.1491

0.21

0.1491

0

3

0

0.1066

0.1605

0.1066

0

4

0

0.0556

0.0938

0.0556

0

5

0

0.0019

0.0148

0.0019

0

6

0

−0.0501

−0.0682

−0.0501

0

7

0

−0.0970

−0.1453

−0.0970

0

8

0

−0.1361

−0.2073

−0.1361

0

2/24/2023 5:37:40 PM

Bibliography There are several outstanding text and reference books on applied numerical methods, optimization, and MATLAB that merit consultation for those readers who wish to pursue these topics further. The following list is but a representative sample of many excellent books. Numerical Methods Abramowitz, M., and Stegun, I., Handbook of Mathematical Functions, Dover Press, New York, 1964. Akai, T. J., Applied Numerical Methods for Engineers, Wiley, New York, 1993. Al-Khafaji, A. W., and Tooley, J. R., Numerical Methods in Engineering Practice, Holt, Rinehart, and Winston, New York, 1986. Allen, M., and Iaacson, E., Numerical Analysis for Applied Science, Wiley, New York, 1998. Ames, W. F., Numerical Methods for Partial Differential Equations, 3rd ed., Academic Press, New York, 1992. Ascher,U., Mattheij, R., and Russell, R., Numerical Solution of Boundary Value Problems for Ordinary Differential Equations, Prentice Hall, Englewood cliffs, NJ, 1988. Atkinson, K., and Han, W., Elementary Numerical Analysis, 3rd ed, Wiley, New York, 2004. Atkinson, K. E., An Introduction to Numerical Analysis, 2nd ed., Wiley, New York, 1993.

ANMF.CH19_Bib_1PP.indd 1175

2/24/2023 5:43:29 PM

1176 • Applied Numerical Methods Using MATLAB Atkinson, L. V., and Harley, P. J., Introduction to Numerical Methods with PASCAL, Addison Wesley, Reading, MA, 1984. Atkinson, L. V., Harley, P. J., and Hudson, J. D., Numerical Methods with FORTRAN 77, Addison Wesley, Reading, MA, 1989. Axelsson, K., Iterative Solution Methods, Cambridge University Press, ­Cambridge, UK, 1994. Ayyub, B. M., and McCuen, R. H., Numerical Methods for Engineers, ­Prentice Hall, Upper Saddle River, NJ, 1996. Baker, A. J., Finite Element Computational Fluid Mechanics, McGraw-Hill, New York, 1983. Balagurusamy, E., Numerical Methods, Tata McGraw Hill, New Delhi, India, 2002. Bathe, K. J., and Wilson, E. L., Numerical Methods in Finite Element ­Analysis, Prentice Hall, Englewood Cliffs, NJ, 1976. Bhat, R. B., and Chakraverty, S., Numerical Analysis in Engineering, Narosa Publishing House, New Delhi, India, 2004. Bhat, R. B., and Gouw, G. J., Numerical Methods in Engineering, Simon & Schuster, Needham Heights, MA, 1996. Bjorck, A., Numerical Methods for Least Squares Problems, Society for ­Industrial and Applied Mathematics (SIAM), Philadelphia, 1996. Booth, A. D., Numerical Methods, Academic Press, New York, 1958. Brice, C., Luther, H. A, and Wilkes, J. O., Applied Numerical Methods, New York, 1969. Buchanan, J. L., and Turner, P. R., Numerical Methods and Analysis, McGraw Hill, New York, 1992. Burden, R. L., and Faires, J. D., Numerical Analysis, 6th ed., Brooks/Cole, Pacific Grove, CA, 1997. Carnahan, B., Luther, A., and Wilkes, J. O., Applied Numerical Methods, Wiley, New York, 1969. Chapra, S. C, and Canale, R. P., Introduction to Computing for Engineers, 2nd ed., McGraw Hill, New York, 1994. Chapra, S. C, and Canale, R. P., Numerical Methods for Engineers with ­Personal Computers, McGraw Hill, New York, 1985.

ANMF.CH19_Bib_1PP.indd 1176

2/24/2023 5:43:29 PM

Bibliography • 1177

Chapra, S. C., Applied Numerical Methods with MATLAB for Engineers and Scientists, McGraw Hill, New York, 2005. Chapra, S. C., Numerical Methods for Engineers with Software and Programming Applications, 4th ed., McGraw Hill, New York, 2002. Cheney, W., and Kincaid, D., Numerical Mathematics and Computing, 2nd ed., Brooks/Cole, Monterey, CA, 1994. Chui, C., An Introduction to Wavelets, Academic Press, Burlington, MA, 1992. Consatantinides, A., Applied Numerical Methods with Personal Computers, McGraw Hill, New York, 1987. Conte, S. D., and DeBoor, C. W., Elementary Numerical Analysis: An ­Algorithm Approach, 2nd ed., McGraw Hill, New York, 1972. Dahlquist, G., and Bjorck, A., Numerical Methods, Prentice Hall, Englewood Cliffs, NJ, 1974. Davis, P., and Rabinowitz, P., Methods of Numerical Integration, Academic Press, 2nd ed, New York, 1998. de Boor, C., A Practical Guide to Splines, Springer Verlag, New York, 1978. Demmel, J. W., Applied Numerical Linear Algebra, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1997. Dennis, J. E., and Schnabel, R. B., Numerical Methods for Unconstrained Optimization and Nonlinear Equations, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1996. Dukkipati, R. V., Applied Numerical Methods with MATLAB Through Solved Problems, New Age International Publishers (P) Ltd., New Delhi, India, 2010. Dukkipati, R. V., and Govindaraja, B., Computer Programming and Numerical Methods, New Age International Publishers (P) Ltd., New Delhi, India, 2010. Dukkipati, R. V., and Govindaraja, B., Computer Programming and Numerical Method Through Solved Problems, New Age International Publishers (P) Ltd., New Delhi, India, 2010. Dukkipati, R. V., MATLAB for Beginners, New Age International Publishers (P) Ltd., New Delhi, India, 2010. Dukkipati, R. V., Numerical Methods Through Solved Problems, New Age International Publishers (P) Ltd., New Delhi, India, 2009.

ANMF.CH19_Bib_1PP.indd 1177

2/24/2023 5:43:29 PM

1178 • Applied Numerical Methods Using MATLAB Dukkipati, R. V., Numerical Methods, New Age International Publishers (P) Ltd., New Delhi, India, 2009. Epperson, J. F., An Introduction to Numerical Methods and Analysis, Wiley, New York, 2001. Fadeev, D. K., and Fadeeva, V. N., Computational Methods of Linear A ­ lgebra, Freeman, San Francisco, 1963. Fadeeva, V. N., (Trans. Curtis D. Benster), Computational Methods of Linear Algebra, Dover, New York, 1959. Fatunla, S. O., Numerical Methods for Initial Value Problems in Ordinary Differential Equations, Academic Press, San Diego, 1988. Ferziger, J. H., Numerical Methods for Engineering Applications, 2nd ed., Wiley, New York, 1998. Forbear, C. E., Introduction to Numerical Analysis, Addison Wesley, ­Reading, MA, 1969. Forsythe, G. E., and Wasow, W. R., Finite-Difference Methods for Partial ­Differential Equations, Wiley, New York, 1960. Forsythe, G. E., Malcolm, M. A., and Moler, C. B., Computer Methods for Mathematical Computation, Prentice Hall, Englewood Cliffs, NJ, 1977. Froberg, C. E., Introduction to Numerical Analysis, Addison-Wesley, ­Reading, MA, 1965. Gautschi, W., Numerical Analysis: An Introduction, Birkhauser, Boston, 1997. Gear, C. W., Numerical Initial Value Problems in Ordinary Differential Equations, Prentice Hall, Englewood Cliffs, NJ, 1971. Gerald, C. F., and Wheatley, P. O., Applied Numerical Analysis, 5th ed., AddisonWesley, Reading, MA, 1994. Gladwell, J., and Wait, R., A Survey of Numerical Methods of Partial ­Differential Equations, Oxford University Press, New York, 1979. Goldberg, D. E., Genetic Algorithms in Search, Optimization, and Machine Learning, Addison-Wesley, Reading, MA, 1989. Golub, G. H., and Van Loan, C. F., Matrix Computations, 3rd ed., Johns ­Hopkins University Press, Baltimore, MD, 1996. Greenbaum, A., Iterative Methods for Solving Linear Systems, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1997.

ANMF.CH19_Bib_1PP.indd 1178

2/24/2023 5:43:29 PM

Bibliography • 1179

Griffiths, D. V., and Smith, I. M., Numerical Methods for Engineers, Oxford University Press, 1991. Guest, P. G., Numerical Methods of Curve Fitting, Cambridge University Press, New York, 1961. Hager, W. W., Applied Numerical Algebra, Prentice Hall, Upper Saddle River, NJ, 1998. Hamming, R. W., Numerical Methods for Scientists and Engineers, 2nd ed., McGraw Hill, New York, 1973. Henrici, P. H., Elements of Numerical Analysis, Wiley, New York, 1964. Higham, N. J., Accuracy and Stability of Numerical Algorithms, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1996. Hildebrand, F. B., Introduction to Numerical Analysis, 2nd ed., ­McGraw-Hill, New York, 1974. Hoffman, J., Numerical Methods for Engineers and Scientists, McGraw Hill, New York, 1992. Hornbeck, R. W., Numerical Methods, Quantum, New York, 1975. Householder, A. S., Principles of Numerical Analysis, McGraw Hill, New York, 1953. Householder, A. S., The Theory of Matrices in Numerical Analysis, Blaisdell, New York, 1964. Iserles, A., A First Course in the Numerical Analysis of Differential ­Equations, Cambridge University Press, New York, 1996. Issaccson, E., Keller, H. B., and Bishop, H., Analysis of Numerical Methods, Wiley, New York, 1966. Jacobs, D. (Ed.), The State of the Art in Numerical Analysis, Academic Press, London, 1977. Jacques, I., and Colin, J., Numerical Analysis, Chapman and Hall, New York, 1987. Jain, M. K., Numerical Analysis for Scientists and Engineers, S.B.W. Publishers, New Delhi, India, 1971. James, M. L., Smith, G. M., and Wolford, J. C., Applied Numerical Methods for Digital Computations With FORTRAN and CSMP, 3rd ed., Harper & Row, New York, 1985.

ANMF.CH19_Bib_1PP.indd 1179

2/24/2023 5:43:29 PM

1180 • Applied Numerical Methods Using MATLAB Johnson, L. W., Riess, R. D., Numerical Analysis, 2nd ed, Addison-Wesley, Reading, MA, 1982. Johnston, R. L., Numerical Methods: A Software Approach, Wiley, New York, 1982. Kahaneer, D., .Moher, C., and Nash, S., Numerical Methods and Software, Prentice Hall, Englewood Cliffs, NJ, 1989. Keller, H. B., Numerical Methods for Two-Point Boundary Value Problems, Wiley, New York, 1968. Kelley, C. T., Iterative Methods of Optimization, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1999. Kharab, A., and Guenther, R.B., An Introduction to Numerical Methods-A MATLAB Approach, CRC Press, Boca Raton, FL, 2001. Kincaid, D., and Cheney, W., Numerical Analysis: Mathematics of Scientific Computing, Brooks/Cole, Pacific Grove, CA, 1996. Kress, R., Numerical Analysis, Springer-Verlag, New York, 1998. Krishnamurthy, E. V., and Sen, S. K., Numerical Algorithms, East West Publishers, New Delhi, India, 1986. Krommer, A. R., and Ueberhuber, C. W., Computational Integration, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1998. Lambert, J. D., Numerical Methods for Ordinary Differential Equations-The Initial Value Problems, Wiley, New York, 1991. Lapidus, L., and Pinder, G. F., Numerical Solution of Ordinary Differential Equations in Science and Engineering, Wiley, New York, 1981. Lapidus, L., and Seinfield, J. H., Numerical Solution of Partial Differential Equations, Academic Press, New York, 1971. Lastman, G. J., and Sinha, N. K., Microcomputer Based Numerical Methods for Science and Engineering, Saunders College Publishing, New York, 1989. Levy, H., and Baggott, E. A., Numerical Solutions of Differential Equations, Dover, New York, 1950. Maron, M. J., Numerical Analysis:A Practical Approach, Macmillan, New York, 1982. Mathews, J. H., Numerical Methods for Mathematics, Science and ­Engineering, 2nd ed, Prentice Hall of India, New Delhi, India, 1994.

ANMF.CH19_Bib_1PP.indd 1180

2/24/2023 5:43:29 PM

Bibliography • 1181

Milne, W. E., Numerical Solution of Differential Equations, Wiley, New York, 1953. Moin, P., Fundamentals of Engineering Numerical Analysis, Cambridge University Press, New York, 2001. Morton, K. W., and Mayers, D. F., Numerical Solution of Partial Differential Equations: An Introduction, Cambridge University Press, Cambridge, UK, 1994. Myron, A., and Issacson, E. L., Numerical Analysis for Applied Science, Wiley, Hoboken, NJ, 1998. Na, T. Y., Computational Methods in Engineering Boundary Value Problems, Academic Press, New York, 1979. Nakamura, S., Computational Methods in Engineering and Science, Wiley, New York, 1977. Nielson, K. L., Methods in Numerical Analysis, Macmillan Company, New York, 1964. Noble, B., Numerical Methods, Vol. 2, Oliver and Boyd, Edinburgh, ­Scotland, 1964. Nocedal, J., and Wright, S. J., Numerical Optimization, Springer-Verlog, New York, 1999. Ortega, J. M., Numerical Analysis:A Second Course, Academic Press, New York, 1972. Powell, M., Approximation Theory and Methods, Cambridge University Press, Cambridge, UK, 1981. Press, W. H., Teukolsky, S. A., Vetterling, W. T., and Flannery, B. P., Numerical Recipes: The Art of Scientific Computing, 2nd ed., Cambridge University Press, New York, 1992. Quarteroni, A., Sacco, R., and Saleri, F., Numerical Mathematics, Springer Verlag, New York, 2000. Ralston, A., and Rabinowitz, P., A First Course in Numerical Analysis, 2nd ed., McGraw Hill, New York, 1978. Ralston, A., and Wilf, H.S. (Eds.), Mathematical Methods for Digital Computers, Vols. 1 and 2, Wiley, New York, 1967. Rao, K. S., Numerical Methods for Scientists and Engineers, Prentice Hall, New Delhi, India, 2001.

ANMF.CH19_Bib_1PP.indd 1181

2/24/2023 5:43:29 PM

1182 • Applied Numerical Methods Using MATLAB Rao, S. S., Applied Numerical Methods for Engineers and Scientists, Prentice Hall, Upper Saddle River, NJ, 2002. Ratschek, H., and Rokne, J., Computer Methods for the Range of Functions, Ellis Horwood, Chichester, England, 1984. Rice, J. R., Numerical Methods, Software and Analysis, McGraw Hill, New York, 1983. Sastry, S. S., Introductory Methods of Numerical Analysis, Prentice Hall of India, New Delhi, India, 2001. Scarborough, J. B., Numerical Mathematical Analysis, 6th ed., Johns Hopkins Press, Baltimore, MD, 1966. Scheid, F., Schaum’s Outline of Theory and Problems in Numerical Analysis, 2nd ed., Schaum’s Outline Series, McGraw Hill, New York, 1988. Schiesser, W. E., Computational Mathematics in Engineering and Applied Science, CRC Press, Boca Raton, FL, 1994. Shampine, L. F., Numerical Solution of Ordinary Differential Equations, Chapman and Hall, New York, 1994. Sharma, J. N., Numerical Methods for Engineers and Scientists, Narosa Publishing House, New Delhi, India, 2004. Smith, G. D., Numerical Solution of Partial Differential Equations: Finite Difference Methods, 3rd ed., Oxford University Press, Oxford, England, 1985. Smith, W. A., Elementary Numerical Analysis, Prentice Hall, Englewood Cliffs, NJ, 1986. Snyder, M. A., Chebyshev Methods in Numerical Approximation, Prentice Hall, Englewood Cliffs, NJ, 1966. Stanton, R. G., Numerical Methods for Science and Engineering, Prentice Hall of India, New Delhi, India, 1967. Stark, P. A., Introduction to Numerical Methods, Macmillan, New York, 1970. Stewart, G. W., Matrix Algorithms, Vol. 1, Basic Decompositions, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1998. Stoer, J., and Bulirsch, R., Introduction to Numerical Analysis, SpringerVerlog, New York, 1980. Stroud, A., and Secrets, D., Gaussian Quadrature Formulas, Prentice-Hall, Englewood Cliffs, NJ, 1966.

ANMF.CH19_Bib_1PP.indd 1182

2/24/2023 5:43:29 PM

Bibliography • 1183

Stroud, A. H., Numerical Quadrature and Solution of Ordinary Differential Equations, Springer-Verlog, New York, 1974. Taylor, J. R., An Introduction to Error Analysis, University Science Books, Mill Valley, CA, 1982. Traub, J. F., Iterative Methods for the Solution of Equations, Prentice Hall, Englewood Cliffs, NJ, 1964. Trefethen, L. N., and Bau, D., Numerical Linear Algebra, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, 1997. Tyrtyshnikov, E. E., A Brief Introduction to Numerical Analysis, Birkhauser, Boston, 1997. Ueberhuber, C. W., Numerical Computation 1: Methods, Software, and Analysis, Springer-Verlog, New York, 1997. Ueberhuber, C. W., Numerical Computation 2: Methods, Software, and Analysis, Springer-Verlog, New York, 1997. Vemuri, V., and Karplus, W. J., Digital Computer Treatment of Partial Differential Equations, Prentice Hall, Englewood Cliffs, NJ, 1981. Vichnevetsky, R., Computer Methods for Partial Differential Equations, Vol. 1: Elliptic Equations and the Finite Element Method, Prentice Hall, ­Englewood Cliffs, NJ, 1981. Vichnevetsky, R., Computer Methods for Partial Differential Equations, Vol. 2: Initial Value Problems, Prentice Hall, Englewood Cliffs, NJ, 1982. Wendroff, B., Theoretical Numerical Analysis, Academic Press, New York, 1966. Wilkinson, J. H., Rounding Errors in Algebraic Processes, Dover, New York, 1994. Yokowitz, S., and Szidarovsky, F., An Introduction to Numerical Computation, Macmillan, New York, 1986. Yong, D. M., and Gregory, R. T., A Survey of Numerical Mathematics, Vol. 1 and 2, Addison-Wesley, Reading, MA, 1972. Young, D., Iterative Solution for Large Linear Systems, Academic Press, New York, 1971. Optimization Arora, J. S., Introduction to Optimal Design, McGraw-Hill, New York, 1989. Bellman, R., Dynamic Programming, Princeton University Press, Princeton, NJ, 1957.

ANMF.CH19_Bib_1PP.indd 1183

2/24/2023 5:43:29 PM

1184 • Applied Numerical Methods Using MATLAB Bertekas, D. P., Constrained Optimization and Lagrange Methods, Academic Press, New York, 1982. Branch, M. A., and Grace, A., Optimization Toolbox: User’s Guide, MathWorks Inc., Natick, MA, 1996. Brent, R. P., Algorithm for Minimization Without Derivatives, Prentice-Hall, Englewood Cliffs, NJ, 1973. Coleman, T. F., Branch, M. A., and Grace, A., Optimization Toolbox—for Use ith MATLAB¿, User’s Guide, Version 2, MathWOrk Inc., Natick, MA, 1999. Fiacco, A. V., and McCormick, G. P., Nonlinear Programming Sequential Unconstrained Minimization Techniques, Wiley, New York, 1968. Fletcher, R., Practical Methods for Optimization, Vol.1, Wiley, New York, 1980. Fox, R. L., Optimization Methods for Engineering Design, Addison-Wesley, Reading, MA, 1971. Gill, P. E., Murray, W., and Wright, M.H., Practical Optimization, Academic Press, New York, 1981. Huang, H. Y., Unified Approach to Quadratically Convergent Algorithms for Function Minimization, Journal of Optimization Theory and Applications, 5, 405–423, 1970. Hooke, R, and Jeeves, T. A., Direct Search of Numerical and Statistical Problems, Journal of the Association of Computing Machinery, 8, 212–229, 1966. Mangasarian, O. L., Nonlinear Programming, McGraw Hill, New York, 1969. Papalambros, P. Y. and Wilde, J. D., Principals of Optimal Design: Modeling and Computation, Cambridge University Press, New York, 1988. Powell, M. J. D., Nonlinear Optimization, Academic Press, New York, 1981. Reklaitis, G. V., Ravindran, A, and Ragsdell, K. M., Engineering Optimization: Methods and Applications, Wiley, New York, 1983. Syslo, M. M., Deo, N., and Kowalik, J. S., Discrete Optimization Algorithms, Prentice-Hall, Englewood Cliffs, NJ, 1983. Vanderpaats, G. N., Numerical Optimization Techniques for Engineering Design, McGraw-Hill, New York, 1984. White, D. J., Dynamic Programming, Holden-Day, San Francisco, 1969.

ANMF.CH19_Bib_1PP.indd 1184

2/24/2023 5:43:29 PM

Bibliography • 1185

MATLAB Chapman, S. J., MATLAB Programming for Engineers, 2nd ed., Brooks/Cole, Thomson Learning, Pacific Grove, CA, 2002. Dabney, J. B., and Harman, T. L., Mastering SIMULINK 4, Prentice Hall, Upper Saddle River, NJ, 2001. Djaferis, T. E., Automatic Control: The Power of Feedback Using MATLAB, Brooks/Cole, Thomson Learning, Pacific Grove, CA, 2000. Dukkipati, R. V., A Guide to MATLAB for Beginners, New Age International Publishing, New Delhi, India, 2010. Dukkipati, R. V., Analysis and Design of Control Systems Using MATLAB, New Age International Publishing, New Delhi, India, 2006. Dukkipati, R. V., and Cupic, K., Applied Numerical Methods With MATLAB, New International Publishing (P) Ltd., New Delhi, India, 2009. Dukkipati, R. V., and Srinivas, J., Solving Engineering Mechanics Problems With MATLAB, New Age International Publishing, New Delhi, India, 2007. Dukkipati, R. V., Applied Numerical Methods With MATLAB Through Solved Problems, New International Publishing (P) Ltd., New Delhi, India, 2010. Dukkipati, R. V., MATLAB for Control Systems Engineer, New Age International Publishing, New Delhi, India, 2010. Dukkipati, R. V., MATLAB for Electrical Engineers, New Age International Publishing, New Delhi, India, 2007. Dukkipati, R. V., MATLAB for Mechanical Engineers, New Age International Publishing, New Delhi, India, 2009. Dukkipati, R. V., MATLAB: An Introduction and Applications, New Age International Publishing, New Delhi, India, 2009. Dukkipati, R. V., Solving Engineering System Dynamics Problems with MATLAB, New Age International Publishing, New Delhi, India, 2006. Dukkipati, R. V., Solving Vibration Analysis Problems With MATLAB, New Age International Publishing, New Delhi, India, 2006. Etter, D. M., Engineering Problem Solving With MATLAB, Prentice-Hall, Englewood Cliffs, NJ, 1993. Gardner, J. F., Simulation of Machines Using MATLAB and SIMULINK, Brooks/Cole, Thomson Learning, Pacific Grove, CA, 2001.

ANMF.CH19_Bib_1PP.indd 1185

2/24/2023 5:43:29 PM

1186 • Applied Numerical Methods Using MATLAB Herniter, M. E., Programming in MATLAB, Brooks/Cole, Pacific Grove, CA, 2001. Leonard, N. E., and Levine, W. S., Using MATLAB to Analyze and Design Control Systems, Addison-Wesley, Redwood City, CA, 1995. Moler, C., The Student Edition of MATLAB for MS-DOS Personal C ­ omputers With 3-1/2” Disks, MATLAB Curriculum Series, The MathWorks, Inc., Natick, MA, 2002. Ogata, K., Designing Linear Control Systems With MATLAB, Prentice Hall, Upper Saddle River, NJ, 1994. Ogata, K., Solving Control Engineering Problems With MATLAB, Prentice Hall, Upper Saddle River, NJ, 1994. Pratap, R., Getting Started With MATLAB- A Quick Introduction for Scientists and Engineers, Oxford University Press, New York, 2002. Saadat, H., Computational Aids in Control Systems Using MATLAB, McGraw Hill, New York, 1993. Sigman, K., and Davis, T. A., MATLAB Primer, 6th ed, Chapman & Hall/ CRCPress, Boca Raton, FL, 2002. The MathWorks, Inc., SIMULINK, Version 3, The MathWorks, Inc., Natick, MA, 1999. The MathWorks, Inc., MATLAB: Application Program Interface Reference Version 6, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Control System Toolbox User’s Guide, ­Version 4, The MathWorks, Inc., Natick, MA, 1992–1998. The MathWorks, Inc., MATLAB: Creating Graphical User Interfaces, Version 1, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Function Reference, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Release Notes for Release 12, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Symbolic Math Toolbox User’s Guide, ­Version 2, The MathWorks, Inc., Natick, MA, 1993–1997. The MathWorks, Inc., MATLAB: Using MATLAB Graphics, Version 6, The MathWorks, Inc., Natick, MA, 2000. The MathWorks, Inc., MATLAB: Using MATLAB, Version 6, The ­MathWorks, Inc., Natick, MA, 2000.

ANMF.CH19_Bib_1PP.indd 1186

2/24/2023 5:43:29 PM

Index A Absolute and relative errors, 12–14 Adams-Bashforth formula, 606 Adams-Moulton corrector formula, 607 Adams-Moulton method, 605–611 Aitken’s acceleration formula, 30–32 Algebraic and transcendental equations Aitken’s Δ2 method, 226–229 Brent’s method, 229–236 Chebyshev’s method, 225–226 direct methods, 177 indirect or iterative methods bracketing methods, 177–178 comparison, 238–239 open methods, 178 MATLAB built-in function, 239–243 Muller’s method, 219–225 secant method, 212–219 Alternating direction implicit (ADI) method, 1032 Approximating curve, 405 Arithmetic errors, 11 B Backward error analysis, 23 Bisection method

ANMF.CH20_Index_1PP.indd 1187

advantages, 181 algorithm, 179–180 error bounds, 180–181 MATLAB program, 182–186 method of finding solution, 179 Blunders and mistakes, 11 Boole’s rule, 506–507 Bracketing method algebraic and transcendental equations, 177–178 numerical optimization methods, 875 Brent’s method, 229–236 Burlirsch-Stoer method, 635–636 C Central difference method, 974 Chebyshev’s formula, 226 Cholesky’s triangularization method, 68–78 Coefficient of determination computational formula, 422 error sum of squares, 425 regression sum of squares, 423, 425 standard deviation of errors, 424 total sum of squares, 422, 425 value of, 423

2/24/2023 5:44:07 PM

1188 • Index Compound quadratures, 523 Crank-Nicolson implicit method, 993–996 Crout’s triangularization method. see Cholesky’s triangularization method Curve fitting, 405 approximating curves, 406 cubic spring function, 455 power function with a specified power, 455 D Direct numerical integration methods MATLAB program, 684–747 multi degree of freedom system (see Explicit and Implicit direct integration schemes) single degree of freedom system central difference method, 663–664 finite difference method, 662–663 Runge-Kutta method, 664–666 Double precision method, 22 E Error propagation, 22 Expansion method, 5 Explicit central difference method, 1044 Explicit direct integration schemes central difference method, 667–669 fourth-order Runge-Kutta method, 671–673 two-cycle iteration with trapezoidal rule, 670–671 Explicit forward Euler method, 989–992 Extrapolation, 285

ANMF.CH20_Index_1PP.indd 1188

F Finite difference operators Δ operator properties, 296–297 average operator μ, 298 backward differences, 288–289 central differences, 289–291 differential operator, D, 299 error propagation in difference table, 293–296 factorial notation, 304–310 forward differences, 287–288 relation among operators, 299–304 shift operator, E, 298 Fletcher-Reeves method, 906–917 Formula truncation error, 19 Forward error analysis, 23 Forward Euler method, 569 Fundamental theorem of calculus, 483 G Gauss elimination method, 50–58 Gaussian quadrature methods, 524 Gauss-Chebyshev quadrature method, 533–535 Gauss-Hermite quadrature, 537–538 Gaussian integration formula, 523–525 Gauss-Lagendre quadrature, 527–533 Gauss-Laguerre quadrature, 535–536 MATLAB programs, 539–543 orthogonal polynomials, 525–527 Gram-Schmidt orthogonalization process, 453–455 H Heun’s method, 579 Hexadecimal (base-16) system, 9–10

2/24/2023 5:44:07 PM

Index • 1189

Hooke and Jeeves method, 917–924 Houbolt method, 673–676 I Implicit direct integration schemes Houbolt method, 673–676 Newmark beta integration method, 679–682 Park Stiffly stable method, 682–684 Wilson theta method, 676–679 Inherent errors, 14 Initial-value problems, ODE, 558 Burlirsch-Stoer method, 635–636 Euler’s formula, 634 fourth-order Runge-Kutta method, 632–634 modified Euler’s formula, 635 Picard’s method, 630–631 Runge-Kutta-Butcher method, 637 Runge-Kutta-Fehlberg method, 636–637 second-order Runge-Kutta method, 631–632 Taylor series method, 628–630 Interior penalty function method, 925–931 Interpolating quadratures, 523 Interpolation central difference formulae Bessel’s formula, 348–351 difference table, 342 Gauss’s backward interpolation formula, 346–348 Gauss’s forward interpolation formula, 343–345 Laplace-Everett’s formula, 352–355 selection of interpolation formula, 355

ANMF.CH20_Index_1PP.indd 1189

Sheppard’s operator δ, 343 Stirling’s formula, 351–352 cubic spline interpolation method advantages, 360 clamped boundary condition, 363 Lagrange’s two-point interpolation, 361 natural boundary condition, 363 piecewise polynomial approximation, 360 divided differences, 355–360 with equal intervals error in the interpolation formula, 328–331 missing values, 310 Newton’s backward interpolation formula, 322–328 Newton’s binomial expansion formula, 310–312 Newton’s forward interpolation formula, 313–322 formulae, 286 generalized spline method cubic splines, 371–375 end conditions, 376 linear splines, 367–368 MATLAB interp1 function, 381–390 MATLAB spline function, 376–378 multidimensional interpolation, 378–381 quadratic splines, 368–371 spline functions, 366 Harper’s definition, 285 Hiral’s definition, 285 Theile’s definition, 285

2/24/2023 5:44:07 PM

1190 • Index with unequal intervals Hermite’s interpolation formula, 338–340 inverse interpolation, 340 Lagrange’s formula for inverse interpolation, 340–342 Lagrange’s formula for unequal intervals, 334–338 linear Lagrange interpolating polynomial for equal intervals, 331–332 Interval arithmetic method, 22–23 Interval halving method, 178. see also Bisection method L Lagrange’s interpolation formula, 336 Least significant digit (LSD), 5 Least-squares approximation, 449 Least-squares method for continuous data, 449–451 Linear convergence theorem, 29 Linear correlation coefficient, 425 explained and unexplained variation, 428–429 perfect positive and negative, 425 properties, 427–428 strong and weak positive/negative, 426 Linear equation criteria for best fit, 410–412 curve fitting, 408–410 with one independent variable, 407 Linear least-squares regression, 412–414 Linear regression, 407 quantification of error, 443–445 Linear regression analysis assumptions, 421

ANMF.CH20_Index_1PP.indd 1190

estimated regression model, 416 MATLAB built-in function polyfit, 418–419 polyval, 419–420 population regression line, 416 random error term, 415 relationship between two variables, 414–415 scatter diagram, 416 Linear regression model, 414 Linear systems of equations algebraic equations, 39–40 Cholesky’s triangularization method, 68–78 Crout’s method, 79–88 direct and iterative methods, 40–41 Gauss elimination method, 50–58 Gauss-Jordan method LU decomposition, 67–68 MATLAB program, 59–67 Gauss-Seidel method, 104–113 inverse of a matrix, 41–44 Jacobi iteration method, 94–104 matrix eigenvalue problems accumulated transformation matrix, 136–137 Gerschgorin’s circle theorem, 137–139 Householder’s method, 133–136 inverse power method, 161–163 Jacobi method, 114–133 power method, 157–161 QR method, 146–157 Sturm sequence, 139–140 matrix inversion method, 44–50 Thomas method for tridiagonal systems, 88–94

2/24/2023 5:44:07 PM

Index • 1191

M Machine epsilon, 21–22 Mathematical approximation errors, 11 MATrix LABoratory (MATLAB) basics abort command, 759 arithmetic operations, 760 with complex numbers, 763 arrays, 766–768 built-in functions, 768–769 operations, 770–772 clc command, 759 commands, 758–759 common math functions, 761 complex number functions, 763 display formats, 760–761 display windows, 758 element-by-element operations, 772–774 Expo, 759 exponential functions, 762 functions, 791–792 general commands directory information, 766 general information, 766 online help, 765 termination, 766 workspace information, 765 graphics 2-D plots, 792–793 3-D plots, 795–796 overlay plots, 794–795 save and print, 802 specialized 2-D plots, 793–794 help, 759 input/output in, 802–803 managing variables, commands for, 765 percent symbol (%), 759 polynomials, 775–778

ANMF.CH20_Index_1PP.indd 1191

predefined variables, 764 programming in built-in functions, 788–789 conditional statements, 789–790 else and elseif clauses, 790 nested if statements, 790 order of precedence, 788 relational and logical operators, 787 while loop, 790–791 random numbers, 774–775 round-off functions, 763 script files, 784–786 semicolon (;), 759 start and quit, 758 statements and variables, 759–760 symbolic mathematics, 803–809 system of linear equations, 778–784 trigonometric and hyperbolic functions, 762 variable names, 764 Maximum likelihood principle, 443 Method of factorization. see Cholesky’s triangularization method Method of false position algorithm, 188 description, 186–187 MATLAB program, 188–194 Milne’s predictor-corrector method, 611–614 Modeling errors, 11 Most and least significant bit, 5 Most significant digit (MSD), 5 Muller’s method, 219–225 Multiple linear regression, 445–448 N Neumann boundary conditions, 976 Newmark beta integration method, 679–682

2/24/2023 5:44:07 PM

1192 • Index Newton-Cotes closed quadrature formula, 484, 486 Newton-Raphson method algorithm, 195 convergence of, 196–197 disadvantages, 211 drawbacks, 194 MATLAB program, 198–203 modified, 203–205 rate of convergence, 197 Newton’s forward interpolation formula, 313–314 Newton’s method, 194 system of nonlinear equations, 236–238 Nonlinear regression model, 414 for curve fitting, 429–435 Numerical computations error considerations absolute and relative errors, 12–14 accuracy and precision, 11–12 error propagation, 22 inherent errors, 14 machine epsilon, 21–22 round-off errors, 14–19 sources of errors, 11 truncation errors, 19–21 error estimates backward error analysis, 23 double precision method, 22 forward error analysis, 23 interval arithmetic method, 22–23 significant digit arithmetic method, 23 statistical approach, 23 error formula function approximation, 24–25 stability and condition, 25–27 uncertainty in data or noise, 27–28

ANMF.CH20_Index_1PP.indd 1192

number representation binary, decimal, and hexadecimal conversion, 6–7 in computer languages, 5 in decimal and binary forms, 5 expansion method, 5 hexadecimal (base-16) system, 9–10 octal (base-8) numbering system, 8 sequences Aitken’s acceleration process, 30–32 linear convergence, 28–29 quadratic convergence, 29–30 Taylor’s theorem, 1–4 Numerical differentiation cubic spline method, 270–271 diff and gradient function, 274–277 maxima and minima of tabulated function, 268–269 Newton’s backward interpolation formula, 260–262 Newton’s forward interpolation formula, 252–259 Richardson extrapolation, 271–273 Stirling’s interpolation formula, 263–267 of unequally spaced data, 273–274 Numerical integration Boole’s rule, 506–507 closed and open integration, 483–484 double integral, 544–549 error of approximation, 485 Gaussian quadrature methods Gauss-Chebyshev quadrature method, 533–535 Gauss-Hermite quadrature, 537–538 Gaussian integration formula, 523–525 Gauss-Lagendre quadrature, 527–533

2/24/2023 5:44:07 PM

Index • 1193

Gauss-Laguerre quadrature, 535–536 MATLAB programs, 539–543 orthogonal polynomials, 525–527 Newton-Cote’s method forward interpolation formula, 485–486 relative error of approximation, 485 Romberg’s integration MATLAB program, 516–523 Richardson extrapolation formula, 512–513 Simpson’s 1/3 rule error estimate in, 495–496 MATLAB built-in functions, 497–502 MATLAB program, 496–497 Simpson’s 3/8 rule, 502–506 trapezoidal rule, 486–488 error estimate in, 489–490 MATLAB built-in function, 490–493 Weddle’s rule, 507–511 O Octal (base-8) numbering system, 8 Optimization conjugate directions quadratic convergence, 893 (i - 1)th search step, 895 in two dimensions, 896–897 Fletcher-Reeves method, 906–917 gradient methods concept of, 885 direction of step in, 886 using line searches, 887 Hooke and Jeeves method, 917–924 interior penalty function method, 925–931 MATLAB built-in functions

ANMF.CH20_Index_1PP.indd 1193

fminbnd, 931–935 fminsearch, 935–940 maxima and minima of a function, 857 minimization with constraints using Lagrange multipliers, 860–864 multidimensional unconstrained optimization techniques, 882–885 Newton’s method, 887–893 numerical methods bracketing method, 875 golden-section search method, 875–878 local and global optima, 873–874 optimization involving single variables, 865–873 Powell’s method, 901–906 successive linear approximation method, 924–925 unconstrained minimization of functions, 859 Ordinary differential equation (ODE) boundary-value problem, 558 explicit and implicit methods, 559–560 first-order equation, 626–627 general form of, 557 initial-value problems, 558 Burlirsch-Stoer method, 635–636 Euler’s formula, 634 fourth-order Runge-Kutta method, 632–634 modified Euler’s formula, 635 Picard’s method, 630–631 Runge-Kutta-Butcher method, 637 Runge-Kutta-Fehlberg method, 636–637 second-order Runge-Kutta method, 631–632 Taylor series method, 628–630

2/24/2023 5:44:07 PM

1194 • Index MATLAB built-in functions, 649 ODE solvers, 614–626 mesh points, 559 one-step or single-step methods, 558, 559 Picard’s method, 561–564 Taylor’s series method, 564–569 particular solution, 558 second-order boundary value problem, 648–649 second-order linear initial value problem, 646–647 single first-order, 558 step-by-step or marching methods, 558, 559 Euler’s explicit method, 569–579 modified Euler’s method, 579–586 predictor-corrector methods, 604–614 Runge-Kutta methods, 586–604 two-point boundary value problems finite difference method, 638–639 second-order differential equation, 639–641 shooting method, 641–646 Orthogonal polynomials, 449 Gram-Schmidt orthogonalization process, 453–455 least-square approximation using, 451–453 P Park Stiffly stable method, 682–684 Partial differential equations (PDE) Cauchy’s problem, 966 classification, 971–972 Dirichlet’s problem, 965–966 elliptic equations

ANMF.CH20_Index_1PP.indd 1194

boundary conditions, 975–977 central difference method, 972–975 Gauss-Seidel method, 986 iterative solution methods, 977–985 Jacobi’s method, 985 successive over-relaxation method, 986–988 finite-difference approximation, 967 heat equation, 969–970 Laplace’s equation, 968–969 linear second-order partial differential equation elliptic equation, 963, 964 hyperbolic equation, 963, 965 parabolic equation, 962, 965 MATLAB built-in functions, 1060–1079 one-dimensional hyperbolic equations D’Alembert’s solution, 1038–1043 explicit central difference method, 1043–1050 one-dimensional parabolic equations Crank-Nicolson implicit method, 993–996 explicit forward Euler method, 989–992 function [alpha,r,x,y,U] = Poisson (a,b,m,n,q,tol,f,g), 1013–1019 function [t,x,U] =Heatone(T,a,m, n,beta,c,f,g), 996–999 function [t,x,U] = Waveone(T,a,m,n,beta,f,g), 1005–1008 function [x,y,U] = Heattwo(T,a, b,m,n,p,beta,f,g), 999–1005 function [x,y,U] = Wavetwo (T,a,b, m,n,p,beta,f,g), 1008–1013

2/24/2023 5:44:07 PM

Index • 1195

implicit backward Euler method, 992–993 two-dimensional hyperbolic equations, 1050–1059 two-dimensional parabolic equations, 1030–1037 wave equation, 970–971 Pearson product moment correlation coefficient, 425 Picard’s method, 561–564 Poisson’s equation, 972 Polynomial regression MATLAB built-in functions, 438–443 normal equations, 437 second-order polynomial, 435 Positional numbering system, 4 Powell’s method, 901–906 Q Quadratic convergence theorem, 30 R Recurrence formula, 664 Regula falsi method, 186. see also Method of false position Round-off errors, 14–19 Runge-Kutta-Butcher method, 637 Runge-Kutta-Fehlberg method, 636–637 Runge-Kutta method, 664–666 fourth-order, 671–673 second-order, 631–632 step-by-step or marching methods, ODE, 586–604 S Scatter diagram, 405 Secant method convergence of, 212–213 formula, 212

ANMF.CH20_Index_1PP.indd 1195

MATLAB program, 214–219 rate of convergence, 213 Significant digit arithmetic method, 23 Significant digits, 13 Simply truncation error, 19 Simpson’s 1/3 rule error estimate in, 495–496 MATLAB built-in functions, 497–502 programs, 496–497 Simpson’s 3/8 rule, 502–506 Spline Toolbox™, 387 Step-by-step or marching methods, ODE, 558, 559 Euler’s explicit method, 569–579 modified Euler’s method, 579–586 predictor-corrector methods Adams-Moulton method, 605–611 advantages, 604 Milne’s predictor-corrector method, 611–614 Runge-Kutta methods of order four vs. Euler’s method, 600 local truncation error, 592 MATLAB program, 594 Runge-Kutta methods of order two local truncation error, 588 Taylor series expansion, 587 Successive approximation method convergence criteria, 206–207 error estimate, 207–211 Successive linear approximation method, 924–925 T Taylor’s series expansion, 2 Taylor’s series method, 564–569 Taylor’s theorem, 1–4 Total numerical error, 20

2/24/2023 5:44:07 PM

1196 • Index Trapezoidal rule, 486–488 error estimate in, 489–490 MATLAB built-in functions, 490–493 Truncation errors, 19–21. see also Mathematical approximation errors

ANMF.CH20_Index_1PP.indd 1196

W Weddle’s rule, 507–511 Weighted least-squares method, 448–449 Wilson theta method, 676–679

2/24/2023 5:44:07 PM