Created
August 6, 2015 03:47
-
-
Save dnmiller/dfd5bd2db663be5bf4b0 to your computer and use it in GitHub Desktop.
Test analytical derivatives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function maxeps = difftest(func, grad, theta, tol) | |
if nargin < 4 || isempty(tol) | |
tol = 1e-6; | |
end | |
function res = err(d) | |
fp = func(theta + d); | |
fm = func(theta - d); | |
res = max(abs((fp - fm)/2/d - grad(theta))); | |
end | |
maxeps = 1e20*eps; | |
while err(maxeps) < tol | |
maxeps = maxeps / 2; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment