How to use scatteredInterpolant in case of dimensions more than 3
2 views (last 30 days)
Show older comments
Hello everyone I am trying to do a multivariate interpolation and i get the following error :
Error using interpn
NTIMES must be a single numeric value.
Error in FVP (line 277)
phi{i,k} = interpn(X',Val_phi{i,k}');
The same code works fine for 2D , however in case of 2D i used
phi{i,k} = scatteredInterpolant(X',Val_phi{i,k}');
and here is the piece of code that genrates the error:
%% Interpolate
VAL_PHI_CONCAT = [];
for i = 1:size(Val_phi,1)
textwaitbar(i, size(Val_phi,1), "Interpolating")
for k = 1:size(Val_phi,2)
disp('size of X')
size(X)
disp('Size of val_phi')
size(Val_phi{i,k})
disp([num2str(i),num2str(k)])
phi{i,k} = interpn(X',Val_phi{i,k}');
phi{i,k} = @(x)(phi{i,k}(x')');
VAL_PHI_CONCAT = [VAL_PHI_CONCAT ; Val_phi{i,k} ];
end
end
Some information about the dimension of the variables :
X : 6 12010
Val_phi{i,k} : 1 12010
It works fine for the first iteration of i.e. for i=1 and k = 1 and then throws error for k=2 ,i=1
Any suggestion on what possibly I am doing wrong?
Thank you .
4 Comments
Bruno Luong
on 23 Aug 2022
I guess you just have tried INTERPN randomy without knowing what it does. That's why you still ask question about the error.
So we cannot take it as what kind of interpolation you want to do.
Accepted Answer
Bruno Luong
on 23 Aug 2022
Edited: Bruno Luong
on 24 Aug 2022
Here is a linear scattered interpolation in any-dimension.
It's bare calculation for a single query point, up to you to adapt for multiple point.
I think it become fragile in larger dimension, and to enhance the bobustess you might need to scale independent variables so they are unity.
n=6;
m=max(n+1,100);
% "Independent" variables
X=rand(m,n);
% "Dependent variable"
y=rand(m,1);
% Preparation, update only when X changes
S = delaunayn(X);
% Query point, take centroid as example
xq=mean(X,1);
% Interpolation
t = tsearchn(X,S,xq);
if isnan(t) % Fix bug
yq = NaN;
else
st = S(t,:);
M = X(st,:)-xq;
w = [zeros(1,n),1] / [M,ones(n+1,1)]; % NOTE: the last eqt might needs to be rescale to be "compatible" with M
yq = w*y(st);
end
yq
3 Comments
Bruno Luong
on 24 Aug 2022
griddata interpolation is alway better if your data have the right shape requirement.
More Answers (0)
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!