% ------------------------------------------------------------
% ïîëàãàìå tau äà å ñòúïêà íà âðåìåòî è h äà å ñòúïêà íà õ
% çà 0 < õ < 5 è tau = 0.04 , h = 0.4 , 0 < t < 1

h = 0.4;
tau = 0.04;

% ñåãà çàäàâàìå îòêúäå äîêúäå ñå ìåíÿò ïðîìåíëèâèòå è ñúîòâåòíèòå ñòúïêè

x = 0:h:5;          % 0 < x < 5
t = 0:tau:100;      % 0 < t < 100 , ïî óñëîâèå å t < 1 (fun)

% çàäàâàìå ìàêñèìàëíèòå äúëæèíè 

N = length(x);   
M = length(t);   

% ñúçäàâàìå äâóìåðåí ìàñèâ ïúëåí ñ íóëè è 
% ìó çàäàâàìå êðàéíèòå ñòîéíîñòè íà õ
% ðàçáèðàéêè u(n,m) = u(x(n),t(m))

u = zeros([N,M]);

% ãðàíè÷íè óñëîâèÿ
for m = 1:M
u(1,m) = (t(m)+3)*exp(t(m).*(-3)) - 3;  % u(0,t) = (t + 3)*exp^(-3*t) - 3
u(N,m) = sin(5*t(m));                   % u(5,t) = sin(5*t)
end

% íà÷àëíè óñëîâèÿ
for n = 1:N
u(n,1) = x(n)*(5 - x(n))*(2 - x(n));  % u(x,0) = x*(5 - x)*(2 - x)
end

% íàìèðàíå íà åëåìåíòèòå
for m = 1:M-1
for n = 2:N-1
u(n,m+1) = u(n,m) + (tau/h*h)*(u(n+1,m) - 2*u(n,m) + u(n-1,m));
end
end

for m = 1:M
plot(x,u(:,m))
axis([0,5,-10,10])
MM(m) = getframe;
end
movie (MM,3,1000)