Distance between two moving particles
Mostrar comentarios más antiguos
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?

Respuesta aceptada
Más respuestas (2)
KSSV
el 6 de Jun. de 2017
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 comentarios
John D'Errico
el 6 de Jun. de 2017
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
el 6 de Jun. de 2017
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
el 6 de Jun. de 2017
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
el 6 de Jun. de 2017
Editada: John D'Errico
el 6 de Jun. de 2017
0 votos
"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.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

