function [tp,yp,abserr] = colloc2(n); % % function [t,y,abserr] = colloc2(n); % % Solves the linear BVP from page 3.61 % using collocation with sines. % Np = 1000; h = 1.0 / (n+1); t = linspace(h,1.0-h,n); tp = linspace(0.0,1.0,Np); % Determine the right-hand side vector g = (400 * (cos(pi*t)).^2 + 2*pi^2 * cos(2*pi*t))'; % Determine the matrices from the evaluation of y'' and y A1 = zeros(n,n); A2 = A1; for l=1:n for k=1:n A1(l,k) = -k*k*pi*pi*sin(k*pi*t(l)); A2(l,k) = sin(k*pi*t(l)); end; end; % Solve the linear system A = A1 - 400.0 * A2; x = (A \ g)'; % Compute the exact solution and the absolute error yexact = -(cos(pi*tp)).^2 + (exp(20*(tp-1)) + exp(-20*tp))/(1+exp(-20)); yp = 0.0 * tp; for k=1:n yp = yp + x(k)*sin(k*pi*tp); end; abserr = norm(yp-yexact,inf);