An Introduction to Scientific Computing Ionut Danaila Pascal Joly Sidi Mahmoud Kaber Marie Postel An Introduction to Scientific Computing Twelve Computational Projects Solved with MATLAB Ionut Danaila Pascal Joly Laboratoire Jacques-Louis Lions Laboratoire Jacques-Louis Lions Université Pierre et Marie Curie Université Pierre et Marie Curie Paris 75252 Paris 75252 FRANCE FRANCE [email protected] [email protected] Sidi Mahmoud Kaber Marie Postel Laboratoire Jacques-Louis Lions Laboratoire Jacques-Louis Lions Université Pierre et Marie Curie Université Pierre et Marie Curie Paris 75252 Paris 75252 FRANCE FRANCE [email protected] [email protected] Library ofCongress Control Number:2006931780 ISBN-10:0-387-30889-X ISBN-13:978-0-387-30889-0 Printed on acid-free paper. © 2007 Springer Science+Business Media,LLC MATLAB® is a trademark of The Math Works,Inc.and is used with permission.The Math Works does not warrant the accuracy of the text or exercises in this book.This book’s use or discussion of MATLAB® software or related products does not constitute endorsement or sponsorship by The Math Works of a particular pedagogical approach or particular use of the MATLAB® software. All rights reserved.This work may not be translated or copied in whole or in part without the written permission ofthe publisher (Springer Science+Business Media,LLC,233 Spring Street, New York,NY 10013,USA),except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation,computer software,or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use in this publication oftrade names,trademarks,service marks,and similar terms,even if they are not identified as such,is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. 9 8 7 6 5 4 3 2 1 springer.com Ionut Danaila Pascal Joly Sidi Mahmoud Kaber Marie Postel An Introduction to Scientific Computing: Twelve Computational Projects Solved with MATLAB SPIN Springer’s internal project number, if known – Monograph – October 13, 2006 Springer Berlin Heidelberg NewYork HongKong London Milan Paris Tokyo To Alice,Luminita Romain,Sylvain Sarah,Thomas Camille,Paul Preface Teaching or learning numerical methods in applied mathematics cannot be conceived nowadays without numerical experimentation on computers. There is a vast literature devoted either to theoretical numerical methods or nu- merical programming of basic algorithms, but there are few texts offering a complete discussion of numerical issues involved in the solution of concrete and relatively complex problems. This book is an attempt to fill this need. It is our belief that advantages and drawbacks of a numerical method cannot beaccountedforwithoutone’sexperiencingallthestepsofscientificcomput- ing, from physical and mathematical description of the problem to numerical formulation and programming and, finally, to critical discussion of numerical results. The book provides twelve computational projects aimed at numerically solving problems selected to cover a broad spectrum of applications, from fluidmechanics,chemistry,elasticity,thermalscience,computer-aideddesign, signal and image processing, etc. Even though the main volume of this text concerns the numerical analysis of computational methods and their imple- mentation, we have tried to start, when possible, from realistic problems of practical interest for researchers and engineers. For each project, an introductory record card summarizes the mathemat- ical and numerical topics explained and the fields of application of the ap- proach. A level of difficulty, scaling from 1 to 3, is assigned to each project. Most of the projects are of level 1 or 2 and can be easily tackled; the reader will no doubt realize that projects of level 3 require a solid background in both numerical analysis and computational techniques. Excepting projects 1 and 3, which are more theoretical, all projects follow thetypicalstepsofscientificcomputing:physicalandmathematicalmodeling of the problem, numerical discretization, construction of a numerical algo- rithm, and, finally, programming. We have placed considerable emphasis on practical issues of computational methods that are not usually available in basic textbooks. Numerical checking of accuracy or stability, the choice of boundary conditions, the effective solving of linear systems, and comparison to exact solutions when available are only a few examples of problems en- VIII Preface countered in the application of numerical methods. The last section of each project contains solutions of all proposed exercises and guides the reader in using the MATLAB scripts that can be accessed via the publisher’s web site www.springer.com. Programming techniques such as vectorial programming and memory storage optimization are also addressed. We finally discuss the physicalmeaningoftheobtainedresults.Thecomplementaryreferencesgiven attheendofeachchapterformaguideforfurther,morespecialized,reading. The text offers two levels of interest. The mathematical framework pro- vides a basic grounding in the subject of numerical analysis of partial differ- ential equations and main discretization techniques (finite differences, finite elements, spectral methods, wavelets). Meanwhile, we hope that the informa- tion contained herein and the wide range of topics covered by the book will allow the reader to select the appropriate numerical method to solve his or her particular problem. ThebookisbasedonmaterialofferedbytheauthorsincoursesatUniver- sit´e Pierre et Marie Curie (Paris, France) and different engineering schools. It is primarily intended as a graduate-level text in applied mathematics, but itmayalsobeusedbystudentsinengineeringorphysicalsciences.Itwillalso be a useful reference for researchers and practicing engineers. Since different possible developments of the projects are suggested, the text can be used to propose assignments at different graduate levels. Despite our efforts to avoid typing, spelling, or other errors, the reader will no doubt find some remaining. We shall appreciate all feedback notifying us of any mistakes, as well as comments and suggestions that will help us to improvethetext.Pleaseusethee-mailaddressesgivenbelowforthispurpose. We conclude by saying a few words about the programs provided with thisbook.TheyarewritteninMATLAB,awidelyusedsoftwareenvironment for scientific computing produced by The MathWorks Inc. We consider that an interpreted language (such as MATLAB, SCILAB, OCTAVE) is the ideal frameworktostartascientificprogrammingactivity.Debuggingisverysimple and the wide variety of available numerical tools (for solving linear systems, integrating ordinary differential equations, etc.) allows one to concentrate on the main features of the resolution algorithm. The highly versatile graphical interface is also very important to easy visualization of the obtained results. Our programs are written with a general concern for simplicity and effi- ciencyonordinarypersonalcomputers;programlinesarecommentedinwhat we hope is sufficient detail for the reader to follow mathematical develop- ments. Programming tricks are discussed in the text when they seem to be of generalinterest.Projects11and12arealsoprovidedwithmoreelaboratever- sions of the programs, using interactive graphical user interfaces. The reader should try to modify these programs to test different suggested run cases or extensions of the projects. We believe that experience with these simple pro- grams will be valuable in writing numerical codes using compiled languages (such as Fortran, C, or C++) to solve real industrial problems on mainframe computers. Preface IX Paris, October 2005 Ionut Danaila ([email protected]) Pascal Joly ([email protected]) Sidi Mahmoud Kaber ([email protected]) Marie Postel ([email protected]) Laboratoire Jacques-Louis Lions Universit´e Pierre et Marie Curie (Paris 6) and Centre National de la Recherche Scientifique (CNRS) Contents 1 Numerical Approximation of Model Partial Differential Equations.................................................. 1 1.1 Discrete Integration Methods for Ordinary Differential Equations .............................................. 1 1.1.1 Construction of Numerical Integration Schemes ....... 2 1.1.2 General Form of Numerical Schemes ................. 6 1.1.3 Application to the Absorption Equation.............. 8 1.1.4 Stability of a Numerical Scheme..................... 9 1.2 Model Partial Differential Equations ....................... 11 1.2.1 The Convection Equation .......................... 11 1.2.2 The Wave Equation ............................... 14 1.2.3 The Heat Equation ................................ 17 1.3 Solutions and Programs .................................. 19 Chapter References........................................... 30 2 Nonlinear Differential Equations: Application to Chemical Kinetics ................................................... 33 2.1 Physical Problem and Mathematical Modeling .............. 33 2.2 Stability of the System................................... 34 2.3 Model for the Maintained Reaction ........................ 36 2.3.1 Existence of a Critical Point and Stability ............ 36 2.3.2 Numerical Solution ................................ 37 2.4 Model of Reaction with a Delay Term...................... 37 2.5 Solutions and Programs .................................. 41 Chapter References........................................... 48 3 Polynomial Approximation ................................ 49 3.1 Introduction ............................................ 49 3.2 Polynomial Interpolation ................................. 50 3.2.1 Lagrange Interpolation............................. 51 3.2.2 Hermite Interpolation.............................. 57