Scientific Computing with Matlab and Octave, Skripte von Informatik

Unvollständige Textvorschau

Nur auf Docsity: Lade Scientific Computing with Matlab and Octave und mehr Skripte als PDF für Informatik herunter!

Alfi o Quarteroni

Fausto Saleri · Paola Gervasio

Scientific Computing

with MATLAB and

Octave

Fourth Edition

Editorial Board

T. J.Barth

M.Griebel

D.E.Keyes

R.M.Nieminen

D.Roose

T.Schlick

Texts in Computational

Science and Engineering 2

Editors

Timothy J. Barth

Michael Griebel

David E. Keyes

Risto M. Nieminen

Dirk Roose

Tamar Schlick

For further volumes: http://www.springer.com/series/

Alfio Quarteroni •^ Fausto Saleri •

Paola Gervasio

Scientific Computing

with MATLAB

and Octave

Fourth Edition

123

Alfio Quarteroni MATHICSE-CMCS Ecole Polytechnique F´ed´erale de Lausanne Lausanne Switzerland

Paola Gervasio DICATAM Universit`a degli Studi di Brescia Brescia Italy

Fausto Saleri (1965-2007) MOX - Politecnico di Milano Milano Italy

ISSN 1611- ISBN 978-3-642-45366-3 ISBN 978-3-642-45367-0 (eBook) DOI 10.1007/978-3-642-45367- Springer Heidelberg New York Dordrecht London

Library of Congress Control Number: 2014932425

Mathematics Subject Classification (2010): 65-01, 68U01, 68N

©c Springer-Verlag Berlin Heidelberg 2003, 2006, 2010, 2014 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illus- trations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or im- plied, with respect to the material contained herein. The picture on the cover shows a re-entrant electrical wave on a slab of homogeneous excitable medium, exhibiting spiral turbulence and spatiotemporal chaos. Computation by Ricardo Ruiz- Baier, IST, University of Lausanne, CH.

Printed on acid-free paper

Springer is part of Springer Science+Business Media (www.springer.com)

Preface

Preface to the First Edition

This textbook is an introduction to Scientific Computing. We will illustrate several numerical methods for the computer solution of cer- tain classes of mathematical problems that cannot be faced by paper and pencil. We will show how to compute the zeros or the integrals of continuous functions, solve linear systems, approximate functions by polynomials and construct accurate approximations for the solution of differential equations. With this aim, in Chapter 1 we will illustrate the rules of the game that computers adopt when storing and operating with real and complex numbers, vectors and matrices. In order to make our presentation concrete and appealing we will adopt the programming environment MATLAB ^1 as a faithful com- panion. We will gradually discover its principal commands, statements and constructs. We will show how to execute all the algorithms that we introduce throughout the book. This will enable us to furnish an im- mediate quantitative assessment of their theoretical properties such as stability, accuracy and complexity. We will solve several problems that will be raised through exercises and examples, often stemming from spe- cific applications. Several graphical devices will be adopted in order to render the read- ing more pleasant. We will report in the margin the MATLAB command along side the line where that command is being introduced for the first

time. The symbol will be used to indicate the presence of exercises,

the symbol to indicate the presence of a MATLAB program, while

(^1) MATLAB is a trademark of TheMathWorks Inc., 24 Prime Park Way, Natick, MA 01760, Tel: 001+508-647-7000, Fax: 001+508-647-7001.

VII

Preface IX

includes many numerical facilities of MATLAB and is freely distributed under the GNU General Public License. Throughout the book, we shall often make use of the expression “MATLAB command”: in this case, MATLAB should be understood as the language which is the common subset of both programs MAT- LAB and Octave. We have striven to ensure a seamless usage of our codes and programs under both MATLAB and Octave. In the few cases where this does not apply, we shall write a short explanation notice at the end of each corresponding section. For this second edition we would like to thank Paola Causin for hav- ing proposed several problems, Christophe Prud´homme, John W. Eaton and David Bateman for their help with Octave, and Silvia Quarteroni for the translation of the new sections. Finally, we kindly acknowledge the support of the Poseidon project of the Ecole Polytechnique F´ed´erale de Lausanne.

Lausanne and Milano Alfio Quarteroni May 2006 Fausto Saleri

Preface to the Third Edition

This third edition features a complete revisitation of the whole book, many improvements in style and content to all the chapters, as well as a substantial new development of those chapters devoted to the numerical approximation of boundary-value problems and initial-boundary-value problems. We remind the reader that all the programs introduced in this text can be downloaded from the web address mox.polimi.it/qs

Lausanne, Milano and Brescia Alfio Quarteroni March 2010 Paola Gervasio

Preface to the Fourth Edition

The fourth edition features the addition of a new chapter on numeri- cal optimization of both univariate and multivariate functions in which several methods are presented, discussed and analyzed. For unconstrained minimization, we consider derivative free methods, descent (or line search) methods, and trust region methods. For constrained minimization we restrict our discussion to penaliza- tion methods and augmented Lagrangian methods.

X Preface

As for the other chapters of this book, also this new chapter is sup- ported by examples, exercises and programs written in both MATLAB and Octave environments. The addition of this chapter made it necessary a renumbering of several other chapters with respect to the previous editions. Moreover, new sections have been added in some other chapters. Finally we remind the reader that all programs presented in this book can be downloaded from the web address

http://mox.polimi.it/qs

Lausanne, Milano and Brescia Alfio Quarteroni December 2013 Paola Gervasio

Contents XV

XVIII Index of MATLAB and Octave programs

8.3 cranknic: Crank-Nicolson method...................... 283 8.4 predcor: predictor-corrector method..................... 306 8.5 feonestep: one step of the forward Euler method.......... 307 8.6 beonestep: one step of the backward Euler method....... 307 8.7 cnonestep: one step of the Crank-Nicolson method........ 307 8.8 newmark: Newmark method........................... 312 8.9 fvinc: forcing term for the spherical pendulum problem..... 316 8.10 threebody: forcing term for the simplified three body system........................................ 318 9.1 bvp: approximation of a two-point diffusion-convection-reaction problem by the finite difference method.................................... 335 9.2 poissonfd: approximation of the Poisson problem with Dirichlet boundary data by the five-point finite difference method.................................... 345 9.3 heattheta: θ-method for the one-dimensional heat equation....................................... 350 9.4 newmarkwave: Newmark method for the wave equation... 369 10.1 gausslegendre: Gauss-Legendre composite quadrature formula, with n = 1.................................. 391 10.2 rk2: Heun (or RK2) method........................... 414 10.3 rk3: explicit Runge-Kutta method of order 3.............. 416 10.4 neumann: numerical solution of a Neumann boundary-value problem............................................ 423 10.5 hyper: Lax-Friedrichs, Lax-Wendroff and upwind schemes... 427

1

What can’t be ignored

In this book we will systematically use elementary mathematical con- cepts which the reader should know already, yet he or she might not recall them immediately. We will therefore use this chapter to refresh them and we will con- dense notions which are typical of courses in Calculus, Linear Algebra and Geometry, yet rephrasing them in a way that is suitable for use in Scientific Computing. At the same time we will introduce new concepts which pertain to the field of Scientific Computing and we will begin to ex- plore their meaning and usefulness with the help of MATLAB (MATrix LABoratory), an integrated environment for programming and visual- ization. We shall also use GNU Octave (in short, Octave), an intepreter for a high-level language mostly compatible with MATLAB which is distributed under the terms of the GNU GPL free-software license and which reproduces a large part of the numerical facilities of MATLAB. In Section 1.1 we will give a quick introduction to MATLAB and Octave, while we will present the elements of programming in Section 1.7. However, we refer the interested readers to [Att11] for a description of the MATLAB language and to [EBH08] for a description of Octave.

1.1 The MATLAB and Octave environments

MATLAB and Octave are integrated environments for Scientific Com- puting and visualization. They are written mostly in C and C++ lan- guages. MATLAB is distributed by The MathWorks (see the website www. mathworks.com). The name stands for MATrix LABoratory since origi- nally it was developed for matrix computation. Octave, also known as GNU Octave (see the website www.octave. org), is a freely redistributable software. It can be redistributed and/or

A. Quarteroni et al., Scientific Computing with MATLAB and Octave, Texts in Computational Science and Engineering 2, DOI 10.1007/978-3-642-45367-0 1, © Springer-Verlag Berlin Heidelberg 2014

1