Professional Documents
Culture Documents
Python For Chemical Engineering v1.0
Python For Chemical Engineering v1.0
Using Python
Zulfan Adi Putra, PDEng, PEng
Internal
Outline
q Resources and Introduction to Python
q Python Basics: Conditionals, Functions, Loops, Arrays, Generate Plots, Solve Equations
q Linear Algebra
q Machine Learning
q Optimization
Internal
Python Resources
Internal
Installation through Anaconda
• https://www.anaconda.com/products/individual
Internal
Python Important Libraries
Internal
Youtube Intro for Beginners in Python
• https://www.youtube.com/watch?v=rfscVS0vtbw&t=4669s
Internal
Other Helps
1
2
Internal
Python Basics
Internal
Spyder Working folder
address
Working
directory All files
Tab file available in
the working
folder
Created variables.
Run (F5), then check your
variables here
Console
Internal
Python Programming Basics
• https://apmonitor.com/che263/index.php/Main/PythonBasics
Internal
Some Basics
(not to memorize, but you’ll see them when you make any error)
• Case-sensitive
• Indentation-sensitive
• Numbering starts from 0
• Code and run line by line for easier debugging. Use “print()” to check
• Use comment “#” often
•…
Internal
Some Basics: Data plotting using numpy and matplotlib
Internal
Another Example
Internal
Some Basics: simple calculation
Internal
Some Basics: For loop
Internal
Some Basics: While loop
• “while” loop
Internal
Some Basics: Range()
• “range()” function
Internal
Some Basics: Define function
Internal
Some Basics: Function + IF-Else
sqrt() is a built-in
function within “math”
package
Internal
Some Basics: Function + If-Else + While
Internal
Linear Algebra
Internal
https://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html
Linear Algebra
x + 3y + 5z = 10 1
2
3 5 x 10
5 1 y 8
2x + 5y + z = 8
2 3 8 z 3
2x + 3y + 8z = 3 x 1 3 5
1
10
y 2 5 1 8
z 2 3 8 3
Internal
Linear Algebra with Gekko
3x + 2y = 1
x + 2y = 0
Internal
Solving Ordinary Differential
Equations (ODE) with Python
Internal
Runge Kutta method
• https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
Internal
Pressure Profile In A Vessel
Internal
https://scipy-
Zombie Population cookbook.readthedocs.io/items/Zombie
_Apocalypse_ODEINT.html
Internal
Zombie Population: Vary initial conditions
Internal
Results
Internal
SEIR model = ODE + Curve Fitting for Parameter Optimization
Internal
Define necessary parameters
Internal
Define more parameters
Internal
Define functions
Internal
Data fitting with data to obtain model parameters
Results:
Internal
Using obtained model parameters to predict the trend
Internal
Extending the prediction
Internal
Further predictions
Internal
Solving Partial Differential
Equations (PDE) with Python
Internal
Partial Differential Equation
North
West East
South
ci , j
Linear Algebra:
Ax = b c
i 1, j
coeff 1 1 h 2k 2 h 2k 2 ci 1, j 0
c
i , j 1
ci , j 1
Node = j*(M+1) + i, with M = 15, N = 15
node = 255
node = 240
i=0 i=M
j=N j=N
i=0 i=M
j=0 j=0
node = 16
node = 1
node = 2 node = 3 node = 15
The Code
C = 0 or matrix b = 0 in all
sides, except at i = 0 (west)
Internal
Linear Regression with Polyfit and Linregress
Internal
Regressions with Gekko
Internal
Machine Learning with Python
Internal
Black Box Machine Learning
Independent Dependent
variables, e.g. ML variables, e.g.
P, T, F
Internal
Machine Learning using NN (DME case)
Internal
Internal
Print and plot results
Internal
Machine Learning Using XGBoost (DME case)
Internal
Internal
Internal
Optimization with Python
Internal
Optimization and Root Finding
min f x, y, z
subject to :
hi x , y , z 0
g j x, y, z 0
x R
y 0 ,1
z I
Internal
Optimization and Root Finding https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html
Internal
Optimization with Gekko https://gekko.readthedocs.io/en/latest/quick_start.html
Internal
Linear Programming with linprog https://realpython.com/linear-programming-python/
Internal
Internal
Linear Programming
“linprog” package ONLY receives general optimization formulation
Internal
Project Scheduling with MIP
Internal
Dummy example
Internal
Model
Internal
Coding the model in Python (1)
Data input
Variables declaration
Internal
Coding the model in Python (2)
General model
Internal
Coding the model in Python (3)
Internal
Coding the model in Python (4)
results printed
here
Project Scheduling Result
Case 1: No MAX total cost constraint
Case 2a: MAX total cost until time 2 (3 period) is 15 AND forcing Project 1 to start earlier
Internal
Non Linear Optimization (unconstrained)
Internal
Non Linear Optimization (Constrained)
Internal
Run and print the result
Internal
Stochastic Optimization (1)
Internal
Stochastic Optimization (2)
Internal
Monte Carlo Simulation with Machine Learning model
(checking inherent randomness in machine learning)
Internal
Monte Carlo simulation loop
Internal
Internal