With just how much numerical detail MATLAB hides in things like its optimization functions, I'm amazed there's no simple way to numerically differentiate a function at a single point.
8 15 Apr 2016 04:36 by u/scorinth
I don't understand why MATLAB can happily compute approximate derivatives until the cows come home with fmincon, but if I just want to find a derivative of a function at a point, I have to pull out the symbolic toolkit.
8 comments
2 u/Grospoliner 15 Apr 2016 07:16
Built by matrices for matrices.
2 u/CanIHazPhD 15 Apr 2016 14:29
Why dont you do it manually? something like
x=0; %or any other point where the function is well behaved
h=1e-5; %experiment with this parameter
d=(f(x+h)-f(x-h))/(2*h);
You could also use the forward or backwards difference, but those converge at a rate h, while the central difference converges at h^2
1 u/scorinth [OP] 15 Apr 2016 17:01
Honestly, I didn't do that because I was rushing to get some homework done before midnight and my brain was already pretty well fried by end-of-semester projects and studying.
If I had a bit more time to work on it (or sleep!) I wouldn't have had a word of complaint, probably.
0 u/CanIHazPhD 15 Apr 2016 17:39
I've been there. Anyway, you should code something like this for the future. You could try a program that calculates the derivative with several values of h and then average them for better accuracy (and a lot less speed), just be careful about very small values of h, as x+h and x-h could not be machine representable, thus giving you a large error when dividing by (2*h)
0 u/chrimata 15 Apr 2016 09:16
You need two points at least, to take a finite difference of something
1 u/scorinth [OP] 15 Apr 2016 16:59
Yes, but MATLAB already implements several algorithms that silently generate points about the given point and estimate the derivative at that point. It's frustrating to discover that they haven't packaged those into the "gradient" function, for example.
0 u/chrimata 15 Apr 2016 18:54
That doesn't make sense to me, either it's not the derivative or it's using points from the original function
1 u/scorinth [OP] 15 Apr 2016 19:01
Well, it's a numerical method, so it's never exactly the derivative, but it's an arbitrarily accurate estimate at most points. And it works by evaluating the function at several points that are very close to the point where you want the derivative, so it's like approximating the derivative by finding the slope of a chord line.