15 - Exam Review (Python)#

This is a short writeup of some of the key points that we have covered in the course thus far. You should be familiar with these points.

Motivation#

  • What specific problems are commonly solved in chemical engineering?

  • What are some societal problems that chemical engineers are solving?

Functions#

  • How to import modules

  • How to call a function with arguments

  • How to define a function with arguments

  • How and why to use default values for arguments

  • Understand what a local and global variable

  • Understand if statements and how to use them

For example, how would you make a function to calculate the area of a circle with a default value of 3?

Objects#

  • Understand what a class and object are

  • How to specify a class and create an object with the param package

  • Understand the benefits of using classes and objects

For example, in Homework 9, you created a model to estimate the time that your body temperature would fall to 35 C from 37 C. You used odeint to determine the time that would take given the balance on the energy change of the body with heat being lost due to radiation. How would you incorporate that into an object?

Debugging#

  • Understand difference between syntax, runtime, and logic errors

  • Understand the debugging approaches for each error type

  • Know about debugging tools such as print statements and simplifying code

Units#

  • Understand dimensional analysis

  • Understand how to track units through your code by using SI or other consistent units

  • Understand how to work with english units in your code including gc = 32.174 lbm-ft/(lbf-s^2)

For example, if I have the following code, what units is P in?

V = 1 #m^3/mol
T = 300 #K
R = 8314 #L-Pa/(mol-K)
P = R*T/V

Equations#

  • Understand how to solve equations using linear algebra (Ax = b)

  • Understand how to solve equations using root finding like fsolve (f(x) = 0)

  • Understand how to use sympy

  • Understand how to solve equations using optimization (minimize f(x))

  • Understand the difference between coupled and uncoupled equations and linear and non-linear

  • Understand what an array is

For example, the following system of equations can be solved any number of ways.

\[\begin{split} 2x + y + q = 4 \\ x + z = 5 \\ y + z = 6 \\ q + x + y = 10 \end{split}\]

How would you go about solving the above using linear algebra?

ODE’s and Derivatives#

  • Understand how to solve derivatives using symbolic calculus with sympy

  • Understand how to solve a differential equation using Euler’s method and odeint

For example if you have the following differential equation (typical of a second order reaction), how would you solve it?

\[ \frac{dx}{dt} = -kx^2 \]

Loops#

  • Understand how to use for and while loops

For example, the sequence of triangular numbers is generated by the following formula:

\[ T_n = \frac{n(n+1)}{2} \]

Lets say you have an existing array of T = (0,1,3,6) and you want to add the next 10 triangular numbers to the array. How would you do that? The sum of the inverse of the Triangular array is a converging series, what is your estimate of the value of that converging series based on the 14 numbers in your array?

Regression#

  • Understand how to use regression to fit a curve to data using scipy curve_fit and scipy minimize

  • Understand how to use interpolation to estimate data between points

  • Understand what pandas is and how to import data from a csv file

How would you use curve_fit to fit a parabolic curve to the following data?

\[\begin{split} x = [1,2,3,3,4,5,5] \\ y = [1.23,3.9,9.3,9.1,16.3,25.5,26] \end{split}\]

How would you use minimize to do the same thing?

File I/O#

Understand how to read and write files in Python

Key#

Here’s a link to the key for the examples on this page: clint-bg/comptools