Practical Numerical Methods with C# (Second Edition)
About this Book
This book provides an in-depth exposition of the various numerical methods used in real-world scientific and engineering computations. It emphasizes the practical aspects of C# numerical methods and mathematical functions programming, and discusses in detail various techniques that will enable you to implement these numerical methods in your own .NET applications. The second edit also incorporate the new advancements in .NET technology and open-source C# math librraies
Ideal for scientists, engineers, and students who would like to become more adept with numerical methods, Practical Numerical Methods with C# familiarizes you with:
- The basics of C# programming.
- The mathematical background and fundamentals of numerical methods.
- Math libraries for real and complex vector and matrix operations, as well as special functions.
- Plotting numerical results using the chart control and Gincker-Graphics playground.
- Numerical methods for generating random numbers and random distribution functions.
- Various numerical methods for solving linear and nonlinear equations.
- Numerical differentiation and integration.
- Interpolations and curve fitting.
- Optimization of single-variable and multi-variable functions using a variety of techniques, including advanced simulated annealing and evolutionary algorithms.
- Numerical techniques for solving ordinary differential equations.
- Numerical methods for solving boundary value problems.
- Eigenvalue problems.
- Fourier Analysis, including Fourier series, DFT and FFT.
- Math expression parser and evaluator.
In addition, this book provides code examples for every math function and numerical method to show you how to use these functions and methods in your own .NET applications.
Table of Contents
Introduction ... 1
Why Second Edition? .... 1
What’s New in Second Edition .... 1
Example Structure .... 1
Use Open Source Libraries .... 2
C# Interactive Window .... 2
Chapter Update ..... 2
Overview ... 3
What this Book Includes....... 4
Is this Book for You?... 4
What Do You Need to Use this Book? ... 5
How this Book Is Organized 5
Use Code Examples ..... 7
Customer Support ........ 8
1 Overview of C# Programming ... 9
1.1 Visual Studio ........ 9
1.2 Windows Forms . 10
1.3 Interactive Window ..... 16
1.3.1 Open C# Interactive Window .. 16
1.3.2 Execute Code in C# Interactive Window ... 18
1.3.3 Interact with Current Project .... 20
1.4 Data Types 21
1.4.1 Value-Type Variables ..... 21
1.4.2 Reference-Type Variables........ 22
1.5 Math Operations and Functions .... 22
1.5.1 Math Operators ...... 23
1.5.2 Logical Operators ... 23
1.5.3 Built-In Math Functions .. 24
1.5.4 Math Operation Example 25
1.6 Selection Statements ... 25
1.6.1 If statement .... 26
1.6.2 Switch Statement ... 29
1.7 Loop Statements . 31
1.7.1 For Loop ........ 31
1.7.2 Foreach Loop 32
1.7.3 While Loop ... 33
1.7.4 Do Loop ........ 33
1.8 Methods ..... 34
1.8.1 Create a Method ..... 34
1.8.2 Call a Method 36
1.9 Properties .. 36
1.9.1 Expose a Property .. 36
1.9.2 Use Properties ........ 37
1.9.3 Read-Only and Write-Only Properties ....... 37
1.9.4 Auto-Implemented Properties .. 38
1.10 NuGet Packages . 39
1.10.1 Install NuGet Packages ... 39
1.10.2 Use NuGet Package ........ 41
2 Vectors and Matrices ....... 43
2.1 Vectors ...... 43
2.1.1 Create Vectors ........ 43
2.1.2 Arithmetics .... 45
2.1.3 Vector Multiplication ...... 46
2.2 Complex Vectors 46
2.2.1 Create Complex Vectors . 46
2.2.2 Arithmetics .... 47
2.3 Matrices ..... 48
2.3.1 Create Matrices ...... 49
2.3.2 Arithmetics .... 51
2.3.3 Determinant and Trace .... 52
2.3.4 Transpose and Inverse ..... 52
2.3.5 Rank and Range ..... 53
2.3.6 Norms ... 54
2.3.7 Matrix Manipulations ...... 55
2.3.8 Upper and Lower Triangular Matrices....... 57
2.3.9 Element-By-Element Operations ...... 58
2.4 Complex Matrices ....... 59
3 Plotting ... 61
3.1 Chart2D User Control . 61
3.1.1 Multiple-Line Charts ....... 61
3.1.2 Sub-Charts ..... 66
3.2 WebView Control ....... 71
3.2.1 Install WebView .... 71
3.2.2 Use WebView for WinForms .. 71
3.3 Introduction to Gincker Graphics.. 72
3.3.1 Main Features 72
3.3.2 Output from Gincker Graphics 73
3.3.3 Templates for Charts and Graphics ... 73
3.3.4 Embed Gincker Graphics within WinForms ....... 75
3.3.5 Create User Account ....... 76
3.4 Get Started . 76
3.4.1 Select Template ...... 76
3.4.2 Layout Window ..... 77
3.4.3 Input Panel .... 78
3.4.4 Chart Area ..... 79
3.4.5 Customization Panel ....... 80
3.5 2D Charts .. 82
3.5.1 2D Multiple-Line Charts . 82
3.5.2 2D Parametric Charts ...... 84
3.5.3 Polar Charts ... 86
3.5.4 Contour Charts ....... 88
3.6 3D Charts .. 91
3.6.1 3D Line Charts ....... 91
3.6.2 3D Parametric Surfaces ... 92
3.6.3 3D Parametric Shapes ..... 95
3.6.4 3D Extruded Surfaces ..... 97
3.6.5 3D Shapes of Revolution 98
3.6.6 3D Tubes ..... 100
3.7 Complex-Variable Charts... 101
3.7.1 3D Complex Charts ....... 101
3.7.2 Complex Heatmap Charts ...... 104
3.7.3 Domain-Coloring Charts ........ 107
3.7.4 Iterated-Function Charts ........ 110
4 Linear Algebra Equations ...... 115
4.1 LU Decomposition .... 115
4.1.1 Algorithm .... 115
4.1.2 LU Factorization .. 116
4.1.3 Solve Linear Equations . 117
4.2 QR Decomposition .... 118
4.2.1 Algorithm .... 118
4.2.2 QR Factorization .. 118
4.2.3 Solve Linear Equations . 119
4.3 Singular Value Decomposition ... 120
4.3.1 Algorithm .... 120
4.3.2 SVD Factorization ........ 120
4.3.3 Solve Linear Equations . 121
4.4 Cholesky Decomposition ... 122
4.4.1 Algorithm .... 122
4.4.2 Cholesky Factorization . 122
4.4.3 Solve Linear Equations . 123
4.5 Bi-Conjugate Gradient Stabilized Method .... 123
4.5.1 Algorithm .... 123
4.5.2 Solve Linear Equations . 124
4.6 Multiple-Lanczos Bi-Conjugate Gradient Stabilized Method . 125
4.6.1 Algorithm .... 125
4.6.2 Solve Linear Equations . 126
4.7 Generalized Product Bi-Conjugate Gradient Method ..... 126
4.7.1 Algorithm .... 127
4.7.2 Solve Linear Equations . 128
4.8 Transpose Free Quasi-Minimal Residual Method . 129
4.8.1 Algorithm .... 129
4.8.2 Solve Linear Equations . 129
5 Nonlinear Equations ...... 131
5.1 Bisection Method ...... 131
5.1.1 Algorithm .... 132
5.1.2 Find Roots ... 133
5.2 Newton-Raphson Method .. 133
5.2.1 Algorithm .... 133
5.2.2 Find Roots ... 134
5.3 Secant Method .. 134
5.3.1 Algorithm .... 135
5.3.2 Find Roots ... 135
5.4 Brent Method ... 136
5.4.1 Algorithm .... 136
5.4.2 Find Roots ... 137
5.5 Broyden Method ....... 138
5.5.1 Algorithm .... 138
5.5.2 Find Roots ... 138
5.5.3 Solve System of Nonlinear Equations ..... 139
6 Special Functions ... 141
6.1 Gamma Functions ..... 141
6.1.1 Definitions ... 141
6.1.2 Incomplete Gamma Functions ........ 142
6.1.3 Test Gamma Functions . 143
6.2 Beta Functions .. 143
6.2.1 Properties .... 143
6.2.2 Incomplete Beta Functions .... 144
6.2.3 Test Beta Functions ....... 144
6.3 Error Functions 145
6.3.1 Properties .... 145
6.3.2 Inverse Error Functions . 146
6.3.3 Test Error Functions ..... 146
6.4 Sine and Cosine Integral Functions ..... 147
6.4.1 Implementation .... 147
6.4.2 Test Sine and Cosine Integrals ........ 148
6.5 Laguerre Polynomials ........ 149
6.5.1 Implementation .... 149
6.5.2 Test Laguerre Polynomials .... 150
6.6 Hermite Polynomials . 150
6.6.1 Implementation .... 150
6.6.2 Test Hermite Polynomials ...... 151
6.7 Chebyshev Polynomials ..... 151
6.7.1 Implementation .... 152
6.7.2 Test Chebyshev Polynomials . 153
6.8 Legendre Polynomials ........ 153
6.8.1 Implementation .... 154
6.8.2 Test Legendre Polynomials .... 154
6.9 Bessel Functions ....... 155
6.9.1 Modified Bessel Functions .... 155
6.9.2 Test Bessel Functions ... 156
6.10 Spherical Bessel Functions 157
6.11 Airy Functions .. 158
6.11.1 Definitions and Properties ...... 158
6.11.2 Test Airy Functions ....... 159
7 Random Numbers and Distribution Functions ........ 161
7.1 Random Number Generators....... 161
7.1.1 Built-In Random Number Generator ....... 161
7.1.2 Random Number Generators in Math.NET ...... 162
7.2 Normal Distribution .. 165
7.2.1 Probability Density Function . 165
7.2.2 Test Normal Distribution ....... 165
7.2.3 Plot Normal Distribution ........ 166
7.3 Exponential Distribution .... 172
7.3.1 Probability Density Function . 173
7.3.2 Test Exponential Distribution 173
7.3.3 Plot Exponential Distribution 174
7.4 Chi Distribution 176
7.4.1 Probability Density Function . 177
7.4.2 Test Chi Distribution ..... 177
7.4.3 Plot Chi Distribution ..... 178
7.5 Chi-Square Distribution ..... 180
7.5.1 Probability Density Function . 181
7.5.2 Test Chi-Square Distribution . 181
7.5.3 Plot Chi-Square Distribution .. 182
7.6 Cauchy Distribution .. 184
7.6.1 Probability Density Function . 184
7.6.2 Test Cauchy Distribution ....... 185
7.6.3 Plot Cauchy Distribution ........ 186
7.7 Student-T Distribution ....... 189
7.7.1 Probability Density Function . 189
7.7.2 Test Student-T Distribution ... 189
7.7.3 Plot Student-T Distribution .... 190
7.8 Gamma Distribution .. 192
7.8.1 Probability Density Function . 193
7.8.2 Test Gamma Distribution ....... 193
7.8.3 Plot Gamma Distribution ....... 194
7.9 Beta Distribution ....... 197
7.9.1 Probability Density Function . 197
7.9.2 Test Beta Distribution ... 197
7.9.3 Plot Beta Distribution ... 198
7.10 Poisson Distribution .. 201
7.10.1 Probability Density Function . 201
7.10.2 Test Poisson Distribution ....... 201
7.10.3 Plot Poisson Distribution ....... 202
7.11 Binomial Distribution ........ 206
7.11.1 Probability Density Function . 206
7.11.2 Test Binomial Distribution .... 206
7.11.3 Plot Binomial Distribution ..... 207
8 Interpolation . 211
8.1 Linear Interpolation ... 211
8.1.1 Algorithm .... 211
8.1.2 Test Linear Interpolation ........ 212
8.1.3 Plot Linear Interpolation ........ 213
8.2 Lagrange Interpolation ....... 216
8.2.1 Neville’s Algorithm ...... 216
8.2.2 Test Lagrange Interpolation ... 217
8.2.3 Plot Lagrange Interpolation ... 218
8.3 Barycentric Interpolation ... 220
8.3.1 Algorithm .... 220
8.3.2 Test Barycentric Interpolation ........ 221
8.3.3 Plot Barycentric Interpolation 222
8.4 Newton divided Difference Interpolation ..... 224
8.4.1 Algorithm .... 225
8.4.2 Implementation .... 226
8.4.3 Test Newton divided difference Interpolation .. 227
8.4.4 Plot Newton Divided Difference Interpolation . 227
8.5 Cubic Spline Interpolation . 230
8.5.1 Algorithm .... 230
8.5.2 Test Cubic Spline Interpolation ...... 231
8.5.3 Plot Cubic Spline Interpolation ....... 232
8.6 Bilinear Interpolation 234
8.6.1 Algorithm .... 235
8.6.2 Implementation .... 236
8.6.3 Test Bilinear Interpolation ..... 237
9 Curve Fitting . 239
9.1 Least Squares Fit ....... 239
9.2 Straight-Line Fit ........ 240
9.2.1 Algorithm .... 240
9.2.2 Test Straight-Line Fit .... 241
9.2.3 Plot Straight-Line Fit .... 242
9.3 Linear Model .... 244
9.3.1 Algorithm .... 245
9.3.2 Test Linear Model 245
9.3.3 Plot Line Model ... 247
9.4 Polynomial Fit .. 249
9.4.1 Algorithm .... 250
9.4.2 Test Polynomial Fit ....... 250
9.5 Exponential Function Fit .... 251
9.6 Linearization of Nonlinear Data.. 253
9.6.1 Exponential Function .... 253
9.6.2 Power Function .... 254
9.6.3 Saturation function ........ 254
9.6.4 Linearization Example .. 254
10 Optimization .... 259
10.1 Bisection Method ...... 259
10.1.1 Algorithm .... 259
10.1.2 Implementation .... 260
10.1.3 Test Bisection Method .. 261
10.2 Golden Section Method...... 262
10.2.1 Algorithm .... 262
10.2.2 Test Golden Section Method . 263
10.3 Brent Method ... 264
10.3.1 Algorithm .... 264
10.3.2 Implementation .... 265
10.3.3 Test Brent Method ........ 268
10.4 Newton Method 268
10.4.1 Algorithm .... 268
10.4.2 Test Functions ...... 269
10.4.3 Test Newton Method ..... 271
10.5 Simplex Method ........ 272
10.5.1 Algorithm .... 273
10.5.2 Test Simplex Method .... 273
10.6 Broyden-Fletcher-Goldfarb-Shanno Method 274
10.6.1 Algorithm .... 274
10.6.2 Test BFGS Method ....... 275
10.7 Simulated Annealing Method ..... 276
10.7.1 Algorithm .... 276
10.7.2 Implementation .... 278
10.7.3 Test Simulated Annealing Method . 279
10.8 Differential Evolution ........ 280
10.8.1 Algorithm .... 281
10.8.2 Implementation .... 281
10.8.3 Test Differential Evolution .... 288
11 Numerical Differentiation 291
11.1 Finite Difference Formulas 291
11.2 Forward Difference Method ........ 292
11.2.1 Implementation .... 293
11.2.2 Testing Forward Difference Method ....... 294
11.3 Backward Difference Method ..... 296
11.3.1 Implementation .... 296
11.3.2 Test Backward Difference Method . 298
11.4 Central Difference Method 298
11.4.1 Implementation .... 299
11.4.2 Test Central Difference Method ..... 300
11.5 Extended Central Difference Method .. 301
11.5.1 Implementation .... 302
11.5.2 Test Extended Central Difference Method ....... 303
11.6 Richardson Extrapolation ... 304
11.6.1 Implementation .... 305
11.6.2 Test Richardson Extrapolation ........ 308
11.7 Derivatives by Interpolation ........ 309
11.7.1 Implementation .... 310
11.7.2 Test Derivatives by Interpolation .... 311
11.8 Partial Differentiation 312
11.8.1 Partial Derivatives 312
11.8.2 Jacobian Matrix .... 313
11.8.3 Hessian Matrix ..... 314
12 Numerical Integration ...... 316
12.1 Newton-Cotes Formulas .... 317
12.1.1 Trapezoidal Rule .. 317
12.1.2 Test Trapezoidal Rule ... 319
12.1.3 Simpson’s Rule .... 320
12.1.4 Test Simpson’s Rule ..... 321
12.1.5 Higher Order rules ........ 322
12.2 Romberg Integration . 323
12.2.1 Implementation .... 324
12.2.2 Test Romberg Method .. 325
12.3 Gaussian Integration.. 326
12.3.1 Gauss-Legendre Integration ... 327
12.3.1.1 Implementation .... 327
12.3.1.2 Test Gauss-Legendre Integration ... 328
12.3.2 Gauss-Laguerre Integration.... 329
12.3.2.1 Implementation .... 329
12.3.2.2 Test Gauss-Laguerre Integration .... 331
12.3.3 Gauss-Hermite Integration ..... 331
12.3.3.1 Implementation .... 332
12.3.3.2 Test Gauss-Hermite Integration ..... 333
12.3.4 Gauss-Chebyshev Integration 334
12.3.4.1 Implementation .... 334
12.3.4.2 Test Gauss-Chebyshev Integration . 335
12.4 Double Integrations ... 336
12.4.1 2D Simpson’s Rule ....... 336
12.4.2 Implementation .... 337
12.4.3 Test 2D Simpson’s Rule ........ 338
12.4.4 Test 2D Gauss-Legendre Integration ....... 338
13 Ordinary Differential Equations ....... 339
13.1 Euler Method .... 340
13.1.1 Implementation .... 340
13.1.2 Test Euler Method 342
13.2 Second-Order Runge-Kutta Method .... 343
13.2.1 Implementation .... 343
13.2.2 Test Second-Order Runge-Kutta Method 344
13.3 Fourth-Order Runge-Kutta Method ..... 345
13.3.1 Implementation .... 345
13.3.2 Test Fourth-Order Runge-Kutta Method . 346
13.4 Adaptive Runge-Kutta Method ... 346
13.4.1 Implementation .... 347
13.4.2 Test Adaptive Runge-Kutta Method ........ 348
13.5 Runge-Kutta Method for Systems ........ 349
13.5.1 Implementation .... 350
13.5.2 Test MultiRungeKutta4 Method ..... 351
13.6 Adaptive Runge-Kutta Method for Systems . 354
13.6.1 Implementation:implementation ..... 354
13.6.2 Test MultiRungeKuttaFehlberg Method .. 356
14 Boundary Value Problems ........ 359
14.1 Shooting Method ....... 359
14.1.1 Implementation .... 360
14.1.2 Test Shooting2 Method . 362
14.2 Finite Difference for Linear Equation .. 363
14.2.1 Algorithm .... 363
14.2.2 Implementation .... 366
14.2.3 Test FiniteDifferenceLinear2 Method ..... 368
14.3 Finite Difference for Nonlinear Equations .... 371
14.3.1 Algorithm .... 371
14.3.2 Implementation .... 371
14.3.3 Test FiniteDifferenceNonlinear2 Method 373
14.4 Finite Difference for Higher-Order Equation ........ 374
14.4.1 Algorithm .... 374
14.4.2 Implementation .... 376
14.4.3 Test FiniteDifferenceLinear4 Method ..... 379
15 Eigenvalue Problems 381
15.1 Jacobi Method .. 381
15.1.1 Algorithm .... 382
15.1.2 Implementation .... 384
15.1.3 Test Jacobi Method ....... 387
15.2 Power Iteration . 388
15.2.1 Algorithm .... 388
15.2.2 Implementation .... 389
15.2.3 Test Power Method ....... 390
15.3 Inverse Iteration ........ 391
15.3.1 Algorithm .... 391
15.3.2 Implementation .... 392
15.3.3 Test Inverse Method ..... 393
15.4 Rayleigh Method ....... 393
15.4.1 Algorithm .... 393
15.4.2 Implementation .... 394
15.4.3 Test Rayleigh Method ... 395
15.5 Rayleigh-Quotient Method. 395
15.5.1 Algorithm .... 395
15.5.2 Implementation .... 396
15.5.3 Test Rayleigh-Quotient Method ..... 397
15.6 Matrix Tridiagonalization .. 398
15.6.1 Algorithm .... 398
15.6.2 Implementation .... 401
15.6.3 Test Tridiagonalize Method ... 403
15.7 EigenValues of Symmetric Tridiagonal Matrices .. 404
15.7.1 LU Decomposition of Tridiagonal Matrices ..... 405
15.7.2 Implementation .... 407
15.7.3 Examples ..... 410
15.8 Use Math.NET Numerics ... 414
16 Fourier Analysis ....... 415
16.1 Fourier Series ... 415
16.1.1 Fourier trigonometric Series .. 415
16.1.2 Fourier Exponential Series ..... 419
16.2 Fourier Transforms.... 419
16.2.1 Discrete Fourier Transform .... 419
16.2.2 Fast Fourier Transform . 420
16.3 FFT Interpolation ...... 423
16.3.1 Data from Math Functions ..... 423
16.3.2 User Provided Data ....... 426
17 Math Expression Parser ... 429
17.1 Get Started ........ 429
17.1.1 Built-In Math Operators 430
17.1.2 Built-In Math Functions 431
17.1.3 Build-In Calculus Operators .. 432
17.2 Basic Math Calculations .... 432
17.2.1 Number Operations ....... 432
17.2.2 Use Variables ....... 433
17.2.3 Use Build-In Functions . 434
17.2.4 User Defined Functions 435
17.3 Random Numbers and Distributions .... 436
17.3.1 Built-In Random Variables .... 436
17.3.2 Random Distributions ... 437
17.3.3 Distribution Functions .. 439
17.4 Calculus ... 440
17.4.1 Finite Difference .. 441
17.4.2 Differentiation ...... 441
17.4.3 Integration ... 442
17.5 Parametric Charts with mXParser ........ 442
Index .. 447
Introduction
        Why Second Edition
        What’s New in Second Edition
        Overview
        What This Book Includes
        Is This Book for You
        What Do You Need to Use This Book
        How This Book is Organized
        Using Code Examples
Why Second Edition
I have received plenty of feedback from readers since the first edition of my book “Practical Numerical Methods with C#” was published in 2008. Many readers asked for an updated edition. I realize that .NET technology has advanced a lot and many open source C# math libraries have been developed in the past few years, so there is a need to incorporate these new developments into my book and examples contained therein.
In the first edition, the example projects were built and tested as console applications on Visual Studio 2008 under Windows 2000 or XP operating system. In this new edition, I decide to port and rebuild all the examples in the book using the C# Windows Forms App (.NET Framework) template and C# Interactive Window based on the latest Visual Studio 2019 and .NET Framework 4.7.
What’s New in Second Edition
In addition to port the example code to the latest .NET technology, the second edition also makes some structural changes and replacement of some chapters with the updated content.
Example Structure
In the first edition, each example was built as a separated project, which results in almost 30 independent C# console applications. As several readers pointed out, there are some issues with this approach:
- Inconvenience when switching from one example to another – you have to close the current example project and open another one in Visual Studio.
- Duplicated code – Since each example is an independent project, it is impossible to share the common code among different example projects. You have to copy and paste a lot of duplicated code from one example to another.
To avoid these problems, in the second edition, I decide to restructure the example project – build all the examples within a single project in Visual Studio. This way, different examples can share common code to avoid code duplication. I also add the navigation button links on the MainForm window, which allows you to switch from one example to another by simply clicking the corresponding navigation link.
Use Open Source Libraries
There were almost no C# math libraries available when the first edition was published in 2008. We had to implement all the math functions from scratch, including even the most basic math structures such as complex number struct, vector struct, matrix struct, etc. Right now, with the progress in .NET technology, there are several C# open-source math libraries available. Therefore, in this second edition, I will use an open source library, Math.NET Numerics, to implement various numerical methods. If the math functions for some numerical computations are included in the library, I will use them directly; otherwise, I will implement them as new C# math functions. This way, we can avoid reinventing the wheel, and concentrate on numerical methods for solving real-world problems.
In addition, we also use another open source library called mXParser to evaluate mathematical expressions, which will be useful for some simple numerical calculations. The mXParser package is an easy, rich, and flexible math expression parser and evaluator of mathematical expressions provided as plain text. The math parser is especially powerful when your applications need to take the math expressions from users.
C# Interactive Window
The C# Interactive Window has been a part of Visual Studio since the Visual Studio 2015 update 1 released in November 30, 2015. C# Interactive is a REPL (Read-Evaluate-Print-Loop) editor. With the C# Interactive Window, we can test our code snippet without compiling or running the complete project code. In the second edition, we will use the C# interactive Window extensively to test our math functions for numerical methods, instead of using separate console applications as we did in the first edition.
Chapter Update
Since the complex struct has been included in both the System.Numerics namespace and Math.NET Numerics, the Chapter 2 “Complex Numbers and Functions” in the first edition has been removed from the second edition. Three new chapters have been added to the second edition:
- Chapter 3 “Plotting”: In the first edition, we plotted some math functions using external plot packages such as Excel and MATLAB. In the second edition, I will use a Chart2D user control to plot math functions within a Windows Forms application. The Chart2D control is taken from the code examples of my recent published book Practical C# Charts and Graphics (Second Edition). This allows you to perform numerical calculations and display the calculated results using a single Windows Forms application, without the need to use external chart packages. This chapter also introduces a new chart and graphics playground called Gincker Graphics, which allows you to create advanced charts and graphics in Windows Forms applications.
- Chapter 16 “Fourier Analysis”: Fourier analysis is a powerful technique, which allows you to view your signals in a different domain, inside which the difficult problems may become very simple to analysis. So, I add a chapter that describes Fourier series, discrete Fourier transform (DFT), and fast Fourier transform (FFT).
- Chapter 17 “Math Expression Parser”: In this chapter, I introduce a simple, but powerful NuGet package called mXParser, which allows you to calculate various math expressions at runtime. The math expressions are defined as plain text with common math operations.
The following is the updated content, which is basically similar to that in the first edition.
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 the .NET Framework. For many years, FORTRAN has been the dominant language of scientific and engineering computation. But as Microsoft C# and .NET Framework gain popularity, you may find that they are 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 the C# programming language, combined with the simplicity of implementing Windows Forms, WPF desktop applications, and Silverlight Web applications based on 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 to 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 include 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 was relatively new to the scientific and engineering community when the first edition was published over 10 years ago. At that time, the lack of C# math libraries prevents many researchers from using the C# programming language in their applications.
However, with the advancement of C# and .NET technology, many good C# math libraries have been implemented since the first edition of this book was published in 2008. Right now, with these C# math libraries, it is fairly easy to develop a powerful C# package for numerical calculations according to your application requirement. It was also 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, 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 begin 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 follow with ease. From this book, you will learn how to perform complicated scientific computations and create your own math libraries based on C# and the .NET Framework.
Practical Numerical Methods with C# is not simply a book, but a powerful C# math library. You will find that you can immediately use some of the examples in this book to solve your real-world problems, and that you can use others to give you inspiration for adding more advanced math libraries to your applications.
What This Book Includes
This book and its sample code listings, which are available for download at this website, provide you with:
- Complete, in-depth instruction on practical scientific computing and programming using C#. After reading this book and running the example programs contained within, you will be able to create various math and numerical libraries in your own C# applications.
- • A detailed description on how to use the open-source C# math libraries for numerical computations in your C# .NET applications, and how to implement your own math functions using the basic mathematical structures implemented in these C# math libraries, including Complex struct, Vector struct, and Matrix struct.
- 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 models 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 solving your real-world scientific and engineering problems. These classes include 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 do not have to be an experienced C# developer or expert to use this book. I designed this book to be useful to scientists and engineers of 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 the Microsoft .NET Framework SDK or Visual Studio .NET, follow the examples 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 to 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 programs provided in this book can be used directly in your own 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 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 to not only easily generate data (computation capability) but also easily represent data graphically (graphics and chart power). The C# and Microsoft Visual Studio .NET development environment makes 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 solving 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 already a plethora of books out there that deal with these topics. Most of the example programs in this book omit error handling. 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 also address the graphical representations of your calculation results using a Chart2D control. 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 on charts and graphics.
What Do You Need to Use This Book
You will need no special equipment to make the best use of this book and understand the algorithms. This book will take full advantage of open source frameworks and libraries. To run and modify the sample programs, you will need a computer capable of running either Windows 7, 8, or 10. The software installed on your computer should include Visual Studio 2019 (Community version is fine) or higher.
If you have Visual Studio 2017 or older, you can also run most of the sample code with few modifications. Please remember, however, that this book is intended for Visual Studio 2019 and that all of the example programs were created and tested on this platform, so it is best to run the sample code on the same platform.
How This Book is Organized
This book is organized into seventeen chapters, each of which covers a different topic of numerical computing. 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, Vectors and Matrices
This chapter introduces a more general n-dimensional vector struct and a general matrix struct implemented in the Math.NET Numerics library, 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 3, Plotting
This chapter illustrates how to use the Chart2D user control to create 2D line charts. It also shows how to embed Gincker Graphics into your Windows Forms applications. Gincker Graphics is a charts and graphics playground that allows you to create a variety of advanced charts and graphics, including 2D specialized charts and 3D surface charts.
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 special functions, which contains popular special functions such as the gamma function, beta function, error function, elliptic integral, Laguerre function, Hermit function, Chebyshev function, Legendre function, and Bessel function, etc.
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. Interpolation 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 variables, 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 and 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 Integration
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, the adaptive
Runge-Kutta method, and the Runge-Kutta methods that can be used to solve a system of ordinary
differential equations.
Chapter 14, Boundary Value Problems
This chapter discusses two methods for solving boundary value problems: the shooting
method and the finite differences method. 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.
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
real symmetric matrix systems.
Chapter 16, Fourier Analysis
This chapter introduces Fourier analysis, which allows you to view your signals in a different domain, inside which the difficult problems may become very simple to analysis. This chapter covers various interesting topics about Fourier analysis, including Fourier series, discrete Fourier transform (DFT), and fast Fourier transform (FFT).
Chapter 17, Math Expression Parser
This chapter introduces a simple, but powerful NuGet package called mXParser, which allows you to calculate various math expressions at runtime. The math expressions are defined as plain text with common math operations. The combination of the mXParser package with the C# interactive window will become a powerful interactive computation environment.
Using Code Examples
You may use the code in this book in your applications and documentation. You do not need to contact me or the publisher 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 does not 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 does require permission. Integrating the example code from this book into your commercial products is not allowed without the written permission from the author.
