function mypde2(N,M)

T = 4; 
kappa = 1; 

k = T/M ;
h = 1/N ;
r = k * kappa/h^2 ; 
x = (0:N)'*h; 
t = (0:M)*k; 

u = zeros(N+1,M+1); 
u(:,1) = 0 ;             
u(1,:) = 0 ;         
u(N+1,:) = 2*sin(5*t) ;           

plot(x,u(:,1))
axis([0,1,-2,2])
drawnow 

for j=1:M
   u(2:N,j+1) = r*u(3:(N+1),j) + (1-2*r)*u(2:N,j) + r*u(1:(N-1),j); 
   plot(x,u(:,j+1))
   axis([0,1,-2,2])
   drawnow
end