Clear Filters
Clear Filters

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

8 views (last 30 days)
% 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?

Answers (1)

Torsten
Torsten on 1 Jun 2024
Moved: Torsten on 1 Jun 2024
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 Comments
Torsten
Torsten on 1 Jun 2024
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 on 1 Jun 2024
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!