%
%  rectangular_plate_constraints.m  ver 1.1  September 5, 2012
%
function[constraint_matrix,ibc,e1,e2,e3,e4]=rectangular_plate_constraints(nx,ny,ibc)
%
e1=1;
e2=1;
e3=1;
e4=1;
%
if(ibc==2)
    constraint_matrix(1,:)=[1 1 0 0]; 
    constraint_matrix(2,:)=[nx 1 0 0];  
    constraint_matrix(3,:)=[(nx*(ny-1)+1) 1 0 0];  
    constraint_matrix(4,:)=[nx*ny 1 0 0];  
    e4=999;
end
%
if(ibc==3)
    constraint_matrix(1,:)=[1 1 1 1]; 
    constraint_matrix(2,:)=[nx 1 1 1];  
    constraint_matrix(3,:)=[(nx*(ny-1)+1) 1 1 1];  
    constraint_matrix(4,:)=[nx*ny 1 1 1];  
    e4=999;
end
%
fixed=[1 1 1]; 
RY_only=[1 0 1];   
RX_only=[1 1 0];   
%
icn=1;
if(ibc==4)
%    
    disp(' ');
    disp(' Select BC for bottom edge ');
    disp(' 1=free   2=simply supported   3=fixed ');
    e1=input(' ');
    if(e1==2)
        for i=1:nx
            constraint_matrix(icn,:)=[i RX_only];
            icn=icn+1;
        end        
    end
    if(e1==3)
        for i=1:nx 
            constraint_matrix(icn,:)=[i fixed];
            icn=icn+1;
        end
    end
%
    disp(' ');
    disp(' Select BC for right edge ');
    disp(' 1=free   2=simply supported   3=fixed ');
    e2=input(' ');
    if(e2==2)
        ijk=nx;
        for j=1:ny
            constraint_matrix(icn,:)=[ijk RY_only];
            icn=icn+1;           
            ijk=ijk+nx;
        end        
    end
    if(e2==3)
        ijk=nx;
        for j=1:ny
            constraint_matrix(icn,:)=[ijk fixed];
            icn=icn+1;           
            ijk=ijk+nx;
        end      
    end    
%
    disp(' ');
    disp(' Select BC for top edge ');
    disp(' 1=free   2=simply supported   3=fixed ');
    e3=input(' ');
    if(e3==2)     
        for i=(nx*(ny-1)+1):(nx*ny) 
            constraint_matrix(icn,:)=[i RX_only];
            icn=icn+1;
        end        
    end
    if(e3==3)  
        for i=(nx*(ny-1)+1):(nx*ny) 
            constraint_matrix(icn,:)=[i fixed];
            icn=icn+1;
        end
    end    
%
    disp(' ');
    disp(' Select BC for left edge ');
    disp(' 1=free   2=simply supported   3=fixed ');
    e4=input(' ');
    if(e4==2)
        ijk=1;
        for j=1:ny
            constraint_matrix(icn,:)=[ijk RY_only];
            icn=icn+1;             
            ijk=ijk+nx;
        end          
    end
    if(e4==3)
        ijk=1;
        for j=1:ny
            constraint_matrix(icn,:)=[ijk fixed];
            icn=icn+1;  
            ijk=ijk+nx;
        end     
    end   
%
end
%
if ibc~=1
    disp(' sort ');
    constraint_matrix=sortrows(constraint_matrix,1);
    sz=size(constraint_matrix);
    nz=sz(1);
%
    i=1;
    while(1)
        if(constraint_matrix(i,1)==constraint_matrix(i+1,1))
    %
            for j=2:4
                an=constraint_matrix(i,j)+constraint_matrix(i+1,j);
                if(an>1)
                   an=1;
                end
                constraint_matrix(i,j)=an;
            end
    %
            constraint_matrix(i+1,:)=[];
            sz=size(constraint_matrix);
            nz=sz(1);
            constraint_matrix;
        else
            i=i+1;
        end
%    
        if(i+1>nz)
            break;
        end
    end
end    