-
Notifications
You must be signed in to change notification settings - Fork 0
/
p12.m
28 lines (28 loc) · 852 Bytes
/
p12.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
% p12.m - accuracy of Chebyshev spectral differentiation
% (compare p7.m)
% Compute derivatives for various values of N:
Nmax = 50; E = zeros(3,Nmax);
for N = 1:Nmax;
[D,x] = cheb(N);
v = abs(x).^3; vprime = 3*x.*abs(x);
% 3rd deriv in BV
E(1,N) = norm(D*v-vprime,inf);
v = exp(-x.^(-2)); vprime = 2.*v./x.^3; % C-infinity
E(2,N) = norm(D*v-vprime,inf);
v = 1./(1+x.^2); vprime = -2*x.*v.^2;
% analytic in [-1,1]
E(3,N) = norm(D*v-vprime,inf);
v = x.^10; vprime = 10*x.^9;
% polynomial
E(4,N) = norm(D*v-vprime,inf);
end
% Plot results:
titles = {'|x^3|','exp(-x^{-2})','1/(1+x^2)','x^{10}'}; clf
for iplot = 1:4
subplot(2,2,iplot)
semilogy(1:Nmax,E(iplot,:),'.','markersize',12)
line(1:Nmax,E(iplot,:),'linewidth',.8)
axis([0 Nmax 1e-16 1e3]), grid on
set(gca,'xtick',0:10:Nmax,'ytick',(10).^(-15:5:0))
xlabel N, ylabel error, title(titles(iplot))
end