# Reconstructing a surface from mesh points and function values (from COMSOL)

15 views (last 30 days)
Oliver Bond on 9 Nov 2022
Commented: Bjorn Gustavsson on 29 Nov 2022
Hi all,
After having gotten more to grips with MATLAB/COMSOL LiveLink, I think I have found how to extract the mesh vertex coordinates and values of the dependent variable at those points.
I have stored these in the variables x, y, and z.
When I run plot3(x,y,z) I get the following (note that x and y are from a triangular mesh and not a rectangular grid):
I then run the following code:
x = x'; y = y'; z = z'; % Making sure these are column vectors
F = scatteredInterpolant(x,y,z);
[X,Y] = meshgrid(x,y);
Z = F(X,Y);
surf(X,Y,Z);
This seems to be a common way of constructing a surface plot from vectors that I have found online.
However, the surface plot I get looks awful:
This is the plot that the COMSOL GUI provides.
Does anyone know how I can fix it so that the surface plot produced by MATLAB (a) is much smoother, and (b) doesn't leave jagged edges at the boundary of the domain?

Bjorn Gustavsson on 9 Nov 2022
Are you sure that you need to use scatteredInterpolant at all? The points in the first plot seem to be on a regular grid. If that is the case then you're already done. If they are on a regular grid but given in some obscure ordering, you should be able to sort them into a correct order, hopefully using reshape and permute.
If your x and y are 2-D arrays you can check that things are OK byt looking at them:
x = x'; y = y'; z = z';
x = reshape(x,[14 14]);
y = reshape(y,[14 14]);
z = reshape(z,[14 14]);
subplot(1,3,1)
imagesc(x)
subplot(1,3,2)
imagesc(y)
subplot(1,3,3)
imagesc(z)
It is definitely your call to meshgrid with repeated values in x and y that generates peculiarly ordered X or Y. Test this snippet just after your meshgrid call.
figure
subplot(1,2,1)
imagesc(X)
subplot(1,2,2)
imagesc(Y)
HTH
Bjorn Gustavsson on 29 Nov 2022
Did you get to a solution of your problem?

### Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by