Distance between two moving particles
이전 댓글 표시
I'm new to programming and just started learning MATLAB last week. If anyone could help me out, I'd very much appreciate it.
Let's say I have two particles moving with constant speeds along straight line paths in 3-D space (figure enclosed). For each time step of 0.1 second, I need to calculate the distance between the moving particles. Is there any code available for this?

채택된 답변
추가 답변 (2개)
KSSV
2017년 6월 6일
Let A, B be your points with locations/ coordinates (x1,y1,z1) and (x2,y2,z2) respectively. You can calculate the distance using:
EuclidDistance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2);
Eg:
A = rand(1,3) ;
B = rand(1,3) ;
EuclidDistance = sqrt((A(1)-B(1))^2 + (A(2)-B(2))^2 + (A(3)-B(3))^2)
댓글 수: 4
John D'Errico
2017년 6월 6일
That is just the distance between two POINTS, not line segments. You can write the distance between points as more simply:
EuclidDistance = norm(A-B);
But it may not be as obvious why norm works.
KSSV
2017년 6월 6일
He wants to calculate the shortest distance....is it not the EuclidDistance? Yes norm(A-B) works....but as he is new I didn't want to confuse him.
Image Analyst
2017년 6월 6일
I read it as he wants the distance "distance between the moving particles" "For each time step" meaning he wants a bunch of distances. Granted, he made it confusing by saying "shortest distance" when in fact, for a particular time point there is only ONE distance between particles, not multiple distances from which we could extract a shortest.
Now if he wanted the shortest distance between paths (past trajectories), then there could be a shortest distance, and you could get that distance, along with the time point (element) at which it occurred using min:
[minDistance, indexOfMin] = min(EuclidDistance);
John D'Errico
2017년 6월 6일
편집: John D'Errico
2017년 6월 6일
0 개 추천
"Is there any code available to do this?"
No. Why should there be? This is not a terribly difficult question of mathematics, or something that requires a toolbox because it would get a huge amount of use. You could look on the file exchange, but you never know the quality of what you find there. :)
Actually, I did look on the FEX. Not sure why you did not look. I did, and found this code. I've seen worse code, and although it has some limitations, it might do what you asked. Apparently there is a problem if one or both of the segments are of zero length. Truly high quality code would watch for problems like that. But it will probably work. It does apparently return the points of closest approach as an output.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

