Skip to content

Instantly share code, notes, and snippets.

@terjehaukaas
Created June 12, 2019 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terjehaukaas/6826be0b8bacb9dc4c31ced14db1a7c6 to your computer and use it in GitHub Desktop.
Save terjehaukaas/6826be0b8bacb9dc4c31ced14db1a7c6 to your computer and use it in GitHub Desktop.
dg()
# ------------------------------------------------------------------------
# The following function is implemented in Python by Professor Terje Haukaas
# at the University of British Columbia in Vancouver, Canada. It is made
# freely available online at terje.civil.ubc.ca together with notes,
# examples, and additional Python code. Please be cautious when using
# this code; it may contain bugs and comes without any form of warranty.
#
# The following definitions apply:
# g(x) = limit-state function in the original x-space of random variables
# dgdx(x) = gradient vector of the limit-state function in the original space
# ------------------------------------------------------------------------
def dg(x, gValue, gFunction):
numRV = len(x)
dgdx = np.zeros(numRV)
for j in range(numRV):
# Take a backup of this random variable
backup = x[j]
# Set the perturbation as a fraction of the x-value
dx = 0.001
if x[j] != 0.0:
dx = dx * x[j]
# Perturb this random variable
x[j] = backup + dx
# Re-evaluate the limit-state function
gPerturbed = gFunction(x)
# Re-set the random variable
x[j] = backup
# Calculate the sought derivative
dgdx[j] = (gPerturbed - gValue) / dx
return dgdx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment