scatteredInterpolant function gives error in input data if I try to interpolate a vectorial field where for each point I have 2 values, but in the doc it says that is allowed.

19 ビュー (過去 30 日間)
I have the following problem where I have a set of nodes produced by the generateMesh function so they are not orthered along cartesian axes and so I decided to use the fuction scatteredInterpolant to interpolate them.
nodes is (1133x2)
ic_sol_1 and ic_sol_2 are (1133x1) and so v is (1133x2) too.
nodes = transpose(model.Mesh.Nodes);
v = [ic_sol_1(:) ic_sol_2(:)];
F_ic = scatteredInterpolant(nodes,v)
that produces the following error: "Error using scatteredInterpolant
Input data point values have invalid dimension. The data must be specified in column-vector format.
Error in NonLinearEq (line 139)
F_ic = scatteredInterpolant(nodes,v)"
I also tried:
v = [ic_sol_1 ic_sol_2];
But nothing changed.
I am referring to the documentation where it says that is possible to interpolate at same sample points multiple values : https://it.mathworks.com/help/matlab/ref/scatteredinterpolant.html#mw_3cb9d79a-f24e-4832-a401-5e076b0b1d6b
s1 = sin(x).^4 .* cos(y);
s2 = sin(x) + cos(y);
s3 = x + y;
s4 = x.^2 + y;
v = [s1 s2 s3 s4];
F = scatteredInterpolant(x,y,v)
My code works instead if I use a singe column vector as data points, in fact the command:
F_ic = scatteredInterpolant(nodes, ic_sol_1)
produces the correct interpolant:
F_ic =
scatteredInterpolant with properties:
Points: [1133×2 double]
Values: [1133×1 double]
Method: 'linear'
ExtrapolationMethod: 'linear'
If is not possible use scatteredInterpolant with multiple values data how can I reshape my nodes coordinates in order to be able to use the function interp2()?

採用された回答

Bruno Luong
Bruno Luong 2024 年 1 月 16 日
編集済み: Bruno Luong 2024 年 1 月 16 日
I guess you use an old MATLAB version that doesn't support vector interpolation.
EDIT the doc is for 2023b more recent than your MATLAB version 2022b.
workaround : Just call scatteredInterpolant twice
x=rand(100,1);
y=rand(100,1);
v=rand(100,2);
f1 = scatteredInterpolant(x,y,v(:,1))
f1 =
scatteredInterpolant with properties: Points: [100×2 double] Values: [100×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
f2 = scatteredInterpolant(x,y,v(:,2))
f2 =
scatteredInterpolant with properties: Points: [100×2 double] Values: [100×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
f = @(xq,yq) [f1(xq(:),yq(:)), f2(xq(:),yq(:))];
xq = rand(10,1)
xq = 10×1
0.3260 0.2209 0.1422 0.6388 0.5312 0.1740 0.1119 0.4486 0.1193 0.2915
yq = rand(10,1)
yq = 10×1
0.8792 0.9518 0.4958 0.6329 0.7724 0.8419 0.9029 0.9683 0.5188 0.1965
f(xq,yq)
ans = 10×2
0.0939 0.5674 0.0510 0.1969 0.6814 0.5922 0.6164 0.1582 0.1885 0.1274 0.3126 0.6081 0.6307 0.2696 0.5001 0.4917 0.4459 0.4857 0.4815 0.2799
% This check only works on recents MATLAB version
fv = scatteredInterpolant(x,y,v);
fv(xq,yq)
ans = 10×2
0.0939 0.5674 0.0510 0.1969 0.6814 0.5922 0.6164 0.1582 0.1885 0.1274 0.3126 0.6081 0.6307 0.2696 0.5001 0.4917 0.4459 0.4857 0.4815 0.2799
  2 件のコメント
Giacomo Marco La Montagna
Giacomo Marco La Montagna 2024 年 1 月 16 日
Thank you for pointing out that the problem could be the non-updated version of MATLAB I am using. In the meanwhile I will have updated my software the solution you suggest works perfectly.
Steven Lord
Steven Lord 2024 年 1 月 16 日
FYI the Release Note for the change to allow scatteredInterpolant to use multivalued interpolation is here and confirms what Bruno said, that this change was made in release R2023b.
In earlier releases, rather than creating two scatteredInterpolant objects, if you could perform the interpolation in multiple steps rather than all at once you might be able to save some time by creating the object once and changing its Values property to the data you want to interpolate (leaving the points at which your values are located unchanged.) See the "Replacement of Sample Values" example on the scatteredInterpolant documentation page.

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

その他の回答 (1 件)

Hassaan
Hassaan 2024 年 1 月 16 日
編集済み: Hassaan 2024 年 1 月 16 日
@Giacomo Marco La Montagna A possible recommendations.
The function scatteredInterpolant in MATLAB is used to perform interpolation on a scattered dataset that cannot be gridded without introducing significant errors. From the error message you received, it seems there is an issue with the dimensions of the input arrays.
When you call scatteredInterpolant, the inputs must satisfy the following conditions:
  • The nodes input must be an M-by-N matrix, where M is the number of points and N is the number of dimensions (in your case, N=2 for x and y coordinates).
  • The v input must be an M-by-P matrix where M is the number of points (which should match the number of nodes) and P is the number of values you want to interpolate at each point (in your case, P=2 for ic_sol_1 and ic_sol_2).
Your v should be a two-column matrix where each column corresponds to one of the functions you want to interpolate (ic_sol_1 and ic_sol_2). The columns represent different value sets, and each value set should be a column vector. It seems you've structured v correctly based on the code you provided.
The error suggests that MATLAB is not interpreting v as an M-by-2 matrix. This could be due to a few reasons:
  • The dimensions of nodes and v might not match correctly.
  • The v array might not be properly formatted as a column vector for each variable you are interpolating.
If you are sure that ic_sol_1 and ic_sol_2 are column vectors of the same length as nodes, your code should work. Let's double-check your variables' dimensions to ensure they are in the correct format:
size(nodes) % Should return [1133 2]
size(ic_sol_1) % Should return [1133 1]
size(ic_sol_2) % Should return [1133 1]
If all dimensions are correct, the following code should work:
v = [ic_sol_1, ic_sol_2]; % Concatenate the vectors side by side
F_ic = scatteredInterpolant(nodes, v);
Regarding interp2, this function is not suitable for scattered data directly as it requires data to be on a regular grid. If you have scattered data and you want to use interp2, you would first need to create a grid (using `grid
data` for example) and then interpolate your scattered data onto this grid. The problem with this approach is that it can introduce significant errors if your data is not well-suited to a grid due to irregular spacing.
If you want to use interp2 and you need to create a grid from scattered data, you could use griddata to interpolate onto a regular grid:
% Define a grid
[xq, yq] = meshgrid(linspace(min(nodes(:,1)), max(nodes(:,1)), numPoints), ...
linspace(min(nodes(:,2)), max(nodes(:,2)), numPoints));
% Interpolate onto the grid for each set of values
vq1 = griddata(nodes(:,1), nodes(:,2), ic_sol_1, xq, yq, 'linear');
vq2 = griddata(nodes(:,1), nodes(:,2), ic_sol_2, xq, yq, 'linear');
This gives you vq1 and vq2, which are the interpolated values on the grid, and you can now use interp2 with these gridded data. However, if your original data is not too scattered or has some structure to it, you may be able to form a grid without much error and then use interp2.
But if you can resolve the dimensionality issue with scatteredInterpolant, it would be a better choice since it's designed for scattered data and you won't need to generate an intermediate grid.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 件のコメント
Giacomo Marco La Montagna
Giacomo Marco La Montagna 2024 年 1 月 16 日
I really appreciated your suggestion: very pertinent and well explained. However the dimensions of my inputs were correct and the problem was the oudated software. Nevertheless I found very usefull the second part about the grid conversion, thank you!

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

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by