An Introduction To Programming And Numerical Methods In .

Transcription

www.MathWorks.ir An Introduction to Programmingand Numerical Methods in MATLAB

www.MathWorks.ir S.R. Otto and J.P. DenierAn Introduction toProgramming andNumerical Methodsin MATLABWith 111 Figures

www.MathWorks.ir S.R. Otto, BSc, PhDThe R & ASt AndrewsFifeKY16 9JDScotlandJ.P. Denier, BSc (Hons), PhDSchool of Mathematical SciencesThe University of AdelaideSouth Australia 5005AustraliaBritish Library Cataloguing in Publication DataOtto, S. R. (Stephen Robert)An introduction to programming and numerical methods inMATLAB1. MATLAB (Computer file) 2. Numerical analysis — DataprocessingI. Title II. Denier, J. P.518′.02855ISBN 1852339195Library of Congress Control Number: 2005923332Apart from any fair dealing for the purposes of research or private study, or criticism or review, aspermitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing ofthe publishers, or in the case of reprographic reproduction in accordance with the terms of licencesissued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those termsshould be sent to the publishers.ISBN-10: 1-85233-919-5ISBN-13: 978-185233-919-7Springer Science Business Mediaspringeronline.com Springer-Verlag London Limited 2005The use of registered names, trademarks, etc. in this publication does not imply, even in the absenceof a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use.The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors oromissions that may be made.Typesetting: Camera-ready by authorsPrinted in the United States of America12/3830-543210 Printed on acid-free paper SPIN 11317333

www.MathWorks.ir ForJulieandJill and Megan.

www.MathWorks.ir PrefaceThis text provides an introduction to the numerical methods that are typicallyencountered (and used) in science and engineering undergraduate courses. Thematerial is developed in tandem with MATLAB which allows rapid prototypingand testing of the methods. The package MATLAB (matrix laboratory) provides an environment in which students can learn to programme and explorethe structure of the numerical methods. The methods included here are of abasic nature and only rely on material which should have been explored priorto the first year undergraduate stage.The methods presented are supplemented with a set of tasks at the endof each chapter (full solutions of these are given in Appendix C). The tasksare introduced in such a way as to allow students to explore the topics asthey evolve. Some are of a mathematical nature, but in the main they involvemanipulating codes which are given in the text of the chapter (or section).Those tasks which we regard as being harder are marked with an asterisk.Throughout the text MATLAB commands appear using this font in the text.In the main the names of MATLAB commands reflect the commands rôle. Wehave taken particular care to highlight many of the problems that occur withinterpreting the syntax of MATLAB commands. In Appendix B we providea glossary of all MATLAB commands within the text and short examples ofhow these commands can be used. Reference is made to the comprehensivehelp facility within MATLAB: however examples are given which are similarto those given in other places within the text.Throughout the text we derive the numerical techniques we use, but alsoemphasise that MATLAB’s rich vocabulary provides commands for performing most of the fundamental tasks encountered in numerical methods. Thisapproach serves to introduce students to the methods and also provides an

www.MathWorks.ir viiiPrefaceunderstanding of their inner workings.Why Do We Need Numerical Methods?Mathematics is an elegant and precise subject: however when numerical answers are required one sometimes needs to rely on approximate methods toobtain useable answers. There are many problems which simply do not haveanalytical solutions, or those whose exact solution is beyond our current stateof knowledge. There are also many problems which are too long (or tedious)to solve by hand. When such problems arise we can exploit numerical analysisto reduce the problem to one involving a finite number of unknowns and use acomputer to solve the resulting equations.The text starts with a description of how we could perform some verybasic calculations (that is, simply using the computer as a calculator). It thenmoves on to solving problems which cannot, in practice, be solved by hand.Sometimes the solution of these problems can become as intricate and involvedas the original problems and requires almost as much finesse and care to obtaina solution. There are several options available to us, both in terms of languageand also overall approach. In this book we elect to express our ideas in termsof the syntax of the computer package MATLAB. Once you have mastered thesyntax of MATLAB it will be easier for you to learn other languages, if youshould decide you need to.The Structure of This TextThis text is designed to be used as a source of reference for MATLAB commands(mainly through the glossary in Appendix B) and the examples given in thechapters. This is an approach we have found works well with our students. Thetext gives an introduction to numerical methods and the manipulation of thequantities used therein (for instance matrices). At each stage, short codes aregiven to allow the reader to try their own examples.Examples of code which can be typed at the MATLAB prompt will appearwithin the general text in this font (this font is also used for the names ofMATLAB codes), while longer examples will be written commands to be enteredResults

www.MathWorks.ir PrefaceixThose codes which are designed to be saved to a file will appear in boxes a 1; (sometimes wider boxes will be used for codes with longer lines).At the end of each section, or chapter, there are a variety of tasks which aredesigned to help the reader understand the topics they have just read. Solutionsof these are given in Appendix C.Chapter 2 concentrates on some aspects of programming. We also introduceanother of MATLAB’s powerful tools, namely its ability to produce high qualityplots of data. Students are shown how to create codes and functions, whichserve to augment MATLAB’s built-in vocabulary. This chapter concludes witha discussion of the important topic of errors, both from the perspective ofclassical numerical analysis and also from human interaction which arise inthe action of coding. We have found that students benefit enormously fromdebugging programmes and it is hoped that this will help them to hone theseskills, which we consider to be essential.The third chapter contains a discussion of looping and logical structureswithin MATLAB. Again the mathematics is developed in tandem. Finally concluding the first part of the book we give some examples of how MATLAB canbe used to solve problems (using just algebra and exploiting MATLAB as anadvanced calculator).In Chapters 4 and 5 we meet some classical numerical methods, in the formof root finding and interpolation (and extrapolation). Although MATLAB hasintrinsic functions which will perform most of these operations (fzero andpolyfit) we have included a detailed description of both topics. This is aimedat helping the students to understand how these methods work, and wherethey can potentially fail. In Chapter 5 we discuss the general form of Newtonforward differences, which are used in this chapter and subsequent ones (forinstance to derive the formula for integration). In the next two chapters weexplore the numerical analysis associated with integration and differentiation.In Chapter 6 we return to the topic of matrices. We start by discussingthe mathematical concepts of rank and linear independence. We also discusseigenvalues and eigenvectors (and their interpretation for 2-by-2 matrices). Thetopic of numerical integration is taken up in Chapter 7 and in Chapter 8 methods for the numerical solution of ordinary differential equations are explored.Finally in Chapter 9 we use MATLAB to calculate some basic statistical quantities and also to explore some maps, some of which may be exhibit chaoticbehaviour.The text finishes with appendices containing an introduction to the basics

www.MathWorks.ir xPrefaceof matrix algebra, a glossary of useful terms and solutions to all the taskscontained within the text. These appendices have deliberately been made quitewordy, since we believe that the material they contain is one of the importantaspects of the book.At the outset we expect readers to be mathematically literate to the level ofbeing able to: and solve algebraic equations (for instance determine the rootsof a quadratic and solve simultaneous equations); integrate and differentiatesimple functions; solve separable differential equations (although examples aregiven within the text). Although exposure to complex numbers and matriceswould be useful at the start of the text, it is not necessary as these conceptsare introduced in some detail in the introductory chapter.We would like to thank students of the University of Birmingham whosepatience and comments have been invaluable in the construction of this text.In particular we would like to identify Sukhjinder Chana and Rob Ackeroydfor their careful proof reading and comments.St Andrews, UKAdelaide, AustraliaS. R. OttoJ. P. Denier

www.MathWorks.ir Contents1.2.Simple Calculations with MATLAB . . . . . . . . . . . . . . . . . . . . . . . .1.1 Introduction and a Word of Warning . . . . . . . . . . . . . . . . . . . . . . . .1.2 Scalar Quantities and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.1 Rules for Naming of Variables . . . . . . . . . . . . . . . . . . . . . . . .1.2.2 Precedence: The Order in Which Calculations Are Performed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.3 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.3 Format: The Way in Which Numbers Appear . . . . . . . . . . . . . . . .1.4 Vectors in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.4.1 Initialising Vector Objects . . . . . . . . . . . . . . . . . . . . . . . . . . .1.4.2 Manipulating Vectors and Dot Arithmetic . . . . . . . . . . . . .1.5 Setting Up Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . .1.6 Some MATLAB Specific Commands . . . . . . . . . . . . . . . . . . . . . . . .1.6.1 Looking at Variables and Their Sizes . . . . . . . . . . . . . . . . . .1.7 Accessing Elements of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.8 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112458121313141720222324Writing Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.1 Creating Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.1.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.1.2 Brief Aside . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.2 Plotting Simple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.2.1 Evaluating Polynomials and Plotting Curves . . . . . . . . . . .2.2.2 More on Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.3 Functions of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.4 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272730353641444951

www.MathWorks.ir xiiContents2.4.1 Numerical Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512.4.2 User Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542.5 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573.Loops and Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . .3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2 Loops Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3 Summing Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .N 3.3.1 Sums of Series of the Formjp, p N . . . . . . . . . . . . . . .6363636873j 13.43.53.63.73.83.3.2 Summing Infinite Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.3 Summing Series Using MATLAB Specific Commands . . .3.3.4 Loops Within Loops (Nested) . . . . . . . . . . . . . . . . . . . . . . . .Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.4.1 Constructing Logical Statements . . . . . . . . . . . . . . . . . . . . .3.4.2 The MATLAB Command switch . . . . . . . . . . . . . . . . . . . .Conditional loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1 The break Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .MATLAB Specific Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Error Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76798283858890929294974.Root Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034.2 Initial Estimates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1044.3 Fixed Point Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094.4 Bisection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1134.5 Newton–Raphson and Secant Methods . . . . . . . . . . . . . . . . . . . . . . 1174.5.1 Derivation of the Newton–Raphson Method . . . . . . . . . . . . 1174.6 Repeated Roots of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234.7 Zeros of Higher-Dimensional Functions(*) . . . . . . . . . . . . . . . . . . . 1254.8 MATLAB Routines for Finding Zeros . . . . . . . . . . . . . . . . . . . . . . . 1284.8.1 Roots of a Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1284.8.2 The Command fzero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1284.9 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1305.Interpolation and Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1335.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1335.2 Saving and Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1345.3 Which Points to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1395.4 Newton Forward Differences and Lagrange Polynomials . . . . . . . 1415.4.1 Linear Interpolation/Extrapolation . . . . . . . . . . . . . . . . . . . 147

www.MathWorks.ir Contentsxiii5.55.65.75.8Calculating Interpolated and Extrapolated Values . . . . . . . . . . . . 148Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150Curves of Best Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Interpolation of Non-Smooth Data . . . . . . . . . . . . . . . . . . . . . . . . . . 1555.8.1 Insufficient Data Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1585.9 Minimisation of Functions and Parameter Retrieval . . . . . . . . . . . 1615.9.1 Parameter Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1635.9.2 Using fmins for Parameter Retrieval . . . . . . . . . . . . . . . . . . 1645.10 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1666.Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1696.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1696.1.1 Initialising Matrices Within MATLAB . . . . . . . . . . . . . . . . 1696.1.2 Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1746.1.3 Operations on Elements of Matrices . . . . . . . . . . . . . . . . . . 1806.1.4 More on Special Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1826.1.5 Matrices Containing Strings . . . . . . . . . . . . . . . . . . . . . . . . . 1856.2 Properties of Matrices and Systems of Equations . . . . . . . . . . . . . 1866.2.1 Determinants of Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1906.3 Elementary Row Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1916.3.1 Solving Many Equations at Once . . . . . . . . . . . . . . . . . . . . . 1986.4 Matrix Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1996.5 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2046.6 Specific MATLAB Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2086.7 Characteristic Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2126.8 Exponentials of Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2146.9 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2177.Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2257.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2257.2 Integration Using Straight Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2267.2.1 Errors in the Trapezium Method . . . . . . . . . . . . . . . . . . . . . 2297.3 Integration Using Quadratics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2307.4 Integration Using Cubic Polynomials . . . . . . . . . . . . . . . . . . . . . . . . 2357.5 Integrating Using MATLAB Commands . . . . . . . . . . . . . . . . . . . . . 2377.6 Specific Examples of Integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2387.6.1 Infinite Integrals and Removable Singularities . . . . . . . . . . 2387.6.2 Indefinite Integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2407.7 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

www.MathWorks.ir xivContents8.Solving Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2478.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2478.2 Euler’s Method and Crank–Nicolson . . . . . . . . . . . . . . . . . . . . . . . . 2478.2.1 Analytical Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2538.3 Banded Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2598.4 Runge–Kutta Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2638.5 Higher-Order Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2668.5.1 Second-Order Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2668.5.2 Bessel’s equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2708.5.3 Airy’s Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2738.5.4 Shooting Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2748.6 Boundary-Value Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2788.7 Population Dynamics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2828.8 Eigenvalues of Differential Systems . . . . . . . . . . . . . . . . . . . . . . . . . 2858.9 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2869.Simulations and Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 2919.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2919.2 Statistical quantities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2919.2.1 Averages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2919.2.2 Other Statistical Measures . . . . . . . . . . . . . . . . . . . . . . . . . . . 2939.3 Random Numbers and Distributions . . . . . . . . . . . . . . . . . . . . . . . . 2959.3.1 Normal Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2989.3.2 Calculating Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2999.3.3 Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3009.4 Maps and White Noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3009.4.1 Modelling Discrete Systems . . . . . . . . . . . . . . . . . . . . . . . . . . 3079.4.2 Periodicity and Chaos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3099.4.3 Random Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3169.5 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319A. A Mathematical Introduction to Matrices . . . . . . . . . . . . . . . . . . 323A.1 Special Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330A.2 Inverses of Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331B. Glossary of Useful Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335B.1 Arithmetic and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 335B.2 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343B.3 Plotting Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347B.4 General MATLAB Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

www.MathWorks.ir ContentsxvC. Solutions to Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389C.1 Solutions for Tasks from Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . 389C.2 Solutions for Tasks from Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . 394C.3 Solutions for Tasks from Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . 400C.4 Solutions for Tasks from Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . 408C.5 Solutions for Tasks from Chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . 414C.6 Solutions for Tasks from Chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . 418C.7 Solutions for Tasks from Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . 429C.8 Solutions for Tasks from Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . . 436C.9 Solutions for Tasks from Chapter 9 . . . . . . . . . . . . . . . . . . . . . . . . . 454Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459

www.MathWorks.ir 1Simple Calculations with MATLAB1.1 Introduction and a Word of WarningMATLAB is an incredibly powerful tool, but in order to use it safely youneed to be able to understand how it works and to be very precise when youenter commands. Changing the way you enter a command, even subtly cancompletely change its meaning.The main aim of this text is to teach you to converse with MATLAB and understand its responses. It is possible to interact with MATLAB using a “phrasebook” approach, which is fine if the answer is what you expect. However it is farbetter to learn the language so that you can understand the response. As wellas learning the language it is essential that you learn the grammar or syntax;this is perhaps even more important with computer languages than conventional languages! MATLAB uses an interpreter to try to understand what youtype and this can come back with suggestions as to where you might have gonewrong: sometimes what you have written makes sense to MATLAB but doesnot mean what you expect! So you need to be careful. It is crucial that youformulate ideas clearly in your head (or on paper) before trying to translatethem into MATLAB (or any other language).We begin by discussing mathematical operations performed on scalars1 . Itis crucial that the material in this chapter is understood before proceeding, asit forms the basis of all that is to follow2 .12That is numbers.MATLAB has a wealth of introductory material available to the user that can

www.MathWorks.ir 21. Simple Calculations with MATLABWe shall start by introducing MATLAB commands which can be typed atthe MATLAB prompt; these will ultimately form part of our vocabulary ofMATLAB commands. MATLAB already has an extensive vocabulary: howeverwe will learn that we can expand this set. As the name MATLAB (MATrixLABoratory) suggests, most of the commands work with matrices and thesewill be discussed in due course. We shall start with scalar operations, for whichMATLAB acts like a very powerful calculator.1.2 Scalar Quantities and VariablesWe will begin with the basic ideas of equations and variables. Try entering thecommands as they are given. Consider the following two commands: a 3a 3 b 4;3These two commands are entered on separate lines; the MATLAB prompt isdenoted by (which does not need to be typed), as distinguished from thestandard greater than sign . The command on the first line sets the variablea to be equal to three (3) and that on the second line sets the variable b tobe equal to four (4). The two commands also differ because the second oneends with a semicolon. This instructs MATLAB to execute the command butsuppress any output; whereas above we can see that the value of a has beenset to 3. These commands can be read asset a equal to 3set b equal to 4 (and suppress output)Reading the commands in this way it should be clear that it is not possibleto have a command of the form 7 x (set 7 equal to x), whereas we couldhave x 7 (set x equal to 7). These variables can now be used again, forinstance3be accessed using the commands demo or tour. There is also a good help facilitywhich, unsurprisingly, can be accessed by typing help followed by the command inquestion. There is also a facility to use a web browser (helpdesk or helpbrowser).Here, you would type a 3, and then press RETURN, and then type b 4; andpress RETURN again. The spaces are included purely for clarity.

www.MathWorks.ir 1.2 Scalar Quantities and Variables3 a 3; b a 1; x a b;The first line sets the variable a to be equal to 3, the semicolon instructingMATLAB to execute the command but to suppress the output. The secondline sets b to be equal to a plus one, namely 4: again the semicolon suppressesoutput. The third line sets x to be a b which is 7 (again output is suppressed).MATLAB can be used as a very powerful calculator and its operations fallinto two basic groups: unary and binary, the former operating on one quantity and the latter on two. We shall begin by considering simple arithmeticoperations, which are binary. For instance typing 3*4 generates 3*4ans 12Notice here that we have multiplied the two integers 3 and 4, and the answerhas been returned correctly as 12. MATLAB uses the variable ans to store theresult of our calculation, in this case the value 12, so that it can be used inthe subsequent commands. For instance the command ans*3 will generate theresult 36 (and now the variable ans will have the value 36). We could also haveused the commands a 3; b 4; x a*b which can be typed on one lineand read asset a equal to 3 (don’t output anything),set b equal to 4 (don’t output anything)and set x equal to a times bDivision works in exactly the same way as in the multiplication example above.If we try the command 3/4, MATLAB returns the value 0.75.It is a good idea to use meaningful variable names and we shall shortlydiscuss valid forms for these.Example 1.1 Try entering the following commands into MATLAB, but beforeyou do so try to work out what output you would expect. 3*5*6z1 34;z2 17;z3 -8;z1/z2

www.MathWorks.ir 41. Simple Calculations with MATLAB z1-z3 z2 z3-z1Hopefully you should get the answers, 90, 2, 42 and 25.Example 1.2 Here we give an example of the simple use of brackets: format rata 2; b 3; c 4;a*(b c)a*b ca/b ca/(b c)formatIn this example you should get the answers, 14, 10, 14/3 and 2/7. Hopefully thisgives you some idea that brack

An introduction to programming and numerical methods in MATLAB 1. MATLAB (Computer file) 2. Numerical analysis — Data processing I. Title II. Denier, J. P. 518′.02855 ISBN 1852339195 Library of Congress Control Number: 2005923332 Apart from any fair dealing for the purposes