taking difference of 2 surf figures of different sizes

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?

 Akzeptierte Antwort

Jaimin
Jaimin am 10 Jan. 2025
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 Kommentare

In the general case, x1 does not completely overlap x2, and y1 does not completely overlap y2. In such a case
Z2_interpolated = interp2(X2, Y2, z2, X1, Y1, 'linear');
has a default extrapolation of NaN so you might get NaN for some of the output locations. z1 - Z2_interpolated would then have nan in some of the locations.
... and maybe that is okay, maybe it is not okay. The matter of how to deal with the fact that the ranges are different is not defined in the question.
Thank you. It will be also nice to consider the general case where they have no overlap.
I have come across another situation below
Error using griddedInterpolant
Sample points must be sorted in ascending order.
and hence interp2 doesn't work. Is there any workaround?
Torsten
Torsten am 11 Jan. 2025
Bearbeitet: Torsten am 11 Jan. 2025
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.
Thank you. Then I'll need to reorder them beforehand

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 10 Jan. 2025
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 Kommentar

Thank you so much for your answer Dr Walter. I like both answers! However it seems I'm not allowed to accept both answers.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by