%%         CHEBFUN  --  SEE www.maths.ox.ac.uk/chebfun

% 1. BASIC CHEBFUNS
x = chebfun('x',[0 10]);       % make a chebfun on [0,10]
f = sin(x) + sin(x.^2);        % overloaded +, ^, sin
plot(f)                        % overloaded plot 
length(f)                      % no. of interpolation points
g = exp(f);  
plot(g), length(g)
length(g.*g)                   % much less than 2*length(g)
  
% 2. HOW THE CONSTRUCTOR WORKS
chebpolyplot(f), grid on       % abs. values of Chebyshev coeffs.
chebpolyplot(g), grid on       %   going down to machine precision
  
% 3. INTEGRALS AND DERIVATIVES
plot(f)
sum(f)                         % definite integral	
f'*g                           % inner product
norm(f), norm(f,inf)           % continuous analogues 		
plot(cumsum(f))	               % indefinite integral
plot(diff(g))                  % derivative
plot(f), grid on
tv = @(f) norm(diff(f),1);     % anonymous function to
tv(x), tv(f)                   %   calculate total variation
  
% 4. ROOTFINDING
r = roots(f)                   % done via matrix eigenvalues
hold on, plot(r,f(r),'.r')     % global rootfinding
r = roots(f-x/10);             % soln. of nonlinear equations
plot(r,f(r),'.k')

% 5. PIECEWISE SMOOTH CHEBFUNS
hat = 1-abs(x-5)/5             % a chebfun with 2 pieces,
plot(hat,'k')                  %   known as "funs"
h = max(f,hat);                % a chebfun with many pieces
hold off, plot(h)

% 6. OPTIMIZATION
hp = diff(h);                  % this is not continuous
extrema = roots(hp);           %   but its roots are defined
hold on
plot(extrema,h(extrema),'.k')  % all extrema of h
hpp = diff(h,2);               % 2nd derivative
mx = extrema(hpp(extrema)<0);  % maxima of h
plot(mx,h(mx),'.g')
[mval,mpos] = max(h{5,10});    % value and position of
plot(mpos,mval,'or')           %   maximum over [5,10]

% 7. THE COMPLEX PLANE
s = scribble('There is no fun like Chebfun');
hold off, plot(s), axis equal  % piecewise linear & complex
plot(exp(3*s)), axis equal   
plot(exp(3i*s)), axis equal    % accurate to 15 digits!

% 8. QUASIMATRICES
x = chebfun('x');              % default interval [-1,1]
A = [1 x x.^2 x.^3 x.^4];      % each "column" is a chebfun
size(A)                        % dimensions of A
plot(A)                        % plot the columns
spy(A)                         % overloaded spy plot
[Q,R] = qr(A);                 % QR factorization
plot(Q)                        % Legendre polynomials
svd(A)                         % singular values

% 9. LINEAR DIFFL. EQS.
L = chebop(-20,20);            % interval
L.op = @(x,u) diff(u,2)-x.*u;  % linear operator (Airy eq.)
L.lbc = 1;                     % boundary
L.rbc = 0;                     %   conditions
u = L\0;                       % solve u"-xu=0
hold off, plot(u)
length(u)
eigs(L)                        % eigenvalues

% 10. NONLINEAR DIFFL. EQS.    
N = chebop(0,10);              % interval 
N.op = @(u) diff(u,2)+sin(u);  % nonlinear pendulum
N.lbc = 2; N.rbc = 2;          % start at end at angle 2 radians
N.init = 2;
u = N\0;                       % solve by Newton iteration
plot(u)
max(u)                         % just slightly less than pi
cheboppref('display','iter')
cheboppref('plotting','on')
u = N\0;

% 11. CHEBGUI
chebgui                        % be sure to run the Demos and
                               % use the "Export to m-file" button