{ "cells": [ { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/A.01-Python-Library-for-CBE30338.html)", "section": "" } }, "source": [ "\n", "*This notebook contains material from [CBE30338](https://jckantor.github.io/CBE30338);\n", "content is available [on Github](https://github.com/jckantor/CBE30338.git).*\n" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/A.01-Python-Library-for-CBE30338.html)", "section": "" } }, "source": [ "\n", "< [A.0 Additional Python](https://jckantor.github.io/CBE30338/A.00-Additional-Python.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [A.2 Modular Simulation using Python Generators](https://jckantor.github.io/CBE30338/A.02-Modular-Approach-to-Simulation-using-Python-Generators.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[A.1 Python Library for CBE 30338](https://jckantor.github.io/CBE30338/A.01-Python-Library-for-CBE30338.html#A.1-Python-Library-for-CBE-30338)",
"section": "A.1 Python Library for CBE 30338"
}
},
"source": [
"# A.1 Python Library for CBE 30338"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[A.1.1 Some Python basics](https://jckantor.github.io/CBE30338/A.01-Python-Library-for-CBE30338.html#A.1.1-Some-Python-basics)",
"section": "A.1.1 Some Python basics"
}
},
"source": [
"## A.1.1 Some Python basics"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[A.1.1.1 Python functions](https://jckantor.github.io/CBE30338/A.01-Python-Library-for-CBE30338.html#A.1.1.1-Python-functions)",
"section": "A.1.1.1 Python functions"
}
},
"source": [
"### A.1.1.1 Python functions\n",
"\n",
"Here are essential things you need to know about Python functions:\n",
"\n",
"* Functions can be treated like Python objects\n",
"* Functions can be defined using either the `def` or `lambda` statements.\n",
"* `def` defines multiline functions that terminate and return values specified by the `return` statement.\n",
"* `lambda` defines single line functions that return a value.\n",
"\n",
"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)$\n",
"\n",
"\\begin{align}\n",
"\\frac{dx}{dt} & = v \\\\\n",
"\\frac{dv}{dt} & = \\frac{1}{m} f(t) \\\\\n",
"\\end{align}\n",
"\n",
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[A.1.1.1.1 Functions are Python objects](https://jckantor.github.io/CBE30338/A.01-Python-Library-for-CBE30338.html#A.1.1.1.1-Functions-are-Python-objects)",
"section": "A.1.1.1.1 Functions are Python objects"
}
},
"source": [
"#### A.1.1.1.1 Functions are Python objects\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"nbpages": {
"level": 4,
"link": "[A.1.1.1.1 Functions are Python objects](https://jckantor.github.io/CBE30338/A.01-Python-Library-for-CBE30338.html#A.1.1.1.1-Functions-are-Python-objects)",
"section": "A.1.1.1.1 Functions are Python objects"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[
"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}