% demo for stiff differential equations 
% (here spatially discretized heat equation)
% Raphael Kruse
% 10. June 2009

function demo_stiff_heq
N = 100; % number of spatial grid points
dx = 1/(N+1); % spatial step size
dt = 0.52*dx^2; % time step size - change constant below 0.5 for   
                % a stable approximation
grid = (dx:dx:1-dx)'; % spatial grid

u0 = @(x) u_ini(x);
figure(1)
plot(grid,u0(grid))
title('initial value u0')
xlabel('x')
ylabel('u0(x)')

% discrete Laplacian
A = -2*diag(ones(N,1)) + diag(ones(N-1,1),1) + diag(ones(N-1,1),-1);
u_n = u0(grid);
% applying Euler's method in time 
figure(2)
for i=1:100
    u_n = u_n + dt*(N+1)^2*A*u_n;
    plot(grid,u_n)
    axis([0 1 0 0.25])
    pause(0.1)
end
    
end


% initial value
function out = u_ini(x)
out = max(min(x-0.25,-x+0.75),0);
end
