Docsity

Computational Mathematics Models Methods and Analysis with Matlab - Robert E. White, Notas de estudo de Engenharia Elétrica

Pré-visualização parcial do texto

Baixe Computational Mathematics Models Methods and Analysis with Matlab - Robert E. White e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!

COMPUTATIONAL

MATHEMATICS

Models, Methods, and Analysis

with MATLAB and MPI

CHAPMAN & HALL/CRC

A CRC Press Company Boca Raton London New York Washington, D.C.

COMPUTATIONAL

MATHEMATICS

ROBERT E. WHITE

Models, Methods, and Analysis

with MATLAB and MPI

Computational Mathematics:

Models, Methods and Analysis

with

MATLAB and MPI

R. E. White
Department of Mathematics
North Carolina State University
Updated on August 3, 2003
To Be Published by CRC Press in 2003
[email protected]

Contents

List of Figures ix

List of Tables xi

Preface xiii

CONTENTS vii

9.6 GMRES(m) and MPI........................ 372

Bibliography 379

List of Figures

  • 1 Discrete Time-Space Models Introduction xv
    • 1.1 Newton Cooling Models
    • 1.2 Heat Diusion in a Wire
    • 1.3 Diusion in a Wire with Little Insulation
    • 1.4 Flow and Decay of a Pollutant in a Stream
    • 1.5 Heat and Mass Transfer in Two Directions
    • 1.6 Convergence Analysis
  • 2 Steady State Discrete Models
    • 2.1 Steady State and Triangular Solves
    • 2.2 Heat Diusion and Gauss Elimination
    • 2.3 Cooling Fin and Tridiagonal Matrices
    • 2.4 Schur Complement
    • 2.5 Convergence to Steady State
    • 2.6 Convergence to Continuous Model
  • 3 Poisson Equation Models
    • 3.1 Steady State and Iterative Methods
    • 3.2 Heat Transfer in 2D Fin and SOR
    • 3.3 Fluid Flow in a 2D Porous Medium
    • 3.4 Ideal Fluid Flow
    • 3.5 Deformed Membrane and Steepest Descent
    • 3.6 Conjugate Gradient Method
  • 4 Nonlinear and 3D Models vi CONTENTS
    • 4.1 Nonlinear Problems in One Variable
    • 4.2 Nonlinear Heat Transfer in a Wire
    • 4.3 Nonlinear Heat Transfer in 2D
    • 4.4 Steady State 3D Heat Diusion
    • 4.5 Time Dependent 3D Diusion
    • 4.6 High Performance Computations in 3D
  • 5 Epidemics, Images and Money
    • 5.1 Epidemics and Dispersion
    • 5.2 Epidemic Dispersion in 2D
    • 5.3 Image Restoration
    • 5.4 Restoration in 2D
    • 5.5 Option Contract Models
    • 5.6 Black-Scholes Model for Two Assets
  • 6 High Performance Computing
    • 6.1 Vector Computers and Matrix Products
    • 6.2 Vector Computations for Heat Diusion
    • 6.3 Multiprocessors and Mass Transfer
    • 6.4 MPI and the IBM/SP
    • 6.5 MPI and Matrix Products
    • 6.6 MPI and 2D Models
  • 7 Message Passing Interface
    • 7.1 Basic MPI Subroutines
    • 7.2 Reduce and Broadcast
    • 7.3 Gather and Scatter
    • 7.4 Grouped Data Types
    • 7.5 Communicators
    • 7.6 Fox Algorithm for AB
  • 8 Classical Methods for Ax = d
    • 8.1 Gauss Elimination
    • 8.2 Symmetric Positive Definite Matrices
    • 8.3 Domain Decomposition and MPI
    • 8.4 SOR and P-regular Splittings
    • 8.5 SOR and MPI
    • 8.6 Parallel ADI Schemes
  • 9 Krylov Methods for Ax = d
    • 9.1 Conjugate Gradient Method
    • 9.2 Preconditioners
    • 9.3 PCG and MPI
    • 9.4 Least Squares
    • 9.5 GMRES
  • 1.1.1 Temperature versus Time
  • 1.1.2 Steady State Temperature
  • 1.1.3 Unstable Computation
  • 1.2.1 Diusion in a Wire
  • 1.2.2 Time-Space Grid
  • 1.2.3 Temperature versus Time-Space
  • 1.2.4 Unstable Computation
  • 1.2.5 Steady State Temperature
  • 1.3.1 Diusion in a Wire with csur = .0000 and .0005
  • 1.3.2 Diusion in a Wire with n = 5 and
  • 1.4.1 Polluted Stream
  • 1.4.2 Concentration of Pollutant
  • 1.4.3 Unstable Concentration Computation
  • 1.5.1 Heat or Mass Entering or Leaving
  • 1.5.2 Temperature at Final Time
  • 1.5.3 Heat Diusing Out a Fin
  • 1.5.4 Concentration at the Final Time
  • 1.5.5 Concentrations at Dierent Times
  • 1.6.1 Euler Approximations
  • 2.1.1 Infinite or None or One Solution(s)
  • 2.2.1 Gaussian Elimination
  • 2.3.1 Thin Cooling Fin
  • 2.3.2 Temperature for c = .1, .01, .001, .0001
  • 2.6.1 Variable r = .1, .2 and .3
  • 2.6.2 Variable n = 4, 8 and
  • 3.1.1 Cooling Fin with T = .05, .10 and .15
  • 3.2.1 Diusion in Two Directions
  • 3.2.2 Temperature and Contours of Fin
  • 3.2.3 Cooling Fin Grid
  • 3.3.1 Incompressible 2D Fluid
  • 3.3.2 Groundwater 2D Porous Flow
  • 3.3.3 Pressure for Two Wells x LIST OF FIGURES
  • 3.4.1 Ideal Flow About an Obstacle
  • 3.4.2 Irrotational 2D Flow y{  x| =
  • 3.4.3 Flow Around an Obstacle
  • 3.4.4 Two Paths to (x,y)
  • 3.5.1 Steepest Descent norm(r)
  • 3.6.1 Convergence for CG and PCG
  • 4.2.1 Change in F
  • 4.2.2 Temperatures for Variable c
  • 4.4.1 Heat Diusion in 3D
  • 4.4.2 Temperatures Inside a 3D Fin
  • 4.5.1 Passive Solar Storage
  • 4.5.2 Slab is Gaining Heat
  • 4.5.3 Slab is Cooling
  • 4.6.1 Domain Decompostion in 3D
  • 4.6.2 Domain Decomposition Matrix
  • 5.1.1 Infected and Susceptible versus Space
  • 5.2.1 Grid with Artificial Grid Points
  • 5.2.2 Infected and Susceptible at Time = 0.3
  • 5.3.1 Three Curves with Jumps
  • 5.3.2 Restored 1D Image
  • 5.4.1 Restored 2D Image
  • 5.5.1 Value of American Put Option
  • 5.5.2 P(S,T-t) for Variable Times
  • 5.5.3 Option Values for Variable Volatilities
  • 5.5.4 Optimal Exercise of an American Put
  • 5.6.1 American Put with Two Assets
  • 5.6.2 max(H 1 + H 2  V 1  V 2 > 0)
  • 5.6.3 max(H 1  V 1 > 0) + max(H 2  V 2 > 0)
  • 6.1.1 von Neumann Computer
  • 6.1.2 Shared Memory Multiprocessor
  • 6.1.3 Floating Point Add
  • 6.1.4 Bit Adder
  • 6.1.5 Vector Pipeline for Floating Point Add
  • 6.2.1 Temperature in Fin at t =
  • 6.3.1 Ring and Complete Multiprocessors
  • 6.3.2 Hypercube Multiprocessor
  • 6.3.3 Concentration at t =
  • 6.4.1 Fan-out Communication
  • 6.6.1 Space Grid with Four Subblocks
  • 6.6.2 Send and Receive for Processors
  • 7.2.1 A Fan-in Communication
  • 1.6.1 Euler Errors at t = List of Tables
  • 1.6.2 Errors for Flow
  • 1.6.3 Errors for Heat
  • 2.6.1 Second Order Convergence
  • 3.1.1 Variable SOR Parameter
  • 3.2.1 Convergence and SOR Parameter
  • 4.1.1 Quadratic Convergence
  • 4.1.2 Local Convergence
  • 4.2.1 Newton’s Rapid Convergence
  • 6.1.1 Truth Table for Bit Adder
  • 6.1.2 Matrix-vector Computation Times
  • 6.2.1 Heat Diusion Vector Times
  • 6.3.1 Speedup and E!ciency
  • 6.3.2 HPF for 2D Diusion
  • 6.4.1 MPI Times for trapempi.f
  • 6.5.1 Matrix-vector Product mflops
  • 6.5.2 Matrix-matrix Product mflops
  • 6.6.1 Processor Times for Diusion
  • 6.6.2 Processor Times for Pollutant
  • 7.6.1 Fox Times
  • 8.3.1 MPI Times for geddmpi.f
  • 8.5.1 MPI Times for sorddmpi.f
  • 9.3.1 MPI Times for cgssormpi.f
  • 9.6.1 MPI Times for gmresmmpi.f

Preface

This book evolved from the need to migrate computational science into under- graduate education. It is intended for students who have had basic physics, programming, matrices and multivariable calculus.

The choice of topics in the book has been influenced by the Undergraduate Computational Engineering and Science Project (a United States Department of Energy funded eort), which was a series of meetings during the 1990s. These meetings focused on the nature and content for computational science undergraduate education. They were attended by a diverse group of science and engineering teachers and professionals, and the continuation of some of

University in fall semesters since 1992. The other four chapters were developed in 2002 and taught in the 2002-03 academic year.

The department of mathematics at North Carolina State University has given me the time to focus on the challenge of introducing computational science materials into the undergraduate curriculum. The North Carolina Supercom-

tutorials and computer time on supercomputers. Many students have made important suggestions, and Carol Cox Benzi contributed some course materials

with the initial use of MATLAB

°R

. MATLAB is a registered trademark of The MathWorks, Inc. For product information, please contact:

The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 USA Tel: 508-647- Fax: 508-647-

? A.

xiii

Variations of Chapters 1-4 and 6 have been taught at North Carolina State

these activities can be found at the Krell Institute, http://www.krellinst.org.

puting Center, http://www.ncsc.org, has provided the students with valuable

Web: www.mathworks.com http://www.mathworks.com/

E-mail: [email protected]

Introduction

Computational science is a blend of applications, computations and mathemat- ics. It is a mode of scientific investigation that supplements the traditional laboratory and theoretical methods of acquiring knowledge. This is done by formulating mathematical models whose solutions are approximated by com- puter simulations. By making a sequence of adjustments to the model and subsequent computations one can gain some insights into the application area under consideration. This text attempts to illustrate this process as a method for scientific investigation. Each section of the first six chapters is motivated by a particular application, discrete or continuous model, numerical method, computer implementation and an assessment of what has been done.

Applications include heat diusion to cooling fins and solar energy storage, pollutant transfer in streams and lakes, models of vector and multiprocessing computers, ideal and porous fluid flows, deformed membranes, epidemic models with dispersion, image restoration and value of American put option contracts. The models are initially introduced as discrete in time and space, and this allows for an early introduction to partial dierential equations. The discrete models have the form of matrix products or linear and nonlinear systems. Methods in- clude sparse matrix iteration with stability constraints, sparse matrix solutions via variation on Gauss elimination, successive over-relaxation, conjugate gradi- ent, and minimum residual methods. Picard and Newton methods are used to approximate the solution to nonlinear systems.

Most sections in the first five chapters have MATLAB

°R

the very aordable current student version of MATLAB. They are intended to be studied and not used as a "black box." The M ATLAB codes should be used as a first step towards more sophisticated numerical modeling. These codes do provide a learning by doing environment. The exercises at the end of each section have three categories: routine computations, variation of models, and mathematical analysis. The last four chapters focus on multiprocessing algorithms, which are implemented using message passing interface, MPI; see

Rocks" cluster software. These chapters have elementary Fortran 9x codes to illustrate the basic MPI subroutines, and the applications of the previous chapters are revisited from a parallel implementation perspective.

xv

codes; see [14] for

[17] for information about building your own multiprocessor via free "NPACI

xvi INTRODUCTION

lectures. Routine homework problems are assigned, and two projects are re-

work experiences. This forms a semester course on numerical modeling using partial dierential equations. be studied after Chapter 1 so as to enable the student, early in the semester, to become familiar with a high performance computing environment. Other course possibilities include: a semester course with an emphasis on mathemat-

parallel computation using Chapters 1 and 6-9 or a year course using Chapters 1-9. This text is not meant to replace traditional texts on numerical analysis, matrix algebra and partial dierential equations. It does develop topics in these areas as is needed and also includes modeling and computation, and so there is more breadth and less depth in these topics. One important component of computational science is parameter identification and model validation, and this requires a physical laboratory to take data from experiments. In this text model assessments have been restricted to the variation of model parameters, model evolution and mathematical analysis. More penetrating expertise in various aspects of computational science should be acquired in subsequent courses and work experiences. Related computational mathematics education material at the first and sec- ond year undergraduate level can be found at the Shodor Education Founda- tion, whose founder is Robert M. Pano, web site [22] and in Zachary’s book on programming [29]. Two general references for modeling are the undergradu- ate mathematics journal [25] and Beltrami’s book on modeling for society and biology [2]. Both of these have a variety of models, but often there are no computer implemenations. So they are a good source of potential computing projects. The book by Landau and Paez [13] has number of computational physics models, which are at about the same level as this book. Slightly more advanced numerical analysis references are by Fosdick, Jessup, Schauble and Domik [7] and Heath [10]. The computer codes and updates for this book can be found at the web site:

The computer codes are mostly in M ATLAB for Chapters 1-5, and in Fortran 9x for most of the MPI codes in Chapters 6-9. The choice of Fortran 9x is the author’s personal preference as the array operations are similar to those in MATLAB. However, the above web site and the web site associated with Pacheco’s book [21] do have C versions of these and related MPI codes. The web site for this book is expected to evolve and also has links to sequences of heat and pollution transfer images, book updates and new reference materials.

At North Carolina State University Chapters 1-4 are covered in 26 75-minute

quired, which can be chosen from topics in Chapters 1-5, related courses or

Chapter 6 on high performance computing can

ical analysis using Chapters 1-3, 8 and 9, a semester course with a focus on

http://www4.ncsu.edu/~white.

2 CHAPTER 1. DISCRETE TIME-SPACE MODELS

Computers use a finite subset of the rational numbers (a ratio of two in- tegers) to approximate any real number. This set of numbers may depend on the computer being used. However, they do have the same general form and are called floating point numbers. Any real number { can be represented by an infinite decimal expansion { = ±(={ 1 · · · {g · · · )10h, and by truncating this we can define the chopped floating point numbers. Let { be any real number and denote a floating point number by

i o({) = ±={ 1 · · · {g 10 h = ±({ 1 @10 + · · · + {g@ 10 g)10h=

This is a floating point number with base equal to 10 where { 1 is not equal to zero, {l are integers between 0 and 9, the exponent h is also a bounded integer and g is an integer called the precision of the floating point system. As- sociated with each real number, {, and its floating point approximate number, i o({), is the floating point error, i o({)  {. In general, this error decreases as the precision, g, increases. Each computer calculation has some floating point or roundo error. Moreover, as additional calculations are done, there is an accumulation of these roundo errors.

Example. Let { =  1 = 5378 and i o({) =  0 =154 10^1 where g = 3. The roundo error is i o({)  { = = 0022 =

The error will accumulate with any further operations containing i o({), for example, i o({)^2 = =237 10^1 and

i o({)^2  {^2 = 2= 37  2 =36482884 = = 00517116 =

Repeated calculations using floating point numbers can accumulate significant roundo errors.

1.1.2 Applied Area

Consider the cooling of a well stirred liquid so that the temperature does not depend on space. Here we want to predict the temperature of the liquid based on some initial observations. Newton’s law of cooling is based on the observation that for small changes of time, k, the change in the temperature is nearly equal to the product of the constant f, the k and the dierence in the room temperature and the present temperature of the coee. Consider the following quantities: xn equals the temperature of a well stirred cup of coee at time wn, xvxu equals the surrounding room temperature, and f measures the insulation ability of the cup and is a positive constant. The discrete form of Newton’s law of cooling is

xn+1  xn = fk(xvxu  xn) xn+1 = (1  fk)xn + fk xvxu = dxn + e where d = 1  fk and e = fk xvxu =

1.1. NEWTON COOLING MODELS 3

The long run solution should be the room temperature, that is, xn should converge to xvxu as n increases. Moreover, when the room temperature is constant, then xn should converge monotonically to the room temperature. This does happen if we impose the constraint

0? d = 1  fk>

called a stability condition, on the time step k. Since both f and k are positive, d? 1.

1.1.3 Model

The model in this case appears to be very simple. It consists of three constants x 0 > d> e and the formula xn+1 = dxn + e (1.1.1)

The formula must be used repeatedly, but with dierent xn being put into the right side. Often d and e are derived from formulating how xn changes as n increases (n reflects the time step). The change in the amount xn is often modeled by gxn + e xn+1  xn = gxn + e

where g = d 1 = The model given in (1.1.1) is called a first order finite dierence model for the sequence of numbers xn+1. Later we will generalize this to a sequence of column vectors where d will be replaced by a square matrix.

1.1.4 Method

The "iterative" calculation of (1.1.1) is the most common approach to solving (1.1.1). For example, if d = 12 > e = 2 and x 0 = 10, then

x 1 =

x 2 =

x 3 =

x 4 =

If one needs to compute xn+1 for large n, this can get a little tiresome. On the other hand, if the calculations are being done with a computer, then the floating point errors may generate significant accumulation errors. An alternative method is to use the following "telescoping" calculation and the geometric summation. Recall the geometric summation

1 + u + u^2 + · · · + un^ and (1 + u + u^2 + · · · + un)(1  u) = 1  un+

1.1. NEWTON COOLING MODELS 5

w is the time array whose first entry is the initial time. The array | stores the approximate temperature values whose first entry is the initial temperature. The value of f is based on a second observed temperature, |_revhu, at time equal to k_revhu. The value of f is calculated in line 10. Once d and e have been computed, the algorithm is executed by the for loop in lines 16-19. Since the time step k = 1, q = 300 will give an approximation of the temperature over the time interval from 0 to 300. If the time step were to be changed from 1 to 5, then we could change q from 300 to 60 and still have an approximation of the temperature over the same time interval. Within the for loop we could look at the time and temperature arrays by omitting the semicolon at the end of the lines 17 and 18. It is easier to examine the graph of approximate temperature versus time, which is generated by the M ATLAB command plot(t,y).

MATLAB Code fofdh.m
  1. % This code is for the first order finite dierence algorithm.
  2. % It is applied to Newton’s law of cooling model.
  3. clear;
  4. t(1) = 0; % initial time
  5. y(1) = 200.; % initial temperature
  6. h = 1; % time step
  7. n = 300; % number of time steps of length h
  8. y_obser = 190; % observed temperature at time h_obser
  9. h_obser = 5;
  10. c = ((y_obser - y(1))/h_obser)/(70 - y(1))
  11. a = 1 - c*h
  12. b = ch
  13. %
  14. % Execute the FOFD Algorithm
  15. %
  16. for k = 1:n
  17. y(k+1) = a*y(k) + b;
  18. t(k+1) = t(k) + h;
  19. end
  20. plot(t,y) An application to heat transfer is as follows. Consider a cup of coee, which is initially at 200 degrees and is in a room with temperature equal to 70, and after 5 minutes it cools to 190 degrees. By using k = k_revhu = 5, x 0 = 200 and x 1 = x_revhu = 190, we compute from (1.1.1) that f = 1@ 65. The first calculation is for this f and k = 5 so that d = 1  fk = 60@ 65 and e = fk @ the steady state room temperature, xvxu = 70. The next calculation is for a larger f = 2@ 13 > which is computed from a new second observed temperature of x_revhu = 100 after k_revhu = 5 minutes. In this case for larger time step k = 10 so that d = 1  (2@13)10 =  7 @ 13 and e = fk70 = (2@13)10 70 = 1400@ 13. the

70 = 350 65. Figure 1.1.1 indicates the expected monotonic decrease to

In Figure 1.1.2 notice that

6 CHAPTER 1. DISCRETE TIME-SPACE MODELS

Figure 1.1.1: Temperature versus Time

computed solution no longer is monotonic, but it does converge to the steady state solution.

The model continues to degrade as the magnitude of d increases. In the This is consistent with formula (1.1.2). Here we kept the same f, but let the step size increase to k = 15 and in this case d = 1  (2@13)15 =  17 @ 13 and e = fk70 = (2@13)1050 = 2100@ 13 = The vertical axis has units multiplied by 104.

1.1.6 Assessment

Models of savings plans or loans are discrete in the sense that changes only occur at the end of each month. In the case of the heat transfer problem, the formula for the temperature at the next time step is only an approximation, which gets better as the time step k decreases. The cooling process is continuous because the temperature changes at every instant in time. We have used a discrete model of this, and it seems to give good predictions provided the time step is suitably small. Moreover there are other modes of transferring heat such as diusion and radiation.

There may be significant accumulation of roundo error. On a computer (1.1.1) is done with floating point numbers, and at each step there is some new roundo error Un+1. Let X 0 = i o(x 0 )> D = i o(d) and E = i o(e) so that

Xn+1 = DXn + E + Un+1= (1.1.3)

Figure 1.1.3 the computed solution oscillates and blows up!