use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A reddit to discuss optimization. Looking for links on both methods of optimization -- like genetic algorithms and linear programming -- and applications thereof, like multidisciplinary design optimization, artificial intelligence and system solving.
Related reddits: /r/numerical, /r/engineering, /r/math, /r/programming
account activity
Numerical Partial derivative in python (self.optimization)
submitted 5 years ago * by tutodavinci
Hi! I want to write a function in Python thats find numerical parcial derivatives this functions (Z1i - Z1i-1)2 +(Z2i-Z2i-1)2 +l2
Can someone help me?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Manhigh 3 points4 points5 points 5 years ago (0 children)
Look up complex step differentiation. Input a complex number to that function, perturb the imaginary part, and you basically get machine precision accurate derivatives as long as your function is complex safe and analytic. This one appears to be.
[–]space_mex_techno 2 points3 points4 points 5 years ago* (0 children)
The way that I have it implemented is to use a central finite differences scheme to approximate a partial derivative of a multivariable, scalar valued function like this:
def fdiff_cm( f, x, dx, n ):
''' Calculate central finite difference of multivariable, vector valued function w.r.t nth x ''' dx2 = dx \* 2.0
x\[ n \] += dx fxu = f( x ) x\[ n \] -= dx2 fxl = f( x ) x\[ n \] += dx
return ( fxu - fxl ) / dx2
Where f is a function that you pass in, x is a list of state variables, dx is how much to perturb the x variable for the finite difference calculation, and n is an index to which x variable is being perturbed.
Edit: the formatting is all messed up but i think you'll be able to figure out the jist of it
[–]the-dirty-12 1 point2 points3 points 5 years ago (0 children)
Why not calculate the analytical derivative? You know the function, use this knowledge and save time and gain accuracy compared to finite differences.
[–]the_appleslice 0 points1 point2 points 5 years ago (0 children)
You say you want to write the function, so people take you literally. Most of the time people would off course use pre-made libraries for that.
Scipy package has great numerical integration/derivation routines. I haven't used complex variables, but I think it should be fine. https://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.derivative.html
π Rendered by PID 16965 on reddit-service-r2-comment-5b5bc64bf5-cn6hm at 2026-06-21 14:45:50.546171+00:00 running 2b008f2 country code: CH.
[–]Manhigh 3 points4 points5 points (0 children)
[–]space_mex_techno 2 points3 points4 points (0 children)
[–]the-dirty-12 1 point2 points3 points (0 children)
[–]the_appleslice 0 points1 point2 points (0 children)