taking difference of 2 surf figures of different sizes

2 ビュー (過去 30 日間)
feynman feynman
feynman feynman 2025 年 1 月 10 日
コメント済み: feynman feynman 2025 年 1 月 11 日
One is to compare two solutions and study the error by taking their difference. The two solutions are two surf figures generated by surf(x1,y1,z1) and surf(x2,y2,z2) but x1 and x2 are different in size and so are y1, y2, z1, z2. One can think of this as two solutions z1, z2 obtained on 2 meshes. Is there a smart way of taking z1-z2 and surf'ing it?

採用された回答

Jaimin
Jaimin 2025 年 1 月 10 日
To compare two solutions, z_1 and z_2", obtained on different meshes, you can interpolate one solution onto the grid of the other. This allows you to compute the difference, z_1 - z_2", on a common grid.
Here is a summarized approach:
  1. Use interpolation to map one solution's values onto the grid of the other. For instance, interpolate z_2 so that it aligns with the grid defined by x_1 and y_1.
  2. Once the interpolation is complete, calculate the difference between the two solutions on this common grid.
  3. Use a surface plot to visualize the difference, providing insights into how the two solutions vary across the grid.
Kindly refer to a following code snippet for understanding.
% Define the first mesh and solution
x1 = linspace(-5, 5, 50);
y1 = linspace(-5, 5, 50);
[X1, Y1] = meshgrid(x1, y1);
z1 = sin(sqrt(X1.^2 + Y1.^2)); % Example solution 1
% Define the second mesh and solution
x2 = linspace(-5, 5, 30);
y2 = linspace(-5, 5, 30);
[X2, Y2] = meshgrid(x2, y2);
z2 = cos(sqrt(X2.^2 + Y2.^2)); % Example solution 2
% Interpolate z2 onto the grid of z1
Z2_interpolated = interp2(X2, Y2, z2, X1, Y1, 'linear');
Z_diff = z1 - Z2_interpolated;
figure;
surf(X1, Y1, z1);
xlabel('X');
ylabel('Y');
zlabel('z1');
title('Solution 1');
colorbar;
shading interp;
figure;
surf(X2, Y2, z2);
xlabel('X');
ylabel('Y');
zlabel('z2');
title('Solution 2');
colorbar;
shading interp;
figure;
surf(X1, Y1, Z_diff);
xlabel('X');
ylabel('Y');
zlabel('Difference (z1 - z2)');
title('Difference between two solutions');
colorbar;
shading interp;
For more information kindly refer to following MathWorks documentation.
I hope this will be helpful.
  4 件のコメント
Torsten
Torsten 2025 年 1 月 11 日
編集済み: Torsten 2025 年 1 月 11 日
What do you mean by "workaround" ?
interp2 expects the independent variable vectors X and Y to be sorted in ascending order.
Thus
X = [0 3 6]
is allowed whereas
X = [0 6 3]
is not (same for Y).
If this is not true in your case, you have to reorder X, Y and Z accordingly.
feynman feynman
feynman feynman 2025 年 1 月 11 日
Thank you. Then I'll need to reorder them beforehand

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2025 年 1 月 10 日
N = 100;
G1 = griddedInterpolant(x1, y1, z1, 'linear', 'none');
G2 = griddedInterpolant(x2, y2, z2, 'linear', 'none');
[minx1, maxx1] = bounds(x1, 'all');
[minx2, maxx2] = bounds(x2, 'all');
minx = max(minx1, minx2);
maxx = min(maxx1, maxx2);
miny = max(miny1, miny2);
maxy = min(maxy1, maxy2);
[XQ, YQ] = ndgrid(linspace(minx, maxx,N), linspace(miny, maxy,N+1));
S1 = G1(XQ, YQ);
S2 = G2(XQ, YQ);
D12 = S1 - S2;
surf(XQ, YQ, D12, 'edgecolor', 'none');
This will only form the difference over the common x and y range. If you want the full range including the portions that do not overlap, then you need to define what you want the output to be in the areas where there is no overlap.
  1 件のコメント
feynman feynman
feynman feynman 2025 年 1 月 10 日
Thank you so much for your answer Dr Walter. I like both answers! However it seems I'm not allowed to accept both answers.

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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by