%---------------------------------------------------------------------
function de=pde
global T t dt dx J x u un B C a b dc sip sim theta
% T je konecny cas
% dt,dx jsou casovy a prostorovy krok
% J je pocet bunek
% x(j) je prostorova souradnice j-teho bodu
% u(j) je reseni v bode x(j) na casove vrstve n
% un(j) je reseni v bode x(j) na casove vrstve n+1
% resime na intervalu (-B,B)
% C je CFL cislo
% resime rovnici u_t + a u_x = 0
B = 20;
J = 200;
T = 12;
C = 0.9;
a = -1;
solve;
%---------------------------------------------------------------------
function so=solve
% resi PDR u_t = a u_x
global T t dt dx J x u un B C a ermax

t=0;
init;
du = u;
while t < T
  dopredu;
  plot(x,un,'x-');
%  axis([-B B 0 1]);
  drawnow;
  %  pause;
  u = un;
  t = t + dt;
end

%---------------------------------------------------------------------
function ii=init
% inicializace, pocatecni podminky
global T dt dx J x u un B C a b

dx = 2*B/J;
x = zeros(J,1);
for j=1:J
  x(j) = -B + j*dx;
end
dt = C*dx/abs(a);
u = zeros(J,1);
un = zeros(J,1);
for j=1:J
  u(j) = exp(-(x(j)/2)^2);
end
%---------------------------------------------------------------------
function dd=dopredu
% jeden casovy krok  schema
global T dt dx J x u un B C a

for j=2:J-1
  un(j) = u(j) - a*dt*(u(j+1) - u(j))/dx;
end
% okrajove podminky
un(1) = un(2);
un(J) = un(J-1);

