Mesh Grid of a 3d array

조회 수: 44(최근 30일)
SA 2021년 6월 21일
댓글: SA 2021년 6월 22일
I want to plot the error when I compare a numerical approximation and an exact solution. I want to use the matlab mesh function (mesh(X,Y,Z)) to visualise the error approximation. With the mesh function the Z component has to be a matrix. My numerical approximation and exact solution are all vectors but they were 3d arrays convected to vectors. How do I use the mesh function to plot the error given that the Z component has to be a matrix? I tried the code below but I am not sure if it's right. Any help will be greatly appreciated. In the code xx (numerical approximation) and yy(exact solution) are vectors of dimension xd^3 and zz is the difference between xx and yy.
xd = nthroot(length(zz),3);
yd = xd;
u = zeros(xd,yd,1);
v = zeros(xd,yd,1);
xstart = -3.0;
xend = 3.0;
h = (xend-xstart)/(xd-1);
x = zeros(xd,1);
y = zeros(xd,1);
for i = 1:xd
x(i) = xstart + (i-1)*h;
y(i) = xstart + (i-1)*h;
err = 0.0;
for i = 1:xd
for j = 1:yd
u(j,i,1) = xx((j-1)*xd+i);
v(j,i,1) = yy((j-1)*xd+i);
if abs(u(j,i,1)-v(j,i,1)) > err %error

채택된 답변

Joel Lynch
Joel Lynch 2021년 6월 22일
편집: Joel Lynch 2021년 6월 22일
So this may be due to confusion about how mesh() works. Mesh produces a 2D surface in a 3D volume, with the Z-axis being the value of the matrix at each X/Y point. If your solution's are truly 3D (a value defined at each X/Y/Z point), mesh cannot really plot any more than one 2D slice at a time (at least cleanly). A good alternative would be to represent error as color in a 3D plot, using slice, but you won't be able to see every point at once.
You could also present the data a few other ways: 1. animated or subplotted 2D frames, plotting 1 2D "slice" at a time, 2. A single Mesh() showing the average or maximum error along the Z axis at each X/Y, and 3. Using something like histogram(err(:)) to see the breakdown of all the errors.
Also, you can greatly simplify the creation of x/y vectors by using linspace:
x = linspace( xstart, xend, nthroot(numel(zz),3) ); y = x;
and you can vectorize (compute all at once) the error generation by:
err = abs(u-v)
The if branch is useless, as error will always be >=0.
  댓글 수: 7
SA 2021년 6월 22일
thanks so much Joel. this is what i needed. I appreciate the help.

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

추가 답변(0개)



Community Treasure Hunt

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

Start Hunting!

Translated by