diff --git a/ex1/grad_check.m b/ex1/grad_check.m index cb18e46..5cddeb5 100644 --- a/ex1/grad_check.m +++ b/ex1/grad_check.m @@ -6,13 +6,18 @@ fprintf(' Iter i err'); fprintf(' g_est g f\n') + % same for all iterations; moved outside loop + T = theta0; + [f,g] = fun(T, varargin{:}); + + % could do randsample(numel(T), num_checks), but this is more Octave-compatible + shuffle = randperm(numel(T)); + for i=1:num_checks - T = theta0; - j = randsample(numel(T),1); + j = shuffle(i); % randsample(n, 1) == sampling WITH replacement... T0=T; T0(j) = T0(j)-delta; T1=T; T1(j) = T1(j)+delta; - [f,g] = fun(T, varargin{:}); f0 = fun(T0, varargin{:}); f1 = fun(T1, varargin{:}); @@ -20,9 +25,9 @@ error = abs(g(j) - g_est); fprintf('% 5d % 6d % 15g % 15f % 15f % 15f\n', ... - i,j,error,g(j),g_est,f); + i,j,error,g_est,g(j),f); % from xuewei4d's ticket sum_error = sum_error + error; end - average=sum_error/num_checks; + average_error=sum_error/num_checks;