-
Notifications
You must be signed in to change notification settings - Fork 5
/
calc_within_domain.m
88 lines (79 loc) · 2.11 KB
/
calc_within_domain.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
function [z, varargout] = calc_within_domain(myfun, domain, limits, resolution)
%CALC_WITHIN_DOMAIN calculate myfun within limits where domain(x) > 0
%
% File: calc_within_domain.m
% Author: Ioannis Filippidis, [email protected]
% Date: 2010.12.21 -
% Language: MATLAB R2010b
% Purpose: function values within algebraically defined domain
% Copyright: Ioannis Filippidis, 2010-
N = numel(limits) /2;
X = cell(1, N);
for i=1:N
n = resolution(1, i);
xmin = limits(2 *(i -1) +1);
xmax = limits(2 *i);
X{1, i} = linspace(xmin, xmax, n);
varargout{i} = X{1, i};
end
%% N=2 (used for most calls, not general but faster)
% also specially arranged for surf (rows <-> y, columns <-> x)
z = zeros(resolution)';
if N == 2
n1 = resolution(1,1);
n2 = resolution(1,2);
for i=1:n1
for j=1:n2
x = [X{1, 1}(1, i);
X{1, 2}(1, j) ];
if domain(x)
z(j, i) = myfun(x);
else
z(j, i) = NaN;
end
end
end
return
end
%% N~=2
z = zeros(resolution);
k = [1, cumprod(resolution(1:end -1) ) ];
indices = ones(1, N);
flag = 1;
while flag
x = zeros(N, 1);
for i=1:(N-1)
x(i, 1) = X{1, i}(1, indices(1, i) );
end
% iterate over deepest dimension
n = resolution(1, N);
for i=1:n
indices(1, N) = i;
x(N, 1) = X{1, N}(1, i);
% linear index
ndx = 1;
for j=1:N
v = indices(1, j);
ndx = ndx + (v-1)*k(j);
end
% function evaluation
if domain(x)
z(ndx) = myfun(x);
else
z(ndx) = NaN;
end
end
% check if it is not full and increase deepest dimension full, 1 deeper
flag = 0;
for i=N:-1:1
n = resolution(1, i);
if indices(1, i) < n
indices(1, i) = indices(1, i) +1;
for j=(i+1):N
indices(1, j) = 1;
end
flag = 1;
end
end
end
z = z';