You are on page 1of 9

Numerical

Methods,

Algorithms

and Tools in

CRC Press is an imprint of the

Taylor & Francis Group, an informa business

Boca Raton London New York

© 2010 by Taylor and Francis Group, LLC

All the source codes for the material contained in this book can be downloaded directly from the
publisher’s

website: http://www.crcpress.com/product/isbn/9780849374791 followed by selecting the option


for “Downloads

& Updates.”

CRC Press

Taylor & Francis Group

6000 Broken Sound Parkway NW, Suite 300

Boca Raton, FL 33487-2742

© 2010 by Taylor and Francis Group, LLC

CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works

Printed in the United States of America on acid-free paper

10 9 8 7 6 5 4 3 2 1

International Standard Book Number: 978-0-8493-7479-1 (Hardback)

This book contains information obtained from authentic and highly regarded sources. Reasonable
efforts

have been made to publish reliable data and information, but the author and publisher cannot
assume

responsibility for the validity of all materials or the consequences of their use. The authors and
publishers
have attempted to trace the copyright holders of all material reproduced in this publication and
apologize to

copyright holders if permission to publish in this form has not been obtained. If any copyright
material has

not been acknowledged please write and let us know so we may rectify in any future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted,

or utilized in any form by any electronic, mechanical, or other means, now known or hereafter
invented,

including photocopying, microfilming, and recording, or in any information storage or retrieval


system,

without written permission from the publishers.

For permission to photocopy or use material electronically from this work, please access
www.copyright.

com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222
Rosewood

Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses
and

registration for a variety of users. For organizations that have been granted a photocopy license by
the CCC,

a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and
are used

only for identification and explanation without intent to infringe.

Library of Congress Cataloging-in-Publication Data

Dos Passos, Waldemar.

Numerical methods, algorithms, and tools in C# / Waldemar Dos Passos.

p. cm.

Includes bibliographical references and index.

ISBN 978-0-8493-7479-1 (hardcover : alk. paper)

1. Numerical analysis--Data processing. 2. Algorithms. 3. C# (Computer program


language) I. Title.

QA297.D684 2010

518.0285’5133--dc22 2009031461

Visit the Taylor & Francis Web site at

http://www.taylorandfrancis.com

and the CRC Press Web site at

http://www.crcpress.com

© 2010 by Taylor and Francis Group, LLC

Preface

Today, more than at any other time in the history of mankind, computers are increasingly

and successfully being exploited to gain a better understanding of our physical

world and as a result, also deepen our appreciation and reverence for God’s Creation.

Consequently, as computers evolve, so must the means to control them through advancements

not just in hardware but also in software.

In order to satisfy this demand for better software, Microsoft released an entirely

new programming language called C# that incorporates the best features of all the

other existing popular programming languages such as Java, C/C++, and Visual Basic.

In spite of considerable resistance by some people who persist on clinging on to

the past and continue to program computers the hard way, C# has now firmly established

itself worldwide as arguably the preferred language for software application

development. Although many excellent books on the topic of general programming

in C# have been written, there is still a considerable lack of published material on

the topic of numerical methods in C#.

Accordingly, Numerical Methods, Algorithms and Tools in C# is a book containing

a large collection of very useful ready-to-use mathematical routines, algorithms

and other computational tools aimed at programmers, mathematicians, statisticians,

scientists, engineers and anyone else interested in developing mathematically oriented


computer applications in the relatively new and easy-to-learn object-oriented

C# programming language from Microsoft. With a heavy emphasis on using well

established numerical methods, object-oriented techniques and the latest state-ofthe-

art Microsoft .NET programming environment, this book provides readers with

working C# code including practical examples that can be easily customized and implemented

to solve complex engineering and scientific problems typically found in

real-world applications.

For the benefit of those readers who are not yet familiar with C#, Chapter 1 provides

a brief outline of the .NET Framework, the C# programming language and the

basic concepts of Object Oriented Programming (OOP). Special attention is given to

topics that illustrate how to best utilize these and other tools to develop accurate and

robust numerical methods in C#.

Chapter 2 is entirely focused on the .NET Framework Math Class Library which

already comes built into Microsoft’s Visual Studio software development system.

Additional material is introduced where appropriate in order to supplement, complete

or otherwise enhance the features already available with this library.

Chapter 3 introduces data structures along with their associated functions that are

particularly useful for programming and working with vectors and matrices. These

iii

© 2010 by Taylor and Francis Group, LLC

iv Numerical Methods, Algorithms and Tools in C#

routines are often used in more advanced applications in later chapters.

Chapter 4 is entirely dedicated to the topic of complex numbers. Since timing

issues can sometimes pose a substantial problem when doing numerical calculations,

complex number functions are presented using both elegant state-of-the-art objectoriented

methods which, although slick, can at times carry some overhead and the

old fashioned but proven methods which at times have been found to actually run
faster on some computers. In addition, important overflow and underflow issues are

also discussed and alternative solutions to avoid those problems are proposed.

Chapter 5 is devoted solely to sorting and searching algorithms. Computers are

often required to perform various types of data sorting for which many different

algorithms exist. Consequently, choosing the most efficient sorting algorithm is a

very important decision that developers frequently have to make. In this chapter,

readers are provided with both a wide selection of sorting and searching algorithms

from which to choose along with a brief explanation of how each algorithm works.

Chapter 6 is centered on the topic of bit manipulation which is typically used in

a variety of programming applications ranging anywhere from computer interfacing

to image processing.

Chapter 7 is focused on interpolation methods. Equations that cannot be solved

analytically often need to be solved using some kind of interpolation scheme, and

this chapter has plenty of practical examples to illustrate how one might handle this

kind of problem.

Chapter 8 centers on the numerical manipulation of linear algebraic equations.

This is actually a huge topic by itself and quite worthy of its own book. Nevertheless,

a substantial amount of useful information can be readily obtained from just a

handful of these powerful tools.

Chapter 9 is focused on numerical methods for calculating approximate solutions

to nonlinear equations which often appear naturally in various branches of science

and engineering.

Chapter 10 is devoted exclusively to the topic of random numbers. Although C#

comes with its own internal random number generator function, it is not regarded to

be sufficiently robust for use in advanced secured applications or in computer simulations

that require thousands and sometimes even millions of random numbers in

order to produce reliable and accurate results. Alternate ways to obtain both computer
generated pseudo-random numbers and real random numbers obtained from

naturally occurring physical phenomena are also discussed. In addition, routines are

also provided for generating random numbers that follow a particular probability

distribution function.

Chapter 11 describes various methods for approximating numerical differentiation

of functions. This is a very tricky and controversial topic whose approximations can

give fairly good to atrociously bad results. Nevertheless, numerical methods do exist

for calculating these types of functions. The trick is really in learning to recognize the

difference between good and bad results and in choosing the best available method

for use in a particular situation.

Chapter 12 centers on developing numerical methods for approximating integrals

of specific functions as well as from collections of rawdata points. Other more exotic

© 2010 by Taylor and Francis Group, LLC

Preface v

ways of calculating integrals, such as by using Monte Carlo methods, are also briefly

discussed.

Chapter 13 contains a considerable number of routines for use in performing statistical

analysis of data.

Chapter 14 is devoted to developing numerical methods for approximating special

functions which are typically found in various branches of mathematics, physics and

engineering.

Chapter 15 is focused on least squares and numerical curve fitting methods that

are frequently used in analyzing experimental data. A brief discussion of the ? 2

goodness-of-fit test is also included.

Chapter 16 centers on developing routines to find numerical solutions to ordinary

differential equations. Although this is really a huge topic, there are some basic

numerical methods which can be used successfully to solve a lot of these types of
equations in many real-world applications.

Chapter 17 introduces some numerical methods for solving partial differential

equations. Although this is also a huge topic by itself and quite deserving of its

own book, there are some standard types of partial differential equations that arise

naturally in many areas of science and engineering, and whose solutions can be approximated

by well established numerical methods.

Chapter 18 focuses on optimization methods which are primarily aimed at the

minimization or maximization of functions and thus have many practical scientific

and engineering applications. Since this is still a very active area of ongoing research,

the examples presented here are more narrowly focused on just a few established

topics with the explicit purpose of illustrating how such methods may be individually

customized and then applied towards solving more advanced problems.

Lastly, I would like to point out that most of the numerical methods described in

this book have actually been around in one form or another for years, and sometimes

even for centuries, and it is only their computer implementation in C# that makes

this book uniquely different from some other book on the topic of numerical analysis.

Accordingly, I have made every effort to track down and give proper credit to

original sources whenever possible as the size of this book’s reference section can

easily attest. In addition, I have also made every effort to provide my readers with

accurate, reliable information to help them in their efforts to successfully complete

their programming projects. Unfortunately, unwanted mistakes including typographical

errors may inadvertently creep up somewhere in this book. As a result, I would

greatly appreciate if my readers would be so kind as to bring to my attention if such

errors are ever found so that I may promptly have the problem corrected for any future

editions of this book. Also, as with just about everything we do in life, there is

always room for improvement. Accordingly, I would also very much welcome any

constructive criticism that my readers may have regarding this book so that I can perhaps
make appropriate changes. Finally, there is an old saying that states, “an author

never finishes a book, but merely abandons it.” I have certainly come to appreciate

that observation after working on this project for so long and making countless revisions.

Nevertheless, this has certainly been a very enjoyable project where just about

every word was carefully chosen and every topic was meticulously researched and

© 2010 by Taylor and Francis Group, LLC

vi Numerical Methods, Algorithms and Tools in C#

documented. Therefore, if it is indeed true that I have willingly chosen to abandon

writing this book, it is only with the modest hope that it may be useful to my readers

in spite of any possible shortcomings.

Waldemar Dos Passos, Ph.D.

Concord, California

e-mail: waldemar007@hotmail.com

website: www.waldemardospassos.com

Acknowledgements

It gives me great pleasure to thank the many people who made this book possible.

First, I would very much like to thank my publisher, Nora Konopka, for not only accepting

this book for publication but also for her exceptional patience as I underwent

a series of unforeseen tumultuous events in my life during the course of writing this

book which unfortunately led to some regrettable delays in its original publication

target date. I would also like to particularly thank both my project director, Theresa

Delforn, and my editor, Amy Rodriguez, for their excellent expert guidance in various

aspects of this project. I would also like to thank Dawn Snider for her excellent

artistic skills in designing the cover for this book. Many thanks to Ashley Gasque

for guiding me through the necessary bureaucratic paperwork and to Shashi Kumar

for some expert LATEX tips he gave me. I would also like to thank all those other

wonderful people at Taylor & Francis who have worked tirelessly behind the scenes
to make this project a success but whose exact names I may likely never come to

know.

I am also very grateful for the support I received from the H.E. Martin Foundation

under grant 13011938. Without their most kind and extraordinary generous financial

assistance, the writing of this book would not have been possible.

I am especially grateful to my third grade teacher, Miss Daly, for all her help,

patience, kindness, and enthusiasm which ultimately sparked my interest in mathematics

and eventually, physics. Looking back over all these years that have elapsed

since I was a student in her class, I can now say unequivocally that Miss Daly was

by far the very best and most caring teacher, professor, or instructor I ever had.

Lastly, I would also like to express my deepest and most heartfelt thanks to my

parents, Helenice and Waldemar Dos Passos (Sr.)

© 2010 by Taylor and Francis Group, LLC

You might also like