python - Calculating gradient with NumPy -


i can not understand numpy.gradient function , how use computation of multivariable function gradient.

for example, have such function:

def func(q, chi, delta):     return q * chi * delta 

i need compute it's 3-dimensional gradient (in other words, want compute partial derivatives respect variables (q, chi, delta)).

how can calculate gradient using numpy?

the problem is, numpy can't give derivatives directly , have 2 options:

with numpy

what have do, define grid in 3 dimension , evaluate function on grid. afterwards feed table of function values numpy.gradient array numerical derivative every dimension (variable).

example here:

from numpy import *  x,y,z = mgrid[-100:101:25., -100:101:25., -100:101:25.]  v = 2*x**2 + 3*y**2 - 4*z # random function potential  ex,ey,ez = gradient(v) 

without numpy

you calculate derivative using centered difference quotient. centered difference quotient

this essentially, numpy.gradient is doing every point of predefined grid.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -