% Implicit, backward Euler for solving the heat equation: created by the AMATH 301 Spring 2009 class, in class. clear all; close all; n = 100; L = 20; x2= linspace(-L/2,L/2,n+1); x=x2(1:n); dx = x(2)-x(1); dt = 1; CFL = dt/dx^2; Time = 4; time_steps=Time/dt; t = 0:dt:Time; u0 = exp(-x.^2)'; usol(:,1) = u0; e1 = ones(n,1); A = spdiags([-CFL*e1 (1+2*CFL)*e1 -CFL*e1],[-1 0 1],n,n); A(1,n) = -CFL; A(n,1) = -CFL; for j = 1:time_steps, u1 = A\u0; u0 = u1; usol(:,j+1) = u1; end waterfall(x,t,usol'); map=[0 0 0] colormap(map);