How to compare between two surface plots in matlab?

A and B are two 2D matrices with dimensions (length(X), length(Y)) which are function of X and Y vectors. I plot A and B in a 3D graph using surf as follows:
figure(1)
surf(X,Y,A)
hold on
surf(X,Y,B)
hold on
But, the generated figure doesn't show the difference in values between A and B clearly.
Is there any other way where I can compare between A and B in a 3D plot and show the differences between them clearly?

 채택된 답변

Star Strider
Star Strider 2020년 11월 12일
Perhaps:
figure(1)
surf(X,Y,A, 'FaceAlpha',0.5)
hold on
surf(X,Y,B, 'FaceAlpha',0.5)
hold off
.

댓글 수: 8

Thankyou. This helps me to make my idea little clear. Is there any other way to make it more clear?
My pleasure!
It would be worth experimenting with the 'FaceAlpha' property for both surf plots in order to determine what combination works best for you. Without your data (and a clear idea of what you want), I can only guess.
Adi Nor
Adi Nor 2020년 11월 12일
편집: Adi Nor 2020년 11월 12일
I need to show the difference between the two matrices more clearer because the values of matrix A is obtained from my optimization, wheras the values of B is obtained from the literature and I want to show the siginficant improvenment in the values of A compared to B through plotting the both matrices. I can give you the data:
X = -20:10:80; %length(X)=11
Y = 2:2:8; %length(Y)=4
%my optimized matrix A (4*11)
A = [0.0046 0.0442 0.3160 1.3217 3.5302 5.8383 6.9030 7.1149 7.1335 7.1327 7.0763;
0.0047 0.0450 0.3329 1.4479 4.1525 6.5922 7.6623 7.8441 7.8713 7.8725 7.8008;
0.0048 0.0466 0.3562 1.5350 4.5591 7.2824 8.5957 8.8450 8.8786 8.8067 8.8070;
0.0049 0.0475 0.3795 1.5952 4.7382 7.9092 9.5009 9.8884 9.9292 9.8430 9.8434]
%matrix B from previous work (4*11)
B = [0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.5441 6.4802;
0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.5441 6.4802;
0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.4801 6.4802;
0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.4801 6.4802]
Try this:
[X,Y] = ndgrid(-2:0.5:2);
Z1 = X.^2 + Y.^2;
Z2 = X.^2 + Y.^2 + 5;
figure
surf(X, Y, Z1, 'FaceAlpha',0.5)
hold on
surf(X, Y, Z2, 'FaceAlpha',0.5)
plot3((X(:)*[1 1]).', (Y(:)*[1 1]).', [Z1(:) Z2(:)], '-r', 'LineWidth',2) % Draw Vertical Red Lines Between The Surfaces
hold off
grid on
It plots both surfaces, then draws red lines connecting the points of both of them. (They must be defined over the same (X,Y) grids.) Because off the 'FaceAlpha' properties, the vertical lines are visible for the entire plot.
Is this code applicable for my data listed in the previous comment? what's Z1 and Z2? and why ndgrid(-2:0.5:2)?
I think it needs some modifications?
I did not see your previous Comment until now. (I was likely writing mine at the same time.)
My code appears to work with your data (I renamed your ‘X’ and ‘Y’ as ‘Xv’ and ‘Yv’ respectively):
[X,Y] = meshgrid(Xv, Yv);
figure
surf(X, Y, A, 'FaceAlpha',0.5)
hold on
surf(X, Y, B, 'FaceAlpha',0.5)
plot3((X(:)*[1 1]).', (Y(:)*[1 1]).', [A(:) B(:)], '-r', 'LineWidth',2) % Draw Vertical Red Lines Between The Surfaces
hold off
grid on
producing this plot:
.
Thank you! it works well now.
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 12일
Sometime plotting the difference can be helpful for visualization
figure(1)
surf(X,Y,A-B)

댓글 수: 7

I understand your idea, but I don't need to plot the difference. I need to show it by plotting the two matrices. This is because the values of matrix A is obtained from my optimization, wheras the values of B is obtained from the literature and I want to show the siginficant improvenment in the values of A compared to B through plotting the both matrices.
Ok. In that case, I think plotting the estimated values as surface, and the original data points as scattered points will be helpful. For example
figure(1)
surf(X,Y,A)
hold on
scatter3(X(:),Y(:),B(:), 'r+')
hold on
I have an error using your proposed code
Error using scatter3 (line 64)
X, Y and Z must be vectors of the same length.
It should be noted that
X=-20:10:8; %length(X)=11
Y=2:2:8; %length(Y)=4
A and B are a 4*11 2D matrix
Ok, If X and Y are vectors then modify the code to this
figure(1)
surf(X,Y,A)
hold on
[Xg, Yg] = meshgrid(X, Y);
scatter3(Xg(:),Yg(:),B(:), 'r+')
hold on
The data is given as follows:
X = -20:10:80; %length(X)=11
Y = 2:2:8; %length(Y)=4
%my optimized matrix A (4*11)
A = [0.0046 0.0442 0.3160 1.3217 3.5302 5.8383 6.9030 7.1149 7.1335 7.1327 7.0763;
0.0047 0.0450 0.3329 1.4479 4.1525 6.5922 7.6623 7.8441 7.8713 7.8725 7.8008;
0.0048 0.0466 0.3562 1.5350 4.5591 7.2824 8.5957 8.8450 8.8786 8.8067 8.8070;
0.0049 0.0475 0.3795 1.5952 4.7382 7.9092 9.5009 9.8884 9.9292 9.8430 9.8434]
%matrix B from previous work (4*11)
B = [0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.5441 6.4802;
0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.5441 6.4802;
0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.4801 6.4802;
0.0047 0.0445 0.3077 0.8624 2.4259 4.9777 6.2328 6.5002 6.5413 6.4801 6.4802]
Try the code in my last comment.
Thank you! .. your solution is acceptable for me.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

질문:

2020년 11월 12일

댓글:

2020년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by