computing for numerical methods using visual...

30
COMPUTING FOR NUMERICAL METHODS USING VISUAL C++ Shaharuddin Salleh Universiti Teknologi Malaysia Skudai, Johor, Malaysia Albert Y. Zomaya University of Sydney Sydney, New South Wales, Australia Sakhinah Abu Bakar National University of Malaysia Bangi, Selangor, Malaysia

Upload: buithien

Post on 06-Feb-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 17, 2007 21:23 Char Count= 0

    COMPUTING FORNUMERICAL METHODSUSING VISUAL C++

    Shaharuddin SallehUniversiti Teknologi MalaysiaSkudai, Johor, Malaysia

    Albert Y. ZomayaUniversity of SydneySydney, New South Wales, Australia

    Sakhinah Abu BakarNational University of MalaysiaBangi, Selangor, Malaysia

    iii

    Innodata9780470192627.jpg

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    i

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    COMPUTING FORNUMERICAL METHODSUSING VISUAL C++

    i

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    ii

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 17, 2007 21:23 Char Count= 0

    COMPUTING FORNUMERICAL METHODSUSING VISUAL C++

    Shaharuddin SallehUniversiti Teknologi MalaysiaSkudai, Johor, Malaysia

    Albert Y. ZomayaUniversity of SydneySydney, New South Wales, Australia

    Sakhinah Abu BakarNational University of MalaysiaBangi, Selangor, Malaysia

    iii

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    Copyright C 2008 by John Wiley & Sons, Inc. All rights reserved

    Published by John Wiley & Sons, Inc., Hoboken, New JerseyPublished simultaneously in Canada

    No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form orby any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except aspermitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the priorwritten permission of the Publisher, or authorization through payment of the appropriate per-copy fee tothe Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax(978) 750-4470, or on the web at www.copyright.com. Requests to the Publisher for permission shouldbe addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken,NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permission.

    Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their bestefforts in preparing this book, they make no representations or warranties with respect to the accuracyor completeness of the contents of this book and specifically disclaim any implied warranties ofmerchantability or fitness for a particular purpose. No warranty may be created or extended by salesrepresentatives or written sales materials. The advice and strategies contained herein may not be suitablefor your situation. You should consult with a professional where appropriate. Neither the publisher norauthor shall be liable for any loss of profit or any other commercial damages, including but not limited tospecial, incidental, consequential, or other damages.

    For general information on our other products and services or for technical support, please contact ourCustomer Care Department within the United States at (800) 762-2974, outside the United States at(317) 572-3993 or fax (317) 572-4002.

    Wiley also publishes its books in a variety of electronic formats. Some content that appears in print maynot be available in electronic formats. For more information about Wiley products, visit our web site atwww.wiley.com.

    Wiley Bicentennial Logo: Richard J. Pacifico

    Library of Congress Cataloging-in-Publication Data:

    Salleh Shaharuddin, 1956-Computing for numerical methods using Visual c++ / by Shaharuddin Salleh, Albert Y.Zomaya, Sakhinah Abu Bakar.

    p. cm. (Wiley series on parallel and distributed computing)Includes index.ISBN 978-0-470-12795-7 (cloth)1. Microsoft Visual C# .NET. 2. Numerical analysisData processing.

    I. Zomaya, Albert Y. II. Bakar, Sakhinah Abu, 1982 III. Title.

    QA76.73.C154S28 2007005.2768dc22

    2007023224Printed in the United States of America

    10 9 8 7 6 5 4 3 2 1

    iv

    http://www.copyright.comhttp://www.wiley.com/go/permissionhttp://www.wiley.com

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    To our families,for their help, courage, support, and patience.

    v

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    vi

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    TRADEMARKS

    Microsoft is the trademark of Microsoft Corporation Redmond, WA.Visual Studio and Visual Studio.Net are the trademarks of Microsoft Corporation.Visual C++ and Visual C++.Net are the trademarks of Microsoft Corporation.Microsoft Foundation Classes is the trademark of Microsoft Corporation.Matlab is the trademark of The Mathworks, Inc., Natick, MA.Maple is the trademark of Waterloo Maple, Inc., Waterloo, Ontario, Canada.Mathematica is the trademark of Wolfram Research, Inc., Champaign, IL.

    vii

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    viii

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    CONTENTS

    Preface xiii

    Codes for Download xvii

    1. Modeling and Simulation 1

    1.1 Numerical Approximation 11.2 C++ for Numerical Modeling 31.3 Mathematical Modeling 41.4 Simulation and Its Visualization 61.5 Numerical Methods 71.6 Numerical Applications 7

    2. Fundamental Tools for Mathematical Computing 13

    2.1 C++ for High-Performance Computing 132.2 Dynamic Memory Allocation 142.3 Matrix Reduction Problems 222.4 Matrix Algebra 352.5 Algebra of Complex Numbers 432.6 Number Sorting 512.7 Summary 54

    Programming Challenges 55

    3. Numerical Interface Designs 56

    3.1 Microsoft Foundation Classes 563.2 Graphics Device Interface 573.3 Writing a Basic Windows Program 603.4 Displaying Text and Graphics 683.5 Events and Methods 693.6 Standard Control Resources 713.7 Menu and File I/O 783.8 Keyboard Control 873.9 MFC Compatibility with .Net 923.10 Summary 95

    ix

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    x CONTENTS

    4. Curve Visualization 96

    4.1 Tools for Visualization 964.2 MyParser 964.3 Drawing Curves 1064.4 Generating Curves Using MyParser 1154.5 Summary 126

    Programming Challenges 126

    5. Systems of Linear Equations 127

    5.1 Introduction 1275.2 Existence of Solutions 1285.3 Gaussian Elimination Techniques 1315.4 LU Factorization Methods 1425.5 Iterative Techniques 1615.6 Visualizing the Solution: Code5 1725.7 Summary 189

    Numerical Exercises 190Programming Challenges 192

    6. Nonlinear Equations 193

    6.1 Introduction 1936.2 Existence of Solutions 1946.3 Bisection Method 1956.4 False Position Method 1986.5 NewtonRaphson Method 2016.6 Secant Method 2036.7 Fixed-Point Iteration Method 2066.8 Visual Solution: Code6 2086.9 Summary 225

    Numerical Exercises 225Programming Challenges 226

    7. Interpolation and Approximation 227

    7.1 Curve Fitting 2277.2 Lagrange Interpolation 2287.3 Newton Interpolations 2317.4 Cubic Spline 2397.5 Least-Squares Approximation 2447.6 Visual Solution: Code7 2497.7 Summary 264

    Numerical Exercises 265Programming Challenges 265

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    CONTENTS xi

    8. Differentiation and Integration 267

    8.1 Introduction 2678.2 Numerical Differentiation 2688.3 Numerical Integration 2718.4 Visual Solution: Code8 2798.5 Summary 286

    Numerical Exercises 286Programming Challenges 287

    9. Eigenvalues and Eigenvectors 288

    9.1 Eigenvalues and Their Significance 2889.2 Exact Solution and Its Existence 2899.3 Power Method 2919.4 Shifted Power Method 2929.5 QR Method 2949.6 Visual Solution: Code9 3029.7 Summary 322

    Numerical Exercises 322Programming Challenges 323

    10. Ordinary Differential Equations 324

    10.1 Introduction 32410.2 Initial-Value Problem for First-Order ODE 32510.3 Taylor Series Method 32710.4 RungeKutta of Order 2 Method 33010.5 RungeKutta of Order 4 Method 33310.6 Predictor-Corrector Multistep Method 33510.7 System of First-Order ODEs 33810.8 Second-Order ODE 34110.9 Initial-Value Problem for Second-Order ODE 34210.10 Finite-Difference Method for Second-Order ODE 34510.11 Differentiated Boundary Conditions 35110.12 Visual Solution: Code10 35810.13 Summary 378

    Numerical Exercises 378Programming Challenges 380

    11. Partial Differential Equations 381

    11.1 Introduction 38111.2 Poisson Equation 38511.3 Laplace Equation 39411.4 Heat Equation 397

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    xii CONTENTS

    11.5 Wave Equation 40611.6 Visual Solution: Code11 41111.7 Summary 437

    Numerical Exercises 437Programming Exercises 438

    Index 441

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    PREFACE

    Computing for Numerical Methods Using Visual C++ has been written to promotethe use of Visual C++ in scientific computing. C++ is a beautiful language thathas contributed to shaping the modern world today. The language has contributedto many device drivers in electronic equipment, as a tool in the development ofmany computer software programs, and as a tool for both research and teaching.Therefore, its involvement in providing the solution for numerical methods is verymuch expected.

    Today, research has no boundary. A problem for study in a topic in research mayinvolve people from several disciplines. A typical problem in engineering for study-ing the effect of chemical spills in a lake may involve engineers, chemists, biologists,medical doctors, mathematicians, economists, urban planners, and politicians. A com-prehensive solution that satisfies all parties can only be produced if people from thesedisciplines cooperate, rather than having them acting as rivals.

    Numerical computing is an important area of research in science and engineering.The topic is widely implemented in the modeling of a problem and its simulation.Additional work involves visualization, which makes the problem and its solutionacceptable to the general audience. In the early days of computing in the 1960sand 1970s, the solutions to problems were mostly presented as text and numbers.A programming language like FORTRAN was the dominant tool, and there wereno friendly interfaces to present the solutions. Things have improved much sincethen, as new advancements in hardware and software produce friendly tools basedon Microsoft Windows. Numerical computing benefits much from Windows as theresults from computation can now be visualized as graphs, numbers, moving images,as well as text.

    We select numerical methods as the main title in the book as the concepts inthis topic serve as the fundamentals in science and engineering. The importance ofnumerical methods has been proven as nearly all problems involving mathematicalmodeling and simulations in science and engineering have their roots in numericalmethods. All numeric-intensive applications involving arrays and vectors have theirconcepts defined in numerical methods. Numerical methods discusses vital and crit-ical techniques in implementing algorithms for providing fast, reliable, and stablesolutions to these problems.

    Numerical analysis is a branch of mathematics that studies the numerical solutionsto problems involving nonlinear equations, systems of linear equations, interpolationand approximation for curve fittings, differentiation, integrals, ordinary differentialequations, and partial differential equations. Numerical solutions are needed for these

    xiii

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    xiv PREFACE

    problems as an alternative to the exact solutions, which may be difficult to obtain.The exact solutions for these problems are studied using analytical methods basedon mathematical techniques and theorems. The numerical solutions produced maynot be exact, but they are good enough for acceptance as they only differ by a smallmargin.

    The scope for numerical analysis is very broad. The study involves the analyticalderivation of the methods or techniques using mathematical principles and rules.The study also involves a detailed analysis of the errors between the approximatedsolutions and the exact solutions, so as to provide faster convergence as well as moreaccurate solutions.

    Numerical methods is different from numerical analysis. Numerical methods isa branch of numerical analysis that specially deals with the implementation of themethods for solving the problems. The details about the derivation of algorithmsand techniques for solving the problems and the analysis of errors are not in themain agenda of numerical methods. The main objective in numerical methods isapplying the given methods for solving the problems. It is the implementation ofthe numerical methods that attracts interest from the practitioners who comprise thebiggest consumer market. Engineers, scientists, and technologists belong to this groupof people who view numerical methods as an important tool for solving their problems.On the other hand, numerical analysis is mostly confined to die-hard mathematicianswho love further challenges in developing new numerical techniques for solving theproblems.

    There are several objectives in developing Computing for Numerical MethodsUsing Visual C++. First, no books on the market today discuss the visual solutions toproblems in numerical methods using C++. There are similar books using softwarepackages such as Matlab, Maple, and Mathematica. These software packages arenot really primitive programming languages. They have been developed to hide theprogramming details and to implement the solution as a black box. In other words,software packages do not really teach the mathematical concepts and principles insolving a problem. For example, the inverse of a matrix can be computed using asingle line of command in these packages. The user only needs to know the formatand syntax of the command in order to produce the desired solution. It is not importantfor the user to know the underlying concepts in solving the problem. Computing forNumerical Methods Using Visual C++ is one effort to integrate C++ with the visualsolution to problems using numerical methods.

    A student cannot be too reliant on software packages. There are cases wheresoftware packages fail to provide a solution because of the lack of special routines.For example, a software package may only support a maximum of five levels of therectangular grids in a boundary-value problem involving partial differential equations.To produce 10 levels, the user will have to use C++ as the language because it is moreflexible. Flexibility and versatility are two features in C++ that cannot be matched byany software package.

    Our second objective is to promote C++ as a language for numerical computing.C++ has all the necessary ingredients for numerical computing because of its flexiblelanguage format, its object-oriented methodology, and its support for high numerical

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    PREFACE xv

    precisions. However, in the past, the popularity of C++ has suffered from the emer-gence of several new languages. Among them are Java, Python, and C#. These newlanguages have been developed with the main objective to handle Web and networkprogramming requirements. Other than that, C++ is still dominant and practical for im-plementation. Because of this reason, C++ is still popular in schools and universities,mostly for teaching and research purposes. C++ is also used widely in the manufac-turing sectors such as in the design of device drivers for electronic components.

    Our third objective is to make numerical problems friendly and approachable.This goal is important as the general public perception about mathematics is that itis tough, unfriendly, boring, and not applicable in daily life. A mathematician shouldnot be placed in the basement floor of a building under the feeling that he is notimportant for people to meet. A mathematician can become a role model if he canexert his usefulness in a friendly and acceptable way, which can be done by makingmathematics interesting and approachable through a series of friendly interfaces. Aweak or average student can become motivated with mathematics if the right toolsfor understanding mathematics are provided.

    A visual approach based on Windows in Computing for Numerical Methods UsingVisual C++ is our step in achieving this objective. The book teaches the reader onthe friendly interfaces in tackling problems in numerical methods. The interfacesinclude buttons, dialog boxes, menus, and mouse clicks. The book also provides avery useful tool called MyParser, which can be used to develop various friendlynumerical applications on Windows. MyParser is an equation parser that reads anequation input by the user in the form of a string, processes the string, and producesits solution. In promoting its use, we hide all technical details in the development ofthe parser so that the reader can concentrate on producing the solution to the problem.But the best part is MyParser is free for distribution for those who are interested.

    Our last objective is to maintain links with the Microsoft family of products throughthe .Net platform. Microsoft is unarguably the driver in providing visual solutionsbased on Windows, and the .Net platform provides a common multilanguage programdevelopment for applications on Windows. As Visual C++ is one product supportedin the .Net platform, there is a guarantee of continued support from Microsoft for itsusers. A Visual C++ follower can also enjoy the benefit of integrating her product withother products within the .Net platform with very minimum effort. This flexibilityis important as migrating from one system to a different system by bringing alongdata and programs can be a very expensive, time-consuming, and resource-dependentaffair.

    In providing the solutions, this book does not provide detailed coverage of eachtopic in numerical methods. There are already many books on the market that docover these topics, and we do not wish to compete against them. Instead, we focuson the development stages of each topic from the practical point of view, usingVisual C++ as the tool. Knowing how to write the visual interfaces for the numericalproblems will definitely contribute to guiding the reader toward the more ambitiousnumerical modeling and simulation projects. This objective is the main benefit thatcan be expected from the book. The reader can take advantage of the supplied codesto create several new projects for high-performance computing.

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 8:36 Char Count= 0

    xvi PREFACE

    The book is accompanied by source codes that can be downloaded from the givenWiley website. This website will be maintained and updated by the authors from timeto time. The codes have been designed to be as compact as possible to make themeasy to understand. They are based on the Microsoft Foundation Classes library forproviding the required user-friendliness tools. In designing the codes, we opted for theunguided (or non-wizard) approach in order to show the detailed steps for producingthe output. This is necessary as the guided (or wizard) approach does not teach somekey steps, and it causes the program to be extremely long in size. However, the readershould be able to convert each unguided program code to the guided code if the needarises.

    In preparing the manuscript for Computing for Numerical Methods Using VisualC++, the authors would like to thank several people who have been directly orindirectly involved. The authors would like to thank Tan Sri Prof. Dr. Ir. Mohd. ZulkifliTan Sri Mohd. Ghazali, Vice Chancellor of Universiti Teknologi Malaysia, for hisforward vision in leading the university towards becoming a world-class university by2010. Special thanks also to Professor Dr. Alias Mohd. Yusof and Professor Dr. Md.Nor Musa from Universiti Teknologi Malaysia, and Professor Stephan Olariu fromOld Dominion University, for their support and encouragement.

    Shaharuddin SallehAlbert Y. ZomayaSakhinah Abu BakarApril 2007

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 10:53 Char Count= 0

    CODES FOR DOWNLOAD

    All codes discussed in this book can be downloaded from the following URL:

    ftp://ftp.wiley.com/public/sci tech med/computing numerical

    The files in the URL are organized into a directory called SALLCode. The codesfor the program are located in the folders bearing the chapter numbers; for example,Code4 is the project for Chapter 4. The files included are the executable (.exe), header(.h), C++ (.cpp) and the parser object file, MyParser.obj.

    The ftp site will constantly be maintained and updated. Any questions, comments,and suggestions should be addressed to the first author at [email protected].

    The system requirements for the codes are as follows:

    Intel Pentium-based Personal computer with 256 MB RM and above.

    Microsoft Windows 1998 and above.

    Microsoft Visual C++ version 6, and above.

    All code files have been tested using Microsoft Visual C++.Net version 2003. Thesame files should be compatible with Visual C++ version 6 and below and withMicrosoft Visual C++.Net version 2005 and above.

    xvii

  • KDO/KEH

    JWDD068-Salleh-FM JWDD068-Salleh October 11, 2007 10:53 Char Count= 0

    xviii

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    CHAPTER 1

    Modeling and Simulation

    1.1 Numerical Approximations1.2 C++ for Numerical Modeling1.3 Mathematical Modeling1.4 Simulation and Its Visualization1.5 Numerical Methods1.6 Numerical Applications

    References

    1.1 NUMERICAL APPROXIMATIONS

    Numerical methods is an area of study in mathematics that discusses the solutionsto various mathematical problems involving differential equations, curve fittings,integrals, eigenvalues, and root findings through approximations rather than exactsolutions. This discussion is necessary because the exact solutions to these problemsare difficult to obtain through the analytical approach. For example, it may be wiseto evaluate

    51

    x sin x dx,

    as the exact solution can be obtained through a well-known technique in calculuscalled integration by parts. However, it is not possible to apply the same method orany other analytical method to solve

    20

    ex

    3 sin x + x2 dx .

    The given equation in the above integral is difficult to solve as it is not subjectto the exact methods discussed in ordinary calculus. Therefore, a numerical methodis needed to produce a reasonably good approximated solution. A good approxi-mated solution, whose value may differ from the exact solution by some fractions, isdefinitely better than nothing.

    1

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    2 MODELING AND SIMULATION

    Numerical methods are also needed in cases where the mathematical function fora given problem is not given. In many practical situations, the governing equationsfor a given problem cannot be determined. Instead, an engineer may have a set of ndata (xi , yi ) collected from the site to analyze in order to produce a working model.In this case, a numerical method is applied to fit a curve that corresponds to this setof data for modeling the scenario.

    Solutions to numerical problems can be obtained on the computer in two ways:using a ready-made software program or programming using a primitive language.A ready-made software program is a commercial package that has been designedto solve specific problems without the hassle of going through programming. Thesoftware provides quick solutions to the problems with just a few commands. Thesolution to a problem, such as finding the inverse of a matrix, is obtained by typingjust one or two lines of command. Matlab, Maple, and Mathematica are some of themost common examples of ready-made software programs that are tailored to solvenumerical problems.

    The easy approach using ready-made software programs has its drawback. Thesoftware behaves like a black box where the user does not need to know the details ofthe method for solving the problem. The underlying concepts in solving the problemare hidden in the software, and this approach does not really test the mathematicalskill of the user. Very often, the user does not understand how the method works asall he or she gets is the generated solution. In addition, the user may face difficultyin trying to figure out why a particular solution fails because of problems such assingularity in the domain.

    A ready-made software program also does not provide the flexibility of customizingthe solution according to the users requirement. The user may need special featuresto visualize the solution, but these features may not be supported in the software. Inaddition, a ready-made software program generates files that are relatively large insize. The large size is the result of the large number of program modules from itslibrary that are stored in order to run the program.

    The real challenge in solving a numerical problem is through the native languageprogramming. It is through programming that a person will understand the wholemethod comprehensively. The developer will need to start from scratch and willneed to understand all the fundamental concepts for solving a given problem beforethe solution to the problem can be developed. The whole process in providing thesolution may take a long time, but a successful solution indicates the programmerfully understands the whole process.

    It is also important for us to accept both approaches. Ready-made software pro-grams are needed in cases where the program needs to be delivered fast. A ready-madesoftware program can produce the desired solution within a short period of time ifall required routines are available in the software, which is done by following theright commands and procedures in handling the software. In cases where some re-quired actions are not supported in the software, it may be necessary to integrate thesolution with a programming language such as C++. Conversely, if one starts from aprogramming language, it may be necessary call a ready-made software program tohandle some difficult tasks. For example, C++ may be used to draw up the numerical

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    C++ FOR NUMERICAL MODELING 3

    solution to the heat distribution problem. To see the solution in the form of surfacegraph, it may be wise to call a few routines from Matlab as the same feature in C++will require a long time to develop.

    1.2 C++ FOR NUMERICAL MODELING

    C++ is a language that has its origin from C, developed in early 1980s. C++ retainsall the procedural structure of C but adds the object-oriented features in order tomeet the new requirements in computing. Both C and C++ are heavily structuredhigh-level languages that produce small executable files. The two languages are alsosuitable for producing low-level routines that run the device drivers of many electroniccomponents.

    C++ is popular because of its general-purpose features to support a wide varietyof applications, such as data processing, numerical, scientific, and engineering. C++is available in all computing platforms, including Windows, UNIX, Macintosh, andoperating systems for mainframe and minicomputers. C++ is a revolutionary languagethat has a very strong following from students, practitioners, researchers, and softwaredevelopers all over the world. The language is taught in most universities and collegesin the world as a one- or two-semester subject to support numerical and general-purpose applications.

    C++ is a language that strongly supports object-oriented programming. Object-oriented programming is a programming approach based on objects. An object is aninstance of a class. A class is a set of entities that share the same parent. As it stood,C++ is one of the most popular object-oriented programming languages in the world.The main reason for its popularity is because it is a high-level language, but at thesame time, it runs as powerful as the assembly language. But the real strength of C++lies in its takeover from C to move to the era of object-oriented programming in thelate 1980s. This conquest provides C++ with the powerful features of the procedural Cand an added flavor for object-oriented programming.

    The original product from Microsoft consists of the C compiler that runs under theMicrosoft DOS (disk operating system), and it has been designed to compete againstTurbo C, which was produced by the Borland Corp. In 1988, C++ was added to Cand the compiler was renamed Microsoft C++. In early 1989, Microsoft launchedthe Microsoft Windows operating system, which includes the Windows API (appli-cation programming interface). This interface is based on 16 bits and it supports theprocedural mode of programming using C.

    Improvements were made over the following years that include the WindowsSoftware Development Kit (SDK). This development takes advantage of the APIfor the graphical user interface (GUI) applications with the release of the Microsoft Ccompiler. As this language is procedural, the demands in the applications requirean upgrade to the object-oriented language design approach, and this contributesto the release of the Microsoft C++ compiler. With the appearance of the 32-bitWindows API (or Win32 API) in the early 1990s, C++ was reshaped to tackle theextensive demands on Windows programming and this brings about the release ofthe Microsoft Foundation Classes (MFC) library. The library is based on C++, and it

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    4 MODELING AND SIMULATION

    has been tailored with the object-oriented methodology for supporting the applicationarchitecture and implementation.

    The main reason why Visual C++.Net is needed in numerical methods is its pow-erful simulation and visualization tools. The Net platform refers to a huge collectionof library functions and objects for creating full-featured applications on both thedesktop and the enterprise Web. The classes and objects provide support for friendlyuser interface functions like multiple windows, menus, dialog boxes, message boxes,buttons, scroll bars, and labels. Besides, the platform also includes several tedioustask-handling jobs like file management, error handling, and multiple threading. Thisplatform also supports advanced frameworks and environments such as the Passport,Windows XP, and the Tablet PC. The strength of the Net platform is obvious in pro-viding the Internet and Web enterprise solutions. Web services include informationsharing, e-commerce, HTTP, XML, and SOAP. XML, or Extensible Markup Lan-guage, is a platform-independent approach for creating markup languages needed ina Web application.

    A new approach in Visual C++.Net is the Managed Extension, which performsautomatic garbage collection for optimizing the code. Garbage collection involvesthe removal of memory and resources unused any more in the application, whichis often neglected by the programmer. The managed extension is a more structuredway in programming, and it is now the default in Visual C++.Net. Central to the .Netplatform is the Visual Studio integrated development environment (IDE). It is in thisplatform that applications are built from a choice of several powerful programminglanguages that include Visual Basic, Visual C++, Visual C#, and Visual J++.

    In addition, IDE also provides the integration of these languages in tackling a par-ticular problem under the .Net banner. Visual C++.Net is one of the high-performancecompilers that makes up the .NET platform. This highly popular language has its rootin C and was improved to include the object-oriented elements; now with the .Netextension, it is capable of creating solutions for the Web enterprise requirements. Arelatively new language called Visual C# in the .Net family was developed by takingthe best features from Visual Basic visual tools with the programming power of VisualC++.

    In addition to its single-machine prowess, Visual C++.Net presents a powerfulapproach for building applications that interact with databases through ADO.NET.This product evolves from the earlier ActiveX Data Objects (ADO) technology, andit encompasses XML and other tools for accessing and manipulating databases forseveral large-scale applications. This feature makes possible Visual C++.Net as anideal tool for several Web-based database applications.

    1.3 MATHEMATICAL MODELING

    Many problems arise in science and engineering that have their roots in mathematics.Problems of this nature are best described through mathematical models that providethe fundamental concepts needed in solving the problem. A successful mathematicalmodeling always leads to a successful implementation of the given project.

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    MATHEMATICAL MODELING 5

    A mathematical model is an abstract model that uses mathematical language todescribe the behavior of a system. It is an attempt to find the analytical solutions forenabling the prediction of the behavior of the system from a set of parameters, andtheir initial and boundary conditions in a given problem. A mathematical model iscomposed of variables and operators to represent a given problem. Variables are theabstractions of the quantities of interest in the described systems, whereas operatorsare the mechanisms that act on these variables. An operator can be expressed in theform of an algebraic operator, a function, a differential operator, and so on.

    One good example of mathematical modeling is in the heat distribution problemin a two-dimensional plane, which is modeled as a Laplace function given by

    2u = 2u

    x2+

    2u

    y2.

    In the above equation, 2 = 2x2 +

    2

    y2 is an operator that acts on the heat quan-tity u, whose independent variables are x and y. The model describes the analyt-ical solution of heat distribution in a given domain subject to certain initial andboundary conditions. We will discuss the numerical solution to this problem later inChapter 11.

    A mathematical model is often represented as variables in terms of objectivefunctions and constraints. If the objective functions and constraints in the problemare represented entirely by linear equations, then the model is regarded as a linearmodel. If one or more of the objective functions or constraints are represented with anonlinear equation, then the model is known as a nonlinear model. Most problems inscience and engineering today are modeled as nonlinear.

    A deterministic model is one where every set of variable states is uniquely deter-mined by parameters in the model and by sets of previous states of these variables.For example, the path defined by a delivery truck for distributing petrol in a city isdefined as a deterministic model in the form of a graph. In this case, the petrol stationscan be modeled as the nodes of the graph, whereas the path is defined as the edgesbetween the nodes. Conversely, in a probabilistic or stochastic model, randomnessis present, and the variable states are not described by unique values. Very often, themodel is represented in the form of probability distribution functions.

    A mathematical model can be classified as static or dynamic. A static model doesnot account for the element of time, whereas a dynamic model does. Dynamic mod-els typically are represented with difference equations or differential equations. Amodel is said to be homogeneous if it is in a consistent state throughout the entire sys-tem. If the state varies according to certain controlling mechanism, then the model isheterogeneous. If the model is homogeneous, then the parameters are lumped, or con-fined to a central depository. A heterogeneous, model has its parameters distributed.Distributed parameters are typically represented with ordinary or partial differentialequations.

    Mathematical modeling problems are also classified into black-box or white-boxmodels, according to how much a priori information is available in the system. Ablack-box model is a system of which no a priori information is available. A white-box

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    6 MODELING AND SIMULATION

    model, also called glass box or clear box, is a system where all necessary informationis available.

    1.4 SIMULATION AND ITS VISUALIZATION

    Mathematical modeling is often followed by a series of numerical simulations tosupport and verify its validity and correctness. Simulation is an imitation of some realthing, state of affairs, or process representing certain key characteristics or behaviorsof a selected physical or abstract system. It is also the objective of a simulation to op-timize the results by controlling the variables that make up the problem. By changingthe variables during simulation, predictions may be made about the behavior of thesystem. Simulation is necessary to save time, cost, human capital, and other resources.Good results from a simulation contribute in some critical decision-making process.

    Simulation can be implemented using three approaches: microscopic, macro-scopic, and mesoscopic. In the microscopic simulation, the detail physical and per-formance characteristics, such as the properties of the elements that make up theproblem, are considered. The simulation involves some tiny properties of the individ-uals or elements that make up the pieces. The results from a microscopic simulationare always reliable and accurate. However, this approach could be very costly andtime consuming as data from the individuals or elements are not easy to obtain.

    An easier approach is the macroscopic simulation that considers the deterministicfactors of the whole population, rather than all the individuals or elements. In this ap-proach, factors such as the governing mathematical equations and their macro data areconsidered. The steps in this approach may skip the detail components, and therefore,the results may not be as accurate as the one produced in the microscopic approach.However, this approach saves time and is not as costly as the microscopic approach.

    A more realistic and practical approach is the mesoscopic simulation that combinesthe good parts of the microscopic and macroscopic approaches to produce a moreversatile model. In this approach, some deterministic properties of the elements inthe system are integrated with the detail information to produce a workable model.

    A good simulation has several visualization features that accurately describe theelements in a system. Visualization is a form of graphical or textual presentationthat easily describes the solution to a particular problem. An effective visualizationincludes components such as text, graphics, diagrams, images, animation, and soundin order to describe the system.

    In most cases, numerical simulations are carried out effectively in a computer. Acomputer can accurately describe the functionality and behavior of the elements thatmake up the system. Todays computers are fast and have all the required resources toperform most college-level numerical simulations. Supercomputers also exist that aremultiprocessor systems capable of processing numeric-intensive applications witha whopping gigaflop speed. Several parallel and distributed computer systems arealso available in processing these numeric-intensive applications. Computers are alsogrouped into clusters to work cooperatively in grid computing networks that spanacross many countries in the globe.

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    NUMERICAL METHODS 7

    1.5 NUMERICAL METHODS

    Numerical methods is a branch of mathematics that consists of seven core areas, asfollows:

    Nonlinear equations System of linear equations Interpolation and approximation Differentiation and integration Eigenvalues and eigenvectors Ordinary differential equations Partial differential equations

    The basic problem in a nonlinear equation is in finding the zeros of a given function.The problem translates into finding the roots of the equation, or the points along thex-axis where the function crosses. The roots may exist as real numbers. In cases wherethe real roots do not exist, their corresponding imaginary roots may become a topicof study.

    Linear equations are often encountered in various science and engineering prob-lems. The problem arises frequently as the original problem reduces to linear equationsat some stage in the solution. For example, mesh modeling using the finite-elementmethod in a fluid dynamics results in a system of hundreds of linear equations. Asthe size of the matrix in this problem is large, the solution needs to be tackled usinga fast numerical method.

    Interpolation and approximation are curve fitting problems that contribute in thingslike designing the surface of an aircraft. The techniques are also applied in otherproblems, such as forecasting, pattern matching, and routing.

    Differentiation and integration are fundamental topics that arise in many problems.Good approximations are needed to these two topics as their exact values may not beeasy to obtain.

    Problems involving ordinary differential equations arise in modeling and simu-lation. Exact solutions are difficult to obtain as the models are subject to variationbecause of the presence of constraints and nonlinear factors. Therefore, numericalmethods are needed in their successful implementation.

    Modeling and simulation involving partial differential equations commonly usenumerical techniques as their fundamental elements. Numerical methods contributeto provide the desired solutions in most cases as the exact solutions are not practicalfor implementation on the computer.

    1.6 NUMERICAL APPLICATIONS

    Most problems in science and engineering are inherently nonlinear in nature. Thisnonlinearity is because the problems are dependent on variables and parametersthat are nonlinear and are subject to many constraints. Many problems are also

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    8 MODELING AND SIMULATION

    dynamic and nondeterministic with no a priori information. Because of their nature,the solution to these problems will not be a straightforward task.

    In most cases, the normal approach for solving nonlinear problems in science andengineering is to start from the fundamental concepts that are based on mathematics.A mathematical model that describes the system needs to be developed to representthe problem. Data from the problem are collected as an input. Numerical simulationbased on the theoretical model is then performed on the data. During the simulation,the results obtained are periodically compared and matched with some real values inorder to verify the correctness of the simulation.

    We discuss some common modeling and simulation work that makes use of nu-merical methods.

    Bacteria Population Growth

    Population growth of bacteria in a geographical region over a period of time has beensuccessfully modeled using a differential equation, given as

    dx

    dt= kx,

    where x(t) represents the size of the population at time t and k is a constant. In abroader scope, x(t) in the above equation may represent the number of bacteria in asample container.

    The numerical solution to this model consists of solving an initial value probleminvolving the first-order ordinary differential equation. A suitable solution is providedin the form of the RungeKutta method of order 4, as will be discussed in Chapter 10.However, the solution is not solely provided by this method alone. Constraints suchas temperature and the acidity of the fluid in the container may affect the validity ofthe mathematical model. The bacteria may grow faster when the temperature is highand when the acidity of the fluid is low. Therefore, a numerical simulation is neededto integrate the mathematical model with other variables and parameters. Severalparameters can be included in the simulation in order to produce the correct modelfor this problem.

    Computational Fluid Dynamics

    Computational fluid dynamics (CFD) is an area of research that deals with the dynamicbehavior and movement of fluid under certain physical conditions. Numerical methodsand algorithms are used extensively to solve and analyze problems involving fluid flow.For example, the interaction between particles in fluids and gases are studied throughsimulation on the computer. Millions of calculations are performed in this simulation,as the original problem reduces to numerical problems such as matrix multiplications,matrix inverse, system of linear equations, and the computation of eigenvalues.

    One area of study in computational fluid dynamics is the blood flow modeling instenosed artery of the human body. The study contributes in predicting the occurrenceof cardiovascular diseases such as heart attack and stroke. CFD simulations have also

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    NUMERICAL APPLICATIONS 9

    been carried out in the aerospace and automotive industries for evaluating the air flowaround moving aircrafts and cars.

    The fundamental tool in CFD simulation is the NavierStokes equation, whichdescribes a single-phase fluid flow. Also, a set of ordinary or partial differentialequations with their initial and boundary conditions is given. The solution to theseproblems makes use of the finite-difference method or finite-element methods, whichreduces the problem to several systems of linear equations.

    Finite-Element Modeling

    The finite-element method is a numerical technique for evaluating things like stressesand displacements in mechanical objects and systems. The finite-element methodhas been successfully applied for modeling problems involving heat transfer, fluiddynamics, electromagnetism, and solid state diffusion. At the National Aeronauticsand Space Administration (NASA), the finite-element method has been applied inmodeling the turbulence that occurs during the aircraft flight.

    The finite-element method requires the domain in the problem to be divided intoseveral elements in the form of line segments, triangles, rectangular meshes, volume,and so on. The method provides flexibility where the elements need not be of equalin terms of dimension and size. Solutions are obtained from these elements, and theyare grouped to produce the overall solution.

    The fundamentals of finite-element methods rest heavily on numerical methods.They include curve and surface fittings using interpolation or approximation tech-niques, systems of linear equation, and ordinary and partial differential equations.

    Printed-Circuit Board Design

    Massive simulations are carried out to produce optimal designs for printed-circuitboards (PCBs). A PCB forms the main circuitry of all electronic devices. A typicalPCB can accommodate thousands or millions of microelectronic components suchas pins, vias, transistors, processors, and memory chips. In addition, the PCB hasmassive wirings that connect these components.

    As the space on a PCB is limited, the designer must optimize the placement of com-ponents and its routing (wiring) so that the board is capable of accommodating as manycomponents as possible within the limited space area. We can imagine a PCB functionlike a city where the buildings and streets need to be designed properly so that the citywill not be too congested with problems such as improper housing and traffic jams.

    One technique commonly applied for routing in the PCB design is single-rowrouting. The problem is about designing non-crossing tracks between pairs of pinsthat are arranged in a single row in such a way that the tracks do not cross. Single-rowrouting has been known to be NP-complete with many interacting degrees of freedom.Figure 1.1 shows an optimal output from single-row routing involving 42 pins. Theproven methods for solving this problem involve computer simulations using graphtheory, simulated annealing, and genetic algorithm. A technique from the authorscalled ESSR in 2003 has been successful in producing optimal results for the problem.

  • KDO/SPI

    JWDD068-Salleh-CH-01 JWDD068-Salleh September 15, 2007 6:10 Char Count= 0

    10 MODELING AND SIMULATION

    1 2 3 4 5 6 78

    9 10 11 12 13 14 1516 17

    18 19 20 21

    22 23

    24 25 26 27 28 29 30

    31 32 33

    34 35 36

    37 38 39 40 41

    42

    FIGURE 1.1. Optimal single-row routing involving 42 pins.

    Wireless Sensor Networks

    A sensor network is a deployment of massive numbers of small, inexpensive, self-powered devices called motes that can sense, compute, and communicate with otherdevices for the purpose of gathering local information to make global decisions abouta physical environment. The object of the network is to detect certain items over ageographical region, such as the presence of harmful chemical, bacteria, electromag-netic field, and temperature distribution. In one scenario, thousands of tiny sensormotes are distributed from an aircraft over a region. Sensor network research origi-nates from a DARPA project called Smartdust in late 1990s [2]. The research attractsinterest from many disciplines because of several developments in the micro-electromechanical system (MEMS) technology that produces many cheap and small sensormotes.

    Locating sensor motes at their geographical location has become one major issue informing the network, because many constraints need to be considered in the problem.Each node in a sensor network has a short lifetime based on a battery. To save energy,the node sleeps most of the time and becomes awake only occasionally. Therefore,the process of training the nodes in order to locate their correct location is a highlynonlinear problem with many constraints.

    A coarse-grain model developed in Ref. 3 proposed a method called asynchronoustraining for locating the nodes in a sensor network. Figure 1.2 shows a model con-sisting of three networks for training the sensor nodes. Each network is indicated byconcentric circles that originate from a center called sink. The sink is representedby a device called the aggregating and forwarding node (AFN), which has a pow-erful transmitter and receiver for reaching all nodes in its network. In this model,AFN is responsible for training the nodes, storing and retrieving data from the sensornodes, and performing all the necessary computations on the data received to producethe desired results. The model makes use of the dynamic coordinate system based oncoronas and wedges to locate the nodes through a series of transmission from the sink.