Use of interp2 in an arbitrary dataset
조회 수: 15 (최근 30일)
이전 댓글 표시
Hello,
I would appreciate any help on the following issue.
I have a small dataset (3x3 matrix) which descirbes a mechanical property (say Z) in 2d space. Each line describes the mechanical property in the 2d space, hence I basically have 3 values of Z in the 2d space. Column 1 has the x-coordinate, column 2 has the y-coordinate and column 3 the respective value for Z.
For the sake of an example, my data looks like this:
X Y Z
0.25 0.25 0.5
0.50 0.60 1.5
0.75 0.35 3.0
I want to interpolate (between the given values) and extrapolate (from 0 up to 1 for both X and Y), ideally using spline, cubic or makima.
My plan was to use the following code:
x=[0.25;0.50;0.75];
y=[0.25;0.60;0.35];
[X,Y] = meshgrid(x,y);
Z = [0.5; 1.5; 3.0];
[Xq,Yq] = meshgrid(0:.01:1, 0:.01:1);
Zq = interp2(X,Y,Z,Xq,Yq,'spline');
However, when I try that, I get the following error:
Error using griddedInterpolant
Interpolation requires at least two sample points for each grid dimension.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
Could anyone advise?
Thanks
댓글 수: 0
답변 (2개)
Walter Roberson
2024년 1월 27일
x=[0.25;0.50;0.75];
y=[0.25;0.60;0.35];
Z = [0.5; 1.5; 3.0];
[Xq,Yq] = meshgrid(0:.01:1, 0:.01:1);
F = scatteredInterpolant(x, y, Z);
Zq = F(Xq, Yq);
surf(Xq, Yq, Zq)
댓글 수: 4
Torsten
2024년 1월 27일
편집: Torsten
2024년 1월 27일
If your data are not gridded, you will have to live with "ScatteredInterpolant" and its interpolation methods.
And your four points in 2d constitute a curve. So Z could be interpolated on this curve maybe, but it makes little sense to treat them as sufficient to interpolate on a real two-dimensional rectangle.
x=[0.25;0.50;0.75;0.9];
y=[0.25;0.60;0.35; 0.7];
Z = [0.5; 1.5; 3.0; 3.5];
plot3(x,y,Z)
grid on
Sulaymon Eshkabilov
2024년 1월 27일
Is this what you wanted to obtain:
% Given DATA:
Data = [ 0.25 0.25 0.5;
0.50 0.60 1.5;
0.75 0.75 3.0];
% Extract x, y, and z from Data
x = Data(:, 1);
y = Data(:, 2);
z = Data(:, 3);
[X,Y]=meshgrid(x,y);
Z = meshgrid(z);
% Interpolate using griddata
[Xq,Yq] = meshgrid(0:.01:1);
Zq = interp2(X,Y,Z,Xq,Yq,'spline');
% Plot the results
figure;
scatter3(x, y, z, 'ro', 'filled'); % Original data points in red
hold on;
surf(Xq, Yq, Zq, 'EdgeColor', 'none', 'FaceAlpha', 0.5); % Interpolated/extrapolated surface
xlabel('X');
ylabel('Y');
zlabel('Z(X,Y)');
title('Interpolation and Extrapolation of Mechanical Property');
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!