Programming Projects in C
for students of
Engineering, Science, and Mathematics
Rouben Rostamian
2014
Book Cover Image

This website is associated with the book Programming Projects in C
SIAM, 2014
The URL http://www.siam.org/books/cs13 redirects here

What readers are saying about this book…

Computing Reviews #1505–0335, May 15, 2015, pp. 267–268
This text is extraordinary in many respects. In terms of precision and detail, it benefits from Professor Rostamian’s mathematical background as well as from his deep appreciation for a wide range of scientific and engineering applications. The
Hasan Nagib at Mechlab 3D, May 16, 2015
I have recently come across a fantastic book published by SIAM. It’s titled “Programming projects in C for Students of Engineering, Science and Mathematics” by Rouben Rostamian. I will be posting some of my codes for projects from this book as well. This book has been an excellent resource for learning C. I would highly recommend it to the other engineering undergraduates who would like to develop their programming skills beyond what is required in a typical undergraduate degree.
David's Blog, May 25, 2015
It's good to see that some new C programming books still get published. These days they are quite rare, but it's great to see when one does appear. At the moment I am enjoying this one: Programming projects in C for Students of Engineering, Science and Mathematics. I bought mine when I found a copy whilst browsing the
Zentralblatt Math, September 2015, Zbl 1308.68004
This book is written for graduate and advanced undergraduate students of sciences, engineering, and mathematics as a tutorial on how to think about, organize, and implement programs in scientific computing. The goal is to provide an interesting and instructive set of
SIAM Reviews, vol. 57, no. 4, December 2015
This is a textbook for a scientific computing course for graduate students and advanced undergraduates. It can also be used for self study. Why C? C is relatively old and well established; it’s not this week’s fashionable language. Compilers are

Table of Contents

Links marked [resources] lead to pages containing notes, comments, and known errata, if any. If you encounter other typos or errors of any nature, please let me know.

Part I: A common background

  1. Introduction
  2. File organization
  3. Streams and the Unix shell
  4. Pointers and arrays
  5. From strings to numbers
  6. Make [resources]

Part II: Projects

  1. Reading lines fetch_line()
  2. Allocating memory xmalloc()
  3. Dynamic memory allocation for vectors and matrices [resources]
  4. Generating random numbers
  5. Storing sparse matrices
  6. Sparse systems: The UMFPACK library [resources]
  7. Haar wavelets [resources]
  8. Image I/O [resources]
  9. Image analysis
  10. Linked lists [resources]
  11. The evolution of species [resources]
  12. The Nelder–Mead downhill simplex [resources]
  13. Trusses [resources]
  14. Finite difference schemes for the heat equation in 1D [resources]
  15. The porous medium equation [resources]
  16. Gaussian quadrature [resources]
  17. Triangulation with Triangle [resources]
  18. Integration on triangles [resources]
  19. Finite elements [resources]
  20. Finite Elements: Part 2
  21. [Supplemental chapter] Neural networks for solving ODEs [resources]
  22. [Supplemental chapter] Neural networks for solving PDEs [resources]

Appendix: Barycentric coordinates

Chapter 17: Simulated Evolution

Bugs (red dots) evolve into two distinct species in response to environmental factors. A region where food (green dots) is plentiful (near the center) leads to the emergence of a sluggish species that tends to stay in one place. A region where food is scarce (away from the center) leads to the emergence of a species that tends to move about a lot.

[evolution.gif]

Chapter 18: The Nelder–Mead minimization algorithm

The Nelder–Mead algorithm is a gradient-free method for minimization of functions of the type $f : R^n \to R$. It works through a moving and deforming adaptive simplex (a triangle in 2D) that “sniffs around” for the location of the minimum.

[nelder-mead.gif] [energy.png]

Chapter 19: Large deformations of trusses

Our truss solver handles statically indeterminate trusses with nonlinear elastic members and large deformations (assuming that no buckling occurs).

Large deformations of a Pratt truss under variable load [pratt.gif]
large deformations of a cantilever truss under variable load [cantilever.gif]

Chapters 26 & 27: A finite element solver

Our finite element implementation solves the elliptic PDE $\nabla ( A \nabla u ) + f = 0$ over a polygonal domain (possibly with holes) in $R^2$, subject to mixed Dirichlet and Neumann boundary conditions. The functions $A$ and $f$ are given. We partition the domain into an unstructured triangular mesh, and approximate the solution $u$ by linear functions within the triangular elements.

Example 1:
A sequence of solutions to the Poisson problem $\nabla^2 u + 1 = 0$ on an L-shaped domain with zero Dirichlet boundary data, and a progressively refined mesh:
[l-shaped-anim.gif]
Example 2:
The solution of the Poisson problem $\nabla^2 u + f = 0$ on an annular domain, where $f(r,\theta) = \cos \theta\,$ in polar coordinates. The boundary conditions are $u=0$ on the outer boundary, $\partial u/\partial n=0$ on the inner boundary:
[annulus.png]

Chapter 15: Image analysis through Haar wavelets

A grayscale image sample

The following sequence depicts a 512×512 grayscale image at various levels of resolution. Each image is constructed through a superposition Haar of wavelets. The lowest resolution is made of merely 113 wavelets. The highest resolution requires the superposition of 241,570 (about a quarter million!) wavelets.

[aya_matsuura-animated.gif]

A color image sample

The original 256×256 image was transformed via Haar wavelets. Then the least significant coefficients were dropped and the image was reconstructed with the remaining coefficients. The relative L2 errors of truncation in the three reconstructed images are 2, 3, and 6 percent. The information from the original image retained are 10%, 5%, and 1%, respectively.

[vivian000.jpg]
Original image
[vivian000.jpg]
...with 10% of the original information
[vivian000.jpg]
...with 5% of the original information
[vivian000.jpg]
...with 1% of the original information

Chapters 20 & 21: Finite difference schemes for PDEs

We introduce various finite difference schemes for solving the initial/boundary value problem of the heat equation \begin{align} &\frac{\partial u}{\partial t} = \frac{\partial^2 u}{\partial x^2}, & &x \in (a,b), \; t > 0, \\ &u(x,0) = u_0(x), && x \in (a,b), \\ &u(a,t) = u_L(t), \quad u(b,t) = u_R(t), && t > 0. \end{align}

Here is a graph of the solution $u(x,t)$ produced by one of the schemes:

[cn1.png]

An improper application of a finite difference scheme can lead to numerical instabilities:

[fd-unstable.png]

We extend one of the schemes to solve the highly nonlinear and degenerate porous medium equation \begin{align*} &\frac{\partial u}{\partial t} = \frac{\partial^2 \phi(u)}{\partial x^2}, & &x \in (a,b), \; t > 0, \\ &u(x,0) = u_0(x), && x \in (a,b), \\ &u(a,t) = u_L(t), \quad u(b,t) = u_R(t), && t > 0, \end{align*} where we take $\phi(u) = u^3$, but the method may be applied to arbitrary monotonically increasing $\phi$. Here is a sample solution:

[pme1-sediman-sweep.png]

Coding style (by Linus Torvalds)

The Linux kernel coding style is written by Linus Torvalds as a style-guide for his collaborators on developing and maintaining the Linux kernel. The advice in chapters 1–9 is generic and applies to any C programming project. I do follow that style myself, and recommend that you do it too.



Author: Rouben Rostamian Valid HTML 5 Valid CSS