function [M,K,C] = FEM_matrices_unsteady(X,T,referenceElement,example,dt)


% reference element information
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;


%PDE Coefficients and methods
dom=example.dom;
nx=example.nx;
ny=example.ny;
velo=example.velo;
a=example.a;
nu=example.nu;
sigma=example.sigma;
methodSD=example.methodSD;
methodtm=example.methodtm;
hx=(dom(2)-dom(1))/nx;
hy=(dom(4)-dom(3))/ny;
h=(hx+hy)/2;


% Number of nodes and elements
nPt = size(X,1); 
nElem = size(T,1); 


M = zeros(nPt);
K = zeros(nPt); 
C = zeros(nPt);

if methodtm==1
    St_L=zeros(nPt);
    St_R=zeros(nPt);
    W=1/2;
    w=1;
    
    tau_R11= ((1/(2*dt))+(2*a/h)+4*nu\(h^2)+sigma)^-1;
    disp(strcat('Recommended stabilization parameter = ',num2str(tau_R11)));
    tau=cinput('Enter the value of stabilization parameter: ',tau_R11);

elseif methodtm==2
    St_L=zeros(nPt);
    St_R=zeros(nPt);
    W=1/2;
    w=1;
    
    tau_R20= ((1/(2*dt))+(2*a/h)+4*nu\(h^2)+sigma)^-1;
    disp(strcat('Recommended stabilization parameter = ',num2str(tau_R20)));
    tau=cinput('Enter the value of stabilization parameter: ',tau_R20);
else
    disp('not implemented')
end


% Loop on elements
for ielem=1:nElem
    Te = T(ielem,:); 
    Xe = X(Te,:); 
        [Me,Ke,Ce] = EleMat(Xe,nen,ngaus,wgp,N,Nxi,Neta);
    % Assembly
    M(Te,Te) = M(Te,Te) + Me; 
    K(Te,Te) = K(Te,Te) + Ke; 
    C(Te,Te) = C(Te,Te) + Ce; 
end



function [Me,Ke,Ce] = EleMat(Xe,nen,ngaus,wgp,N,Nxi,Neta)

Me = zeros(nen);
Ke = zeros(nen);
Ce = zeros(nen);

if methodtm==1;
    St_Le=zeros(nen);
    St_Re=zeros(nen);
    
elseif methodtm==2;
    St_Le=zeros(nen);
    St_Re=zeros(nen);
else
    disp('not implementeed')
    return
end

% Loop on Gauss points 
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))];
         
    dvolu = wgp(ig)*det(Jacob);
    res = Jacob\[Nxi_ig;Neta_ig];
    Nx = res(1,:);
    Ny = res(2,:);
    a = N_ig*Conve; ax = a(1); ay = a(2); 
    
    Me = Me + N_ig'*N_ig*dvolu; 
    aGradN = ax*Nx + ay*Ny; 
    Ke = Ke + aGradN'*aGradN*dvolu;
    Ce = Ce + N_ig'*aGradN*dvolu;
end

disp(Me,Ke,Ce)
