% p11.m - Chebyshev differentation of a smooth function
% Source: http://people.maths.ox.ac.uk/trefethen/p11.m

k = 1.87510407;

% note: to get this use:
%
% fzero(@(x)(1+cos(x)*cosh(x)),1.875)

c1 = -1/(sin(k)+sinh(k));
c2 = -1;
c3 = 1/(sin(k)+sinh(k));
c4 = 1;

xx = linspace(-1,1);

ya = c1*sin(k*xx)+c2*cos(k*xx)+c3*sinh(k*xx)+c4*cosh(k*xx);

for N = [10 20]
    [D,x] = cheb(N); 
    y = c1*sin(k*x)+c2*cos(k*x)+c3*sinh(k*x)+c4*cosh(k*x);
    subplot('position',[.15 .66-.4*(N==20) .31 .28])
    plot(x,y,'.','markersize',14), grid on
    line(xx,ya)
    title(['y(x),  N=' int2str(N)])
    error = D^4*y - k^4*y;
    subplot('position',[.55 .66-.4*(N==20) .31 .28])
    plot(x,error,'.','markersize',14), grid on
    line(x,error)
    title(['  error in y''''(x),  N=' int2str(N)])
end
