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 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: Simple truss
Input: Simple frame
Input: Simple Quad4 problem
Input: Parameterized Quad4 cantilever
Function: Stiffness matrix for 2D truss element
Function: Stiffness matrix for 2D frame element
Function: Stiffness matrix for Quad4 element
Function: Assemble structural stiffness
Linear static structural analysis (with P-delta and shear deformation)
Plot beam diagrams (w, θ, M, V)
MDOF buckling analysis (eigenvalues/vectors)
Input: Open cross-section
Input: Cross-section with one cell
Input: Cross-section with one cell and flanges
Input: Cross-section with two cells
Cross-section analysis (yo, zo, A, Iy, Iz, J, Cw)
Ground acceleration: x-direction
Ground acceleration: y-direction
Ground acceleration: z-direction
Function: SDOF Newmark algorithm
Function: Trapezoidal integration
SDOF dynamic analysis (input/output spectra)
MDOF dynamic analysis (earthquake)
Function: Finite difference for single-variable objective function
Function: Finite difference for multi-variable objective function
Function: Golden section line search algorithm
Function: Newton line search algorithm
Function: Bisection line search algorithm
Function: Secant line search algorithm
Function: Armijo line search algorithm
Function: Steepest descent search direction
Function: Conjugate gradient search direction
Function: Quasi Newton search direction
Downhill simplex optimization analysis
Directional line search optimization analysis
1D optimization analysis
Input: Random-variables-and-limit-state function
Functions: Modify correlation matrix
Function: Transform y to x
Function: Finite difference for limit-state function
FORM analysis
Sampling 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