function [tp,yp,abserr] = colloc1(n); % % function [t,y,abserr] = colloc1(n); % % Solves the linear BVP from page 3.61 % using collocation with polynomials. % Np = 1000; h = 1.0 / n; t = linspace(h,1.0,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))'; g(n,1) = 0.0; % Determine the matrix from the evaluation of y'' A1 = zeros(n,n); for l=2:n for k=1:n-1 A1(k,l) = l*(l-1)*(t(k))^(l-2); end; end; % Determine the matrix from the evaluation of y A2 = zeros(n,n); for k=1:n-1 for l=1:n A2(k,l) = (t(k))^l; end; end; for l=1:n A2(n,l) = 1.0; 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 + x(n); for k=n-1:-1:1 yp = yp .* tp + x(k); end; yp = yp .* tp; abserr = norm(yp-yexact,inf);