Instead of developing pre-packaged computer programs we should develop libraries of algorithms that can be shared, understood, improved, and used in ad hoc programs. This page presents a few algorithms, implemented in Python. It is perhaps fun to observe how algorithms play an increasingly large role in our moden society. Elon Musk suggests that artificial intelligence algorithms could become a danger to the public. Yuval Noah Hari discusses the role of algorithms, including the ones in your brain, in the book Homo Deus. The joke that the word “algorithm” stems from Al Gore is funny but of course not true, although he promoted the public use of ARPANET, which is now the Internet. Rather, the word algorithm stems from the Persian mathematician Muhammad bin Musa al-Khwarizmi who was born in 780 and worked at the House of Wisdom in Baghdad. Euclid’s Algorithm, probably the earliest known algorithm, is from before that time and attributed to Euclid, born in 335 BC. He devised an algorithm, i.e., systematic approach, for determining the greatest common divisor of two integers.

Python is a free scripting language, similar to Matlab, used in several courses taught by Professor Haukaas at UBC Vancouver. In contrast to compiled languages, such as C++, a Python program compiles during run-time. This makes a scripting language it slower than compiled programs, but it makes experimentation with new code easier. There are two ways to get things done with Python. You can run it as a calculator by entering statements directly in a command prompt window, or you can run input files that you prepare in a text editor. The latter is the preferred approach and several Integrated Developer Environments (IDEs) are available to help you edit and execute Python files, i.e., “.py” files. Python is only useful in engineering applications when certain external packages are added to it. In the courses taught by Professor Haukaas we use three such libraries pf functions; namely, NumPy for vectors, matrices, linear algebra, etc., SciPy for probability distributions, etc., and Matplotlib for plotting. You can install those three packages using PyCharm. To get started:

  • Install the latest version of Python3 from
  • Install the IDE called PyCharm from
  • In PyCharm, select File > New Project and give it a file path and a name
  • In PyCharm, select Preferences to open the settings dialog box for the project
  • On the left-hand side of that dialog box, select the Project: (name) tab and specifically the Project Interpreter
  • Now on the right-hand side, make sure that some version of Python3 is selected as Project Interpreter
  • In the same dialog box, press + near the bottom to install NumPy, SciPy, and Matplotlib, and then close the dialog box
  • In PyCharm, right-click on the project at the top of the left-most pane and select New > Python file and give it a name
  • Select Python code from below and enter it in that file; at any time, right-click anywhere in the file to run it

The following library of algorithms written in Python may get you started. At the top of each file that you store this code in, you should typically include the following packages: “import numpy as np” / “import matplotlib.pyplot as plt” / “from scipy.stats import norm, lognorm, uniform”. Several functions are posted below, and they are called like this: fcns.steepestDescentSearchDirection(), assuming they have been stored in a separate file and imported by “import fileWithFunctions as fcns”.

Input for simple truss
Input for simple frame
Input for simple Quad4 problem
Input for parameterized Quad4 cantilever
Stiffness matrix for 2D truss element
Stiffness matrix for 2D frame element
Stiffness matrix for Quad4 element
Loop over elements
Assemble stiffness matrix for full structure
Assemble load vector for full structure
Introduce boundary conditions and remove zero-stiffness DOFs
Solve the system of equations and get ua
Recover element forces
Print axial forces in preparation for P-delta analysis
Plot deformed structure
Plot beam diagrams
MDOF buckling analysis
Input for open cross-section
Input for cross-section with one cell
Input for cross-section with one cell and flanges
Input for cross-section with two cells
Plot cross-section
Identify cells and flanges
Area and centroid-location
Moments of inertia
St. Venant torsion constant
Warping torsion constant
Input for multi-variable optimization
Downhill simplex algorithm
Directional line search algorithm
Finite difference for multi-variable objective function
Steepest descent search direction
Conjugate gradient search direction
Quasi Newton search direction
Input for 1D optimization
Finite difference for single-variable objective function
Golden section line search algorithm
Newton line search algorithm
Bisection line search algorithm
Secant line search algorithm
Armijo line search algorithm
Input for FORM analysis
Input for sampling analysis
Cholesky decomposition of modified correlation matrix
Modify correlation matrix
Transform y to x
Finite difference for limit-state function
FORM algorithm
Sampling algorithm
x-direction ground acceleration
y-direction ground acceleration
z-direction ground acceleration
SDOF Newmark algorithm
MDOF Newmark algorithm
Trapezoidal integration
Read and plot ground motions
Calculate and plot SDOF response
Calculate and plot response spectra
MDOF dynamic analysis

a place of mind, The University of British Columbia

Department of Civil Engineering
6250 Applied Science Lane,
Vancouver, BC, V6T 1Z4, Canada
Dr. Terje Haukaas
Tel: 604-827-5557

Emergency Procedures | Accessibility | Contact UBC  | © Copyright The University of British Columbia