Philip Joyce
Numerical C
Applied Computational Programming with Case Studies
Philip Joyce
Goostrey, UK
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484250631 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-5063-1 e-ISBN 978-1-4842-5064-8
https://doi.org/10.1007/978-1-4842-5064-8
Philip Joyce 2019
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction
This book is about learning to write computer programs in C to solve problems in mathematics and to show how C can be used to get data information in other areas like economics and biochemistry.
Computers are used in most areas of life, and it can be vital to your area of work to realize how helpful computer software can be. This book aims to show you how you can write software in C to help you in your particular field.
The book uses your existing knowledge of basic mathematics to demonstrate to yourself how useful software can be. Many mathematical problems are solved using logical algebraic techniques which are taught at school. These are done with pen and paper and form the foundation of mathematics. However, there are a lot of problems which would take a group of people working together hours to solve in this way but could be solved in seconds using computer software. In fact, some problems can only be solved using computer software.
The book starts by introducing the C language and showing you how to write a simple program very quickly. The chapters take you through mathematics you probably already know from school. You will have solved problems in algebra using pen and paper. One topic is Trial and Improvement whereby you take an equation that cannot be solved using normal analytical methods, but you can try any value out in the equation and see if you get the value at the right of the equation. As an example, we can substitute any value of x into the following equation and see if we get 13.
5x 4 + 17x 3 3x = 13
If your answer is higher than 13, you try a lower value of x to plug into the equation. If this gives a value lower than 13, then you know that the correct value of x must be somewhere in between your first value of x and your second value. So using this method, you can just keep narrowing down closer and closer to the correct right-hand value. This is a perfect problem to be solved by writing a C program. Whereas the problem could take you half an hour to solve using pen, paper, and a calculator (depending on the complexity of the equation), a C program will do it in seconds.
Another problem you may have done at school is using the quadratic formula to solve quadratic equations. (Remember that a quadratic equation is one where the highest power of x is x 2 , e.g., 2x 2 + 3x 5 = 0.) These are solved using a formula which, at first sight, might look a bit daunting.
But this is just the solution to the quadratic equation
ax 2 + bx + c = 0
where in our equation earlier, a is 2, b is 3, and c is 5.
We can just substitute these values into the formula in our C program and get the solution to the equation.
We can also write software to solve simultaneous equations. In calculus we can write a quick and efficient way of using the Trapezium Method of integration. Another method of integration has the exotic name Monte Carlo Integration in which the C program makes use of the way it can generate random numbers. Other mathematical methods include Simpsons Rule, matrix arithmetic, regression, the Product moment correlation coefficient (sounds impressive), another Monte Carlo method (this time to find pi), the augmented matrix method for solving simultaneous equations, and the solution of differential equations.
Finally, we look at methods of using C to access and retrieve information from data files. These methods can have applications in many fields, but in this book we will look at simple examples in medicine and economics.
The book can be used in educational organizations and commercial organizations or can be used by individuals who wish to further their knowledge.
If you dont have the C program development tools, you can download them free of charge from Microsoft and from other organizations.
You can also download software packages to draw graphs. One of these is Graph which is free to download. In Graph you can just enter an equation and it draws the curve. You can also insert a set of data points, from a file that your program creates, into the Graph package and it will display your points. You can then see from the pattern of the points if it follows the shape of a known function.
Acknowledgments
Thanks to my wife, Anne, for her support, my son Michael, and my daughter Katharine. Michael uses regression techniques in his work and has shared some ideas with me. Katharine was the catalyst for me writing the book. While she was at university, I taught her Fortran programming with applications in the mathematics she had done in her first year. The work we did was based on my MSc course in Computational Physics but tailored toward mathematics applications. This work then became the basis for this book.
Thanks to everyone on the Apress team who helped me with the publication of this, my first book. Special thanks to Mark Powers, the coordinating editor, for his advice; Steve Anglin, the acquisitions editor; Matthew Moodie, the development editor; and Michael Thomas, the technical reviewer.