%% Visualize the subgradient of the TV functional % Copyright by Björn Bringmann %Create Matrix K=spdiags([-ones(4,1) ones(4,1)],0:1,4,4); K=full(K); K(4,4)=0; %Now compute subdifferential at v==0 %Notice that the last component plays no role, we have to create 8 vectors %with +-1 in each of the first three components vertices=zeros(4,8); for i=0:7 vertices(1,i+1)=(-1)^(floor(i/4)); vertices(2,i+1)=(-1)^(floor(i/2)); vertices(3,i+1)=(-1)^(i); end %Now apply K' vertices=K'*vertices; %Compute Principal Components of K' [U,S,V]=svd(K'); %Compute new coordinates by using the scalar products with the principal %components coordinates=zeros(3,8); for i=1:3 for j=1:8 coordinates(i,j)=U(:,i)'*vertices(:,j); end end %Now plot the polyhedron using patches %Create a list of nodes which have to be connected connect=[1,3,7,5; 2,4,8,6; 5,7,8,6; 1,3,4,2; 1,5,6,2; 3,7,8,4; ]; n=size(connect,1); figure(1) f=patch('vertices',coordinates','faces',connect,'facecolor','r','LineWidth',2); view(3) grid on; title('Subdifferential at (0,0,0,0)') %Now compute subdifferential at v=(0,1,1,1) %At lot of this is just copy&paste v=[0;1;1;1]; e=K*v; vertices_=zeros(4,4); vertices_(1,:)=ones(1,4); for i=0:3 vertices_(2,i+1)=(-1)^(floor(i/2)); vertices_(3,i+1)=(-1)^(i); end vertices_=K'*vertices_; coordinates_=zeros(3,4); for i=1:3 for j=1:4 coordinates_(i,j)=U(:,i)'*vertices_(:,j); end end connect_=[1,2,4,3]; m=size(connect_,1); figure(2); g=patch('vertices',coordinates','faces',connect,'facecolor','none','LineWidth',2); view(3) title('Subdifferential at (0,1,1,1)') grid on; h=patch('vertices',coordinates_','faces',connect_,'facecolor','b'); hold off;