- 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”.
- Once the interpolation is complete, calculate the difference between the two solutions on this common grid.
- Use a surface plot to visualize the difference, providing insights into how the two solutions vary across the grid.
taking difference of 2 surf figures of different sizes
조회 수: 5 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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:
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
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.
추가 답변 (1개)
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!