-
Notifications
You must be signed in to change notification settings - Fork 0
/
p30.m
30 lines (30 loc) · 908 Bytes
/
p30.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
29
30
% p30.m - spectral integration, ODE style (compare p12.m)
% Computation: various values of N, four functions:
Nmax = 50; E = zeros(4,Nmax); clf
for N = 1:Nmax; i = 1:N;
% Choose one of the three options:
% Gauss quadrature
[x,w] = gauss(N);
% Clenshaw-Curtis
% [x,w] = clencurt(N);
% spectral integration, ODE style
%[D,x] = cheb(N); x = x(i); Di = inv(D(i,i)); w = Di(1,:);
f = abs(x).^3;
E(1,N) = abs(w*f - .5);
f = exp(-x.^(-2)); E(2,N) = abs(w*f - ...
2*(exp(-1)+sqrt(pi)*(erf(1)-1)));
f = 1./(1+x.^2);
E(3,N) = abs(w*f - pi/2);
f = x.^10;
E(4,N) = abs(w*f - 2/11);
end
% Plot results:
labels = {'|x|^3','exp(-x^{-2})','1/(1+x^2)','x^{10}'};
for iplot = 1:4,
subplot(3,2,iplot)
semilogy(E(iplot,:)+1e-100,'.','markersize',12), hold on
plot(E(iplot,:)+1e-100,'linewidth',.8)
axis([0 Nmax 1e-18 1e3]), grid on
set(gca,'xtick',0:10:Nmax,'ytick',(10).^(-15:5:0))
ylabel error, text(32,.004,labels(iplot))
end