4-D Interpolation using interpn

Hi,
I have 4 vectors as input and 1 vector as output. I want to do an interpolation following the instructions given here:
where Xq1, ..., Xq4 are scalars and equal to Xqi = Xi(1) (it is just a test).
However, I got the following error:
Interpolation requires at least two sample points for each grid dimension.
If I look at section 4-D Interpolation, it can be seen that 4D grids are constructed and after these points are passed through the function and a 4D structure is obtained for V.
In my case, I don't have a function. I just have a vector for the output V.
How I can make it work with my vector type of data?

13 件のコメント

Jan
Jan 2022 年 11 月 26 日
"I have 4 vectors as input and 1 vector as output."
X1,X2,X3,X4,V,Xq1,Xq2,Xq3,Xq4 - these are 9 inputs.
mary
mary 2022 年 11 月 26 日
Jan,
X1,X2,X3,X4 are 4 input vectors and V the corresponding output. Xq2,Xq3,Xq4 are the query points. In the link, it is written:
"Vq = interpn(X1,X2,...,Xn,V,Xq1,Xq2,...,Xqn) returns interpolated values of a function of n variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X1,X2,...,Xn contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq1,Xq2,...,Xqn contain the coordinates of the query points."
For 1D application, they use: vq = interpn(x,v,xq,'cubic');
So, probably, we cannot apply it as Vq = interpn(X1,X2,X3,X4,V,Xq1,Xq2,Xq3,Xq4) for my case. That is why in the link, they use ngrid. But the problem is that in that link for a 4D interpolation there is a function f(x,y,z,t) that can give a value for any combination of x, y, z. In my case, I have an output vector for the specific values of 4 input vectors.
Torsten
Torsten 2022 年 11 月 26 日
What is size(X1),size(X2),size(X3), size(X4) and size(V) in your case ?
mary
mary 2022 年 11 月 26 日
they all have the same size which is 597x1.
Torsten
Torsten 2022 年 11 月 26 日
編集済み: Torsten 2022 年 11 月 26 日
But if X1,X2,X3,X4 have size 597 x 1, V must have size 597x597x597x597
You must supply V(i,j,k,l) for X1(i),X2(j),X3(k),X4(l) in order to make a grid interpolation using interpn.
mary
mary 2022 年 11 月 26 日
Ok, I see. So you mean that each time one parameter changes and the other 3 remain constants. Like the example given in that link and reported here, we see that in each row, X1 remains unchanged while X2 is changing.
[X1,X2] = ndgrid(-1:3,(1:4))
Otherwise, we cannot do the interpolation. Is it right?
Torsten
Torsten 2022 年 11 月 26 日
編集済み: Torsten 2022 年 11 月 26 日
Otherwise, we cannot do the interpolation. Is it right?
One can always interpolate somehow, but not with interpn.
Are your points (X1,X2,X3,X4) somehow regularly distributed in 4-dimensional space or wildly scattered ?
Maybe a fit for a, b, c and d with the equation
V = a*X1 + b*X2 + c*X3 + d*X4
can give satisfactory results.
Try "fun" as linear approximation function for V:
A = [X1,X2,X3,X4];
b = V;
sol = A\b;
fun = @(x1q,x2q,x3q,x4q) sol(1)*x1q+sol(2)*x2q+sol(3)*x3q+sol(4)*x4q
mary
mary 2022 年 11 月 26 日
編集済み: mary 2022 年 11 月 26 日
Thank you Torsten for your response. Actually the function that links X1, X2, X3 and X4 to V is highly non-linear. My objective is to build a look-up table that can be coupled to another program. So, if there is any best practice to make this lookup table, I will follow. So, this means that I can make my (X1,X2,X3,X4) regularly distributed. What are the best practice to follow? Make one parameter constant, and make 3 other change?
Torsten
Torsten 2022 年 11 月 27 日
So you have a code to generate V from given values for X1,...,X4 ? And in order to save computing time, you want to build a look-up table instead of calling the function that calculates V from X1,...,X4 every time ?
mary
mary 2022 年 11 月 27 日
Yes, that is what I want to do.
Matt J
Matt J 2022 年 11 月 27 日
編集済み: Matt J 2022 年 11 月 27 日
X1,X2,X3,X4 are 4 input vectors and V the corresponding output. Xq2,Xq3,Xq4 are the query points.
I suggest attaching them in a .mat file, so that we can run the interpolation ourselves.
The error message is complaining that one of X1,X2,X3,X4 is a scalar, rather than a 597x1 vector, as you expect it to be.
Torsten
Torsten 2022 年 11 月 27 日
Yes, that is what I want to do.
Then why don't you call your function V(X1,X2,X3,X4) over a full grid as required by "interpn", save the results in an array V(x1,x2,x3,x4) and then use this V-array for a table-lookup by "interpn" ?
Something like
for i = 1:597
for j = 1:597
for k = 1:597
for l = 1:597
V(i,j,k,l) = function_for_V(X1(i),X2(j),X3(k),X4(l));
end
end
end
end
mary
mary 2022 年 11 月 27 日
I'll do that. Thank you.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation についてさらに検索

質問済み:

2022 年 11 月 26 日

コメント済み:

2022 年 11 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by