Create a mesh within a triangle and interpolate within this mesh

4 vues (au cours des 30 derniers jours)
Dirk
Dirk le 22 Sep 2023
Modifié(e) : Bruno Luong le 22 Sep 2023
Hi folks,
I have a 2D triangle and corrosponding data values at each vertices. I want to generate a mesh within the triangle, interpolate my data across that mesh, then average out all the values to get a mean across the triangles surface. I have a fairly clunky solution and was wondering if there was something more streamlined available. Here's what I have:
coord=[470590,7333138;470642,7333275;470643,7333214]; % coordinates of the triangle (these are eastings and northings)
v=[1 2 3] ; % data values at each vertices of the triangle
x=coords(:,1);
y=coords(:,2);
F=scatteredInterpolant(x,y,v);
% now I generate the query coordinates using a function from exchange called "generate triangle mesh" where X and Y are the coordinates of the new points
n=10 % number of interpolant points I want to generate in the triangle
[X,Y]=Triangle_Mesh(coords(1,:),coords(2,:),coords(3,:),n); % the function
vq=F(X,Y); % interpolates in the triangle which I can then average out
Thanks
Dirk
  2 commentaires
KSSV
KSSV le 22 Sep 2023
Share the link for the fileexchange function.
Dirk
Dirk le 22 Sep 2023
Note that I put [X Y] as the coordinates but the coordinates are actually contained in X only

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 22 Sep 2023
coords=[470590,7333138;470642,7333275;470643,7333214];
v=[1 2 3] ; % data values at each vertices of the triangle
x=coords(:,1);
y=coords(:,2);
% Replace your mesh data
[X,Y]=meshgrid(linspace(min(x),max(x),10),linspace(min(y),max(y),10));
F=scatteredInterpolant(x,y,v(:));
vq=F(X,Y) % interpolates in the triangle which I can then average out
vq = 10×10
1.0000 1.3524 1.7047 2.0571 2.4095 2.7619 3.1142 3.4666 3.8190 4.1714 0.7654 1.1178 1.4701 1.8225 2.1749 2.5272 2.8796 3.2320 3.5844 3.9367 0.5308 0.8831 1.2355 1.5879 1.9403 2.2926 2.6450 2.9974 3.3498 3.7021 0.2962 0.6485 1.0009 1.3533 1.7057 2.0580 2.4104 2.7628 3.1151 3.4675 0.0615 0.4139 0.7663 1.1187 1.4710 1.8234 2.1758 2.5282 2.8805 3.2329 -0.1731 0.1793 0.5317 0.8841 1.2364 1.5888 1.9412 2.2935 2.6459 2.9983 -0.4077 -0.0553 0.2971 0.6494 1.0018 1.3542 1.7066 2.0589 2.4113 2.7637 -0.6423 -0.2899 0.0625 0.4148 0.7672 1.1196 1.4719 1.8243 2.1767 2.5291 -0.8769 -0.5245 -0.1722 0.1802 0.5326 0.8850 1.2373 1.5897 1.9421 2.2944 -1.1115 -0.7591 -0.4068 -0.0544 0.2980 0.6503 1.0027 1.3551 1.7075 2.0598
% Compute vq directly without the need of scatteredInterpolant
vq = reshape((([X(:) Y(:) ones(numel(X),1)] / [coords, [1;1;1]]) * v(:)), size(X))
vq = 10×10
1.0000 1.3524 1.7047 2.0571 2.4095 2.7619 3.1142 3.4666 3.8190 4.1714 0.7654 1.1178 1.4701 1.8225 2.1749 2.5272 2.8796 3.2320 3.5844 3.9367 0.5308 0.8831 1.2355 1.5879 1.9403 2.2926 2.6450 2.9974 3.3498 3.7021 0.2962 0.6485 1.0009 1.3533 1.7057 2.0580 2.4104 2.7628 3.1151 3.4675 0.0615 0.4139 0.7663 1.1187 1.4710 1.8234 2.1758 2.5282 2.8805 3.2329 -0.1731 0.1793 0.5317 0.8841 1.2364 1.5888 1.9412 2.2935 2.6459 2.9983 -0.4077 -0.0553 0.2971 0.6494 1.0018 1.3542 1.7066 2.0589 2.4113 2.7637 -0.6423 -0.2899 0.0625 0.4148 0.7672 1.1196 1.4719 1.8243 2.1767 2.5291 -0.8769 -0.5245 -0.1722 0.1802 0.5326 0.8850 1.2373 1.5897 1.9421 2.2944 -1.1115 -0.7591 -0.4068 -0.0544 0.2980 0.6503 1.0027 1.3551 1.7075 2.0598
  1 commentaire
Bruno Luong
Bruno Luong le 22 Sep 2023
Modifié(e) : Bruno Luong le 22 Sep 2023
Alternative way
% Compute vq directly without the need of scatteredInterpolant
c = [coords, ones(3,1)] \ v(:);
vq = c(1)*X + c(2)*Y + c(3)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by