-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbfcreate.m
194 lines (164 loc) · 5.5 KB
/
rbfcreate.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
function options = rbfcreate(x, y, varargin)
%RBFCREATE Creates an RBF interpolation
% OPTIONS = RBFSET(X, Y, 'NAME1',VALUE1,'NAME2',VALUE2,...) creates an
% radial base function interpolation
%
% RBFCREATE with no input arguments displays all property names and their
% possible values.
%
%RBFCREATE PROPERTIES
%
%
% Alex Chirokov, [email protected]
% 16 Feb 2006
tic;
% Print out possible values of properties.
if (nargin == 0) & (nargout == 0)
fprintf(' x: [ dim by n matrix of coordinates for the nodes ]\n');
fprintf(' y: [ 1 by n vector of values at nodes ]\n');
fprintf(' RBFFunction: [ gaussian | thinplate | cubic | multiquadrics | {linear} ]\n');
fprintf(' RBFConstant: [ positive scalar ]\n');
fprintf(' RBFSmooth: [ positive scalar {0} ]\n');
fprintf(' Stats: [ on | {off} ]\n');
fprintf('\n');
return;
end
Names = [
'RBFFunction '
'RBFConstant '
'RBFSmooth '
'Stats '
];
[m,n] = size(Names);
names = lower(Names);
options = [];
for j = 1:m
options.(deblank(Names(j,:))) = [];
end
%**************************************************************************
%Check input arrays
%**************************************************************************
[nXDim nXCount]=size(x);
[nYDim nYCount]=size(y);
if (nXCount~=nYCount)
error(sprintf('x and y should have the same number of rows'));
end;
if (nYDim~=1)
error(sprintf('y should be n by 1 vector'));
end;
options.('x') = x;
options.('y') = y;
%**************************************************************************
%Default values
%**************************************************************************
options.('RBFFunction') = 'linear';
options.('RBFConstant') = (prod(max(x')-min(x'))/nXCount)^(1/nXDim); %approx. average distance between the nodes
options.('RBFSmooth') = 0;
options.('Stats') = 'off';
%**************************************************************************
% Argument parsing code: similar to ODESET.m
%**************************************************************************
i = 1;
% A finite state machine to parse name-value pairs.
if rem(nargin-2,2) ~= 0
error('Arguments must occur in name-value pairs.');
end
expectval = 0; % start expecting a name, not a value
while i <= nargin-2
arg = varargin{i};
if ~expectval
if ~isstr(arg)
error(sprintf('Expected argument %d to be a string property name.', i));
end
lowArg = lower(arg);
j = strmatch(lowArg,names);
if isempty(j) % if no matches
error(sprintf('Unrecognized property name ''%s''.', arg));
elseif length(j) > 1 % if more than one match
% Check for any exact matches (in case any names are subsets of others)
k = strmatch(lowArg,names,'exact');
if length(k) == 1
j = k;
else
msg = sprintf('Ambiguous property name ''%s'' ', arg);
msg = [msg '(' deblank(Names(j(1),:))];
for k = j(2:length(j))'
msg = [msg ', ' deblank(Names(k,:))];
end
msg = sprintf('%s).', msg);
error(msg);
end
end
expectval = 1; % we expect a value next
else
options.(deblank(Names(j,:))) = arg;
expectval = 0;
end
i = i + 1;
end
if expectval
error(sprintf('Expected value for property ''%s''.', arg));
end
%**************************************************************************
% Creating RBF Interpolatin
%**************************************************************************
switch lower(options.('RBFFunction'))
case 'linear'
options.('rbfphi') = @rbfphi_linear;
case 'cubic'
options.('rbfphi') = @rbfphi_cubic;
case 'multiquadric'
options.('rbfphi') = @rbfphi_multiquadrics;
case 'thinplate'
options.('rbfphi') = @rbfphi_thinplate;
case 'gaussian'
options.('rbfphi') = @rbfphi_gaussian;
otherwise
options.('rbfphi') = @rbfphi_linear;
end
phi = options.('rbfphi');
A=rbfAssemble(x, phi, options.('RBFConstant'), options.('RBFSmooth'));
b=[y'; zeros(nXDim+1, 1)];
%inverse
rbfcoeff=A\b;
%SVD
% [U,S,V] = svd(A);
%
% for i=1:1:nXCount+1
% if (S(i,i)>0) S(i,i)=1/S(i,i); end;
% end;
% rbfcoeff = V*S'*U*b;
options.('rbfcoeff') = rbfcoeff;
if (strcmp(options.('Stats'),'on'))
fprintf('%d point RBF interpolation was created in %e sec\n', length(y), toc);
fprintf('\n');
end;
function [A]=rbfAssemble(x, phi, const, smooth)
[dim n]=size(x);
A=zeros(n,n);
for i=1:n
for j=1:i
r=norm(x(:,i)-x(:,j));
temp=feval(phi,r, const);
A(i,j)=temp;
A(j,i)=temp;
end
A(i,i) = A(i,i) - smooth;
end
% Polynomial part
P=[ones(n,1) x'];
A = [ A P
P' zeros(dim+1,dim+1)];
%**************************************************************************
% Radial Base Functions
%**************************************************************************
function u=rbfphi_linear(r, const)
u=r;
function u=rbfphi_cubic(r, const)
u=r.*r.*r;
function u=rbfphi_gaussian(r, const)
u=exp(-0.5*r.*r/(const*const));
function u=rbfphi_multiquadrics(r, const)
u=sqrt(1+r.*r/(const*const));
function u=rbfphi_thinplate(r, const)
u=r.*r.*log(r+1);