function [K,f] = FEM_system_steady(X,T,referenceElement,example,src)


velo = example.velo;
a = example.a;
nu = example.nu;
sigma = example.sigma;
methodSD = example.methodSD; 
sr = example.source;

 
Te = T(1,:); Xe = X(Te,:); 
hx = max(Xe(:,1)) - min(Xe(:,1)); 
hy = max(Xe(:,2)) - min(Xe(:,2)); 
h = (hx+hy)/2; 


%ax = velo(1);
%ay = velo(2);


nen = referenceElement.nen;
ngaus = referenceElement.ngaus;
wgp = referenceElement.GaussWeights;
N = referenceElement.N;
Nxi = referenceElement.Nxi;
Neta = referenceElement.Neta;
N2xi = referenceElement.N2xi; 
N2eta = referenceElement.N2eta;
N2xieta = referenceElement.N2xieta;
N2etaxi = referenceElement.N2etaxi;
sr= src;

nElem = size(T,1);
nPt = size(X,1);


K = zeros(nPt); 
f = zeros(nPt,1);
    

   
   Pe = a*h/(2*nu);
   tau_p = h*(1 + 9/Pe^2)^(-1/2)/(2*a);
    disp(strcat('Recommended stabilization parameter = ',num2str(tau_p)));
    tau = cinput('Enter the Stabilization parameter',tau_p);
    if isempty(tau)
        tau = tau_p;
    end
       
 % Loop on elements
for ielem=1:nElem
    Te = T(ielem,:); 
    Xe = X(Te,:); 
    [Ke,fe] = EleMat(Xe,nen,ngaus,wgp,N,Nxi,Neta,N2xi,N2eta,N2xieta,N2etaxi,velo,nu,tau,methodSD,sigma,sr);
    % Assembly
   
    K(Te,Te) = K(Te,Te) + Ke; 
    f(Te) = f(Te) + fe;
     
end   


function [Ke,fe] = EleMat(Xe,nen,ngaus,wgp,N,Nxi,Neta,N2xi,N2eta,N2xieta,N2etaxi,velo,nu,tau,methodSD,sigma,sr);
%

ax = velo(1);
ay = velo(2);


Ke = zeros(nen);
fe = zeros(nen,1);
   
for ig = 1:ngaus
    N_ig = N(ig,:);
    Nxi_ig = Nxi(ig,:);
    Neta_ig = Neta(ig,:);
    N2xi_ig = N2xi(ig,:);
    N2eta_ig = N2eta(ig,:);
    N2xieta_ig = N2xieta(ig,:);
    N2etaxi_ig = N2etaxi(ig,:);
    
    Jacob = [Nxi_ig*(Xe(:,1))	Nxi_ig*(Xe(:,2))
             Neta_ig*(Xe(:,1))	Neta_ig*(Xe(:,2))];
         
    Laplace = [(Nxi_ig*Xe(:,1))^2 (Nxi_ig*Xe(:,2))*(Nxi_ig*Xe(:,1)) N2xi_ig*Xe(:,1) (Nxi_ig*Xe(:,1))*(Nxi_ig*Xe(:,2)) (Nxi_ig*Xe(:,2))^2 N2xi_ig*Xe(:,2);
             (Neta_ig*Xe(:,1))*(Nxi_ig*Xe(:,1)) (Neta_ig*Xe(:,2))*(Nxi_ig*Xe(:,1)) N2xieta_ig*Xe(:,1) (Neta_ig*Xe(:,1))*(Nxi_ig*Xe(:,2)) (Neta_ig*Xe(:,2))*(Nxi_ig*Xe(:,2)) N2xieta_ig*Xe(:,2)
             0 0 Nxi_ig*Xe(:,1) 0 0 Nxi_ig*(Xe(:,2))
             (Nxi_ig*Xe(:,1))*(Neta_ig*Xe(:,1)) (Nxi_ig*Xe(:,2))*(Neta_ig*Xe(:,1)) N2etaxi_ig*Xe(:,1) (Nxi_ig*Xe(:,1))*(Neta_ig*Xe(:,2)) (Nxi_ig*Xe(:,2))*(Neta_ig*Xe(:,2)) N2etaxi_ig*Xe(:,2)
             (Neta_ig*Xe(:,1))^2 (Neta_ig*Xe(:,2))*(Neta_ig*Xe(:,1)) N2eta_ig*Xe(:,1) (Neta_ig*Xe(:,1))*(Neta_ig*Xe(:,2)) (Neta_ig*Xe(:,2))^2 N2eta_ig*Xe(:,2);
             0 0 Neta_ig*Xe(:,1) 0 0 Neta_ig*(Xe(:,2))];
         
    dvolu = wgp(ig)*det(Jacob);
    
    res   = Laplace\[N2xi_ig;N2xieta_ig;Nxi_ig;N2etaxi_ig;N2eta_ig;Neta_ig];
    Nx = res(3,:);
    Ny = res(6,:);
    Nxx = res(1,:);
    Nyy = res(5,:);
    LP = Nxx+Nyy;
    
    
       if methodSD == 1  %SUPG 
          Ke = Ke + (nu*(Nx'*Nx+Ny'*Ny) + N_ig'*(ax*Nx+ay*Ny) + ...
            sigma*(N_ig'*N_ig)+tau*(ax*Nx+ay*Ny)'*(ax*Nx+ay*Ny+nu*LP+sigma*N_ig))*dvolu;
          aux = N_ig*Xe; 
          f_ig = SourceTerm(aux,sr);
          fe = fe + (N_ig+tau*(ax*Nx+ay*Ny))'*(f_ig*dvolu);
        
       elseif methodSD == 2 %GLS
          Ke = Ke + (nu*(Nx'*Nx+Ny'*Ny) + N_ig'*(ax*Nx+ay*Ny) + ...
           sigma*(N_ig'*N_ig)+tau*(ax*Nx+ay*Ny+nu*LP+sigma*N_ig)'*(ax*Nx+ay*Ny+nu*LP+sigma*N_ig))*dvolu;
          aux = N_ig*Xe; 
          f_ig = SourceTerm(aux,sr);
          fe = fe + (N_ig+tau*(ax*Nx+ay*Ny+nu*LP+sigma*N_ig))'*(f_ig*dvolu);
       end
       
end
end
end


