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()?
0 个评论
采纳的回答
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))
f2 = scatteredInterpolant(x,y,v(:,2))
f = @(xq,yq) [f1(xq(:),yq(:)), f2(xq(:),yq(:))];
xq = rand(10,1)
yq = rand(10,1)
f(xq,yq)
% This check only works on recents MATLAB version
fv = scatteredInterpolant(x,y,v);
fv(xq,yq)
2 个评论
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!