% advect_FEM.m: Trapezoidally time-differenced FEM for
%
%        dq/dt + dq/dx = 0, 0<x<1, 0<t
%        q(x,0) = 0, q(0,t) = sin(50*t).
%
% on uniform grid with dx = 1/nx. Solution is plotted at time
% T=0.9. Uses function advect_FEMsolve.m

  c = 1;  % Uniform advection speed
  nx = 100;
  mu = 0.5;  % Courant number
  T = 0.9;
  L = 1;  % Domain size

  dx = L/nx;
  dt = mu*dx;
  nt = round(T/dt);
  t = dt*(0:nt);
  x = dx*(0:nx); 
  q0 = zeros(nx,1);  % Zero initial condition (as column vector)
  ql = sin(50*t);  % Left BC
  
  [q,qr] = advect_FEMsolve(x,c,dt,q0,ql);

  t0 = T-x;
  t0(t0<0)=0;
  qex = sin(50*t0);
  plot(x,[qex(1) q'],'b-',x,qex,'r--');
  xlabel('x')
  ylabel(['q(x,t = ' num2str(T) ')'])
  ylim([-1.1 1.1])
  legend('FEM','Exact','Location','NorthWest')
  title('FEM/trap on q_t+q_x = 0, q(0,t)= sin(50t)')


  
  
  
