Reconstructing a surface from mesh points and function values (from COMSOL)
조회 수: 10 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (1개)
Bjorn Gustavsson
2022년 11월 9일
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
댓글 수: 8
Bjorn Gustavsson
2023년 2월 8일
If you look into surfir you will see that it is an explicit use of the suggestions I've made:
% Below I will mark my comments BG
% h=surfir(x,y,z,s,opt)
%
% Surface plot based on irregularly spaced data points.
% x,y,z are coordinate vectors of the same length.
% s (default 0.5) is the shrink factor in the Matlab function 'boundary'
% (type 'help boundary' for details)
% opt is a list of options to be passed to the 'trisurf' function
% (type 'help trisurf' for details)
function h=surfir(x,y,z,s,opt)
%% default parameters
if (nargin<4)||isempty(s) % no shrink factor provided
s=0.5; % default value
end
if nargin<5 % no options provided
opt={'FaceColor','interp','edgecolor','none'}; % default
end
%% Remove duplicate data points
[xy,ind] = unique([x,y],'rows');
z=z(ind);
x=xy(:,1);
y=xy(:,2);
%% triangulate data points
% BG - here the dealaunay-triangulation I suggested is done:
dt = delaunayTriangulation(x, y); % Delaunay triangulation
x=dt.Points(:,1);
y=dt.Points(:,2);
%% find enclosing boundary
k=boundary(x,y,s); % define boundary enclosing all data points
c=[k(1:end-1),k(2:end)]; % constraints
dt.Constraints=c; % add constraints
io = dt.isInterior(); % triangles which are in the interior of domain
tri=dt.ConnectivityList; % read triangles
tri=tri(io,:); % use only triangles in the interior
%% plot
% BG - Here the function uses trisurf to make the plot
h=trisurf(tri,x,y,z,z,opt{:}); % plot triangles and interpolate colors
The triangulation will return a triangulation that covers the convex hull around your points. You would somehow find a way to remove those triangles that lie outside of the boundary you like.
Perhaps you can figure out which triangles that are and what corner-points they have and simply remove those triangles. You could do that programming work yourself, or test different values for the fourth input-argument to surfir.
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!