1function y = chebEval(x,wts,chebApproxInfo)
2% CHEBEval retruns the approximation of a function at x
3%
4% y = chebApprox(x,ax,h,wts,ninters,nord) approximates a function f
5% (included in the wts argument) using chebyshev polynomial expansion on
6% subintervals of a domain that starts at ax
7%
8% INPUTS:
9% x: value in the domain where f is to be approximated
10% wts: chebyshev weights multiplied by function values in all
11% subintervals, wts(nord,ninters)
12% chebApproxInfo.domain: domain [ax,bx] overwhich wts are computed.
13% chebApproxInfo.ninters: number of subintervals
14% chebApproxInfo.nord: order of polynomial interpolation; number of cheb
15% nodes
16% NOTE: code adjusted from code provided by Leslie Greengard.
18if nargin == 0, test_chebEval; return; end
20domain = chebApproxInfo.domain; ax = domain(1); bx = domain(2);
21ninters = chebApproxInfo.ninters;
22nord = chebApproxInfo.nord;
24Nx = length(x);
25y = zeros(Nx,1);
27h = (bx-ax)/ninters;
29for ix = 1:Nx
30 % get index of left node in current interval
31 iint = min((floor((x(ix)-ax)/h) + 1),ninters);
33 % get interval endpoints [a,b]
34 a = ax + (iint-1)*h; b = ax + iint*h;
36 % transform to u in [-1,1]
37 u = (2*x(ix) - a - b)/(b-a);
39 % Evaluate the cheb polynomial
40 y(ix) = wts(1,iint);
41 for J = 2:nord
42 TJ = cos((J - 1) * acos(u)); % Chebyshev polynomial T_J(X)
43 y(ix) = y(ix) + TJ * wts(J,iint); % Accumulate the value
44 end
45end
46end
48function test_chebEval
49ninters = 12; % number of subintervals
50nord = 4; % number of cheb nodes in each interval
52% define the function that we wish to approximate
53fun = @(x) log(x + 3).*exp(-2*x).*(x.^2).*cos(20*x);
55% define the domain over which f is defined
56ax = -2; bx = -1;
57chebApproxInfo.domain = [ax,bx];
58chebApproxInfo.ninters = ninters;
59chebApproxInfo.nord = nord;
61% table of cheb weights times function values
62wts = mktab_wts(fun, chebApproxInfo);
64Nx = 120;
65x = linspace(ax,bx,Nx)';
66y = chebEval(x,wts,chebApproxInfo);
68plot(x,fun(x),'xb',x,y,'or','LineWidth',2);
69xlabel x; ylabel y; legend('true','approx');
70title(sprintf('Approximation of f(x) = x^2e^{-2x}cos(20x)log(x + 3) via\n order %d Chebyshev polynomials\n on %d subintervals',nord,ninters))
71set(gca,'fontsize',15);
72end