Skip to content

Instantly share code, notes, and snippets.

@dnmiller
Created August 6, 2015 03:47
Show Gist options
  • Save dnmiller/dfd5bd2db663be5bf4b0 to your computer and use it in GitHub Desktop.
Save dnmiller/dfd5bd2db663be5bf4b0 to your computer and use it in GitHub Desktop.
Test analytical derivatives
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