% This is an example to understand the methods proposed in the book in section 4.5 and 4.6
% programmer: C. Vuik
% date: 26-03-2020
% e-mail: c.vuik@tudelft.nl
clear
clf
n = 100;
h = 1/n;
% possible choices for the time step
% dt = 1.1/n;
dt = 1/n;
% dt = 0.99/n;
% dt = 0.5/n;
for j = 1:n
x(j) = (j-1)*h;
a(j,j+1) = -1;
a(j+1,j) = 1;
b(j,j+1) = 0.5;
b(j+1,j) = 0.5;
end
x(n+1) = j*h;
a(1,n+1) = 1;
a(n+1,1) = -1;
b(1,n+1) = 0.5;
b(n+1,1) = 0.5;
a = a/(2*h);
for j = 1:n+1
q(j,1) = cos(2*pi*j*h);
end
plot(x,q);
% the time loop
for j = 1:n
q = b*q+dt*a*q;
plot(x,q);
pause(0.1)
end