Mesh Grid of a 3d array

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;
end
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
err=abs(u(j,i,1)-v(j,i,1));
end
end
end
mesh(x,y,u-v);
xlabel('x');
ylabel('y');
zlabel('maxerror');

 Accepted Answer

Joel Lynch
Joel Lynch on 22 Jun 2021
Edited: Joel Lynch on 22 Jun 2021
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 Comments

SA
SA on 22 Jun 2021
Edited: SA on 22 Jun 2021
Thanks Joel but how do I use the slice function since I have no function for V?
Joel Lynch
Joel Lynch on 22 Jun 2021
Edited: Joel Lynch on 22 Jun 2021
If the size of err is 3D, for example
disp(size(err)); % returns, say, -> [5 10 3]
Then err is your V data in slice.
If the size of err is 2D, for example
disp(size(err)); % returns, say, -> [5 10]
then you are working with 2D data, and mesh will work. Which do you have? It looks like 2D from your code, but your description suggests 3D.
Remember that the data contained in any matrix takes up it's own dimension (Z-axis) or color (surf/mesh). So a 2D matrix needs either: (1) 3 dimension plot (using mesh/surf) or (2) a 2D plot and a colormap. A 3D matrix needs a 3 dimension plot AND a color map, but can still only show slices because it cannot show a volume with different colors.
So my vectors are of dimension xd^3, it was a 3d array in x,y and z directions but converted to a vectors so now I need the meshgrid of the difference betweent the vectors but the mesh/surf functions use matrices and I not sure how to convert the vectors to matrices since there were 3d arrays in order to use the mesh/surf functions.
Ok i will try the
disp(size(err)); % returns, say, -> [5 10 3]
thanks
Can you include/attach the data for xx,yy, and zz? - that will help.
SA
SA on 22 Jun 2021
Edited: SA on 22 Jun 2021
load analytical_soln.txt
xx = importdata('analytical_soln.txt');
load recovered_potential.txt
yy = importdata('recovered_potential.txt');
zz = abs(xx-yy);
Okay, how does this look? - I used the reshape command to convert error to a 3D matrix, then plotted with slice.
% Load Data
load analytical_soln.txt
exact_solution = importdata('analytical_soln.txt');
load recovered_potential.txt
computed_solution = importdata('recovered_potential.txt');
% Compute Error
error = abs(exact_solution-computed_solution);
% Get number of elements from cubed-root of the number of elements in error
xd = nthroot(numel(error),3);
% Create x/y/z vectors, and X/Y/Z Meshgrid
x = linspace( -3, 3, xd ); y = x; z = x;
[X,Y,Z] = meshgrid(x,y,z);
% Reshape error to 3D matrix
error = reshape(error,[xd,xd,xd]);
% 3D Slice plot
xslice = [0, 3];
yslice = [0, 3];
zslice = [-3, 0];
slice(X,Y,Z,error,xslice,yslice,zslice);
xlabel('x');
ylabel('y');
zlabel('z');
colorbar
title('3D Error Plot with slice')
SA
SA on 22 Jun 2021
thanks so much Joel. this is what i needed. I appreciate the help.

Sign in to comment.

More Answers (0)

Products

Tags

Asked:

SA
on 21 Jun 2021

Commented:

SA
on 22 Jun 2021

Community Treasure Hunt

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

Start Hunting!