function ShowCollocation()
% Checks out BVP_Collocation by comparing it to the finite difference
% approach on a simple problem

% -u"(x) + q(x)u(x) = r(x) u(a) = ua, u(b) = ub

% where q(x) = x, r(x) = sin(2*x) - exp(-x/3).*cos(x) and
a = 0; b = 10; ua = 1; ub = 1;

% Collocation points are linspace(a,b,n) where
n = 800;
h = (b-a)/(n-1);

% The method of collocation...
alpha = BVP_Collocation(a,ua,b,ub,@q,@r,n);

% Evaluate the approximate solution across [a,b]...
N = 1000;
xVals = linspace(a,b,N)';
yValsColl = zeros(N,1);
for i=1:N
for k=0:n+1
xk = a+(k-1)*h;
yValsColl(i) = yValsColl(i) + alpha(k+1)*derBstar((xVals(i)-xk)/h);
end
end

% Same problem via finite differences...
u = BVP_FiniteDiff(a,ua,b,ub,@q,@r,n);
% Fit a spline ...
yValsFD = spline(linspace(a,b,n)',u,xVals);

% Display both solutions and report on how different they are...
plot(xVals,yValsColl,xVals,yValsFD,'r')
title(sprintf('n = %1d h = %8.2e err = %8.2e',n,(b-a)/(n-1),max(abs(yValsFD-yValsColl))),'fontsize',14)
shg

function y = q(x)
y = x;

function y = r(x)
y = sin(2*x) - exp(-x/3).*cos(x);