필터 지우기
필터 지우기

interpolate value of 3D points on a meshgrid with a CUSTOM FUNCTION

조회 수: 8 (최근 30일)
Andrea Somma
Andrea Somma 2024년 6월 1일
댓글: Andrea Somma 2024년 6월 1일
% particle to mesh interpolation function (NOT ARBITRARY)
H = 3/pi/rCut^2;
u = @(r) H*(1-r/rCut).*(r.^2<=rCut^2);
for k = 1:length(ptcls.q)
r = sqrt((X - ptcls.x(1, k)).^2 + (Y - ptcls.x(2, k)).^2 + (Z - ptcls.x(3, k)).^2);
rho_lr = rho_lr + ptcls.q(k)*u(r);
end
I want to speed up this simple loop where ptcls.q is the value I want to interpolate, X,Y,Z are the points of a meshgrid and ptcls.x is the 3D scattered position of particles inside the mesh. I cannot use scatteredInterpolant function since is slow and does not fit the math behind the problem, any idea on how to speed up the calculation?

답변 (1개)

Torsten
Torsten 2024년 6월 1일
이동: Torsten 2024년 6월 1일
I don't understand why you use ptcls.q on the right-hand side of an equation (thus as known) if you want to interpolate it.
Assuming ptcls.q is a row vector, the loop can easily be replaced by
r = sqrt((X - ptcls.x(1, :)).^2 + (Y - ptcls.x(2, :)).^2 + (Z - ptcls.x(3, :)).^2);
H = 3/pi/rCut^2;
u = H*(1-r/rCut).*(r.^2<=rCut^2);
rho_lr = sum(ptcls.q.*u)
  댓글 수: 6
Torsten
Torsten 2024년 6월 1일
Yes that's exactly what I sent, my question was if there is a smart way to select nodes of the mesh in order to reduce the numer of calculations with a matlab function
How should this be possible ? For each particle, you have to "order" the (X,Y,Z) coordinates with respect to their distance to the particle position in order to interpolate. And in order to achieve this, given a particle, you have to evaluate r for each triple (X,Y,Z).
Andrea Somma
Andrea Somma 2024년 6월 1일
Since the particle has coordinates x,y,z and the grid X,Y,Z with linear spacing you can in some way extract a subset of XYZ by the next node of the mesh i = floor(x/hX) and neighbours inside hx*n_neighbours > rCut, where hX is the grid spacing. Then performing the interpolation only on that subset of nodes, I supposed that matlab had a sort of nearest neighbours algorithm to do that

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by