-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspquad.m
60 lines (53 loc) · 1.35 KB
/
spquad.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
function q = spquad(z)
% SPQUAD Compute integral value of sparse grid interpolant.
% Q = SPQUAD(Z) Computes the integral over the sparse grid
% domain. The sparse grid data must be given as a structure Z
% containing the hierarchical surpluses (computed with
% SPVALS).
%
% Example:
% f = inline('x.^2 + y.^2 - 2.*z');
% options = spset('GridType','Chebyshev','Vectorized','on');
% z = spvals(f,3,[],options);
% F_quad = spquad(z)
% F_exact = -1/3
% error = abs(F_exact - F_quad)
%
% See also SPVALS.
gridtype = z.gridType;
d = z.d;
if isfield(z, 'indices');
sparseIndices = 'on';
levelseq = z.indices;
else
sparseIndices = 'off';
n = size(z.vals,2) - 1;
end
if isfield(z, 'selectOutput')
output = z.selectOutput;
else
output = 1;
end
if ~isempty(z.range)
scale = prod(z.range(:,2)-z.range(:,1));
else
scale = 1;
end
z = z.vals;
% do multiple levels at once
if strcmpi(sparseIndices, 'off')
options = spset('SparseIndices','off','GridType',gridtype);
q = 0;
for k = n:-1:0
% get the sequence of levels
levelseq = spgetseq(k,d,options);
quadw = spquadw(levelseq,[],options);
q = q + sum(quadw .* z{output,k+1});
end
else
options = spset('SparseIndices','on','GridType',gridtype);
quadw = spquadw(levelseq,[],options);
q = sum(quadw .* z{output});
end
% Rescale to domain
q = q * scale;