# Gradient of a function in Python

I've defined a function in this way:

def qfun(par):
return(par[0]+atan(par[3])*par[1]+atan(par[4])*par[2])



How can I obtain the gradient of this function for only some of the elements (par [0:2]) in a specific point? I only find functions with only one "x", so for those cases it is simple, but when your function has more parameters what should I do?

Several options:

• You can use the defintion of the derivative to have an approximation....

def f(x):
return x[0]**2 + 3*x[1]**3

def der(f, x, der_index=[]):
# der_index: variable w.r.t. get gradient

epsilon = 2.34E-10

for idx in der_index:
x_ = x.copy()
x_[idx]+=epsilon