How to use scatteredInterpolant in case of dimensions more than 3

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 Commenti

"X : 6 12010
Val_phi{i,k} : 1 12010"
Something is not right.
If you are working in six dimension for X, then Val_phi cannot belong to the 6-dimensional space where X lives.
You want to interpolate in dimension 12010 with 6 points? Then this must be a joke.
I think i am taking the transpose of the matrix X and vector Val_phi{i,k} : interpn(X',Val_phi{i,k}')
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.
So i tried doing the same thing on a small problem of where the data has two independent columns and I used scatterInterpolant , and everything worked fine. Now I am trying to generlize it to 6 independent columns and later to 18.
This is what I have been doing so far:
So My actual problem has 6 dimensions of independent data, I assumed the first five columns are independent variables and the fifth column is the dependent variable that I seek an interpolation value for.
To use interpn, I need to supply the function with a multidimensional array – with the number of rows matching the number of rows of independent variables ?

Accedi per commentare.

 Risposta accettata

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 Commenti

I put here the code for multiple query points and also to show the results is idetical to scatteredInterpolation in lower dimensions (2 or 3).
Note that the time needed increase very rapidly with the dimension (taken by delaunayn), which is expected. It is reasonable to do interpolation not far way from 3.
n=2; % dimension
m=max(n+1,1000); % number of points
% "Independent" variables
X =rand(m,n)-0.5;
% "Dependent variable", define multi-raviable function that maps X to y
if n==2
% peaks function as example
fun = @(X) peaks(5*X(:,1),10*X(:,2));
else
fun = @(X) sin(sqrt(sum(X.^2,2))*pi/2);
end
y = fun(X);
% Preparation, update only when X changes
S = delaunayn(X);
% Random query points
xq = (rand(10,n)-0.5);
% Allocation
yq = nan(size(xq,1),1);
% Linear interpolation
t = tsearchn(X,S,xq);
inside = ~isnan(t);
p = sum(inside); % number of points inside the convex hull of X
st = S(t(inside),:); % p x n+1
M = reshape(X(st,:),[p, n+1, n]);
M = M - reshape(xq(inside,:),[p, 1, n]);
M = permute(M,[2 3 1]); % n+1 x n x p
% bary centric coordinates
w = pagemrdivide([zeros(1,n),1], [M,ones(n+1,1,p)]);
w = reshape(w,[n+1,p]).';
yq(inside) = sum(w.*y(st),2);
% Check
% the above implementation result
yq
yq = 10×1
1.2102 1.5261 -0.1406 0.0735 -0.0000 1.6086 0.0000 3.3251 0.0003 0.0039
if ismember(n, [2 3])
% it should be equal to (at rounding precision)
% to MATLAB scattered interpolation
f=scatteredInterpolant(X,y);
yf = f(xq)
else
% it should be "close" to model
yf = fun(xq)
end
yf = 10×1
1.2102 1.5261 -0.1406 0.0735 -0.0000 1.6086 0.0000 3.3251 0.0003 0.0039
Thank you very much for taking time to resolve my problem, I really appreciate it.
I used griddatan and the time taken to process this increases significantly even with the increase in the number of data points ( keeping the dimension of the data constant).
I will try your code and will update you how it does.
Once again I really appreciate your help.
Thank you.
griddata interpolation is alway better if your data have the right shape requirement.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interpolation in Centro assistenza e File Exchange

Prodotti

Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by