-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoolQuad.m
177 lines (152 loc) · 5.95 KB
/
FoolQuad.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
%Fool MATLAB's quad routine with a carefully chosen function
%% Initialize
clear all, close all %garbage collection
format compact, format long e %eliminate blank lines
set(0,'defaultaxesfontsize',20,'defaulttextfontsize',20) %make font labels large enough
nplot=500; %number of points to plot
tol=1e-14;
%% Set up function to fool quad
h=0.13579; %secret constant in quad
%A function that is constant at the ends and oscillatory in the middle
testfun=@(x) 1+cos(8*pi*min(max((x-2*h)/(1-4*h),0),1));
%Exact integral of this function
tic;
syms xsym hsym; hsym=h;
fsym=1+cos(8*pi*(xsym-2*hsym)/(1-4*hsym)); %expression in the middle
exactInteg=double(int(fsym,2*hsym,1-2*hsym)... %middle part
+8*hsym); %plus end part
elapsedexacttime=toc;
%Use quad to find the answer
tic
[quadInteg,quadn]=quad(testfun,0,1,tol);
elapsedquadtime=toc;
%Use quadgk to find the answer
tic
[quadgkInteg,quadgkerr]=quadgk(testfun,0,1,'AbsTol',tol,'RelTol',0);
elapsedquadgktime=toc;
%Use quadl to find the answer
tic
[quadlInteg,quadln]=quadl(testfun,0,1,tol);
elapsedquadltime=toc;
%Use cubMC with iid sampling to find the answer
tic
paramiid.tol=1e-3;
paramiid.sample='iid';
[cubMCiidInteg,paramiid]=cubMC(testfun,[0 1],paramiid);
elapsedcubMCiidtime=paramiid.time;
%Use cubMC with Sobol sampling to find the answer
tic
paramSobol.tol=1e-3;
paramSobol.sample='Sobol';
[cubMCSobolInteg,paramSobol]=cubMC(testfun,[0 1],paramSobol);
elapsedcubMCSoboltime=paramSobol.time;
disp('For the function with well-placed wiggles')
disp(['Exact integral = ' num2str(exactInteg,'%10.15g')])
disp([' which took ' num2str(elapsedexacttime) ' seconds to compute'])
disp(['Approximate integral using quad = ' num2str(quadInteg,'%10.15g') ...
' +/- ' num2str(tol,'%3.1g')])
disp([' which took ' num2str(elapsedquadtime) ' seconds and'])
disp([' ' num2str(quadn) ' function evaluations to compute'])
disp(['Approximate integral using quadgk = ' num2str(quadgkInteg,'%10.15g') ...
' +/- ' num2str(quadgkerr,'%3.1g')])
disp([' which took ' num2str(elapsedquadgktime) ' seconds to compute'])
disp(['Approximate integral using quadl = ' num2str(quadlInteg,'%10.15g') ...
' +/- ' num2str(tol,'%3.1g')])
disp([' which took ' num2str(elapsedquadltime) ' seconds and'])
disp([' ' num2str(quadln) ' function evaluations to compute'])
disp(['Approximate integral using cubMC with iid sampling = '...
num2str(cubMCiidInteg,'%10.15g') ...
' +/- ' num2str(paramiid.tol,'%3.1g')])
disp([' which took ' num2str(elapsedcubMCiidtime) ' seconds and'])
disp([' ' num2str(paramiid.n) ' function evaluations to compute'])
disp(['Approximate integral using cubMC with Sobol sampling = '...
num2str(cubMCSobolInteg,'%10.15g') ...
' +/- ' num2str(paramSobol.tol,'%3.1g')])
disp([' which took ' num2str(elapsedcubMCSoboltime) ' seconds and'])
disp([' ' num2str(paramSobol.n) ' function evaluations to compute'])
disp(' ')
%% Plot the fooling function
figure;
xplot=(0:nplot)/nplot;
fplot=testfun(xplot);
xnode=[(0:0.5:2)*h 2*h+(0:0.25:1)*(1-4*h) 1-(0:0.5:2)*h];
fnode=testfun(xnode);
han=plot(xplot,fplot,'b-',...
xnode,fnode,'r.',...
[0 1],exactInteg*[1 1],'k--', ...
'linewidth',2,'markersize',20);
xlabel('{\it x}')
%ylabel('{\it f(x)}')
legend(han([1 3 2]),{'{\it f(x)}','integral','data'},...
'location','southwest')
print -depsc FoolQuadFunction.eps
%% Now try the step function
exactInteg=1;
stepfun = @(x,mu,sig,p) mu ...
+ (sig*sqrt((1-p)/p)).*(x<=p) ...
- (sig*sqrt(p/(1-p))).*(x>p);
p=0.01;
offset=sqrt(2)/3;
%offset=rand(1);
testfun = @(x) stepfun(mod(x-offset,1),exactInteg,1,p);
%Use quad to find the answer
tic
[quadInteg,quadn]=quad(testfun,0,1,tol);
elapsedquadtime=toc;
%Use quadgk to find the answer
tic
[quadgkInteg,quadgkerr]=quadgk(testfun,0,1,'AbsTol',tol,'RelTol',0);
elapsedquadgktime=toc;
%Use quadl to find the answer
tic
[quadlInteg,quadln]=quadl(testfun,0,1,tol);
elapsedquadltime=toc;
%Use cubMC with iid sampling to find the answer
tic
paramiid.tol=1e-3;
paramiid.sample='iid';
[cubMCiidInteg,paramiid]=cubMC(testfun,[0 1],paramiid);
elapsedcubMCiidtime=paramiid.time;
%Use cubMC with Sobol sampling to find the answer
tic
paramSobol.tol=1e-3;
paramSobol.sample='Sobol';
[cubMCSobolInteg,paramSobol]=cubMC(testfun,[0 1],paramSobol);
elapsedcubMCSoboltime=paramSobol.time;
disp('For the square spike function')
disp(['Exact integral = ' num2str(exactInteg,'%10.15g')])
disp(['Approximate integral using quad = ' num2str(quadInteg,'%10.15g') ...
' +/- ' num2str(tol,'%3.1g')])
disp([' which took ' num2str(elapsedquadtime) ' seconds and'])
disp([' ' num2str(quadn) ' function evaluations to compute'])
disp(['Approximate integral using quadgk = ' num2str(quadgkInteg,'%10.15g') ...
' +/- ' num2str(quadgkerr,'%3.1g')])
disp([' which took ' num2str(elapsedquadgktime) ' seconds to compute'])
disp(['Approximate integral using quadl = ' num2str(quadlInteg,'%10.15g') ...
' +/- ' num2str(tol,'%3.1g')])
disp([' which took ' num2str(elapsedquadltime) ' seconds and'])
disp([' ' num2str(quadln) ' function evaluations to compute'])
disp(['Approximate integral using cubMC with iid sampling = '...
num2str(cubMCiidInteg,'%10.15g') ...
' +/- ' num2str(paramiid.tol,'%3.1g')])
disp([' which took ' num2str(elapsedcubMCiidtime) ' seconds and'])
disp([' ' num2str(paramiid.n) ' function evaluations to compute'])
disp(['Approximate integral using cubMC with Sobol sampling = '...
num2str(cubMCSobolInteg,'%10.15g') ...
' +/- ' num2str(paramSobol.tol,'%3.1g')])
disp([' which took ' num2str(elapsedcubMCSoboltime) ' seconds and'])
disp([' ' num2str(paramSobol.n) ' function evaluations to compute'])
disp(' ')
%% Plot the peak function
figure;
nplot=2000;
xplot=(0:nplot)/nplot;
fplot=testfun(xplot);
han=plot(xplot,fplot,'b-',...
[0 1],exactInteg*[1 1],'k--', ...
'linewidth',2,'markersize',20);
xlabel('{\it x}')
%ylabel('{\it f(x)}')
legend(han,{'{\it f(x)}','integral'},'location','best')
axis([0 1 0 12])
print -depsc FoolQuadStepFunction.eps