To be able to edit code and run cells, you need to run the notebook yourself. Where would you like to run the notebook?

This notebook takes about 20 seconds to run.

In the cloud (experimental)

Binder is a free, open source service that runs scientific notebooks in the cloud! It will take a while, usually 2-7 minutes to get a session.

On your computer

(Recommended if you want to store your changes.)

  1. Copy the notebook URL:
  2. Run Pluto

    (Also see: How to install Julia and Pluto)

  3. Paste URL in the Open box

Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1

Initializing packages

When running this notebook for the first time, this could take up to 10 minutes. Hang in there!

👀 Reading hidden code
md"""
### Initializing packages

*When running this notebook for the first time, this could take up to 10 minutes.
Hang in there!*
"""
280 μs
using Plots
👀 Reading hidden code
1.2 s

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:

👀 Reading hidden code
1.8 ms
2
1 + 1
👀 Reading hidden code
11.2 μs
6.283185307179586
π * 2
👀 Reading hidden code
11.7 μs

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?

👀 Reading hidden code
530 μs

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!} ~.$$



👀 Reading hidden code
619 μs

Example

Supposed we are tasked with calculating $\tilde{f} (0.2 \, ; y_0 = 0, M = 3)$.

👀 Reading hidden code
239 μs
1.2213333333333334
1 + 0.2 + 0.2^2/2 + 0.2^3/6
👀 Reading hidden code
6.4 ms

How does this compare to the actual value of the function?

👀 Reading hidden code
192 μs
1.2214027581601699
ℯ^0.2
👀 Reading hidden code
11.1 μs
1.2214027581601699
exp(0.2)
👀 Reading hidden code
10.2 μs

What is the truncation error that arises?

👀 Reading hidden code
192 μs
6.942482683647277e-5
abs(ℯ^0.2 - (1 + 0.2 + 0.2^2/2 + 0.2^3/6))
👀 Reading hidden code
23.1 μs


👀 Reading hidden code
156 μs

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.

👀 Reading hidden code
521 μs

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

👀 Reading hidden code
539 μs
0.2
y = 0.2
👀 Reading hidden code
10.0 μs
3
M = 3
👀 Reading hidden code
10.4 μs
1.2214027581601699
f_exact = ℯ^y
👀 Reading hidden code
14.4 μs
1.2213333333333334
f_approx = 1 + y + y^2/2 + y^3/6
👀 Reading hidden code
24.5 μs
Loading cells...