surf() to bar3() "Matrix dimensions must agree, not rendering mesh"

조회 수: 1 (최근 30일)
Huy
Huy 2014년 5월 7일
편집: mcan06 2021년 10월 18일
This is my attempt to simulate the water surface. It works fine when I use the surf() function. But when I change it to bar3(), this error occurs: "Matrix dimensions must agree, not rendering mesh". Can some one please tell me how to fix this? Thank you. Here's my code:
n=60;
i = 2:n-1;
j = 2:n-1;
H = ones(n,n);
Dropx=30; %x and y coordinate of the droplet
Dropy=30;
width=20;
r=width/2;
dt=0.1;
dx=0.3;
%%%add droplet to the surface %%%
[x,y] = ndgrid(-1.5:(2/(width/1.5-1)):1);
D = 8*exp(-5*(x.^2+y.^2));
w = size(D,1);
i2 = (Dropx-r):w+(Dropx-r)-1;
j2 = (Dropy-r):w+(Dropy-r)-1;
H(i2,j2) = H(i2,j2) + D;
oldH=H;
newH=H;
h=surf(newH); % cannot change this to bar3
axis([1 n 1 n -2 8]);
k=0.2; %damping constant
c=2; %wave speed
while 1==1
newH(i,j)=H(i,j)+(1-k*dt)*(H(i,j)-oldH(i,j))-...
dt^2*c^2/dx^2*((4*H(i,j)-H(i+1,j)-H(i-1,j)-H(i,j+1)-H(i,j-1))...
+0.4*(4*H(i,j)-H(i+1,j+1)-H(i+1,j-1)-H(i-1,j+1)-H(i-1,j-1)));
set(h,'Zdata', newH(i,j));
oldH=H;
H=newH;
pause(0.05);
end
  댓글 수: 1
mcan06
mcan06 2021년 10월 18일
편집: mcan06 2021년 10월 18일
Could you share with me if you had any reference which you derived the newH part of this code?

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

채택된 답변

Star Strider
Star Strider 2014년 5월 7일
That has to be the coolest simulation I’ve seen posted on MATLAB Answers!
I figured out how to make bar3 work.
1. Comment-out (or delete) this line ( 5 lines above the while loop ) and the axis line just below it:
% h=surf(newH); % cannot change this to bar3
2. Comment out (or delete) this line in the while loop:
% set(h,'Zdata', newH(i,j));
3. Insert these lines just below the commented-out ‘set(h, 'ZData' ...’ line:
h = bar3(newH);
% h=surf(newH);
axis([1 n 1 n -2 8]);
Then choose either bar3 or surf to see the result.
  댓글 수: 2
Huy
Huy 2014년 5월 8일
Thank you for your answer. So basically we have to replott the data at every timestep. This will be slow but it solves the problem. Thank you.
Star Strider
Star Strider 2014년 5월 8일
My pleasure!
You only have to replot it if you use bar3. Your original code works fine with surf. I didn’t time it, but I didn’t notice a difference between the original and the code revised to use bar3. (Actually, I was so awed just watching it that I didn’t notice any difference in performance.)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scripts에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by