None Notebook

This notebook contains material from CBE30338; content is available on Github.

< A.0 Additional Python | Contents | Tag Index | A.2 Modular Simulation using Python Generators >

Open in Colab

Download

A.1 Python Library for CBE 30338

A.1.1 Some Python basics

A.1.1.1 Python functions

Here are essential things you need to know about Python functions:

The following cells demonstrate these points in the process of computing a numerical solution to the system of differential equations describing the motion of a mass $m$ subject to a time-varying force $f(t)$

\begin{align} \frac{dx}{dt} & = v \\ \frac{dv}{dt} & = \frac{1}{m} f(t) \\ \end{align}

for various choices of $f(t)$. The cells show several different ways of coding $f(t)$, and the vector valued right hand sides of this system of differential equations, as Python functions.

A.1.1.1.1 Functions are Python objects

The first aspect of this simulation is to establish a specific function to describe the time-varying force $f(t)$. As an example, the following cell creates a plot of $\cos(t)$ using the numpy library.

In Python, the notation np.cos(t) returns the value of $cos(t)$ for a specific value of time $t$. The notation np.cos (note the absence of parentheses after the function name) refers to the function itself, the function being the object that does the computation. An important feature of Python is that functions can be assigned and manipulated like other Python objects.

The following cell shows how the function np.cos can be assigned to a Python symbol f and then used in subsequent calculations.

Notice that f and f(t) are different things. By itself, the symbol f refers to the function. With a following parentheses and any required arguments, f(t) returns whatever was listed after the return statement inside of the function. If these is no return statement of if there is nothing listed then the function returns None.

A.1.1.1.2 Create simple functions with lambda

There are many situations where all you need is a simple function to encapsulate a calculation that can be done in one line of Python code. Python's lambda statement is ideal for this purpose. Learning to use the lambda function well allows you to write more compact and readable code.

The following cell uses lambda to create a function f(t) with a single argument t that returns the exponentially damped sinusoid $e^{-t/4} sin(t)$.

It's hard to overstate the utility of lambda functions when combined with other features of Python. For example, while lambda functions are limited to one line statements, when combined with Python's conditional expression your code can incorporate logical conditions.

A.1.1.1.3 Create complex functions with def

Python provides two methods of creating functions. The most general method is the def statement that defines the name and arguments of a function, followed by accompanying return statement to specifies what the function returns.

As an example, the following code creates a function that takes a value time ($t$), position ($x$), and velocity ($v$), and returns the a two element array corresponding to the right hand side of the differential equations

A.1.1.2 Comprehensions

A.1.1.3 map, filter, reduce

https://medium.com/better-programming/how-to-replace-your-python-for-loops-with-map-filter-and-reduce-c1b5fa96f43a

A.1.2 CBE30338.plotter()

CBE30338.plotter() is a function that simplifies creation of figures with the mulitple plotting axes.

< A.0 Additional Python | Contents | Tag Index | A.2 Modular Simulation using Python Generators >

Open in Colab

Download