Practical Numerical Methods with C# Numerical Programming and Math Functions for Real-World .NET Applications with C#
Practical Numerical Methods with C# Numerical Programming and Math Functions for Real-World .NET Applications with C#
Jack Xu, Ph.D
UniCAD Publishing
Practical Numerical Methods with C# Copyright © 2008 by Jack Xu, Ph.D Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1UC Editor: Betty Hsu All rights reserved. No part of the contents of this book and corresponding example source code may be reproduced or transmitted in any form or by any means without the written permission of the publisher and the author. The author and publisher have made every effort in the preparation of this book to ensure the accuracy of the information, however this book is sold without warranty, either express or implied. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained in the book. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and /or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
[email protected] Visit us on the website: www.drxudotnet.com Published by UniCAD Publishing. Phoenix, USA ISBN-13: 978-0-9793725-3-7 ISBN-10: 0-9793725-3-4 Publisher’s Cataloging-in-Publication Data Xu, Jack Practical Numerical Methods with C# – Numerical Programming and Math Functions for Real-World .NET Applications with C# / Jack Xu – 1st ed. p.cm. ISBN-13: 978-0-9793725-3-7 ISBN-10: 0-9793725-3-4 1. C# Programming. 2. Numerical Method. 3.Windows Presentation Foundation . 4. .NET Framework 5. Math Functions. 6. .NET Applications. I. Title. II. Title III Title: Practical Numerical Methods with C#
For my wonderful family
Contents Introduction ........................................................................................... xix Overview .........................................................................................................xix What This Book Includes ................................................................................xx Is This Book for You? .....................................................................................xx What Do You Need to Use This Book? ........................................................ xxii How the Book Is Organized.......................................................................... xxii Using Code Examples ...................................................................................xxiv Customer Support .........................................................................................xxiv
Chapter 1 Overview of C# Programming .............................................. 1 Your First C# Program ......................................................................................1 Data Types .........................................................................................................4 Value-Type Variables ..............................................................................4 Reference-Type Variables ........................................................................5 Math Operators and Functions ...........................................................................6 Math Operators.........................................................................................6 Logical Operators .....................................................................................7 Built-In Math Functions ...........................................................................7 Math Operation Example .........................................................................8 Selection Statements ..........................................................................................9 If Statement ..............................................................................................9 Switch Statement ....................................................................................11 Loop Statements ..............................................................................................13 For Loop .................................................................................................13 Foreach Loop .........................................................................................14 While Loop .............................................................................................15 Do Loop .................................................................................................16 Methods ...........................................................................................................17 Creating a Method ..................................................................................17 Calling a Method ....................................................................................18
viii | Contents
Properties .........................................................................................................19 Field Members .......................................................................................19 Exposing a Property ...............................................................................20 Using Properties .....................................................................................20 Read-Only and Write-Only Properties ...................................................21 Auto-Implemented Properties ................................................................22
Chapter 2 Complex Numbers and Functions ...................................... 23 Complex Numbers and Operators....................................................................23 Complex Numbers in C# ........................................................................23 Common Public Properties ...........................................................24 Test Public Properties ...................................................................26 Common Public Methods .............................................................27 Equality and Hashing....................................................................29 Complex Operators ................................................................................30 Unary Operators ...........................................................................30 Addition Operator .........................................................................30 Subtraction Operator.....................................................................31 Multiplication Operator ................................................................32 Division Operator .........................................................................32 Testing Math Operators ................................................................33 Complex Functions ..........................................................................................33 Basic Math Functions .............................................................................33 Square Root Function ...................................................................33 Exponential Function ....................................................................34 Pow Function ................................................................................34 Logarithm Function ......................................................................34 Testing Math Functions ................................................................35 Trigonometric Functions ........................................................................35 Sine Function ................................................................................35 Cosine Functions ..........................................................................36 Tangent Function ..........................................................................36 Testing Trigonometric Functions .................................................36 Inverse Trigonometric Functions ...........................................................37 Inverse Sine Function ...................................................................37 Inverse Cosine Function ...............................................................37 Inverse Tangent Function .............................................................37 Testing Inverse Trigonometric Functions.....................................38 Hyperbolic Trigonometric Functions .....................................................38 Hyperbolic Sine Function .............................................................38
Contents | ix
Hyperbolic Cosine Functions .......................................................39 Hyperbolic Tangent Function .......................................................39 Testing Hyperbolic Trigonometric Functions ..............................39 Inverse Trigonometric Hyperbolic Functions ........................................40 Inverse Hyperbolic Sine Function ................................................40 Inverse Hyperbolic Cosine Functions...........................................40 Inverse Hyperbolic Tangent Function ..........................................40 Testing Inverse Hyperbolic Trigonometric Functions..................41
Chapter 3 Vectors and Matrices ........................................................... 43 Real Vector Structure.......................................................................................43 Basic Definitions ....................................................................................44 Mathematical Operators .........................................................................47 Vector Multiplications ...........................................................................50 Scalar or Dot Product ...................................................................50 Cross Product ................................................................................51 Triple Scalar Product ....................................................................51 Triple Vector Product ...................................................................52 Complex Vector Class .....................................................................................53 Implementation ......................................................................................54 Testing the VectorC Class ......................................................................62 Real Matrix Class.............................................................................................63 Basic Definitions ....................................................................................64 Matrix and Vector Manipulations ..........................................................66 Vector from Matrix .......................................................................67 Swap, Transpose, and Trace .........................................................67 Transformations ............................................................................68 Matrix Multiplication ...................................................................70 Mathematical Operators .........................................................................71 Determinant ............................................................................................74 Inverse ....................................................................................................75 Testing the Real Matrix ..........................................................................76 Complex Matrix Class .....................................................................................77 Implementation ......................................................................................79 Testing the Complex Matrix ..................................................................89
Chapter 4 Linear Algebraic Equations ................................................ 93 Gauss-Jordan Elimination ................................................................................93 Algorithms..............................................................................................94 Implementation ......................................................................................94
x | Contents
Testing Gauss-Jordan Elimination .........................................................96 LU Decomposition ...........................................................................................96 Algorithms..............................................................................................96 Implementation ......................................................................................97 Matrix Inverse ........................................................................................98 Testing LU Decomposition ....................................................................99 Iteration Methods ...........................................................................................100 Gauss-Jacobi Iteration ..........................................................................100 Gauss-Seidel Iteration ..........................................................................101 Testing Iteration Methods ....................................................................102
Chapter 5 Nonlinear Equations .......................................................... 105 Incremental Method .......................................................................................105 Implementation ....................................................................................106 Testing the Incremental Method ..........................................................107 Fixed Point Method .......................................................................................108 Implementation ....................................................................................109 Testing the Fixed Point Method ...........................................................110 Bisection Method ...........................................................................................110 Implementation ....................................................................................111 Testing the Bisection Method ..............................................................112 False Position Method ...................................................................................112 Implamentation ....................................................................................113 Testing the False Position Method .......................................................114 Newton-Raphson Method ..............................................................................114 Implementation ....................................................................................114 Testing the Newton-Raphson Method .................................................115 Secant Method ............................................................................................... 116 Implamentation ....................................................................................116 Testing the Secant Method ...................................................................117 Newton Multiroot Method .............................................................................117 Implementation ....................................................................................118 Testing the Newton Multiroot Method ................................................119 Birge-Vieta Method .......................................................................................119 Implementation ....................................................................................120 Testing the BirgeVieta Method ............................................................121 System of Equations ......................................................................................122 Algorithm .............................................................................................122 Implementation ....................................................................................123 Testing the NewtonMultiEquations Method ........................................124
Contents | xi
Chapter 6 Special Functions ............................................................... 127 Gamma and Beta Functions ...........................................................................127 Gamma Function ..................................................................................127 Implementation ...........................................................................128 Testing the Gamma Method .......................................................129 Beta Function .......................................................................................130 Implementation ...........................................................................130 Testing the Beta Method.............................................................130 Error Function ................................................................................................ 131 Implementation ....................................................................................131 Testing the Erf and Erfc Methods ........................................................133 Sine and Cosine Integral Functions ............................................................... 133 Implementation ....................................................................................134 Testing the Si and Ci Methods .............................................................135 Laguerre Polynomials ....................................................................................135 Implementation ....................................................................................136 Testing the Laguerre Method ............................................................... 136 Hermite Polynomials .....................................................................................137 Implementation ....................................................................................137 Testing the Hermite Method ................................................................ 138 Chebyshev Polynomials .................................................................................139 Implementation ....................................................................................139 Testing Chebyshev Methods ................................................................ 140 Legendre Polynomials ...................................................................................141 Implementation ....................................................................................141 Testing the Legendre Method ..............................................................142 Bessel Functions ............................................................................................142 Implementation ....................................................................................143 Testing the Bessel Method ...................................................................143
Chapter 7 Random Numbers and Distribution Functions .............. 145 Built-in Random Number Generators ............................................................145 Normal Distribution .......................................................................................146 Probability Density Function ............................................................... 146 Normal Random Number Generator ....................................................147 Testing the Normal Distribution ..........................................................148 Exponential Distribution ................................................................................151 Probability Density Function ............................................................... 151 Exponential Random Number Generator .............................................153 Testing the Exponential Distribution ...................................................154
xii | Contents
Chi and Chi-Square Distributions ..................................................................156 Probability Density Function ............................................................... 156 Chi and Chi-Square Random Number Generators ............................... 158 Testing the Chi and Chi-Square Distributions .....................................159 Cauchy Distribution .......................................................................................162 Probability Density Function ............................................................... 162 Cauchy Random Number Generator ....................................................163 Test the Cauchy Distribution ............................................................... 163 Student T Distribution ...................................................................................164 Probability Density Function ............................................................... 164 Student T Random Number Generator ................................................166 Testing the Student T Distribution .......................................................166 Gamma Distribution ......................................................................................169 Probability Density Function ............................................................... 169 Gamma Random Number Generator ...................................................169 Testing the Gamma Distribution ..........................................................170 Beta Distribution ............................................................................................171 Probability Density Function ............................................................... 171 Beta Random Number Generator .........................................................172 Testing the Beta Distribution ............................................................... 173 Poisson Distribution.......................................................................................174 Probability Density Function ............................................................... 174 Poisson Random Number Generator ....................................................175 Testing the Poisson Distribution ..........................................................176 Binomial Distribution ....................................................................................177 Probability Density Function ............................................................... 177 Binomial Random Number Generator .................................................178 Testing the Binomial Distribution ........................................................179
Chapter 8 Interpolation ....................................................................... 181 Linear Interpolation .......................................................................................181 Algorithm .............................................................................................181 Implementation ....................................................................................182 Testing the Linear Interpolation ...........................................................183 Lagrange Interpolation ...................................................................................183 Algorithm .............................................................................................183 Implementation ....................................................................................184 Testing the Lagrange Interpolation ......................................................184 Barycentric Interpolation ...............................................................................185 Algorithm .............................................................................................185
Contents | xiii
Implementation ....................................................................................186 Testing the Barycentric Interpolation ...................................................187 Newton Divided Difference Interpolation .....................................................187 Algorithm .............................................................................................187 Implementation ....................................................................................189 Testing the Newton Divided Difference Interpolation.........................190 Cubic Spline Interpolation .............................................................................191 Algorithm .............................................................................................191 Implementation ....................................................................................192 Testing the Spline Interpolation ...........................................................194 Bilinear Interpolation .....................................................................................194 Algorithm .............................................................................................195 Implementation ....................................................................................196 Testing the Bilinear Interpolation ........................................................197
Chapter 9 Curve Fitting ...................................................................... 199 Least Squares Fit ............................................................................................199 Straight Line Fit .............................................................................................200 Implementation ....................................................................................201 Testing the Straight Line Fit ................................................................ 202 Linear Regression ..........................................................................................202 Implementation ....................................................................................203 Testing the Linear Regression ..............................................................205 Polynomial Fit................................................................................................ 206 Implementation ....................................................................................207 Testing the Polynomial Fit ...................................................................208 Weighted Linear Regression ..........................................................................209 Implementation ....................................................................................210 Exponential Function Fit ......................................................................210 Simple Moving Average ................................................................................212 Implementation ....................................................................................213 Testing the Simple Moving Average ...................................................213 Weighted Moving Average ............................................................................214 Implementation ....................................................................................215 Testing the Weighted Moving Average ...............................................216 Exponential Moving Average ........................................................................216 Implementation ....................................................................................218 Testing the Exponential Moving Average ...........................................218
Chapter 10 Optimization ..................................................................... 221
xiv | Contents
Bisection Method ...........................................................................................221 Implementation ....................................................................................222 Testing the Bisection Method ..............................................................223 Golden Search Method ..................................................................................224 Implementation ....................................................................................225 Testing the Golden Search Method ......................................................226 Newton Method .............................................................................................226 Implementation ....................................................................................227 Testing the Newton Method .................................................................227 Brent Method .................................................................................................227 Implementation ....................................................................................228 Testing the Brent Method.....................................................................231 Newton Method for Multi-Variable Functions ..............................................231 Implementation ....................................................................................232 Testing the MultiNewton Method ........................................................232 Simplex Method.............................................................................................233 Implementation ....................................................................................234 Testing the Simplex Method ................................................................ 238 Simulated Annealing Method ........................................................................239 Algorithm .............................................................................................239 Implementation ....................................................................................241 Testing the Simulated Annealing Method ............................................242 Differential Evolution ....................................................................................243 Algorithm .............................................................................................244 Implementation ....................................................................................244 Testing Differential Evolution .............................................................251
Chapter 11 Numerical Differentiation ............................................... 253 Finite Difference Formulas ............................................................................253 Forward Difference Method ..........................................................................254 Implementation ....................................................................................255 Testing the Forward Difference Method ..............................................256 Backward Difference Method ........................................................................258 Implementation ....................................................................................258 Testing the Backward Difference Method ...........................................260 Central Difference Method ............................................................................261 Implementation ....................................................................................262 Testing the Central difference Method ................................................263 Extended Centeral Difference Method ..........................................................264 Implementation ....................................................................................265
Contents | xv
Testing the Extended Central difference Method ................................ 267 Richardson Extrapolation ..............................................................................268 Implementation ....................................................................................269 Testing the Richardson Extrapolation ..................................................272 Derivatives by Interpolation ..........................................................................273 Implementation ....................................................................................274 Testing Derivatives by Interpolation ....................................................275
Chapter 12 Numerical Integration ..................................................... 277 Newton-Cotes Formulas ................................................................................278 Trapezoidal Rule ..................................................................................278 Implementation ...........................................................................280 Testing the Trapezoidal Method .................................................281 Simpson’s Rule ....................................................................................282 Implementation ...........................................................................283 Testing the Simpson Method ......................................................285 Higher Order Rules ..............................................................................285 Romberg Integration ......................................................................................286 Implementation ....................................................................................288 Testing the Romberg Method............................................................... 289 Gaussian Integration ......................................................................................289 Gauss-Legendre Integration .................................................................290 Implementation ...........................................................................291 Testing Gauss-Legendre Integration ..........................................292 Gauss-Laguerre Integration ..................................................................292 Implementation ...........................................................................293 Testing the Gauss-Laguerre Integration .....................................294 Gauss-Hermite Integration ...................................................................295 Implementation ...........................................................................295 Testing Gauss-Hermite Integration ............................................297 Gauss-Chebyshev Integration ..............................................................298 Implementation ...........................................................................298 Testing Gauss-Chebyshev Integration ........................................299
Chapter 13 Ordinary Differential Equations .................................... 301 Euler Method .................................................................................................302 Implementation ....................................................................................303 Testing Euler’s Method ........................................................................304 Second-Order Runge-Kutta Method ..............................................................305 Implementation ....................................................................................306
xvi | Contents
Testing the Second-Order Runge-Kutta Method .................................306 Fourth-Order Runge-Kutta Method ............................................................... 307 Implementation ....................................................................................308 Testing the Fourth-Order Runge-Kutta Method ..................................308 Adaptive Runge-Kutta Method ......................................................................309 Implementation ....................................................................................310 Testing the Adaptive Runge-Kutta Method .........................................311 Runge-Kutta Method for Systems .................................................................312 Implementation ....................................................................................313 Testing the MultiRungeKutta4 Method ...............................................314 Adaptive Runge-Kutta Method for Systems ..................................................316 Implementation ....................................................................................317 Testing the MultiRungeKuttaFehlberg Method ...................................318
Chapter 14 Boundary Value Problems .............................................. 321 Shooting Method............................................................................................321 Implementation ....................................................................................322 Testing the Shooting2 Method .............................................................324 Finite Difference for Linear Equation ...........................................................325 Algorithm .............................................................................................325 Implementation ....................................................................................328 Testing FiniteDifferenceLinear2 Method ............................................330 Finite Diference for Nonlinear Equations ......................................................333 Algorithm .............................................................................................333 Implementation ....................................................................................334 Testing FiniteDifferenceNonlinear2 Method .......................................335 Finite Difference for Higher-Order Equation ................................................336 Algorithm .............................................................................................336 Implementation ....................................................................................339 Testing FiniteDifferenceLinear4 Method ............................................341
Chapter 15 Eigenvalue Problems ....................................................... 343 Jacobi Method ................................................................................................ 343 Algorithm .............................................................................................344 Implementation ....................................................................................346 Testing the Jacobi Method ...................................................................349 Power Iterration .............................................................................................350 Algorithm .............................................................................................350 Implementation ....................................................................................351 Testing Power Method .........................................................................352
Contents | xvii
Inverse Iteration .............................................................................................352 Algorithm .............................................................................................352 Implementation ....................................................................................353 Testing the Inverse Method ..................................................................354 Rayleigh Method............................................................................................354 Algorithm .............................................................................................355 Implementation ....................................................................................355 Testing the Rayleigh Method ............................................................... 356 Rayleigh-Quotient Method ............................................................................356 Algorithm .............................................................................................356 Implementation ....................................................................................357 Testing Rayleigh-Quotient Method......................................................358 Matrix Tridiagonalization ..............................................................................359 Algorithm .............................................................................................359 Implementation ....................................................................................361 Testing the Tridiagonalize Method ......................................................364 Eigenvalues of Symmetric Tridiagonal Matrices ..........................................365 LU Decomposition of Tridiagonal Matrices ........................................366 Implementation ....................................................................................367 Examples ..............................................................................................371
Index ...................................................................................................... 375
Introduction
Overview Welcome to Practical Numerical Methods with C#. This book is intended for scientists, engineers, and .NET developers who want to create scientific and engineering applications using C# and .NET Framework. For many years, FORTRAN has been the dominant language of scientific and engineering computation. As Microsoft’s C# and .NET Framework gain popularity, you may find them also suitable for technical computing. This book presents C#-based procedures that perform fundamental mathematical and numerical computations critical to scientists and engineers. The power of C# programming language, combined with the simplicity of implementing Windows Form, WPF desktop applications, and Silverlight Web applications based on the Visual Studio .NET framework, makes real-world .NET program development faster and easier than ever before. Visual C# is a versatile and flexible tool that allows users with even the most elementary programming abilities to not only perform complicated computations, but also display the calculated results in a variety of graphical representations. In this regard, C# is more powerful than FORTRAN, because it is hard to show results graphically using FORTRAN. The main advantage of using FORTRAN in scientific and engineering computing is its rich math libraries. These libraries implement a complete collection of mathematical, statistical, and numerical algorithms, which have been evolving steadily for several decades. Each subroutine and algorithm in these libraries has undergone rigorous testing and quality assurance, providing users with more time to focus on their applications. On the other hand, the C# programming language is relatively new to the scientific and engineering community. The lack of C# math libraries prevents many researchers from using the C# programming language in their applications. In this book, I will show you that it’s fairly easy to develop math libraries in C#, and that it is worth developing scientific and engineering applications using C# due to its computing power and graphical representations capability. This book is aimed to provide scientists and engineers with a comprehensive explanation of scientific computing using C#. Much of the work in this book is original, based on my own programming experience in developing commercial Computer Aided Design (CAD) packages, which involve intensive scientific computations and sophisticated graphical representations. With FORTRAN, developing advanced graphics and chart applications is a difficult and time-consuming task. To add even simple charts or graphs to your applications, you have to waste effort in creating a chart program,
xx | Introduction or money in buying commercial graphics and chart add-on packages. Visual C# and its rich graphics features make it possible to easily implement both powerful math libraries and professional graphics using entirely managed C# codes. Practical Numerical Methods with C# provides an in-depth introduction to performing complicated scientific computations using C# applications. In this book, I will begins with an overview of the C# and .NET Framework, and then present procedural descriptions of linear algebra, numerical solution of nonlinear and ordinary differential equations, optimization, parameter estimation, and special functions of mathematical physics. I will show you how to create useful C# mathematical and numerical libraries that you can use in real-world scientific and engineering problems. I will try my best to introduce the C# program to scientists and engineers in a simple way – simple enough for C# beginners to easily follow. From this book, you can learn how to perform complicated scientific computations and create your own math libraries based on C# and .NET Framework. Practical Numerical Methods with C# is not simply a book, but a powerful C# math library. You may find that you can immediately use some of the examples in this book in your real-world problems, and that you can use others to give you inspiration on adding more advanced math libraries to your applications.
What This Book Includes This book and its sample code listings, which are available for download from our website at www.drxudotnet.com, provide you with:
A complete, in-depth instruction on practical scientific computing and programming using C#. After reading this book and running the example programs within, you will be able to create various math and numerical libraries in your own C# applications.
Ready-to-run example programs that allow scientists and engineers to explore the numerical methods described in the book. You can use these examples to better understand how the mathematical model and algorithms work. You can also modify the code or add new features to them to form the basis of your own programs. Some of the example code listings provided in this book are already sophisticated math libraries; these can be used directly in your own real-world applications.
Many C# classes in the sample code listings that you will find useful in your real-world scientific and engineering problems. These classes contain linear algebra, matrix manipulation, numerical approaches, and other useful utility classes. You can extract these classes and plug them into your own applications.
Is This Book for You? You don’t have to be an experienced C# developer or expert to use this book. I designed this book to be useful to scientists and engineers with all levels of C# programming experience. In fact, I believe that if you have some experience with programming languages other than C#, you will be able to sit down in front of your computer, start up Microsoft .NET Framework SDK or Visual Studio .NET, follow the examples that are provided with this book, and quickly become familiar with C# programming in scientific computing. For those of you who are already experienced C# developers, I believe this book has plenty to offer you as well. The information in this book about creating C# math libraries is not available in any other C# tutorial and reference book. In addition, most of the example
Introduction | xxi programs provided with this book can be used directly in your real-world application development. This book will provide you with a level of detail, explanation, instruction, and sample program code that will enable you to do just about anything scientific and engineering computing related with visual C#. This book is specifically designed for scientists and engineers. In fact, my own background is in theoretical physics, a field involving extensive numerical calculations as well as graphical representations of calculated data. I have been dedicated to this field for many years. My first computer experience was also with FORTRAN. Later on, I gained programming experience in Basic, C, C++, and MATLAB. I still remember how hard it was in those early days to present computational results graphically. I often spent hours creating a publication-quality chart by hand, using a ruler, graph paper, and rub-off lettering. During that time, I started to pay attention to various development tools that could be used to create integrated applications. I tried to find an ideal development tool that would allow me not only to easily generate data (computation capability) but also to easily represent data graphically (graphics and chart power). The C# and Microsoft Visual Studio .NET development environment made it possible to develop such integrated applications. Ever since Microsoft .NET 1.0 came out, I have been in love with the C# language, and have used this tool to successfully create powerful scientific and plotting applications, including commercial CAD packages. The majority of the example programs in this book can be used routinely by scientists and engineers. Throughout this book, I will emphasize the usefulness of C# programming in real-world scientific and engineering problems. If you follow this book closely, you will be able to easily develop various math and numerical libraries. At the same time, I will not spend too much time discussing program style, execution speed, and code optimization, because there is a plethora of books out there that already deal with those topics. Most of the example programs in this book omit error handlings. This makes the code easier to understand by focusing on the key concepts. Note that this book focuses on numerical computing methods and math library development using C#. It will not address the graphical representations of your calculation results. In fact, the real power of the .NET Framework is its ability to create graphics and user interfaces. If you are interested in graphics and user interface programming in C#, you can read my other books: Practical C# Charts and Graphics – This book is a perfect guide to learning all the basics for creating advanced chart and graphics applications in C#, GDI+, and Windows Form. It clearly explains practical chart and graphics methods and their underlying algorithms. The 2D and 3D chart packages contained in the book can be directly used in your C# applications. Practical WPF Programming – This book provides all the information you need to add advanced graphics to your .NET applications using C# and Windows Presentation Foundation (WPF), which comes with the new version (3.0 or later) of .NET framework. From 2D shapes and charts to complex interactive 3D models, this book uses code examples to explain every step it takes to build a variety of WPF graphics applications. Practical Silverlight Programming – This book shows you how to develop rich interactive applications (RIAs) for Web using C# and Silverlight. Silverlight is a subset of WPF and enables you to create advanced graphics and user interfaces for Web applications. You will learn from this book how to display your computation results graphically and interactively over the internet.
xxii | Introduction
What Do You Need to Use This Book? You’ll need no special equipment to make the best use of this book and understand the algorithms. To run and modify the sample programs, you need a computer capable of running either the Windows Vista or XP operating systems. The software installed on your computer should include .NET Framwork SDK 2.0 or later, which is available at Microsoft Website for free download. It will be better if you have Visual Studio 2005 or 2008 standard edition or higher. Please note that all of the example programs and math libraries were created and tested on Visual Studio 2008. However, the example code should be independent of which platform you use.
How the Book Is Organized This book is organized into fifteen chapters, each of which covers a different topic of numerical computating. The following summaries of each chapter should give you an overview of the book’s contents: Chapter 1, Overview of C# Programming This chapter introduces the basics of C# programming, including the basic types, properties, methods, mathematical operations, and how to create branches and loops. Chapter 2, Complex Numbers and Functions This chapter demonstrates how to implement a complex structure, which contains the definition of complex numbers, complex operators, and commonly used complex functions. This structure allows you to perform various computations using complex numbers and functions. Chapter 3, Vectors and Matrices This chapter introduces a more general n-dimensional vector class and a general matrix class with n m dimension, which can be used in many scientific and engineering computations involving the solution of linear equations with multiple variables. Matrix analysis is a basic theory of these linear operations. Chapter 4, LinearAlgebraic Equations This chapter introduces various numerical methods for solving linear equations with an arbitrary number of unknowns. Solving linear equations is one of the most commonly used operations in numerical analysis and scientific and engineering applications. Chapter 5, Nonlinear Equations This chapter describes several numerical methods for solving nonlinear equations. These numerical methods are all iterative in nature, and may be used for equations that contain one or several variables. Chapter 6, Special Functions This chapter discusses a special function class, which contains popular special functions such as the gamma function, beta function, error function, elliptic intergral, Laguerre function, Hermit function, Chebyshev function, Legendre function, and Bessel function, etc.
Introduction | xxiii Chapter 7, Random Numbers and Distribution Functions This chapter covers a variety of random number generators and different probability distribution functions, which can be used to simulate the different chaotic circumstances that can be found in the real world. Chapter 8, Interpolation This chapter explains the implementation of several interpolation methods, which can be used to construct new data points within the range of a discrete set of known data points. The interpolation is usually called curve fitting or regression analysis, and can be regarded as a special case of curve fitting, in which the function must go exactly through the data points. Chapter 9, Curve Fitting This chapter explains a variety of curve fitting approaches that can be applied to data containing noise, usually due to measurement errors. Curve fitting tries to find the best fit to a set of given data. Thus, the curve does not necessarily pass through all of the given data points. Chapter 10, Optimization This chapter covers several popular methods for optimizing functions with multiple variable, including the golden search, Newton, simplex, simulated annealing, and differential evolution techniques. In particular, simulated annealing and differential evolution can deal with highly nonlinear models, chaotic, noisy data, and constraints. Chapter 11, Numerical Differentiation This chapter discusses several methods of numerical differentiation, such as forward and backward difference, central difference, extended central difference, Richardson extrapolation, and derivatives by interpolation. These methods provide you with different tools for estimating the derivative of a function. Chapter 12, Numerical Intergration This chapter covers a variety of methods for numerical integration, including methods based on Newton-Cotes formulas, Romberg integration, and Gaussian quadrature methods. These methods can be used to estimate the finite and infinite integrals of functions. Chapter 13, Ordinary Differential Equations This chapter focuses on solving ordinary differential equations numerically. It presents several popular methods including the Euler method, second- and fourth-order Runge-Kutta methods, Adaptive Runge-Kutta method, and the Runge-Kutta methods that can be used for solving a system of ordinary differential equations. Chapter 14, Boundary Value Problems This chapter discusses two methods for solving boundary value problems: solution by the shooting method and by finite differences. The shooting method involves guessing the missing values and the resulting solution is very unlikely to satisfy boundary conditions at the other end. The finite difference method involves approximating the differential equations by finite differences at evenly spaced mesh points.
xxiv | Introduction Chapter 15, Eigenvalue Problems This chapter presents several popular methods for solving eigenvalue problems, including the Jacobi method, power iteration, Rayleigh method, Rayleigh-quotient method, and matrix tridiagonalization method. These methods offer you nontrivial tools for calculating eigenvalues and eigenvectors of a real symmetric matrix system.
Using Code Examples You may use the code in this book in your applications and documentations. You don’t need to contact the author for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book doesn’t require permission. Selling or distributing the example code listings does require permission. Incorporating a significant amount of example code from this book into your applications and documentation also requires permission. Integrating the example code from this book into commercial products isn’t allowed except with the author’s written permission.
Customer Support I am always interested in hearing from readers, and would like to know your thoughts on this book. You can send me comments by e-mail to
[email protected]. I also provide updates, bug fixes, and ongoing support via my website: www.drxudotnet.com. You can also obtain the complete source code for all of examples in this book from the above website. Note that this website was developed using Silveright. In order to view the site, you’ll be prompted to install Silverlight runtime to your browser.