Initializing packages
When running this notebook for the first time, this could take up to 10 minutes. Hang in there!
Introduction
Welcome to CHE 348: Numerical Methods in Chemical Engineering in Fall 2024!
The significant increase in available compute over the last several decades has led to a transformation in the methods and techniques for solving science and engineering problems. To use new tools effectively, it is crucial that we understand their theoretical underpinnings and limitations, as well as practical details of their implementation.
CHE 348 is a one-semester advanced undergraduate course providing students with a fundamental understanding of common numerical methods spanning
chemical engineering,
computational science, and
applied mathematics.
In problems studied, the underlying physics is emphasized throughout, with principles from non-dimensionalization frequently employed.
While the ideas presented are general (and thus not tied to any specific programming language), the entirety of the course will be carried out in Julia—a powerful, easy-to-use language designed for scientists and engineers.
Administrative items
Lectures will be uploaded to the course website, and problem sets will be submitted via canvas
In the discussion section yesterday (26 Aug.), you should have downloaded Julia, installed it along with the Pluto package. Video instructions are provided to aid you in this.
Starting with Julia
Launch the REPL and verify that Julia is installed appropriately. You should be able to do the following on your machine:
2
6.283185307179586
Taylor series
Review
The Taylor series is often said to be the most important concept from calculus. From Wikipedia:
The Taylor series of a function is an infinite sum of terms that are expressed in terms of the function's derivatives at a single point. For most common functions, the function and the sum of its Taylor series are equal near this point.
Consider a function $f(y)$, where $y \in \mathbb{C}$ and $f \in \mathbb{C}$, for which one could write
$$\begin{aligned} f(y) \, &= \, f(y_0) \, + \, f'(y_0) \, (y - y_0) \, + \, \dfrac{1}{2!} f''(y_0) \, (y - y_0)^2 \, + \, \ldots \\[5pt] \, &= \, \sum_{n = 0}^\infty \dfrac{1}{n!} \, \dfrac{\mathrm{d}^n f(y)}{\mathrm{d} y^n} \bigg\rvert_{y = y_0} \big(y - y_0 \big)^{\! n} ~. \end{aligned}$$
The Taylor series yields many of the relations you have seen in the past. For example,
$$\mathrm{e}^y \, = \, 1 \, + \, y \, + \, \dfrac{y^2}{2!} \, + \, \dfrac{y^3}{3!} \, + \, \ldots$$
Practice Question
What value is the above series expanded about?
Numerical approximation
In a computer, one cannot sum an infinite number of terms. A truncation error arises because only a finite number of terms are included.
Let us define the function $\tilde{f} (y \, ; y_0, M)$ as the truncated polynomial series of order $M$, written as
$$\tilde{f} (y \,; y_0, M) \, := \, \sum_{n = 0}^M \dfrac{1}{n!} \, \dfrac{\mathrm{d}^n f(y)}{\mathrm{d} y^n} \bigg\rvert_{y = y_0} \big(y - y_0 \big)^{\! n} ~.$$
Practice Question
How many terms are in $\tilde{f} (y \, ; y_0, M)$?
Practice Question
What is the highest-order polynomial term in $\tilde{f} (y \, ; y_0, M)$?
Let us now use the exponential function as an example. To this end, we choose
$$f(y) \, = \, \mathrm{e}^y ~,$$
for which
$$\tilde{f} (y \, ; y_0 = 0, M) \, := \, 1 \, + \, y \, + \, \dfrac{y^2}{2!} \, + \, \dfrac{y^3}{3!} \, + \, \ldots \, + \, \dfrac{y^M}{M!} ~.$$
Example
Supposed we are tasked with calculating $\tilde{f} (0.2 \, ; y_0 = 0, M = 3)$.
1.2213333333333334
How does this compare to the actual value of the function?
1.2214027581601699
1.2214027581601699
What is the truncation error that arises?
6.942482683647277e-5
Analytically, we denote the truncation error—sometimes called the residual—as $r(y \, ; y_0, M)$, which is defined to be
$$\begin{aligned} r(y \, ; y_0, M) \, :=& \ \big\lvert f(y) \, - \, \tilde{f} (y \, ; y_0, M) \big\rvert \\[4pt] \, =& \, \sum_{n = M + 1}^\infty \dfrac{1}{n!} \, \dfrac{\mathrm{d}^n f(y)}{\mathrm{d} y^n} \bigg\rvert_{y = y_0} \big(y - y_0 \big)^{\! n} ~. \end{aligned}$$
Notice that if the $n^{\text{th}}$ derivative of the function $f$ is "of order unity," i.e. it is relatively constant for all $n$, then as $n$ increases the $n!$ in the denominator will become larger than the $(y - y_0)^n$ in the numerator. When using Taylor series, we are generally concerned with how large the residual is for a given choice of $y_0$ and $M$.
You will investigate this dependence in Problem Set 1
Note that you could, in principle, calculate all these errors—for many different choices of $y_0$ and $M$— one by one. However, these sorts of repetitive tasks are much more conveniently done with a computer! Let us begin to see how to do so.
Numerical implementation
Let us begin by storing relevant information into variables. As a rough approximation, one can think of a computer as doing one of three things:
storing data
computing with data
displaying data
0.2
3
1.2214027581601699
1.2213333333333334