Introduction to 06-623 Mathematical Modeling of Chemical Engineering Processes

Table of Contents


1 Jupyter notebook introduction

Jupyter notebooks are an interactive, browser-based tool for running Python. We will use them exclusively in this class. We will quickly build up the skills required to solve engineering problems. We will not learn everything about programming, mathematical modeling or engineering processes. My goal is to get you thinking about a very general computational way of thinking about these problems, and to learn how to use computation as a way to augment your analytical skills.

I will lecture from the notebooks, and show you how I use them. The notes will be available to you during class for you to work along with me.

print('Hello World!')
# Type C-Enter to run this cell
# Type shift-Enter to run this cell and got to the next one or create a new one.

Hello World!

What is the value of \(a\) that satisfies the equation \(a + 4 = 5\)?

We can document our solution here, e.g. we find \(x\) by algebra. If we subtract 4 from each side of the equation, \(a\) will be isolated, and equal to the value of the right hand side of the equation.

<div class="alert alert-warning"> You can double-click on any text block to see what the Markdown is that generates it. Type C-Enter to re-render the cell. </div>

Here is the code that implements that explanation:

a = 5 - 4
a  # this makes the value of x get displayed
1

1.1 numpy

numpy is a Python library for arrays. We have to import this library to access the functionality in it. The conventional way to import this library is:

<div class="alert alert-warning"> Remember this cell. You will use it almost every time. </div>

import numpy as np

To see help on the numpy library, run this cell:

?np

Now, we can access functions in the numpy module using "dot notation". For example, let us start by creating an array of linearly spaced points using the linspace function. First, we access the help to see how to use it.

?np.linspace

<div class="alert alert-warning"> np.pi is a constant for the number \(\pi\) </div>

x = np.linspace(0, 2 * np.pi)
x
array([0.        , 0.12822827, 0.25645654, 0.38468481, 0.51291309,
       0.64114136, 0.76936963, 0.8975979 , 1.02582617, 1.15405444,
       1.28228272, 1.41051099, 1.53873926, 1.66696753, 1.7951958 ,
       1.92342407, 2.05165235, 2.17988062, 2.30810889, 2.43633716,
       2.56456543, 2.6927937 , 2.82102197, 2.94925025, 3.07747852,
       3.20570679, 3.33393506, 3.46216333, 3.5903916 , 3.71861988,
       3.84684815, 3.97507642, 4.10330469, 4.23153296, 4.35976123,
       4.48798951, 4.61621778, 4.74444605, 4.87267432, 5.00090259,
       5.12913086, 5.25735913, 5.38558741, 5.51381568, 5.64204395,
       5.77027222, 5.89850049, 6.02672876, 6.15495704, 6.28318531])

Most mathematical operations are element-wise on arrays.

2 * x
array([ 0.        ,  0.25645654,  0.51291309,  0.76936963,  1.02582617,
        1.28228272,  1.53873926,  1.7951958 ,  2.05165235,  2.30810889,
        2.56456543,  2.82102197,  3.07747852,  3.33393506,  3.5903916 ,
        3.84684815,  4.10330469,  4.35976123,  4.61621778,  4.87267432,
        5.12913086,  5.38558741,  5.64204395,  5.89850049,  6.15495704,
        6.41141358,  6.66787012,  6.92432667,  7.18078321,  7.43723975,
        7.69369629,  7.95015284,  8.20660938,  8.46306592,  8.71952247,
        8.97597901,  9.23243555,  9.4888921 ,  9.74534864, 10.00180518,
       10.25826173, 10.51471827, 10.77117481, 11.02763136, 11.2840879 ,
       11.54054444, 11.79700098, 12.05345753, 12.30991407, 12.56637061])

We can define new variables

y1 = np.sin(x)
y2 = np.cos(x)

1.2 plotting

We can make plots using matplotlib. First we need these two lines. The first one makes the plots appear inline in the notebook. The second imports the plotting library. These should be used in this order.

<div class="alert alert-warning"> Remember this cell. You will use it almost every time you make a plot. </div>

%matplotlib inline
import matplotlib.pyplot as plt

You call functions in the plt library to create plots. These are automatically saved in the notebook.

plt.plot(x, y1, x, y2)
plt.xlabel('x')
plt.ylabel('y')
plt.legend(['y1', 'y2'])
# Always include axis labels and legends when appropriate
<Figure size 432x288 with 1 Axes>

cf9a95bf6dbd8cc50ba1007089843e0de28f5500.png

1.3 scipy

scipy contains numerous libraries for a broad range of scientific computing needs.

Suppose we want to perform the following integral: \(I = \int_0^{4.5} J_{2.5}(x) dx\). The function \(J_{2.5}\) is a special function known as a Bessel function. scipy provides both the integration function, and an implementation of the special function we can use.

from scipy.integrate import quad
from scipy.special import jv
?quad
?jv

To evaluate this integral, we have to define a function for the integrand, and use the quad function to compute the integral. The quad function returns two values, the value of the integral, and an estimate of the maximum error in the integral.

# This is how we define a function. There is a function name, and arguments
# The function returns the output of the jv function.
def integrand(x):
    return jv(2.5, x)

I, err = quad(integrand, 0, 4.5)

I, err
(1.1178179380783253, 7.866317216380692e-09)

2 Summary

Today we introduced several ideas about using Jupyter notebooks to run Python computations. The main points are:

  1. Code is run in code cells
  2. You have to import some functions from libraries
  3. numpy, scipy and matplotlib are three of the main scientific programming libraries we will use a lot.
  4. We saw some ways to get help on functions

Next time we will dig into defining functions more deeply, and how to print formatted strings containing results.

Date: 2019-08-26 Mon 00:00

Author: John Kitchin

Created: 2020-03-31 Tue 07:32

Validate