Skip to content

Commit

Permalink
Merge pull request #47 from ICB-DCM/fixes_fabi
Browse files Browse the repository at this point in the history
visual output for testgradient
  • Loading branch information
Fabian Fröhlich authored Mar 2, 2017
2 parents bfe9ddf + f89d908 commit 702334e
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions testGradient.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
else
ig = 2;
end

if nargin >= 6
fplot = varargin{6};
else
fplot = false;
end


theta = theta(:);

if(~ischar(ig))
Expand Down Expand Up @@ -174,3 +182,95 @@
eval(['g_fd_c(' repmat(':,',1,numel(size(g))-1) 'i) = (l_i_f-l_i_b)/(2*eps);'])
end
end

if(fplot)
figure
subplot(2,2,1)
scatter(abs(g_fd_f(:)-g_fd_c(:)),abs(g(:)-g_fd_c(:)),'rx')
hold on
scatter(abs(g_fd_b(:)-g_fd_c(:)),abs(g(:)-g_fd_c(:)),'bo')
set(gca,'YScale','log')
set(gca,'XScale','log')
e = [abs(g_fd_f(:)-g_fd_c(:));abs(g_fd_b(:)-g_fd_c(:));abs(g(:)-g_fd_c(:))];
mine = min(e(e>0))*0.5;
maxe = max(e(e>0))*2;
xlim([mine,maxe])
ylim([mine,maxe])
plot([mine,maxe],[mine,maxe],'k:');
xlabel('FD precision (upper bound)')
ylabel('difference |Gradient-FD|')
axis square
box on
legend('forward FD','backward FD','Location','NorthWest')

subplot(2,2,2)
scatter(abs(g(:)),abs(g(:)-g_fd_c(:)),'rx')
hold on
scatter(abs(g_fd_c(:)),abs(g(:)-g_fd_c(:)),'bo')
set(gca,'YScale','log')
set(gca,'XScale','log')
e = [abs(g_fd_f(:)-g_fd_c(:));abs(g(:));abs(g_fd_c(:))];
mine = min(e(e>0))*0.5;
maxe = max(e(e>0))*2;
xlim([mine,maxe])
ylim([mine,maxe])
plot([mine,maxe],[mine,maxe],'k:');
legend('Gradient','FD','Location','NorthWest')
xlabel('derivative value')
ylabel('difference |Gradient-FD|')
axis square
box on

subplot(2,2,3)
scatter(abs(g_fd_f(:)-g_fd_c(:)),abs(g(:)./g_fd_c(:)),'rx')
hold on
scatter(abs(g_fd_b(:)-g_fd_c(:)),abs(g(:)./g_fd_c(:)),'bo')
set(gca,'YScale','log')
set(gca,'XScale','log')
e = [abs(g_fd_f(:)-g_fd_c(:));abs(g_fd_b(:)-g_fd_c(:));abs(g(:)-g_fd_c(:))];
mine = min(e(e>0))*0.5;
maxe = max(e(e>0))*2;
r = [abs(g(:)./g_fd_c(:))];
minr = min(r(r>0))*0.5;
maxr = max(r(r>0))*2;
xlim([mine,maxe])
try
ylim([minr,maxr])
catch
ylim([1e-1,1e1])
end
plot([mine,maxe],[1,1],'k:');
xlabel('FD precision (upper bound)')
ylabel('ratio Gradient/FD')
axis square
box on
legend('forward FD','backward FD','Location','SouthEast')

subplot(2,2,4)
scatter(abs(g(:)),abs(g(:)./g_fd_c(:)),'rx')
hold on
scatter(abs(g_fd_c(:)),abs(g(:)./g_fd_c(:)),'bo')
set(gca,'YScale','log')
set(gca,'XScale','log')
e = [abs(g_fd_f(:)-g_fd_c(:));abs(g(:));abs(g_fd_c(:))];
mine = min(e(e>0))*0.5;
maxe = max(e(e>0))*2;
r = [abs(g(:)./g_fd_c(:))];
minr = min(r(r>0))*0.5;
maxr = max(r(r>0))*2;
xlim([mine,maxe])
try
ylim([minr,maxr])
catch
ylim([1e-1,1e1])
end
plot([mine,maxe],[1,1],'k:');
legend('Gradient','FD','Location','SouthEast')
xlabel('derivative value')
ylabel('ratio Gradient/FD')
axis square
box on
end



0 comments on commit 702334e

Please sign in to comment.