Computational Mathematics Models Methods and Analysis With Matlab - Robert E. White

388
COMPUTATIONAL MATHEMATICS Models, Methods, and Analysis with MATLAB and MPI © 2004 by Chapman & Hall/CRC

Transcript of Computational Mathematics Models Methods and Analysis With Matlab - Robert E. White

  • COMPUTATIONALMATHEMATICS

    Models, Methods, and Analysiswith MATLAB and MPI

    2004 by Chapman & Hall/CRC

  • CHAPMAN & HALL/CRCA CRC Press Company

    Boca Raton London New York Washington, D.C.

    COMPUTATIONALMATHEMATICS

    ROBERT E. WHITE

    Models, Methods, and Analysiswith MATLAB and MPI

    2004 by Chapman & Hall/CRC

  • This book contains information obtained from authentic and highly regarded sources. Reprinted materialis quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonableefforts have been made to publish reliable data and information, but the author and the publisher cannotassume responsibility for the validity of all materials or for the consequences of their use.

    Neither this book nor any part may be reproduced or transmitted in any form or by any means, electronicor mechanical, including photocopying, microfilming, and recording, or by any information storage orretrieval system, without prior permission in writing from the publisher.

    The consent of CRC Press LLC does not extend to copying for general distribution, for promotion, forcreating new works, or for resale. Specific permission must be obtained in writing from CRC Press LLCfor such copying.

    Direct all inquiries to CRC Press LLC, 2000 N.W. Corporate Blvd., Boca Raton, Florida 33431.

    Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and areused only for identification and explanation, without intent to infringe.

    2004 by Chapman & Hall/CRC

    No claim to original U.S. Government worksInternational Standard Book Number 1-58488-364-2

    Library of Congress Card Number 2003055207Printed in the United States of America 1 2 3 4 5 6 7 8 9 0

    Printed on acid-free paper

    Library of Congress Cataloging-in-Publication Data

    White, R. E. (Robert E.)Computational mathematics : models, methods, and analysis with MATLAB and MPI /

    Robert E. White. p. cm.Includes bibliographical references and index.ISBN 1-58488-364-2 (alk. paper) 1. Numerical analysis. 2. MATLAB. 3. Computer interfaces. 4. Parallel programming

    (Computer science) I. Title.

    QA297.W495 2003 519.4dc21 2003055207

    2004 by Chapman & Hall/CRC

    Visit the CRC Press Web site at www.crcpress.com

  • Computational Mathematics:Models, Methods and Analysis

    withMATLAB and MPI

    R. E. WhiteDepartment of Mathematics

    North Carolina State University

    Updated on August 3, 2003To Be Published by CRC Press in 2003

    2004 by Chapman & Hall/CRC

    [email protected]

  • Contents

    List of Figures ix

    List of Tables xi

    Preface xiii

    Introduction xv

    1 Discrete Time-Space Models 11.1 Newton Cooling Models . . . . . . . . . . . . . . . . . . . . . . . 11.2 Heat Diusion in a Wire . . . . . . . . . . . . . . . . . . . . . . . 91.3 Diusion in a Wire with Little Insulation . . . . . . . . . . . . . 171.4 Flow and Decay of a Pollutant in a Stream . . . . . . . . . . . . 251.5 Heat and Mass Transfer in Two Directions . . . . . . . . . . . . . 321.6 Convergence Analysis . . . . . . . . . . . . . . . . . . . . . . . . 42

    2 Steady State Discrete Models 512.1 Steady State and Triangular Solves . . . . . . . . . . . . . . . . . 512.2 Heat Diusion and Gauss Elimination . . . . . . . . . . . . . . . 592.3 Cooling Fin and Tridiagonal Matrices . . . . . . . . . . . . . . . 682.4 Schur Complement . . . . . . . . . . . . . . . . . . . . . . . . . . 772.5 Convergence to Steady State . . . . . . . . . . . . . . . . . . . . 862.6 Convergence to Continuous Model . . . . . . . . . . . . . . . . . 91

    3 Poisson Equation Models 993.1 Steady State and Iterative Methods . . . . . . . . . . . . . . . . 993.2 Heat Transfer in 2D Fin and SOR . . . . . . . . . . . . . . . . . 1073.3 Fluid Flow in a 2D Porous Medium . . . . . . . . . . . . . . . . . 1163.4 Ideal Fluid Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 1223.5 Deformed Membrane and Steepest Descent . . . . . . . . . . . . 1303.6 Conjugate Gradient Method . . . . . . . . . . . . . . . . . . . . . 138

    v

    2004 by Chapman & Hall/CRC

  • vi CONTENTS

    4 Nonlinear and 3D Models 1454.1 Nonlinear Problems in One Variable . . . . . . . . . . . . . . . . 1454.2 Nonlinear Heat Transfer in a Wire . . . . . . . . . . . . . . . . . 1524.3 Nonlinear Heat Transfer in 2D . . . . . . . . . . . . . . . . . . . 1594.4 Steady State 3D Heat Diusion . . . . . . . . . . . . . . . . . . . 1664.5 Time Dependent 3D Diusion . . . . . . . . . . . . . . . . . . . . 1714.6 High Performance Computations in 3D . . . . . . . . . . . . . . . 179

    5 Epidemics, Images and Money 1895.1 Epidemics and Dispersion . . . . . . . . . . . . . . . . . . . . . . 1895.2 Epidemic Dispersion in 2D . . . . . . . . . . . . . . . . . . . . . . 1975.3 Image Restoration . . . . . . . . . . . . . . . . . . . . . . . . . . 2045.4 Restoration in 2D . . . . . . . . . . . . . . . . . . . . . . . . . . . 2135.5 Option Contract Models . . . . . . . . . . . . . . . . . . . . . . . 2195.6 Black-Scholes Model for Two Assets . . . . . . . . . . . . . . . . 228

    6 High Performance Computing 2376.1 Vector Computers and Matrix Products . . . . . . . . . . . . . . 2376.2 Vector Computations for Heat Diusion . . . . . . . . . . . . . . 2446.3 Multiprocessors and Mass Transfer . . . . . . . . . . . . . . . . . 2496.4 MPI and the IBM/SP . . . . . . . . . . . . . . . . . . . . . . . . 2586.5 MPI and Matrix Products . . . . . . . . . . . . . . . . . . . . . . 2636.6 MPI and 2D Models . . . . . . . . . . . . . . . . . . . . . . . . . 268

    7 Message Passing Interface 2757.1 Basic MPI Subroutines . . . . . . . . . . . . . . . . . . . . . . . . 2757.2 Reduce and Broadcast . . . . . . . . . . . . . . . . . . . . . . . . 2827.3 Gather and Scatter . . . . . . . . . . . . . . . . . . . . . . . . . . 2887.4 Grouped Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 2947.5 Communicators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3017.6 Fox Algorithm for AB . . . . . . . . . . . . . . . . . . . . . . . . 307

    8 Classical Methods for Ax = d 3138.1 Gauss Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . 3138.2 Symmetric Positive Definite Matrices . . . . . . . . . . . . . . . . 3188.3 Domain Decomposition and MPI . . . . . . . . . . . . . . . . . . 3248.4 SOR and P-regular Splittings . . . . . . . . . . . . . . . . . . . . 3288.5 SOR and MPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3338.6 Parallel ADI Schemes . . . . . . . . . . . . . . . . . . . . . . . . 339

    9 Krylov Methods for Ax = d 3459.1 Conjugate Gradient Method . . . . . . . . . . . . . . . . . . . . . 3459.2 Preconditioners . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3509.3 PCG and MPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3569.4 Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3609.5 GMRES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

    2004 by Chapman & Hall/CRC

  • CONTENTS vii

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

    Bibliography 379

    2004 by Chapman & Hall/CRC

  • List of Figures

    1.1.1 Temperature versus Time . . . . . . . . . . . . . . . . . . . . . . 61.1.2 Steady State Temperature . . . . . . . . . . . . . . . . . . . . . . 71.1.3 Unstable Computation . . . . . . . . . . . . . . . . . . . . . . . . 71.2.1 Diusion in a Wire . . . . . . . . . . . . . . . . . . . . . . . . . . 111.2.2 Time-Space Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.2.3 Temperature versus Time-Space . . . . . . . . . . . . . . . . . . . 151.2.4 Unstable Computation . . . . . . . . . . . . . . . . . . . . . . . . 151.2.5 Steady State Temperature . . . . . . . . . . . . . . . . . . . . . . 161.3.1 Diusion in a Wire with csur = .0000 and .0005 . . . . . . . . . . 221.3.2 Diusion in a Wire with n = 5 and 20 . . . . . . . . . . . . . . . 231.4.1 Polluted Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . 261.4.2 Concentration of Pollutant . . . . . . . . . . . . . . . . . . . . . 301.4.3 Unstable Concentration Computation . . . . . . . . . . . . . . . 311.5.1 Heat or Mass Entering or Leaving . . . . . . . . . . . . . . . . . 341.5.2 Temperature at Final Time . . . . . . . . . . . . . . . . . . . . . 371.5.3 Heat Diusing Out a Fin . . . . . . . . . . . . . . . . . . . . . . 381.5.4 Concentration at the Final Time . . . . . . . . . . . . . . . . . . 401.5.5 Concentrations at Dierent Times . . . . . . . . . . . . . . . . . 401.6.1 Euler Approximations . . . . . . . . . . . . . . . . . . . . . . . . 45

    2.1.1 Infinite or None or One Solution(s) . . . . . . . . . . . . . . . . . 522.2.1 Gaussian Elimination . . . . . . . . . . . . . . . . . . . . . . . . . 642.3.1 Thin Cooling Fin . . . . . . . . . . . . . . . . . . . . . . . . . . . 692.3.2 Temperature for c = .1, .01, .001, .0001 . . . . . . . . . . . . . . 752.6.1 Variable r = .1, .2 and .3 . . . . . . . . . . . . . . . . . . . . . . 942.6.2 Variable n = 4, 8 and 16 . . . . . . . . . . . . . . . . . . . . . . . 95

    3.1.1 Cooling Fin with T = .05, .10 and .15 . . . . . . . . . . . . . . . 1053.2.1 Diusion in Two Directions . . . . . . . . . . . . . . . . . . . . . 1083.2.2 Temperature and Contours of Fin . . . . . . . . . . . . . . . . . . 1133.2.3 Cooling Fin Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . 1143.3.1 Incompressible 2D Fluid . . . . . . . . . . . . . . . . . . . . . . . 1173.3.2 Groundwater 2D Porous Flow . . . . . . . . . . . . . . . . . . . . 118

    ix

    2004 by Chapman & Hall/CRC

  • x LIST OF FIGURES

    3.3.3 Pressure for Two Wells . . . . . . . . . . . . . . . . . . . . . . . . 1223.4.1 Ideal Flow About an Obstacle . . . . . . . . . . . . . . . . . . . . 1233.4.2 Irrotational 2D Flow y{ x| = 0 . . . . . . . . . . . . . . . . . . 1243.4.3 Flow Around an Obstacle . . . . . . . . . . . . . . . . . . . . . . 1283.4.4 Two Paths to (x,y) . . . . . . . . . . . . . . . . . . . . . . . . . . 1293.5.1 Steepest Descent norm(r) . . . . . . . . . . . . . . . . . . . . . . 1373.6.1 Convergence for CG and PCG . . . . . . . . . . . . . . . . . . . . 144

    4.2.1 Change in F1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1544.2.2 Temperatures for Variable c . . . . . . . . . . . . . . . . . . . . . 1584.4.1 Heat Diusion in 3D . . . . . . . . . . . . . . . . . . . . . . . . . 1674.4.2 Temperatures Inside a 3D Fin . . . . . . . . . . . . . . . . . . . . 1704.5.1 Passive Solar Storage . . . . . . . . . . . . . . . . . . . . . . . . . 1714.5.2 Slab is Gaining Heat . . . . . . . . . . . . . . . . . . . . . . . . . 1784.5.3 Slab is Cooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1784.6.1 Domain Decompostion in 3D . . . . . . . . . . . . . . . . . . . . 1824.6.2 Domain Decomposition Matrix . . . . . . . . . . . . . . . . . . . 186

    5.1.1 Infected and Susceptible versus Space . . . . . . . . . . . . . . . 1965.2.1 Grid with Artificial Grid Points . . . . . . . . . . . . . . . . . . . 1995.2.2 Infected and Susceptible at Time = 0.3 . . . . . . . . . . . . . . 2035.3.1 Three Curves with Jumps . . . . . . . . . . . . . . . . . . . . . . 2065.3.2 Restored 1D Image . . . . . . . . . . . . . . . . . . . . . . . . . . 2135.4.1 Restored 2D Image . . . . . . . . . . . . . . . . . . . . . . . . . . 2195.5.1 Value of American Put Option . . . . . . . . . . . . . . . . . . . 2225.5.2 P(S,T-t) for Variable Times . . . . . . . . . . . . . . . . . . . . . 2265.5.3 Option Values for Variable Volatilities . . . . . . . . . . . . . . . 2265.5.4 Optimal Exercise of an American Put . . . . . . . . . . . . . . . 2275.6.1 American Put with Two Assets . . . . . . . . . . . . . . . . . . . 2295.6.2max(H1 +H2 V1 V2> 0) . . . . . . . . . . . . . . . . . . . . . . 2345.6.3max(H1 V1> 0) + max(H2 V2> 0) . . . . . . . . . . . . . . . . . 235

    6.1.1 von Neumann Computer . . . . . . . . . . . . . . . . . . . . . . . 2386.1.2 Shared Memory Multiprocessor . . . . . . . . . . . . . . . . . . . 2396.1.3 Floating Point Add . . . . . . . . . . . . . . . . . . . . . . . . . . 2396.1.4 Bit Adder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2406.1.5 Vector Pipeline for Floating Point Add . . . . . . . . . . . . . . . 2416.2.1 Temperature in Fin at t = 60 . . . . . . . . . . . . . . . . . . . . 2486.3.1 Ring and Complete Multiprocessors . . . . . . . . . . . . . . . . 2506.3.2 Hypercube Multiprocessor . . . . . . . . . . . . . . . . . . . . . . 2506.3.3 Concentration at t = 17 . . . . . . . . . . . . . . . . . . . . . . . 2566.4.1 Fan-out Communication . . . . . . . . . . . . . . . . . . . . . . 2626.6.1 Space Grid with Four Subblocks . . . . . . . . . . . . . . . . . . 2696.6.2 Send and Receive for Processors . . . . . . . . . . . . . . . . . . 270

    7.2.1 A Fan-in Communication . . . . . . . . . . . . . . . . . . . . . . 283

    2004 by Chapman & Hall/CRC

  • List of Tables

    1.6.1 Euler Errors at t = 10 . . . . . . . . . . . . . . . . . . . . . . . . 451.6.2 Errors for Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481.6.3 Errors for Heat . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    2.6.1 Second Order Convergence . . . . . . . . . . . . . . . . . . . . . 96

    3.1.1 Variable SOR Parameter . . . . . . . . . . . . . . . . . . . . . . . 1043.2.1 Convergence and SOR Parameter . . . . . . . . . . . . . . . . . 113

    4.1.1 Quadratic Convergence . . . . . . . . . . . . . . . . . . . . . . . . 1494.1.2 Local Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . 1494.2.1 Newtons Rapid Convergence . . . . . . . . . . . . . . . . . . . . 157

    6.1.1 Truth Table for Bit Adder . . . . . . . . . . . . . . . . . . . . . . 2396.1.2 Matrix-vector Computation Times . . . . . . . . . . . . . . . . . 2436.2.1 Heat Diusion Vector Times . . . . . . . . . . . . . . . . . . . . . 2466.3.1 Speedup and E!ciency . . . . . . . . . . . . . . . . . . . . . . . 2526.3.2 HPF for 2D Diusion . . . . . . . . . . . . . . . . . . . . . . . . 2546.4.1 MPI Times for trapempi.f . . . . . . . . . . . . . . . . . . . . . . 2626.5.1 Matrix-vector Product mflops . . . . . . . . . . . . . . . . . . . . 2656.5.2 Matrix-matrix Product mflops . . . . . . . . . . . . . . . . . . . . 2686.6.1 Processor Times for Diusion . . . . . . . . . . . . . . . . . . . . 2726.6.2 Processor Times for Pollutant . . . . . . . . . . . . . . . . . . . . 273

    7.6.1 Fox Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

    8.3.1 MPI Times for geddmpi.f . . . . . . . . . . . . . . . . . . . . . . 3288.5.1 MPI Times for sorddmpi.f . . . . . . . . . . . . . . . . . . . . . . 338

    9.3.1 MPI Times for cgssormpi.f . . . . . . . . . . . . . . . . . . . . . . 3609.6.1 MPI Times for gmresmmpi.f . . . . . . . . . . . . . . . . . . . . . 376

    xi

    2004 by Chapman & Hall/CRC

  • 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 UndergraduateComputational Engineering and Science Project (a United States Departmentof Energy funded eort), which was a series of meetings during the 1990s.These meetings focused on the nature and content for computational scienceundergraduate education. They were attended by a diverse group of scienceand engineering teachers and professionals, and the continuation of some of

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

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

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

    with the initial use of MATLABR. 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 USATel: 508-647-7000Fax: 508-647-7001

    ? A.

    xiii

    2004 by Chapman & Hall/CRC

    Variations of Chapters 1-4 and 6 have been taught at North Carolina Statethese activities can be found at the Krell Institute, http://www.krellinst.org.

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

    http://www.mathworks.com/Web: www.mathworks.comE-mail: [email protected]

  • xiv PREFACE

    I thank my close friends who have listened to me talk about this eort, andespecially Liz White who has endured the whole process with me.

    Bob White, July 1, 2003

    2004 by Chapman & Hall/CRC

  • Introduction

    Computational science is a blend of applications, computations and mathemat-ics. It is a mode of scientific investigation that supplements the traditionallaboratory and theoretical methods of acquiring knowledge. This is done byformulating mathematical models whose solutions are approximated by com-puter simulations. By making a sequence of adjustments to the model andsubsequent computations one can gain some insights into the application areaunder consideration. This text attempts to illustrate this process as a methodfor scientific investigation. Each section of the first six chapters is motivatedby 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 multiprocessingcomputers, ideal and porous fluid flows, deformed membranes, epidemic modelswith dispersion, image restoration and value of American put option contracts.The models are initially introduced as discrete in time and space, and this allowsfor an early introduction to partial dierential equations. The discrete modelshave the form of matrix products or linear and nonlinear systems. Methods in-clude sparse matrix iteration with stability constraints, sparse matrix solutionsvia variation on Gauss elimination, successive over-relaxation, conjugate gradi-ent, and minimum residual methods. Picard and Newton methods are used toapproximate the solution to nonlinear systems.

    Most sections in the first five chapters have MATLABR

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

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

    xv

    2004 by Chapman & Hall/CRC

    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 usingpartial 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. Othercourse possibilities include: a semester course with an emphasis on mathemat-

    parallel computation using Chapters 1 and 6-9 or a year course using Chapters1-9.

    This text is not meant to replace traditional texts on numerical analysis,matrix algebra and partial dierential equations. It does develop topics in theseareas as is needed and also includes modeling and computation, and so thereis more breadth and less depth in these topics. One important component ofcomputational science is parameter identification and model validation, and thisrequires a physical laboratory to take data from experiments. In this text modelassessments have been restricted to the variation of model parameters, modelevolution and mathematical analysis. More penetrating expertise in variousaspects of computational science should be acquired in subsequent courses andwork 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 Zacharys bookon programming [29]. Two general references for modeling are the undergradu-ate mathematics journal [25] and Beltramis book on modeling for society andbiology [2]. Both of these have a variety of models, but often there are nocomputer implemenations. So they are a good source of potential computingprojects. The book by Landau and Paez [13] has number of computationalphysics models, which are at about the same level as this book. Slightly moreadvanced numerical analysis references are by Fosdick, Jessup, Schauble andDomik [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 MATLAB for Chapters 1-5, and in Fortran9x for most of the MPI codes in Chapters 6-9. The choice of Fortran 9x isthe authors personal preference as the array operations are similar to thosein MATLAB. However, the above web site and the web site associated withPachecos book [21] do have C versions of these and related MPI codes. Theweb site for this book is expected to evolve and also has links to sequences ofheat and pollution transfer images, book updates and new reference materials.

    2004 by Chapman & Hall/CRC

    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.

  • Chapter 1

    Discrete Time-SpaceModels

    The first three sections introduce diusion of heat in one direction. This is anexample of model evolution with the simplest model being for the temperatureof a well-stirred liquid where the temperature does not vary with space. Themodel is then enhanced by allowing the mass to have dierent temperatures indierent locations. Because heat flows from hot to cold regions, the subsequentmodel will be more complicated. In Section 1.4 a similar model is considered,and the application will be to the prediction of the pollutant concentrationin a stream resulting from a source of pollution up stream. Both of thesemodels are discrete versions of the continuous model that are partial dierentialequations. Section 1.5 indicates how these models can be extended to heat and

    In the last section variations of the mean value theorem are usedto estimate the errors made by replacing the continuous model by a discretemodel. Additional introductory materials can be found in G. D. Smith [23],and in R. L. Burden and J. D. Faires [4].

    1.1 Newton Cooling Models

    1.1.1 Introduction

    Many quantities change as time progresses such as money in a savings accountor the temperature of a refreshing drink or any cooling mass. Here we willbe interested in making predictions about such changing quantities. A simplemathematical model has the form x+ = dx + e where d and e are given realnumbers, x is the present amount and x+ is the next amount. This calculation isusually repeated a number of times and is a simple example of an of algorithm.A computer is used to do a large number calculations.

    1

    2004 by Chapman & Hall/CRC

    mass transfer in two directions, which is discussed in more detail in Chapters3 and 4.

  • 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 onthe computer being used. However, they do have the same general form andare called floating point numbers. Any real number { can be represented by aninfinite decimal expansion { = (={1 {g )10h, and by truncating this wecan define the chopped floating point numbers.

    Let { be any real number and denote a floating point number by

    io({) = ={1 {g10h= ({1@10 + + {g@10g)10h=

    This is a floating point number with base equal to 10 where {1 is not equalto zero, {l are integers between 0 and 9, the exponent h is also a boundedinteger 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,io({), is the floating point error, io({) {. In general, this error decreases asthe precision, g, increases. Each computer calculation has some floating pointor roundo error. Moreover, as additional calculations are done, there is anaccumulation of these roundo errors.Example. Let { = 1=5378 and io({) = 0=154 101 where g = 3. Theroundo error is

    io({) { = =0022=The error will accumulate with any further operations containing io({), forexample, io({)2 = =237 101 and

    io({)2 {2 = 2=37 2=36482884 = =00517116=Repeated calculations using floating point numbers can accumulate significantroundo errors.

    1.1.2 Applied Area

    Consider the cooling of a well stirred liquid so that the temperature does notdepend on space. Here we want to predict the temperature of the liquid based onsome initial observations. Newtons law of cooling is based on the observationthat for small changes of time, k, the change in the temperature is nearlyequal to the product of the constant f, the k and the dierence in the roomtemperature and the present temperature of the coee. Consider the followingquantities: xn equals the temperature of a well stirred cup of coee at time wn,xvxu equals the surrounding room temperature, and f measures the insulationability of the cup and is a positive constant. The discrete form of Newtons lawof 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=

    2004 by Chapman & Hall/CRC

  • 1.1. NEWTON COOLING MODELS 3

    The long run solution should be the room temperature, that is, xn shouldconverge to xvxu as n increases. Moreover, when the room temperature isconstant, 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 constantsx0> 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 theright side. Often d and e are derived from formulating how xn changes as nincreases (n reflects the time step). The change in the amount xn is oftenmodeled by gxn + e

    xn+1 xn = gxn + ewhere g = d1= The model given in (1.1.1) is called a first order finite dierencemodel for the sequence of numbers xn+1. Later we will generalize this to asequence 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 x0 = 10, then

    x1 =1

    210 + 2 = 7=0

    x2 =1

    27 + 2 = 5=5

    x3 =1

    25=5 + 2 = 4=75

    x4 =1

    24=75 + 2 = 4=375

    If one needs to compute xn+1 for large n, this can get a little tiresome. Onthe other hand, if the calculations are being done with a computer, then thefloating point errors may generate significant accumulation errors.

    An alternative method is to use the following "telescoping" calculation andthe geometric summation. Recall the geometric summation

    1 + u + u2 + + un and (1 + u + u2 + + un)(1 u) = 1 un+1

    2004 by Chapman & Hall/CRC

  • 4 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Or, for u not equal to 1

    (1 + u + u2 + + un) = (1 un+1)@(1 u)=Consequently, if |u| ? 1, then

    1 + u + u2 + + un + = 1@(1 u)is a convergent geometric series.

    In (1.1.1) we can compute xn by decreasing n by 1 so that xn = dxn1 + e.Put this into (1.1.1) and repeat the substitution to get

    xn+1 = d(dxn1 + e) + e= d2xn1 + de+ e= d2(dxn2 + e) + de+ e= d3xn2 + d2e+ de+ e

    ...

    = dn+1x0 + e(dn + + d2 + d+ 1)= dn+1x0 + e(1 dn+1)@(1 d)= dn+1(x0 e@(1 d)) + e@(1 d)= (1.1.2)

    The error for the steady state solution, e@(1 d)> will be small if |d| is small,or n is large, or the initial guess x0 is close to the steady state solution. Ageneralization of this will be studied in Section 2.5.

    Theorem 1.1.1 (Steady State Theorem) If d is not equal to 1, then the so-lution of (1.1.1) has the form given in (1.1.2). Moreover, if |d| ? 1, then thesolution of (1.1.1) will converge to the steady state solution x = dx+ e, that is,x = e@(1 d). More precisely, the error is

    xn+1 x = dn+1(x0 e@(1 d))=

    Example. Let d = 1@2> e = 2> x0 = 10 and n = 3= Then (1.1.2) gives

    x3+1 = (1@2)4(10 2@(1 1@2)) + 2@(1 1@2) = 6@16 + 4 = 4=375=

    The steady state solution is x = 2@(1 12) = 4 and the error for n = 3 is

    x4 x = 4=375 4 = (1

    2)4(10 4)=

    1.1.5 Implementation

    The reader should be familiar with the information in MATLABs tutorial. Theinput segment of the MATLAB code fofdh.m is done in lines 1-12, the executionis done in lines 16-19, and the output is done in line 20. In the following m-file

    2004 by Chapman & Hall/CRC

  • 1.1. NEWTON COOLING MODELS 5

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

    MATLAB Code fofdh.m

    1. % This code is for the first order finite dierence algorithm.2. % It is applied to Newtons law of cooling model.3. clear;4. t(1) = 0; % initial time5. y(1) = 200.; % initial temperature6. h = 1; % time step7. n = 300; % number of time steps of length h8. y_obser = 190; % observed temperature at time h_obser9. h_obser = 5;10. c = ((y_obser - y(1))/h_obser)/(70 - y(1))11. a = 1 - c*h12. b = c*h*7013. %14. % Execute the FOFD Algorithm15. %16. for k = 1:n17. y(k+1) = a*y(k) + b;18. t(k+1) = t(k) + h;19. end20. 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 to70, and after 5 minutes it cools to 190 degrees. By using k = k_revhu = 5,x0 = 200 and x1 = x_revhu = 190, we compute from (1.1.1) that f = [email protected] first calculation is for this f and k = 5 so that d = 1 fk = 60@65 ande = fk @the steady state room temperature, xvxu = 70.

    The next calculation is for a larger f = 2@13> which is computed from a newsecond 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@13and e = fk70 = (2@13)10 70 = 1400@13. the

    2004 by Chapman & Hall/CRC

    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 steadystate solution.

    The model continues to degrade as the magnitude of d increases. In theThis is consistent

    with formula (1.1.2). Here we kept the same f, but let the step size increaseto 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 occurat the end of each month. In the case of the heat transfer problem, the formulafor the temperature at the next time step is only an approximation, which getsbetter as the time step k decreases. The cooling process is continuous becausethe temperature changes at every instant in time. We have used a discretemodel of this, and it seems to give good predictions provided the time step issuitably small. Moreover there are other modes of transferring heat such asdiusion 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 newroundo error Un+1. Let X0 = io(x0)> D = io(d) and E = io(e) so that

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

    2004 by Chapman & Hall/CRC

    Figure 1.1.3 the computed solution oscillates and blows up!

  • 1.1. NEWTON COOLING MODELS 7

    Figure 1.1.2: Steady State Temperature

    Figure 1.1.3: Unstable Computation

    2004 by Chapman & Hall/CRC

  • 8 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Next, we want to estimate the

    dffxpxodwlrq huuru = Xn+1 xn+1

    under the assumption that the roundo errors are uniformly bounded|Un+1| U ?4=

    For ease of notation, we will assume the roundo errors associated with d ande have been put into the Un+1 so that Xn+1 = dXn+ e+Un+1. Subtract (1.1.1)and this variation of (1.1.3) to get

    Xn+1 xn+1 = d(Xn xn) +Un+1 (1.1.4)= d[d(Xn1 xn1) +Un] +Un+1= d2(Xn1 xn1) + dUn +Un+1

    ...

    = dn+1(X0 x0) + dnU1 + +Un+1Now let u = |d| and U be the uniform bound on the roundo errors. Use thegeometric summation and the triangle inequality to get

    |Xn+1 xn+1| un+1|X0 x0|+U(un+1 1)@(u 1)= (1.1.5)Either r is less than one, or greater, or equal to one. An analysis of (1.1.4) and(1.1.5) immediately yields the next theorem.

    Theorem 1.1.2 (Accumulation Error Theorem) Consider the first order finitedierence algorithm. If |d| ? 1 and the roundo errors are uniformly bounded byU, then the accumulation error is uniformly bounded. Moreover, if the roundoerrors decrease uniformly, then the accumulation error decreases.

    1.1.7 Exercises

    2. Execute fofdh.m four times for f = 1@65> variable k = 64, 32, 16, 8 with q= 5, 10, 20 and 40, respectively. Compare the four curves by placing them onthe same graph; this can be done by executing the MATLAB command "holdon" after the first execution of fofdh.m3. Execute fofdh.m five times with k = 1> variable f = 8/65, 4/65, 2/65, 1/65,and .5/65, and q = 300. Compare the five curves by placing them on the samegraph; this can be done by executing the MATLAB command "hold on" afterthe first execution of fofdh.m4. Consider the application to Newtons discrete law of cooling. Use (1.1.2) toshow that if kf ? 1, then xn+1 converges to the room temperature.5. Modify the model used in Figure 1.1.1 to account for a room temperaturethat starts at 70 and increases at a constant rate equal to 1 degree every 5

    2004 by Chapman & Hall/CRC

    1. Using fofdh.m duplicate the calculations in Figures 1.1.1-1.1.3.

  • 1.2. HEAT DIFFUSION IN A WIRE 9

    minutes. Use the f = 1/65 and k = 1.

    6. We wish to calculate the amount of a savings plan for any month, n> given afixed interest rate, u, compounded monthly. Denote these quantities as follows:xn is the amount in an account at month n, u equals the interest rate com-pounded monthly, and g equals the monthly deposit. The amount at the endof the next month will be the old amount plus the interest on the old amountplus the deposit. In terms of the above variables this is with d = 1 + u@12 ande = g

    xn+1 = xn + xn u@12 + g= dxn + e=

    (a). Use (1.1.2) to determine the amount in the account by depositing $100each month in an account, which gets 12% compounded monthly, and over timeintervals of 30 and 40 years ( 360 and 480 months).

    (b). Use a modified version of fofdh.m to calculate and graph the amountsin the account from 0 to 40 years.7. Show (1.1.5) follows from (1.1.4).8. Prove the second part of the accumulation error theorem.

    1.2 Heat Diusion in a Wire1.2.1 Introduction

    In this section we consider heat conduction in a thin electrical wire, which isthermally insulated on its surface. The model of the temperature has the formxn+1 = Dxn+e where xn is a column vector whose components are temperaturesfor the previous time step, w = nw> at various positions within the wire. Thesquare matrix will determine how heat flows from warm regions to cooler regionswithin the wire. In general, the matrix D can be extremely large, but it willalso have a special structure with many more zeros than nonzero components.

    1.2.2 Applied Area

    In this section we present a second model of heat transfer. In our first model weconsidered heat transfer via a discrete version of Newtons law of cooling whichinvolves temperature as only a discrete function of time. That is, we assumedthe mass was uniformly heated with respect to space. In this section we allowthe temperature to be a function of both discrete time and discrete space.

    The model for the diusion of heat in space is based on empirical observa-tions. The discrete Fourier heat law in one direction says that

    (a). heat flows from hot to cold,(b). the change in heat is proportional to the

    cross-sectional area,

    2004 by Chapman & Hall/CRC

    Compare the new curve with Figure1.1.1.

  • 10 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    change in time and(change in temperature)/(change in space).

    The last term is a good approximation provided the change in space is small,and in this case one can use the derivative of the temperature with respect tothe single direction. The proportionality constant, N, is called the thermal con-ductivity. The N varies with the particular material and with the temperature.Here we will assume the temperature varies over a smaller range so that N isapproximately a constant. If there is more than one direction, then we mustreplace the approximation of the derivative in one direction by the directionalderivative of the temperature normal to the surface.

    Fourier Heat Law. Heat flows from hot to cold, and the amount of heattransfer through a small surface area D is proportional to the product of D> thechange in time and the directional derivative of the temperature in the directionnormal to the surface.

    Consider a thin wire so that the most significant diusion is in one direction,{. The wire will have a current going through it so that there is a source ofheat, i , which is from the electrical resistance of the wire. The i has units of(heat)/(volume time). Assume the ends of the wire are kept at zero tempera-ture, and the initial temperature is also zero. The goal is to be able to predictthe temperature inside the wire for any future time and space location.

    1.2.3 Model

    In order to develop a model to do temperature prediction, we will discretizeboth space and time and let x(lk> nw) be approximated by xnl where w =W@pd{n> k = O@q and O is the length of the wire. The model will have thegeneral form

    change in heat content (heat from the source)+(heat diusion from the right)+(heat diusion from the left)=

    For time on the right side we can choose either nw or (n+1)w. Presently, wewill choose nw, which will eventually result in the matrix version of the firstorder finite dierence method.

    The heat diusing in the right face (when (xnl+1 xnl )@k A 0) isDwN(xnl+1 xnl )@k=

    The heat diusing out the left face (when (xnl xnl1)@k A 0) isDwN(xnl xnl1)@k.

    Therefore, the heat from diusion is

    2004 by Chapman & Hall/CRC

    This is depicted in the Figure 1.2.1 where the time step has not been indicated.

  • 1.2. HEAT DIFFUSION IN A WIRE 11

    Figure 1.2.1: Diusion in a Wire

    DwN(xnl+1 xnl )@k DwN(xnl xnl1)@k.The heat from the source is

    Dk w i .

    The heat content of the volume Dk at time nw is

    fxnlDk

    where is the density and f is the specific heat. By combining these we have thefollowing approximation of the change in the heat content for the small volumeDk:

    fxn+1l Dk fxnlDk = Dkw i +DwN(xnl+1xnl )@kDwN(xnl xnl1)@k=

    Now, divide by fDk, define = (N@f)(w@k2) and explicitly solve for xn+1l .Explicit Finite Dierence Model for Heat Diusion.

    xn+1l = (w@f)i + (xnl+1 + xnl1) + (1 2)xnl (1.2.1)for l = 1> ===> q 1 and n = 0> ===>pd{n 1>x0l = 0 for l = 1> ===> q 1 (1.2.2)xn0 = xnq = 0 for n = 1> ===>pd{n= (1.2.3)

    Equation (1.2.2) is the initial temperature set equal to zero, and (1.2.3) is thetemperature at the left and right ends set equal to zero. Equation (1.2.1) maybe put into the matrix version of the first order finite dierence method. Forexample, if the wire is divided into four equal parts, then q = 4 and (1.2.1) maybe written as three scalar equations for the unknowns xn+11 > xn+12 and xn+13 :

    xn+11 = (w@f)i + (xn2 + 0) + (1 2)xn1xn+12 = (w@f)i + (xn3 + xn1) + (1 2)xn2xn+13 = (w@f)i + (0 + xn2) + (1 2)xn3 =

    2004 by Chapman & Hall/CRC

  • 12 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    These three scalar equations can be written as one 3D vector equation

    xn+1 = Dxn + e where

    xn =

    57xn1xn2xn3

    68 > e = (w@f )i

    57

    111

    68 and

    D =

    57

    1 2 0 1 2 0 1 2

    68 =

    An extremely important restriction on the time step w is required to makesure the algorithm is stable in the same sense as in Section 1.1 . For example,consider the case q = 2 where the above is a single equation, and we have thesimplest first order finite dierence model. Here d = 12 and we must required = 1 2 ? 1. If d = 1 2 A 0 and A 0, then this condition will hold. Ifq is larger than 2, this simple condition will imply that the matrix products Dnwill converge to the zero matrix. This will imply there are no blowups providedthe source term i is bounded. The illustration of the stability condition andan analysis will be presented in Section 2.5.

    Stability Condition for (1.2.1).

    1 2 A 0 and = (N@f)(w@k2) A 0=Example. Let O = f = = 1=0> q = 4 so that k = 1@4> and N = =001=Then = (N@f)(w@k2) = (=001)w16 and so that 1 2(N@f)(w@k2) =1 =032w A 0= Note if q increases to 20, then the constraint on the time stepwill significantly change.

    1.2.4 Method

    The numbers xn+1l generated by equations (1.2.1)-(1.2.3) are hopefully goodapproximations for the temperature at { = l{ and w = (n + 1)w= The tem-perature is often denoted by the function x({> w)= In computer code xn+1l will bestored in a two dimensional array, which is also denoted by x but with integerindices so that xn+1l = x(l> n+1) x(l{> (n+1)w) = temperature function.In order to compute all xn+1l , which we will henceforth denote by x(l> n + 1)with both l and n shifted up by one, we must use a nested loop where thei-loop (space) is the inner loop and the k-loop (time) is the outer loop. This

    x(l> n + 1) on the threepreviously computed x(l 1> n), x(l> n) and x(l + 1> n). In Figure 1.2.2 theinitial values in (1.2.2) are given on the bottom of the grid, and the boundaryconditions in (1.2.3) are on the left and right of the grid.

    1.2.5 Implementation

    The implementation in the MATLAB code heat.m of the above model for tem-perature that depends on both space and time has nested loops where the outer

    2004 by Chapman & Hall/CRC

    is illustrated in the Figure 1.2.2 by the dependency of

  • 1.2. HEAT DIFFUSION IN A WIRE 13

    Figure 1.2.2: Time-Space Grid

    loop is for discrete time and the inner loop is for discrete space. These loops aregiven in lines 29-33. Lines 1-25 contain the input data. The initial temperaturedata is given in the single i-loop in lines 17-20, and the left and right boundarydata are given in the single k-loop in lines 21-25. Lines 34-37 contain the outputdata in the form of a surface plot for the temperature.

    MATLAB Code heat.m

    1. % This code models heat diusion in a thin wire.2. % It executes the explicit finite dierence method.3. clear;4. L = 1.0; % length of the wire5. T = 150.; % final time6. maxk = 30; % number of time steps7. dt = T/maxk;8. n = 10.; % number of space steps9. dx = L/n;10. b = dt/(dx*dx);11. cond = .001; % thermal conductivity12. spheat = 1.0; % specific heat13. rho = 1.; % density14. a = cond/(spheat*rho);15. alpha = a*b;16. f = 1.; % internal heat source17. for i = 1:n+1 % initial temperature18. x(i) =(i-1)*dx;19. u(i,1) =sin(pi*x(i));20. end21. for k=1:maxk+1 % boundary temperature

    2004 by Chapman & Hall/CRC

  • 14 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    22. u(1,k) = 0.;23. u(n+1,k) = 0.;24. time(k) = (k-1)*dt;25. end26. %27. % Execute the explicit method using nested loops.28. %29. for k=1:maxk % time loop30. for i=2:n; % space loop31. u(i,k+1) = f*dt/(spheat*rho)

    + (1 - 2*alpha)*u(i,k)+ alpha*(u(i-1,k) + u(i+1,k));

    32. end33. end34. mesh(x,time,u)35. xlabel(x)36. ylabel(time)37. zlabel(temperature)

    heat.m with the parameters as listed in the code. The space steps are .1 andgo in the right direction, and the time steps are 5 and go in the left direction.The temperature is plotted in the vertical direction, and it increases as timeincreases. The left and right ends of the wire are kept at zero temperature andserve as heat sinks. The wire has an internal heat source, perhaps from electricalresistance or a chemical reaction, and so, this increases the temperature in theinterior of the wire.

    The second calculation increases the final time from 150 to 180 so that thetime step from increases 5 to 6, and consequently, the stability condition does

    The third computation uses a larger final time equal to 600 with 120 time

    the same, and for large values of time it is shaped like a parabola with a maxi-mum value near 125.

    1.2.6 Assessment

    The heat conduction in a thin wire has a number of approximations. Dierentmesh sizes in either the time or space variable will give dierent numericalresults. However, if the stability conditions hold and the mesh sizes decrease,then the numerical computations will dier by smaller amounts.

    The numerical model assumed that the surface of the wire was thermallyinsulated. This may not be the case, and one may use the discrete versionof Newtons law of cooling by inserting a negative source term of F(xvxu xnl )k 2uw where u is the radius of the wire. The constant F is a measureof insulation where F = 0 corresponds to perfect insulation. The k2u is

    2004 by Chapman & Hall/CRC

    The first calculation given by Figure 1.2.3 is a result of the execution of

    not hold. Note in Figure 1.2.4 that significant oscillations develop.

    steps. Notice in Figure 1.2.5 as time increases the temperature remains about

  • 1.2. HEAT DIFFUSION IN A WIRE 15

    Figure 1.2.3: Temperature versus Time-Space

    Figure 1.2.4: Unstable Computation

    2004 by Chapman & Hall/CRC

  • 16 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Figure 1.2.5: Steady State Temperature

    the lateral surface area of the volume kD with D = u2. Other variationson the model include more complicated boundary conditions, variable thermalproperties and diusion in more than one direction.

    In the scalar version of the first order finite dierence models the scheme wasstable when |d| ? 1. In this case, xn+1 converged to the steady state solutionx = dx + e. This is also true of the matrix version of (1.2.1) provided thestability condition is satisfied. In this case the real number d will be replacedby the matrix D, and Dn will converge to the zero matrix. The following is amore general statement of this.

    Theorem 1.2.1 (Steady State Theorem) Consider the matrix version of thefirst order finite dierence equation xn+1 = Dxn+e where D is a square matrix.If Dn converges to the zero matrix and x = Dx+e, then, regardless of the initialchoice for x0, xn converges to x.Proof. Subtract xn+1 = Dxn + e and x = Dx+ e and use the properties ofmatrix products to get

    xn+1 x =Dxn + e

    (Dx+ e)

    = D(xn x)= D(D(xn1 x))= D2(xn1 x)

    ...

    = Dn+1(x0 x)

    2004 by Chapman & Hall/CRC

  • 1.3. DIFFUSION IN A WIRE WITH LITTLE INSULATION 17

    Since Dn converges to the zero matrix, the column vectors xn+1 x must con-verge to the zero column vector.

    1.2.7 Exercises

    1.2. In heat.m let pd{n = 120 so that gw = 150@120 = 1=25. Experimentwith the space step sizes g{ = =2> =1> =05 and q = 5> 10> 20, respectively.3. In heat.m let q = 10 so that g{ = =1. Experiment with time step sizesgw = 5> 2=5> 1=25 and pd{n = 30> 60 and 120, respectively.4. In heat.m experiment with dierent values of the thermal conductivityfrqg = =002> =001 and .0005. Be sure to adjust the time step so that the stabilitycondition holds.5. Consider the variation on the thin wire where heat is lost through the sur-face of the wire. Modify heat.m and experiment with the F and u parameters.Explain your computed results.6. Consider the variation on the thin wire where heat is generated by i =1 + vlq(10w)= Modify heat.m and experiment with the parameters.7. Consider the 33 D matrix for (1.2.1). Compute Dn for n = 10> 100> 1000for dierent values of alpha so that the stability condition either does or doesnot hold.8. Suppose q = 5 so that there are 4 unknowns. Find the 4 4 matrixversion of the finite dierence model (1.2.1). Repeat the previous problem forthe corresponding 4 4 matrix.9. Justify the second and third lines in the displayed equations in the proofof the Steady State Theorem.10. Consider a variation of the Steady State Theorem where the columnvector e depends on time, that is, e is replaced by en. Formulate and prove ageneralization of this theorem.

    1.3 Diusion in a Wire with Little Insulation1.3.1 Introduction

    In this section we consider heat diusion in a thin electrical wire, which is notthermally insulated on its lateral surface. The model of the temperature willstill have the form xn+1 = Dxn + e, but the matrix D and column vector e willbe dierent than in the insulated lateral surface model in the previous section.

    1.3.2 Applied Area

    In this section we present a third model of heat transfer. In our first modelwe considered heat transfer via a discrete version of Newtons law of cooling.That is, we assumed the mass had uniform temperature with respect to space.In the previous section we allowed the temperature to be a function of both

    2004 by Chapman & Hall/CRC

    Using the MATLAB code heat.m duplicate Figures 1.2.3-1.2.5.

  • 18 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    discrete time and discrete space. Heat diused via the Fourier heat law eitherto the left or right direction in the wire. The wire was assumed to be perfectlyinsulated in the lateral surface so that no heat was lost or gained through thelateral sides of the wire. In this section we will allow heat to be lost throughthe lateral surface via a Newton-like law of cooling.

    1.3.3 Model

    Discretize both space and time and let the temperature x(lk> nw) be approx-imated by xnl where w = W@pd{n, k = O@q and O is the length of the wire.The model will have the general form

    change in heat in (kD) (heat from the source)+(diusion through the left end)+(diusion through the right end)+(heat loss through the lateral surface)=

    whose length is k and cross section is D = u2= So the lateral surface area isk2u.

    The heat loss through the lateral surface will be assumed to be directlyproportional to the product of change in time, the lateral surface area and tothe dierence in the surrounding temperature and the temperature in the wire.Let fvxu be the proportionality constant that measures insulation. If xvxu isthe surrounding temperature of the wire, then the heat loss through the smalllateral area is

    fvxu w 2uk(xvxu xnl )= (1.3.1)Heat loss or gain from a source such as electrical current and from left and rightdiusion will remain the same as in the previous section. By combining thesewe have the following approximation of the change in the heat content for thesmall volume Dk:

    fxn+1l Dk fxnlDk = Dk w i+D w N(xnl+1 xnl )@kD w N(xnl xnl1)@k+fvxu w 2uk(xvxu xnl ) (1.3.2)

    Now, divide by fDk, define = (N@f)(w@k2) and explicitly solve for xn+1l .Explicit Finite Dierence Model for Heat Diusion in a Wire.

    xn+1l = (w@f)(i + fvxu(2@u)xvxu) + (xnl+1 + xnl1)+(1 2 (w@f)fvxu(2@u))xnl (1.3.3)

    for l = 1> ===> q 1 and n = 0> ===>pd{n 1>x0l = 0 for l = 1> ===> q 1 (1.3.4)xn0 = xnq = 0 for n = 1> ===>pd{n= (1.3.5)

    2004 by Chapman & Hall/CRC

    This is depicted in the Figure 1.2.1 where the volume is a horizontal cylinder

  • 1.3. DIFFUSION IN A WIRE WITH LITTLE INSULATION 19

    Equation (1.3.4) is the initial temperature set equal to zero, and (1.3.5) is thetemperature at the left and right ends set equal to zero. Equation (1.3.3) maybe put into the matrix version of the first order finite dierence method. Forexample, if the wire is divided into four equal parts, then n = 4 and (1.3.3) maybe written as three scalar equations for the unknowns xn+11 > xn+12 and xn+13 :

    xn+11 = (w@f)(i + fvxu(2@u)xvxu) + (xn2 + 0) +(1 2 (w@f)fvxu(2@u))xn1

    xn+12 = (w@f)(i + fvxu(2@u)xvxu) + (xn3 + xn1) +(1 2 (w@f)fvxu(2@u))xn2

    xn+13 = (w@f)(i + fvxu(2@u)xvxu) + (0 + xn2) +(1 2 (w@f)fvxu(2@u))xn3 =

    These three scalar equations can be written as one 3D vector equation

    xn+1 = Dxn + e where (1.3.6)

    xn =

    57xn1xn2xn3

    68 > e = (w@f )I

    57

    111

    68 >

    D =

    57

    1 2 g 0 1 2 g 0 1 2 g

    68 and

    I = i + fvxu(2@u)xvxu and g = (w@f)fvxu(2@u)=An important restriction on the time step w is required to make sure the

    algorithm is stable. For example, consider the case q = 2 where equation (1.3.6)is a scalar equation and we have the simplest first order finite dierence model.Here d = 1 2 g and we must require d ? 1. If d = 1 2 g A 0 and> g A 0, then this condition will hold. If q is larger than 2, this simple conditionwill imply that the matrix products Dn will converge to the zero matrix, andthis analysis will be presented later in Chapter 2.5.

    Stability Condition for (1.3.3).

    1 2(N@f)(w@k2) (w@f)fvxu(2@u) A 0=Example. Let O = f = = 1=0> u = =05> q = 4 so that k = 1@4>N ==001> fvxu = =0005> xvxu = 10= Then = (N@f)(w@k2) = (=001)w16 and g =(w@f)fvxu(2@u) = w(=0005)(2@=05) so that 1 2(N@f)(w@k2) (w@f)fvxu(2@u) = 1 =032ww(=020) = 1 =052w A 0= Note if q increases to 20, thenthe constraint on the time step will significantly change.

    1.3.4 Method

    The numbers xn+1l generated by equations (1.3.3)-(1.3.5) are hopefully goodapproximations for the temperature at { = l{ and w = (n + 1)w= The tem-perature is often denoted by the function x({> w)= Again the xn+1l will be stored

    2004 by Chapman & Hall/CRC

  • 20 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    in a two dimensional array, which is also denoted by x but with integer indicesso that xn+1l = x(l> n+1) x(l{> (n+1)w) = temperature function. In orderto compute all xn+1l , we must use a nested loop where the i-loop (space) is theinner loop and the k-loop (time) is the outer loop. This is illustrated in the

    x(l> n+1) on the three previously computedx(l 1> n), x(l> n) and x(l+ 1> n).

    1.3.5 Implementation

    A slightly modified version of heat.m is used to illustrated the eect of changingthe insulation coe!cient, fvxu. The implementation of the above model fortemperature that depends on both space and time will have nested loops wherethe outer loop is for discrete time and the inner loop is for discrete space. Inthe MATLAB code heat1d.m these nested loops are given in lines 33-37. Lines1-29 contain the input data with additional data in lines 17-20. Here the radiusof the wire is u = =05, which is small relative to the length of the wire O = 1=0.The surrounding temperature is xvxu = 10= so that heat is lost through thelateral surface when fvxu A 0. Lines 38-41 contain the output data in the formof a surface plot for the temperature.

    MATLAB Code heat1d.m

    1. % This code models heat diusion in a thin wire.2. % It executes the explicit finite dierence method.3. clear;4. L = 1.0; % length of the wire5. T = 400.; % final time6. maxk = 100; % number of time steps7. dt = T/maxk;8. n = 10.; % number of space steps9. dx = L/n;10. b = dt/(dx*dx);11. cond = .001; % thermal conductivity12. spheat = 1.0; % specific heat13. rho = 1.; % density14. a = cond/(spheat*rho);15. alpha = a*b;16. f = 1.; % internal heat source17. dtc = dt/(spheat*rho);18. csur = .0005; % insulation coe!cient19. usur = -10; % surrounding temperature20. r = .05; % radius of the wire21. for i = 1:n+1 % initial temperature22. x(i) =(i-1)*dx;23. u(i,1) =sin(pi*x(i));24. end

    2004 by Chapman & Hall/CRC

    Figure 1.2.1 by the dependency of

  • 1.3. DIFFUSION IN A WIRE WITH LITTLE INSULATION 21

    25. for k=1:maxk+1 % boundary temperature26. u(1,k) = 0.;27. u(n+1,k) = 0.;28. time(k) = (k-1)*dt;29. end30. %31. % Execute the explicit method using nested loops.32. %33. for k=1:maxk % time loop34. for i=2:n; % space loop35. u(i,k+1) = (f +csur*(2./r))*dtc

    + (1-2*alpha - dtc*csur*(2./r))*u(i,k)+ alpha*(u(i-1,k)+u(i+1,k));

    36. end37. end38. mesh(x,time,u)39. xlabel(x)40. ylabel(time)41. zlabel(temperature)

    Two computations with dierent insulation coe!cients, fvxu, are given inIf one tries a calculation with fvxu = =0005 with a time step size

    equal to 5, then this violates the stability condition so that the model fails. Forfvxu =0005 the model did not fail with a final time equal to 400 and 100 timesteps so that the time step size equaled to 4. Note the maximum temperaturedecreases from about 125 to about 40 as fvxu increases from .0000 to .0005. Inorder to consider larger fvxu, the time step may have to be decreased so thatthe stability condition will be satisfied.

    In the next numerical experiment we vary the number of space steps fromq = 10 to q = 5 and 20. This will change the k = g{, and we will have toadjust the time step so that the stability condition holds. Roughly, if we doubleq, then we should quadruple the number of time steps. So, for q = 5 we will letpd{n = 25, and for q = 20 we will let pd{n = 400. The reader should checkthe stability condition assuming the other parameters in the numerical modelare xvxu = 10, fvxu = =0005, N = =001, = 1 and f = 1. Note the secondgraph in Figure 1.3.1 where q

    1.3.6 Assessment

    The heat conduction in a thin wire has a number of approximations. Dierentmesh sizes in either the time or space variable will give dierent numerical re-sults. However, if the stability conditions hold and the mesh sizes decrease, thenthe numerical computations will dier by smaller amounts. Other variationson the model include more complicated boundary conditions, variable thermalproperties and diusion in more than one direction.

    2004 by Chapman & Hall/CRC

    Figure 1.3.1.

    = 10 and those in Figure 1.3.2 are similar.

  • 22 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Figure 1.3.1: Diusion in a Wire with csur = .0000 and .0005

    2004 by Chapman & Hall/CRC

  • 1.3. DIFFUSION IN A WIRE WITH LITTLE INSULATION 23

    Figure 1.3.2: Diusion in a Wire with n = 5 and 20

    2004 by Chapman & Hall/CRC

  • 24 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    The above discrete model will converge, under suitable conditions, to acontinuum model of heat diusion. This is a partial dierential equation withinitial and boundary conditions similar to those in (1.3.3), (1.3.4) and (1.3.5):

    fxw = i + (Nx{){ + fvxu(2@u)(xvxu x) (1.3.7)x({> 0) = 0 and (1.3.8)x(0> w) = 0 = x(O> w) (1.3.9)

    The partial dierential equation in (1.3.6) can be derived from (1.3.2) by re-placing xnl by x(lk> nw), dividing by Dkw and letting k and w go to 0.Convergence of the discrete model to the continuous model means for all l andn the errors

    xnl x(lk> nw)go to zero as k and w go to zero. Because partial dierential equations areoften impossible to solve exactly, the discrete models are often used.

    Not all numerical methods have stability constraints on the time step. Con-sider (1.3.6) and use an implicit time discretization to generate a sequence ofordinary dierential equations

    f(xn+1 xn)@w = i + (Nxn+1{ ){ + fvxu(2@u)(xvxu xn+1)= (1.3.10)

    This does not have a stability constraint on the time step, but at each time stepone must solve an ordinary dierential equation with boundary conditions. Thenumerical solution of these will be discussed in the following chapters.

    1.3.7 Exercises

    1.e!cient. Furthermore, use fvxu = =0002 and =0010.2. In heat1d.m experiment with dierent surrounding temperatures xvxu =5>10>20.3. Suppose the surrounding temperature starts at -10 and increases by onedegree every ten units of time.

    (a). Modify the finite dierence model (1.3.3) is account for this.(b). Modify the MATLAB code heat1d.m. How does this change the

    long run solution?4. Vary the u = =01> =02> =05 and =10. Explain your computed results. Is thismodel realistic for "large" u?5. Verify equation (1.3.3) by using equation (1.3.2).6. Consider the 3 3 D matrix version of line (1.3.3) and the example ofthe stability condition on the time step. Observe Dn for n = 10> 100 and 1000with dierent values of the time step so that the stability condition either doesor does not hold.7. Consider the finite dierence model with q = 5 so that there are fourunknowns.

    2004 by Chapman & Hall/CRC

    Duplicate the computations in Figure 1.3.1 with variable insulation co-

  • 1.4. FLOW AND DECAY OF A POLLUTANT IN A STREAM 25

    (a). Find 4 4 matrix version of (1.3.3).(b). Repeat problem 6 with this 4 4 matrix

    8. Experiment with variable space steps k = g{ = O@q by letting q =5> 10> 20 and 40.steps so that the stability condition holds.9. Experiment with variable time steps gw = W@pd{n by letting pd{n =100> 200 and 400 with q = 10 and W = 400.10. Examine the graphical output from the experiments in exercises 8 and9. What happens to the numerical solutions as the time and space step sizesdecrease?11. Suppose the thermal conductivity is a linear function of the temperature,say, N = frqg = =001 + =02x where x is the temperature.

    (a). Modify the finite dierence model in (1.3.3).(b). Modify the MATLAB code heat1d.m to accommodate this variation.

    Compare the numerical solution with those given in Figure 1.3.1.

    1.4 Flow and Decay of a Pollutant in a Stream

    1.4.1 Introduction

    Consider a river that has been polluted upstream. The concentration (amountper volume) will decay and disperse downstream. We would like to predict atany point in time and in space the concentration of the pollutant. The modelof the concentration will also have the form xn+1 = Dxn+e where the matrix Dwill be defined by the finite dierence model, which will also require a stabilityconstraint on the time step.

    1.4.2 Applied Area

    Pollution levels in streams, lakes and underground aquifers have become veryserious common concern. It is important to be able to understand the conse-quences of possible pollution and to be able to make accurate predictions about"spills" and future "environmental" policy.

    Perhaps, the simplest model for chemical pollution is based on chemicaldecay, and one model is similar to radioactive decay. A continuous model isxw = gx where g is a chemical decay rate and x = x(w) is the unknownconcentration. One can use Eulers method to obtain a discrete version xn+1 =xn +w(g)xn where xn is an approximation of x(w) at w = nw, and stabilityrequires the following constraint on the time step 1wg A 0.

    Here we will introduce a second model where the pollutant changes locationbecause it is in a stream. Assume the concentration will depend on both spaceand time. The space variable will only be in one direction, which correspondsto the direction of flow in the stream. If the pollutant was in a deep lake, thenthe concentration would depend on time and all three directions in space.

    2004 by Chapman & Hall/CRC

    See Figures 1.3.1 and 1.3.2 and be sure to adjust the time

  • 26 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Figure 1.4.1: Polluted Stream

    1.4.3 Model

    Discretize both space and time, and let the concentration x at (l{> nw) beapproximated by xnl where w = W@pd{n>{ = O@q and O is the length of thestream. The model will have the general form

    change in amount (amount entering from upstream)(amount leaving to downstream)(amount decaying in a time interval)=

    This is depicted in Figure 1.4.1 where the steam is moving from left to right andthe stream velocity is positive. For time we can choose either nw ru (n+1)w.Here we will choose nw and this will eventually result in the matrix version ofthe first order finite dierence method.

    Assume the stream is moving from left to right so that the stream velocity ispositive, yho A 0. Let D be the cross sectional area of the stream. The amountof pollutant entering the left side of the volume D{ (yho A 0) is

    D(w yho) xnl1.

    The amount leaving the right side of the volume D{ (yho A 0)is

    D(w yho) xnl =

    Therefore, the change in the amount from the streams velocity is

    D(w yho) xnl1 D(w yho) xnl .

    The amount of the pollutant in the volume D{ at time nw is

    D{ xnl .

    2004 by Chapman & Hall/CRC

  • 1.4. FLOW AND DECAY OF A POLLUTANT IN A STREAM 27

    The amount of the pollutant that has decayed, ghf is decay rate, is

    D{ w ghf xnl .

    By combining these we have the following approximation for the change duringthe time interval w in the amount of pollutant in the small volume D{:

    D{ xn+1l D{ xnl = D(w yho)xnl1 D(w yho)xnlD{ w ghf xnl = (1.4.1)

    Now, divide by D{ and explicitly solve for xn+1l .

    Explicit Finite Dierence Model of Flow and Decay.

    xn+1l = yho(w@{)xnl1 + (1 yho(w@{)w ghf)xnl (1.4.2)l = 1> ===> q 1 and n = 0> ===>pd{n 1>x0l = given for l = 1> ===> q 1 and (1.4.3)xn0 = given for n = 1> ===>pd{n= (1.4.4)

    Equation (1.4.3) is the initial concentration, and (1.4.4) is the concentrationfar upstream. Equation (1.4.2) may be put into the matrix version of the firstorder finite dierence method. For example, if the stream is divided into threeequal parts, then q = 3 and (1.4.2) may be written three scalar equations forxn+11 , xn+12 and xn+13 :

    xn+11 = yho(w@{)xn0 + (1 yho(w@{)w ghf)xn1xn+12 = yho(w@{)xn1 + (1 yho(w@{)w ghf)xn2xn+13 = yho(w@{)xn2 + (1 yho(w@{)w ghf)xn3 .

    These can be written as one 3D vector equation xn+1 = Dxn + e57xn+11xn+12xn+13

    68 =

    57f 0 0g f 00 g f

    6857xn1xn2xn3

    68+

    57gxn000

    68 (1.4.5)

    where g = yho (w@{) and f = 1 g ghf w=

    An extremely important restriction on the time step w is required to makesure the algorithm is stable. For example, consider the case q = 1 where theabove is a scalar equation, and we have the simplest first order finite dierencemodel. Here d = 1 yho(w@{) ghf w and we must require d ? 1. Ifd = 1 yho(w@{) ghf w A 0 and yho> ghf A 0, then this condition willhold. If q is larger than 1, this simple condition will imply that the matrixproducts Dn converge to the zero matrix, and an analysis of this will be givenin Section 2.5.

    2004 by Chapman & Hall/CRC

  • 28 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Stability Condition for (1.4.2).

    1 yho(w@{) ghf w and yho> ghf A 0=Example. Let O = 1=0> yho = =1> ghf = =1> and q = 4 so that { = 1@4= Then1 yho(w@{) ghf w = 1 =1w4 =1w = 1 =5w A 0= If q increases to20, then the stability constraint on the time step will change.

    In the case where ghf = 0, then d = 1yho(w@{) A 0 means the enteringfluid must must not travel, during a single time step, more than one space step.This is often called the Courant condition on the time step.

    1.4.4 Method

    In order to compute xn+1l for all values of l and n, which in the MATLAB codeis stored in the array x(l> n + 1), we must use a nested loop where the i-loop(space) is inside and the k-loop (time) is the outer loop. In this flow modelx(l> n + 1) depends directly on the two previously computed x(l 1> n) (theupstream concentration) and x(l> n). This is dierent from the heat diusionmodel, which requires an additional value x(l+1> n) and a boundary conditionat the right side. In heat diusion heat energy may move in either direction;in our model of a pollutant the amount moves in the direction of the streamsflow.

    1.4.5 Implementation

    The MATLAB code flow1d.m is for the explicit flow and decay model of apolluted stream. Lines 1-19 contain the input data where in lines 12-15 theinitial concentration was a trig function upstream and zero downstream. Lines16-19 contain the farthest upstream location that has concentration equal to.2. The finite dierence scheme is executed in lines 23-27, and three possiblegraphical outputs are indicated in lines 28-30. A similar code is heatl.f90 writtenin Fortran 9x.

    MATLAB Code flow1d.m

    1. % This a model for the concentration of a pollutant.2. % Assume the stream has constant velocity.3. clear;4. L = 1.0; % length of the stream5. T = 20.; % duration of time6. K = 200; % number of time steps7. dt = T/K;8. n = 10.; % number of space steps9. dx = L/n;10. vel = .1; % velocity of the stream11. decay = .1; % decay rate of the pollutant12. for i = 1:n+1 % initial concentration

    2004 by Chapman & Hall/CRC

  • 1.4. FLOW AND DECAY OF A POLLUTANT IN A STREAM 29

    13. x(i) =(i-1)*dx;14. u(i,1) =(i?=(n/2+1))*sin(pi*x(i)*2)+(iA(n/2+1))*0;15. end16. for k=1:K+1 % upstream concentration17. time(k) = (k-1)*dt;18. u(1,k) = -sin(pi*vel*0)+.2;19. end20. %21. % Execute the finite dierence algorithm.22. %23. for k=1:K % time loop24. for i=2:n+1 % space loop25. u(i,k+1) =(1 - vel*dt/dx -decay*dt)*u(i,k)

    + vel*dt/dx*u(i-1,k);26. end27. end28. mesh(x,time,u)29. % contour(x,time,u)30. % plot(x,u(:,1),x,u(:,51),x,u(:,101),x,u(:,151))

    One expects the location of the maximum concentration to move down-stream and to decay.was generated by the mesh command and is concentration versus time-space.The middle graph is a contour plot of the concentration. The bottom graphcontains four plots for the concentration at four times 0, 5, 10 and 15 versusspace, and here one can clearly see the pollutant plume move downstream anddecay.

    The following MATLAB code mov1d.m will produce a frame by frame "movie"which does not require a great deal of memory. This code will present graphsof the concentration versus space for a sequence of times. Line 1 executes theabove MATLAB file flow1d where the arrays { and x are created. The loop inlines 3-7 generates a plot of the concentrations every 5 time steps. The next plotis activated by simply clicking on the graph in the MATLAB figure window. Inthe pollution model it shows the pollutant moving downstream and decaying.

    MATLAB Code mov1d.m

    1. flow1d;2. lim =[0 1. 0 1];3. for k=1:5:1504. plot(x,u(:,k))5. axis(lim);6. k = waitforbuttonpress;7. end

    yho = 1=3, and this, with thesame other constants, violates the stability condition. For the time step equal

    2004 by Chapman & Hall/CRC

    This is illustrated in Figure 1.4.2 where the top graph

    In Figure 1.4.3 we let the streams velocity be

  • 30 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Figure 1.4.2: Concentration of Pollutant

    2004 by Chapman & Hall/CRC

  • 1.4. FLOW AND DECAY OF A POLLUTANT IN A STREAM 31

    Figure 1.4.3: Unstable Concentration Computation

    to .1 and the space step equal to .1, a flow rate equal to 1.3 means that thepollutant will travel .13 units in space, which is more than one space step. Inorder to accurately model the concentration in a stream with this velocity, wemust choose a smaller time step. Most explicit numerical methods for fluid flowproblems will not work if the time step is so large that the computed flow for atime step jumps over more than one space step.

    1.4.6 Assessment

    The discrete model is accurate for suitably small step sizes. The dispersionof the pollutant is a continuous process, which could be modeled by a partialdierential equation with initial and boundary conditions:

    xw = yho x{ ghf x> (1.4.6)x({> 0) = given and (1.4.7)x(0> w) = given. (1.4.8)

    This is analogous to the discrete model in (1.4.2), (1.4.3) and (1.4.4). Thepartial dierential equation in (1.4.6) can be derived from (1.4.1) by replacingxnl by x(l{> nw)> dividing by D{w and letting { and w go to 0. Likethe heat models the step sizes should be carefully chosen so that stability holdsand the errors

    xnl x(l{> nw)between the discrete and continuous models are small.

    2004 by Chapman & Hall/CRC

  • 32 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Often it is di!cult to determine the exact values of the constants yho andghf. Exactly what is the eect of having measurement errors, say of 10%, onconstants yho> ghf or the initial and boundary conditions? What is interaction ofthe measurement errors with the numerical errors? The flow rate, yho, certainlyis not always constant. Moreover, there may be fluid flow in more than onedirection.

    1.4.7 Exercises

    1.2. Vary the decay rate, ghf = =05> =1> 1= and 2.0. Explain your computedresults.3. Vary the flow rate, yho = =05> =1> 1. and 2.0. Explain your computedresults.4. Consider the 3 3 D matrix. Use the parameters in the example of thestability condition and observe Dn when n = 10> 100 and 1000 for dierentvalues of yho so that the stability condition either does or does not hold.5. Suppose q = 4 so that there are four unknowns. Find the 4 4 matrixdescription of the finite dierence model (1.4.2). Repeat problem 4 with thecorresponding 4 4 matrix.6. Verify that equation (1.4.2) follows from equation (1.4.1).7. Experiment with dierent time steps by varying the number of time stepsN = 100> 200> 400 and keeping the space steps constant by using q = 10.8. Experiment with dierent space steps by varying the number space stepsq = 5> 10> 20> 40 and keeping the time steps constant by using N = 200.9. In exercises 7 and 8 what happens to the solutions as the mesh sizesdecrease, provided the stability condition holds?10. Modify the model to include the possibility that the upstream boundarycondition varies with time, that is, the polluting source has a concentration thatdepends on time. Suppose the concentration at { = 0 is a periodic function=1 + =1 vlq(w@20)=

    (a). Change the finite dierence model (1.4.2)-(1.4.4) to account for this.(b). Modify the MATLAB code flow1d.m and use it to study this case.

    11. Modify the model to include the possibility that the steam velocitydepends on time. Suppose the velocity of the stream increases linearly over thetime interval from w = 0 to w = 20 so that yho = =1 + =01w.

    (a). Change the finite dierence model (1.4.2)-(1.4.4) to account for this.(b). Modify the MATLAB code flow1d.m and use it to study this case.

    1.5 Heat and Mass Transfer in Two Directions

    1.5.1 Introduction

    The restriction of the previous models to one space dimension is often not veryrealistic. For example, if the radius of the cooling wire is large, then one should

    2004 by Chapman & Hall/CRC

    Duplicate the computations in Figure 1.4.2.

  • 1.5. HEAT AND MASS TRANSFER IN TWO DIRECTIONS 33

    expect to have temperature variations in the radial direction as well as in thedirection of the wire. Or, in the pollutant model the source may be on a shallowlake and not a stream so that the pollutant may move within the lake in plane,that is, the concentrations of the pollutant will be a function of two spacevariables and time.

    1.5.2 Applied Area

    Consider heat diusion in a thin 2D cooling fin where there is diusion in boththe { and | directions, but any diusion in the } direction is minimal and canbe ignored. The objective is to determine the temperature in the interior of thefin given the initial temperature and the temperature on the boundary. Thiswill allow us to assess the cooling fins eectiveness. Related problems comefrom the manufacturing of large metal objects, which must be cooled so as notto damage the interior of the object. A similar 2D pollutant problem is totrack the concentration of a pollutant moving across a lake. The source will beupwind so that the pollutant is moving according to the velocity of the wind.We would like to know the concentration of the pollutant given the upwindconcentrations along the boundary of the lake, and the initial concentrations inthe lake.

    1.5.3 Model

    The models for both of these applications evolve from partitioning a thin plateor shallow lake into a set of small rectangular volumes, {|W> where W is the

    of heat or pollutant through the right vertical face. In the case of heat diusion,the heat entering or leaving through each of the four vertical faces must be givenby the Fourier heat law applied to the direction perpendicular to the verticalface. For the pollutant model the amount of pollutant, concentration timesvolume, must be tracked through each of the four vertical faces. This type ofanalysis leads to the following models in two space directions. Similar modelsin three space directions are discussed in Sections 4.4-4.6 and 6.2-6.3.

    In order to generate a 2D time dependent model for heat transfer diusion,the Fourier heat law must be applied to both the { and | directions. Thecontinuous and discrete 2D models are very similar to the 1D versions. Inthe continuous 2D model the temperature x will depend on three variables,x({> |> w). In (1.5.1) (Nx|)| models the diusion in the | direction; it modelsthe heat entering and leaving the left and right of the rectangle k = { byk = |= More details of this derivation will be given in Section 3.2.Continuous 2D Heat Model for u = u(x>y> t).

    fxw (Nx{){ (Nx|)| = i (1.5.1)x({> |> 0) = given (1.5.2)x({> |> w) = given on the boundary (1.5.3)

    2004 by Chapman & Hall/CRC

    small thickness of the volume. Figure 1.5.1 depicts this volume, and the transfer

  • 34 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Figure 1.5.1: Heat or Mass Entering or Leaving

    Explicit Finite Dierence 2D Heat Model: unl>m x(lk> mk> nw)=

    xn+1l>m = (w@f)i + (xnl+1>m + xnl1>m + xnl>m+1 + xnl>m1)+(1 4)xnl>m (1.5.4)

    = (N@f)(w@k2)> l> m = 1> ==> q 1 and n = 0> ==>pd{n 1>x0l>m = given> l> m = 1> ==> q 1 (1.5.5)xnl>m = given> n = 1> ===>pd{n, and l> m on the boundary grid. (1.5.6)

    Stability Condition.

    1 4 A 0 and A 0.The model for the dispersion of a pollutant in a shallow lake is similar. Let

    x({> |> w) be the concentration of a pollutant. Suppose it is decaying at a rateequal to ghf units per time, and it is being dispersed to other parts of the lakeby a known wind with constant velocity vector equal to (y1> y2). Following thederivations in Section 1.4, but now considering both directions, we obtain thecontinuous and discrete models. We have assumed both the velocity componentsare nonnegative so that the concentration levels on the upwind (west and south)sides must be given. In the partial dierential equation for the continuous 2Dmodel the term y2x| models the amount of the pollutant entering and leavingin the | direction for the thin rectangular volume whose base is { by |.Continuous 2D Pollutant Model for u(x>y> t).

    xw = y1x{ y2x| ghf x> (1.5.7)x({> |> 0) = given and (1.5.8)x({> |> w) = given on the upwind boundary= (1.5.9)

    2004 by Chapman & Hall/CRC

  • 1.5. HEAT AND MASS TRANSFER IN TWO DIRECTIONS 35

    Explicit Finite Dierence 2D Pollutant Model: xnl>m x(l{> m|> nw).xn+1l>m = y1(w@{)xnl1>m + y2(w@|)xnl>m1 + (1.5.10)

    (1 y1(w@{) y2(w@|)w ghf)xnl>mx0l>m = given and (1.5.11)

    xn0>m and xnl>0 = given= (1.5.12)

    Stability Condition.

    1 y1(w@{) y2(w@|)w ghf A 0=

    1.5.4 Method

    Consider heat diusion or pollutant transfer in two directions and let xn+1lm bethe approximation of either the temperature or the concentration at ({> |> w)= (l{> m|> (n + 1)w). In order to compute all xn+1lm , which will henceforthbe stored in the array x(l> m> n + 1), one must use nested loops where the j-loop and i-loop (space) are inside and the k-loop (time) is the outer loop. Thecomputations in the inner loops depend only on at most five adjacent values:x(l> m> n)> x(l 1> m> n), x(l + 1> m> n), x(l> m 1> n)> and x(l> m + 1> n) all at theprevious time step, and therefore, the x(l> m> n+1) and x(bl>bm> n+1) computationsare independent. The classical order of the nodes is to start with the bottomgrid row and move from left to right. This means the outermost loop will bethe k-loop (time), the middle will be the j-loop (grid row), and the innermostwill be the i-loop (grid column). A notational point of confusion is in the arrayx(l> m> n)= Varying the l corresponds to moving up and down in column m; butthis is associated with moving from left to right in the grid row m of the physicaldomain for the temperature or the concentration of the pollutant.

    1.5.5 Implementation

    The following MATLAB code heat2d.m is for heat diusion on a thin plate,which has initial temperature equal to 70 and has temperature at boundary{ = 0 equal to 370 for the first 120 time steps and then set equal to 70 after 120time steps. The other temperatures on the boundary are always equal to 70.The code in heat2d.m generates a 3D array whose entries are the temperaturesfor 2D space and time. The input data is given in lines 1-31, the finite dierencemethod is executed in the three nested loops in lines 35-41, and some of theoutput is graphed in the 3D plot for the temperature at the final time step inline 43.equal to Whqg = 80 time units, and here the interior of the fin has cooled downto about 84.

    MATLAB Code heat2d.m

    1. % This is heat diusion in 2D space.

    2004 by Chapman & Hall/CRC

    The 3D plot in Figure 1.5.2 is the temperature for the final time step

  • 36 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    2. % The explicit finite dierence method is used.3. clear;4. L = 1.0; % length in the x-direction5. W = L; % length in the y-direction6. Tend = 80.; % final time7. maxk = 300;8. dt = Tend/maxk;9. n = 20.;10. % initial condition and part of boundary condition11. u(1:n+1,1:n+1,1:maxk+1) = 70.;12. dx = L/n;13. dy = W/n; % use dx = dy = h14. h = dx;15. b = dt/(h*h);16. cond = .002; % thermal conductivity17. spheat = 1.0; % specific heat18. rho = 1.; % density19. a = cond/(spheat*rho);20. alpha = a*b;21. for i = 1:n+122. x(i) =(i-1)*h; % use dx = dy = h23. y(i) =(i-1)*h;24. end25. % boundary condition26. for k=1:maxk+127. time(k) = (k-1)*dt;28. for j=1:n+129. u(1,j,k) =300.*(k?120)+ 70.;30. end31. end32. %33. % finite dierence method computation34. %35. for k=1:maxk36. for j = 2:n37. for i = 2:n39 u(i,j,k+1) =0.*dt/(spheat*rho)

    +(1-4*alpha)*u(i,j,k)+alpha*(u(i-1,j,k)+u(i+1,j,k)+u(i,j-1,k)+u(i,j+1,k));

    39. end40. end41. end42. % temperature versus space at the final time43. mesh(x,y,u(:,:,maxk))

    2004 by Chapman & Hall/CRC

  • 1.5. HEAT AND MASS TRANSFER IN TWO DIRECTIONS 37

    Figure 1.5.2: Temperature at Final Time

    The MATLAB code mov2dheat.m generates a sequence of 3D plots of tem-perature versus space. One can see the heat moving from the hot side into theinterior and then out the cooler boundaries. This is depicted for four times in

    You may find it interesting to vary the parameters and also change the 3D plotto a contour plot by replacing mesh by contour.

    MATLAB Code mov2dheat.m1. % This generates a sequence of 3D plots of temperature.2. heat2d;3. lim =[0 1 0 1 0 400];4. for k=1:5:2005. mesh(x,y,u(:,:,k))6. title (heat versus space at dierent times )7. axis(lim);8. k = waitforbuttonpress;9. end

    The MATLAB code flow2d.m simulates a large spill of a pollutant along thesouthwest boundary of a shallow lake. The source of the spill is controlled after25 time steps so that the pollutant plume moves across the lake as depictedby the mesh plots for dierent times. The MATLAB code flow2d.m generatesthe 3D array of the concentrations as a function of the {> | and time grid. Theinput data is given in lines 1-33, the finite dierence method is executed in thethree nested loops in lines 37-43, and the output is given in lines 44 and 45.

    2004 by Chapman & Hall/CRC

    Figure 1.5.3 where the scaling of the vertical axis has changed as time increases.

  • 38 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Figure 1.5.3: Heat Diusing Out a Fin

    MATLAB Code flow2d.m

    1. % The is pollutant flow across a lake.2. % The explicit finite dierence method is used.3. clear;4. L = 1.0; % length in x direction5. W = 4.0; % length in y direction6. T = 10.; % final time7. maxk = 200; % number of time steps8. dt = T/maxk;9. nx = 10.; % number of steps in x direction10. dx = L/nx;11. ny = 20.; % number of steps in y direction12. dy = W/ny;13. velx = .1; % wind speed in x direction14. vely = .4; % wind speed in y direction15. decay = .1; %decay rate16. % Set initial conditions.17. for i = 1:nx+118. x(i) =(i-1)*dx;19. for j = 1:ny+120. y(j) =(j-1)*dy;21. u(i,j,1) = 0.;22. end

    2004 by Chapman & Hall/CRC

  • 1.5. HEAT AND MASS TRANSFER IN TWO DIRECTIONS 39

    23. end24. % Set upwind boundary conditions.25. for k=1:maxk+126. time(k) = (k-1)*dt;27. for j=1:ny+128. u(1,j,k) = .0;29. end30. for i=1:nx+131. u(i,1,k) = (i?=(nx/2+1))*(k?26)

    *5.0*sin(pi*x(i)*2)+(iA(nx/2+1))*.1;

    32. end33. end34. %35. % Execute the explicit finite dierence method.36. %37. for k=1:maxk38. for i=2:nx+1;39. for j=2:ny+1;40. u(i,j,k+1) =(1 - velx*dt/dx

    - vely*dt/dy - decay*dt)*u(i,j,k)+ velx*dt/dx*u(i-1,j,k)+ vely*dt/dy*u(i,j-1,k);

    41. end42. end43. end44. mesh(x,y,u(:,:,maxk))45. % contour(x,y,u(:,:,maxk))

    the concentration at the final time step as computed in

    ious time steps. Note the vertical axis for the concentration is scaled so that

    (.1,.4). The MATLAB code mov2dflow.m generates a sequence of mesh plots.

    MATLAB1. % This generates a sequence of 3D plots of concentration.2. flow2d;3. lim =[0 1 0 4 0 3];4. for k=1:5:2005. %contour(x,y,u(:,:,k))6. mesh(x,y,u(:,:,k))7. title (concentration versus space at dierent times )8. axis(lim);9. k = waitforbuttonpress;10. end

    2004 by Chapman & Hall/CRC

    isFigure

    the concentration plume decreases and moves in the direction of wind velocity

    1.5.4

    Code mov2dflow.m

    flow2d.m. Figure 1.5.5 is sequence of mesh plots for the concentrations at var-

  • 40 CHAPTER 1. DISCRETE TIME-SPACE MODELS

    Figure 1.5.4: Concentration at the Final Time

    Figure 1.5.5: Concentrations at Dierent Times

    2004 by Chapman & Hall/CRC

  • 1.5. HEAT AND MASS TRANSFER IN TWO DIRECTIONS 41

    1.5.6 Assessment

    Diusion of heat or the transfer of a pollutant may occur