# The loop does not take all values

13 views (last 30 days)
Utkarsh Mishra on 20 Sep 2020
Commented: Utkarsh Mishra on 23 Sep 2020 at 3:37
t=1;
coupj=1;
L = 4;
d = 3;
N_hole=2:2:-2;
tic
%for nume = 1:length(N_hole)
Nh = 2;%N_hole(nume)
N_UP=(L-Nh)/2;
N_DN = (L-Nh)/2;
%***************** Basis generation %************************************
%**************** Hamiltonian parameter ********************************
%pbc=1; % pbc=1 ==> PBC, pbc=0 RBC
%sz=0; %Subblock of sz for the computations
Sz0_space=factorial(L)/(factorial(N_UP)*factorial(N_DN)*factorial(Nh))
%***************************************************************************
%Whole basis computation
basis_spin=zeros(d^L,L);
for i1=1:1:L
ba=0;
for i2=1:1:d^L
basis_spin(i2,i1)=ba;
if mod(i2,d^(L-i1))==0
ba=mod(ba+1,d);
end
end
end
% Chagrge conservation and magnetization conservation
% first we count the number of 1's and 2's in each basis states from
% basis_spin. Then we set condition that if number of 1's = N_UP and number
% of 2's=N_DN then we store those basis from d^L basis states.
szbasis_Ne = zeros(Sz0_space,L);
basis_Ne=zeros(Sz0_space,L);
val=1;
for i2=1:d^L
Loca1 = basis_spin(i2,:)==1;
Loca2 = basis_spin(i2,:)==2;
if (sum(Loca1~=0,2)~=0 && sum(Loca2~=0,2)~=0 && sum(Loca1~=0,2)==N_UP && sum(Loca2~=0,2)==N_DN)
basis_Ne(val,:)=basis_spin(i2,:);
order_sz(1,val)=i2-1;
%order_sz_1(1,val) = bi2de(basis_Ne(val,:), 3,'left-msb');
val=val+1;
end
end
dimension = size(basis_Ne);
dimension = dimension(1);
dn_v1 =cell(L,1);
%***********************************************
for i1 = 1:L
cont1=1;
if i1==L
n=1; m=L;
else
n=i1; m=i1+1;
end
n
m
% Loop over the basis elements
for i2=1:dimension
if basis_Ne(i2,m)==2 && basis_Ne(i2,n)==0
%basis_Ne(i2,:)
%order_sz(i2)
basis_Ne(i2,n)=2;
basis_Ne(i2,m)=0;
%basis_Ne(i2,:)
aux_dn = bi2de(basis_Ne(i2,:), 3,'left-msb');
%aux_dn=order_sz(i2)+(-1)^P*value %Output vector in the whole basis
out_dn=find(order_sz==aux_dn); %Output vector position in the subblock
%We construct the matrix and the complex conjugate
dn_v1{i1}(cont1)=out_dn;
dn_vc1{i1}(cont1)=i2;
dn_w1{i1}(cont1)=i2;
dn_wc1{i1}(cont1)=out_dn;
cont1=cont1+1;
end
end
end
The code does not run for all i2 for i1=L. Could any one help me to figure it out?

Image Analyst on 20 Sep 2020
You need to learn how to debug. Once you do you will be fully enabled for figuring out these simple things on your own.
Just step through and look at what the variable values are and see what lines get executed. Seriously, that's all we would do if we had to do it for you.
Utkarsh Mishra on 20 Sep 2020
Yes ture. I am trying. But i posted here, in case I am missing very simple things which Ifail to pin point. This last loop should be simple. In fact, if I replace the i1 loop from i1=L:L, surprisingly, it works.
for i1 = 1:L
cont1=1;
if i1==L
n=1; m=L;
else
n=i1; m=i1+1;
end
n
m
% Loop over the basis elements
for i2=1:dimension
if basis_Ne(i2,m)==2 && basis_Ne(i2,n)==0
%basis_Ne(i2,:)
%order_sz(i2)
basis_Ne(i2,n)=2;
basis_Ne(i2,m)=0;
%basis_Ne(i2,:)
aux_dn = bi2de(basis_Ne(i2,:), 3,'left-msb');
%aux_dn=order_sz(i2)+(-1)^P*value %Output vector in the whole basis
out_dn=find(order_sz==aux_dn); %Output vector position in the subblock
%We construct the matrix and the complex conjugate
dn_v1{i1}(cont1)=out_dn;
dn_vc1{i1}(cont1)=i2;
dn_w1{i1}(cont1)=i2;
dn_wc1{i1}(cont1)=out_dn;
cont1=cont1+1;
end
end
end

per isakson on 20 Sep 2020
Edited: per isakson on 21 Sep 2020
I cannot reproduce the issue that you report. You didn't show us the error message! There is an error message?
First, I tidied your code a bit and added section breaks (%%). Now the code analyzer box is green, , and the section breaks allows me to run the code section by section,
%%
t=1;
coupj=1;
L = 4;
d = 3;
N_hole=2:2:-2;
Nh = 2;
N_UP=(L-Nh)/2;
N_DN = (L-Nh)/2;
%% ***************** Basis generation %************************************
%**************** Hamiltonian parameter ********************************
%pbc=1; % pbc=1 ==> PBC, pbc=0 RBC
%sz=0; %Subblock of sz for the computations
Sz0_space=factorial(L)/(factorial(N_UP)*factorial(N_DN)*factorial(Nh));
fprintf( 1, '\nSz0_space = %d\n\n', Sz0_space );
%***************************************************************************
%Whole basis computation
basis_spin=zeros(d^L,L);
for i1=1:1:L
ba=0;
for i2=1:1:d^L
basis_spin(i2,i1)=ba;
if mod(i2,d^(L-i1))==0
ba=mod(ba+1,d);
end
end
end
%% Chagrge conservation and magnetization conservation
% first we count the number of 1's and 2's in each basis states from
% basis_spin. Then we set condition that if number of 1's = N_UP and number
% of 2's=N_DN then we store those basis from d^L basis states.
szbasis_Ne = zeros(Sz0_space,L);
basis_Ne=zeros(Sz0_space,L);
val=1;
for i2=1:d^L
Loca1 = basis_spin(i2,:)==1;
Loca2 = basis_spin(i2,:)==2;
if sum(Loca1~=0,2)~=0 && sum(Loca2~=0,2)~=0 ...
&& sum(Loca1~=0,2)==N_UP && sum(Loca2~=0,2)==N_DN
basis_Ne(val,:)=basis_spin(i2,:);
order_sz(1,val)=i2-1; %#ok<SAGROW>
%order_sz_1(1,val) = bi2de(basis_Ne(val,:), 3,'left-msb');
val=val+1;
end
end
dimension = size(basis_Ne);
dimension = dimension(1);
dn_v1 = cell(L,1);
% %% ***********************************************
% for i1 = 1:L
% cont1=1;
% if i1==L
% n=1; m=L;
% else
% n=i1; m=i1+1;
% end
% fprintf( 1, '%d, %d\n', n, m );
%
% % Loop over the basis elements
% for i2=1:dimension
% if basis_Ne(i2,m)==2 && basis_Ne(i2,n)==0
% %basis_Ne(i2,:)
% %order_sz(i2)
% basis_Ne(i2,n)=2;
% basis_Ne(i2,m)=0;
% %basis_Ne(i2,:)
% aux_dn = bi2de(basis_Ne(i2,:), 3,'left-msb');
% %aux_dn=order_sz(i2)+(-1)^P*value %Output vector in the whole basis
% out_dn=find(order_sz==aux_dn); %Output vector position in the subblock
% %We construct the matrix and the complex conjugate
% dn_v1{i1}(cont1)=out_dn;
% dn_vc1{i1}(cont1)=i2;
% dn_w1{i1}(cont1)=i2;
% dn_wc1{i1}(cont1)=out_dn;
% cont1=cont1+1;
% end
% end
% end
%% Comment #2
for i1 = 1:L
cont1=1;
if i1==L
n=1; m=L;
else
n=i1; m=i1+1;
end
fprintf( 1, 'n = %d, m = %d\n', n, m );
% Loop over the basis elements
fprintf( 1, 'i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn\n' );
for i2=1:dimension
if basis_Ne(i2,m)==2 && basis_Ne(i2,n)==0
%basis_Ne(i2,:)
%order_sz(i2)
basis_Ne(i2,n)=2;
basis_Ne(i2,m)=0;
%basis_Ne(i2,:)
aux_dn = bi2de(basis_Ne(i2,:), 3,'left-msb');
fprintf( '%2d,%3d,%3d,%3d,%15d,%15d, %f\n' ...
,i1,i2,m,n,basis_Ne(i2,m),basis_Ne(i2,n),aux_dn' );
%aux_dn=order_sz(i2)+(-1)^P*value %Output vector in the whole basis
out_dn=find(order_sz==aux_dn); %Output vector position in the subblock
%We construct the matrix and the complex conjugate
dn_v1{i1}(cont1)=out_dn;
dn_vc1{i1}(cont1)=i2; %#ok<SAGROW>
dn_w1{i1}(cont1)=i2; %#ok<SAGROW>
dn_wc1{i1}(cont1)=out_dn; %#ok<SAGROW>
cont1=cont1+1;
else
fprintf( '%2d,%3d,%3d,%3d,%15d,%15d\n' ...
,i1,i2,m,n,basis_Ne(i2,m),basis_Ne(i2,n)' );
end
end
fprintf('\n');
end
This script outputs
Sz0_space = 12
n = 1, m = 2
i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn
1, 1, 2, 1, 0, 0
1, 2, 2, 1, 0, 0
1, 3, 2, 1, 1, 0
1, 4, 2, 1, 1, 0
1, 5, 2, 1, 0, 2, 55
1, 6, 2, 1, 0, 2, 57
1, 7, 2, 1, 0, 1
1, 8, 2, 1, 0, 1
1, 9, 2, 1, 2, 1
1, 10, 2, 1, 0, 2
1, 11, 2, 1, 0, 2
1, 12, 2, 1, 1, 2
n = 2, m = 3
i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn
2, 1, 3, 2, 1, 0
2, 2, 3, 2, 0, 2, 19
2, 3, 3, 2, 0, 1
2, 4, 3, 2, 2, 1
2, 5, 3, 2, 0, 0
2, 6, 3, 2, 1, 0
2, 7, 3, 2, 0, 0
2, 8, 3, 2, 0, 2, 45
2, 9, 3, 2, 0, 2
2, 10, 3, 2, 0, 0
2, 11, 3, 2, 1, 0
2, 12, 3, 2, 0, 1
n = 3, m = 4
i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn
3, 1, 4, 3, 2, 1
3, 2, 4, 3, 1, 0
3, 3, 4, 3, 0, 2, 15
3, 4, 4, 3, 0, 2
3, 5, 4, 3, 1, 0
3, 6, 4, 3, 0, 1
3, 7, 4, 3, 0, 2, 33
3, 8, 4, 3, 0, 0
3, 9, 4, 3, 0, 0
3, 10, 4, 3, 1, 0
3, 11, 4, 3, 0, 1
3, 12, 4, 3, 0, 0
n = 1, m = 4
i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn
4, 1, 4, 1, 0, 2, 57
4, 2, 4, 1, 1, 0
4, 3, 4, 1, 0, 0
4, 4, 4, 1, 0, 0
4, 5, 4, 1, 1, 2
4, 6, 4, 1, 0, 2
4, 7, 4, 1, 0, 1
4, 8, 4, 1, 0, 1
4, 9, 4, 1, 0, 1
4, 10, 4, 1, 1, 2
4, 11, 4, 1, 0, 2
4, 12, 4, 1, 0, 2

Utkarsh Mishra on 23 Sep 2020 at 2:18
Thanks. But for i=L:L, the output is different from i=1:L. This is the one I coult not figure out.
Sz0_space =
12
n = 1, m = 4
i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn
4, 1, 4, 1, 0, 2, 57
4, 2, 4, 1, 1, 0
4, 3, 4, 1, 0, 2, 63
4, 4, 4, 1, 0, 0
4, 5, 4, 1, 1, 0
4, 6, 4, 1, 0, 0
4, 7, 4, 1, 2, 1
4, 8, 4, 1, 0, 1
4, 9, 4, 1, 0, 1
4, 10, 4, 1, 1, 2
4, 11, 4, 1, 0, 2
4, 12, 4, 1, 0, 2
per isakson on 23 Sep 2020 at 3:27
I've modified the print-statements so that the first part is printed directly before the if-statement.
% Loop over the basis elements
fprintf( 1, 'i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn\n' );
for i2=1:dimension
fprintf( '%2d,%3d,%3d,%3d,%15d,%15d,' ...
,i1,i2,m,n,basis_Ne(i2,m),basis_Ne(i2,n) );
if basis_Ne(i2,m)==2 && basis_Ne(i2,n)==0
%basis_Ne(i2,:)
%order_sz(i2)
basis_Ne(i2,n)=2;
basis_Ne(i2,m)=0;
%basis_Ne(i2,:)
aux_dn = bi2de(basis_Ne(i2,:), 3,'left-msb');
fprintf( '%7d\n', aux_dn );
%aux_dn=order_sz(i2)+(-1)^P*value %Output vector in the whole basis
out_dn=find(order_sz==aux_dn); %Output vector position in the subblock
%We construct the matrix and the complex conjugate
dn_v1{i1}(cont1)=out_dn;
dn_vc1{i1}(cont1)=i2; %#ok<SAGROW>
dn_w1{i1}(cont1)=i2; %#ok<SAGROW>
dn_wc1{i1}(cont1)=out_dn; %#ok<SAGROW>
cont1=cont1+1;
else
fprintf('\n');
end
end
Now I get the following to outputs for i1==4 .
>> n = 1, m = 4
i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn
4, 1, 4, 1, 2, 0, 57
4, 2, 4, 1, 1, 0,
4, 3, 4, 1, 0, 0,
4, 4, 4, 1, 0, 0,
4, 5, 4, 1, 1, 2,
4, 6, 4, 1, 0, 2,
4, 7, 4, 1, 0, 1,
4, 8, 4, 1, 0, 1,
4, 9, 4, 1, 0, 1,
4, 10, 4, 1, 1, 2,
4, 11, 4, 1, 0, 2,
4, 12, 4, 1, 0, 2,
n = 1, m = 4
i1, i2, m, n, basis_Ne(i2,m), basis_Ne(i2,n), aux_dn
4, 1, 4, 1, 2, 0, 57
4, 2, 4, 1, 1, 0,
4, 3, 4, 1, 2, 0, 63
4, 4, 4, 1, 0, 0,
4, 5, 4, 1, 1, 0,
4, 6, 4, 1, 0, 0,
4, 7, 4, 1, 2, 1,
4, 8, 4, 1, 0, 1,
4, 9, 4, 1, 0, 1,
4, 10, 4, 1, 1, 2,
4, 11, 4, 1, 0, 2,
4, 12, 4, 1, 0, 2,
Yes, the two differs on the third line. And that's because the condition of the if-statement returns false in the first case and true in the second. And most likely that is because in the first iteration of the for-loop basis_Ne is modified inside the if-clause by the two statements
basis_Ne(i2,n)=2;
basis_Ne(i2,m)=0;
(you might need to read this twice).
Utkarsh Mishra on 23 Sep 2020 at 3:37
Yes, now I got it.