interpolate value of 3D points on a meshgrid with a CUSTOM FUNCTION
8 次查看(过去 30 天)
显示 更早的评论
% 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?
0 个评论
回答(1 个)
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
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).
另请参阅
类别
在 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!