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

15 views (last 30 days)
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);
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?

Answers (1)

Bjorn Gustavsson
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]);
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by