% script: exerNT181.m
% description: solves exercise 1.8.1, i.e., 
% u_t = u_xx + 6x, 0<x<1, 0<=t<=1
% u(x,0) = x*(1-x^2)+0.5*sin(pi*x)
% solution u(x,t) = x*(1-x^2)+0.5*sin(pi*x)*exp(-pi^2*t)

disp('Exercise 1.8.1 ')
% the experiment
h = 0.1;
kk = [0.1,0.01,0.001,0.0001];
% construct correct initial and final values
x = linspace(0,1,round(1/h)+1)';
yinit = x.*(1-x.*x)+0.5*sin(pi*x);
yfinal =  x.*(1-x.*x)+0.5*sin(pi*x)*exp(-pi^2);
% store stepsizes and errors in this matrix
errors = zeros(length(kk),3);
errors(:,1) = kk';
% do all the calculations
for jj = 1:length(kk)
  k = kk(jj);
  val = Euler('fcn181',0,yinit(2:10),k,round(1/k));
  yapprox = [0;val(:,round(1/k)+1);0];
  errors(jj,2) = norm(yapprox-yfinal,inf);
  val = ClassERK('fcn181',0,yinit(2:10),k,round(1/k));
  yapprox = [0;val(:,round(1/k)+1);0];
  errors(jj,3) = norm(yapprox-yfinal,inf);
end
disp('Space stepsize for this problem:')
h
disp('Table with columns [time stepsize, Euler error, ERK error]:')
format long
errors
format



