-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3.m
63 lines (62 loc) · 1.34 KB
/
p3.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
% p3.m - band-limited interpolation
h = 1; xmax = 10; clf
x = -xmax:h:xmax;
% computational grid
xx = -xmax-h/20:h/10:xmax+h/20;
% plotting grid
for plt = 1:3
subplot(3,1,plt)
switch plt
case 1, v = (x==0);
% delta function
case 2, v = (abs(x)<=3);
% square wave
case 3, v = max(0,1-abs(x)/3);
% hat function
end
plot(x,v,'.','markersize',14), grid on
p = zeros(size(xx));
for i = 1:length(x),
p = p + v(i)*sin(pi*(xx-x(i))/h)./(pi*(xx-x(i))/h);
end
line(xx,p,'linewidth',.7), axis([-xmax xmax -.5 1.5])
set(gca,'xtick',[]), set(gca,'ytick',[0 1])
end
%Excercise 2.7, completed
hold off
figure
Nvec = 2.^(3:6);
%Nvec = 2.^(3);
error = zeros(size(Nvec));
for plt = 2:3
subplot(2,1,plt-1)
meshindex = 0;
for N = Nvec
meshindex = meshindex + 1;
h = 2.0 * xmax / N;
x = -xmax:h:xmax;
xx = -xmax-h/2:h:xmax+h/2;
switch plt
case 2,
v = (abs(x)<=3);
vtocmp = (abs(xx) <=3 );
% square wave
case 3,
v = max(0,1-abs(x)/3);
vtocmp = max(0,1-abs(xx)/3);
% hat function
end
p = zeros(size(xx));
for i = 1:length(x),
p = p + v(i)*sin(pi*(xx-x(i))/h)./(pi*(xx-x(i))/h);
end
error(meshindex) = norm (vtocmp - p, inf);
end
%figure
loglog(Nvec,error,'.','markersize',15);
hold on
grid on, xlabel Nvec, ylabel error
title('Convergence of bandwidth limited interpolant')
semilogy(Nvec,Nvec.^(-1),'-.')
%hold off
end