% This script finds the PS solution of the 
% linear 2-point BVP with non-homogeneous boundary conditions of the form
% y''(t) = exp(4*y(t))
% with BCs y(-1) = 0, y(1) = 1
% PS code based on Trefethen: Spectral Methods in Matlab
clear all
clc
close all
warning off
scrnsz = get(0, 'Screensize');
figure('Position', [5 10 scrnsz(3)-5 scrnsz(4)-60])

%%%% exact solution
tt = linspace(-1,1,101);
exact = ( exp(4*tt) - sinh(4)*tt - cosh(4) )/16 + (tt+1)/2;

N = 16;
[D,t] = cheb(N);        
D2 = D^2;
D2(1,:) = eye(1,N+1);                  % identity rows so that we can set
D2(N+1,:) = fliplr(eye(1,N+1));        % non-homogeneous BCs
y = D2\[1; exp(4*t(2:N)); 0];
plot(t,y,'.','markersize',16)
yy = polyval(polyfit(t,y,N),tt);    % interpolate grid data
line(tt,yy)
title([num2str(N+1) ' point PS: max err = ' num2str(norm(yy-exact,inf))],'fontsize',12)
