function HeatDynamic()
L = 1;    % length of the space interval
n = 9;    % number of interior grid points
n = 29;   % number of interior grid points
r = 0.45; % ratio to compute time step
r = 0.52; % ratio to compute time step
T = 0.1;  % final time
T = 0.1/2;% final time

function r = iv(x);
  r = min([ 2*x/L,2-2*x/L]')';
end%function

dx = L/(n+1);
dt = r*dx^2;
x = linspace(0,L,n+2)';

y = iv(x);
ynew = y;
legend('off')
for t = 0:dt:T+dt;
%  for k = 2:n+1                                     % code with loops
%    ynew(k) = (1-2*r)*y(k)+r*(y(k-1)+y(k+1));
%  endfor
%  y = ynew;
  y(2:n+1) =  (1-2*r)*y(2:n+1)+r*(y(1:n)+y(3:n+2));  % no loops
  plot(x,y)
  axis([0,1,0,1]); grid on
  text(0.1,0.9,['t = ',num2str(t,3)]);
  pause(0.1);
end%for

end%function
