%
%  bignet.m
%
% construct S = A'KA where A and K code bignet of last
% exercise in chapter 2
%

function  S = bignet(N)

n = N*(N-1)*2;	% degrees of freedom
m = 4*(N-1)*(N-1) + N-1;	% number of fibers
h = 1/(n-1);		% hor and vert fiber length
A = zeros(m,n);
k = zeros(m,1);

s = 1/sqrt(2);

fnum = 1;

for i=0:N-3 		% vertical index working top to bottom

    for j=1:N-1		% horiz index working left to right
                        % lay down 4 fibs per cell

        LL = i*N + j;   % node number of Lower Left node in cell
        LR = LL+1;	% Lower Right
        UL = LL+N;      % Upper Left
        UR = UL+1;      % Upper Right

        A(fnum,[2*LL 2*UL]) = [1 -1];
        k(fnum) = 1/h;
        A(fnum+1,[2*LL-1 2*LL 2*UR-1 2*UR]) = [-s s s -s];
        k(fnum+1) = s/h;
        A(fnum+2,[2*LL-1 2*LR-1]) = [-1 1];
        k(fnum+2) = 1/h;
        A(fnum+3,[2*LR-1 2*LR 2*UL-1 2*UL]) = [s s -s -s];
        k(fnum+3) = s/h;

        fnum = fnum + 4;
        
     end

     A(fnum,[2*LR 2*UR]) = [1 -1];	% final vert
     k(fnum) = 1/h;

     fnum = fnum + 1;

end

i = N-2;   % now work on top layer, where there are fixed nodes

for j=1:N-1

    LL = i*N + j;
    LR = LL+1;
    UL = LL+N;
    UR = UL+1;

    A(fnum,[2*LL]) = [1];
    k(fnum) = 1/h;
    A(fnum+1,[2*LL-1 2*LL]) = [-s s];
    k(fnum+1) = s/h;
    A(fnum+2,[2*LL-1 2*LR-1]) = [-1 1];
    k(fnum+2) = 1/h;
    A(fnum+3,[2*LR-1 2*LR]) = [s s];
    k(fnum+3) = s/h;

    fnum = fnum + 4;
        
end

A(fnum,[2*LR]) = [1];
k(fnum) = 1/h;
%keyboard
S = A'*diag(k)*A;
